Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

103 lines
3.0 KiB

  1. #include "byte.h"
  2. #include "buffer.h"
  3. /*
  4. * The functions that are beginning with byte_ offer some handy functions, to
  5. * manipulate with raw bytes.
  6. */
  7. int
  8. main()
  9. { /* The indices: 0123456 */
  10. char memory1[42] = "foo bar";
  11. char memory2[23] = "fnord";
  12. unsigned int pos;
  13. int diff;
  14. /* First we want to search forward for a certain char - we can achieve it
  15. * by using:
  16. *
  17. * unsigned int byte_chr(const char *haystack,unsigned int len,char
  18. * needle);
  19. *
  20. * which returns the _first_ position of the searched char ("needle") or
  21. * the supplied length ("len") if the search fails.
  22. */
  23. pos = byte_chr(memory1, 7, 'b'); /* Returns 4. */
  24. buffer_puts(buffer_1, "byte_chr(memory1, 7, 'b'): ");
  25. buffer_putulong(buffer_1, pos);
  26. buffer_putnlflush(buffer_1);
  27. /* Now let's focus on the opposite: we want to search backward in a
  28. * mem-region:
  29. *
  30. * unsigned int byte_rchr(const void* haystack,unsigned int len,char
  31. * needle);
  32. *
  33. * now it returns the _last_ position of the "needle" or len.
  34. */
  35. pos = byte_rchr(memory1, 7, 'o'); /* Returns 2.*/
  36. buffer_puts(buffer_1, "byte_rchr(memory1, 7, 'o'): ");
  37. buffer_putulong(buffer_1, pos);
  38. buffer_putnlflush(buffer_1);
  39. /* Enough of searching for now -- another important task is copying of
  40. * memory. Of course, libowfat helps you also in this point:
  41. *
  42. * void byte_copy(void* out, unsigned int len, const void* in);
  43. *
  44. * It simply copies len bytes from in to out -- starting at in[0] and
  45. * out[0]. Please recog that it has an another API than memcpy() that has
  46. * the last two parameters swapped.
  47. */
  48. byte_copy(memory1, 2, memory2); /* Makes memory1 look: "fno bar" */
  49. buffer_puts(buffer_1, "byte_copy(memory1, 2, memory2): ");
  50. buffer_puts(buffer_1, memory1);
  51. buffer_putnlflush(buffer_1);
  52. /* There is also a function byte_copyr() that does exactly the same except
  53. * of starting at the _end_ of the strings, i.e. in[len-1] and out[len-1].
  54. * I won't dicuss it in detail as only the internals has changed.
  55. */
  56. /* Another point is the comparing between memory regions -- in the libowfat
  57. * is
  58. *
  59. * int byte_diff(const void* a, unsigned int len, const void* b);
  60. *
  61. * the utility of choice. It returns 0 if the regions are equal, <0 if
  62. * a[0]...a[len] is lexicographically smaller and >0 if it is greater than
  63. * b. After the first difference is found, no further reading beyond this
  64. * difference is done. Beware of checking only for 1 and -1!
  65. */
  66. diff = byte_diff(memory1, 5, memory2);
  67. buffer_puts(buffer_1, "byte_diff(memory1, 5, memory2): ");
  68. buffer_putlong(buffer_1, diff);
  69. buffer_putnlflush(buffer_1);
  70. /* For convience, there is also a macro called byte_equal() for checking
  71. * for equality -- in fact it is just a !byte_diff().
  72. */
  73. /* Last but not feast a quite simple yet useful function that make it
  74. * possible to ban the BSD-legacy-crap called bzero() from your code w/o
  75. * having to use memset() that confused even Rich Stevens:
  76. *
  77. * void byte_zero(char *out,unsigned int len);
  78. *
  79. * fills the specified block of memory with 0.
  80. */
  81. byte_zero(memory1, 42);
  82. return 0;
  83. }