mem.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* CC0 (Public domain) - see LICENSE file for details */
  2. #ifndef CCAN_MEM_H
  3. #define CCAN_MEM_H
  4. #include "config.h"
  5. #include <ccan/compiler/compiler.h>
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #if !HAVE_MEMMEM
  9. PURE_FUNCTION
  10. void *memmem(const void *haystack, size_t haystacklen,
  11. const void *needle, size_t needlelen);
  12. #endif
  13. #if !HAVE_MEMRCHR
  14. PURE_FUNCTION
  15. void *memrchr(const void *s, int c, size_t n);
  16. #endif
  17. /**
  18. * mempbrkm - locates the first occurrence in @data of any bytes in @accept
  19. * @data: where we search
  20. * @len: length of data in bytes
  21. * @accept: array of bytes we search for
  22. * @accept_len: # of bytes in accept
  23. *
  24. * Returns a pointer to the byte in @data that matches one of the bytes in
  25. * @accept, or NULL if no such byte is found.
  26. *
  27. * Example:
  28. * char otherbytes[] = "Hello \0world";
  29. * size_t otherbytes_len = sizeof(otherbytes) - 1;
  30. * char *r = mempbrkm(otherbytes, otherbytes_len, "\0b", 2);
  31. * if (r) {
  32. * printf("Found %c\n", *r);
  33. * } else {
  34. * printf("Nada\n");
  35. * }
  36. *
  37. */
  38. PURE_FUNCTION
  39. void *mempbrkm(const void *data, size_t len, const void *accept, size_t accept_len);
  40. /**
  41. * mempbrk - locates the first occurrence in @data of any bytes in @accept
  42. * @data: where we search
  43. * @len: length of data in bytes
  44. * @accept: NUL terminated string containing the bytes we search for
  45. *
  46. * Returns a pointer to the byte in @data that matches one of the bytes in
  47. * @accept, or NULL if no such byte is found.
  48. *
  49. * Example:
  50. *
  51. * r = mempbrk(otherbytes, otherbytes_len, "abcde");
  52. * if (r) {
  53. * printf("Found %c\n", *r);
  54. * } else {
  55. * printf("Nada\n");
  56. * }
  57. */
  58. PURE_FUNCTION
  59. static inline char *mempbrk(const void *data, size_t len, const char *accept)
  60. {
  61. return mempbrkm(data, len, accept, strlen(accept));
  62. }
  63. /**
  64. * memcchr - scan memory until a character does _not_ match @c
  65. * @data: pointer to memory to scan
  66. * @data_len: length of data
  67. * @c: character to scan for
  68. *
  69. * The complement of memchr().
  70. *
  71. * Returns a pointer to the first character which is _not_ @c. If all memory in
  72. * @data is @c, returns NULL.
  73. *
  74. * Example:
  75. * char somebytes[] = "HI By\0e";
  76. * size_t bytes_len = sizeof(somebytes) - 1;
  77. * r = memcchr(somebytes, ' ', bytes_len);
  78. * if (r) {
  79. * printf("Found %c after trimming spaces\n", *r);
  80. * }
  81. */
  82. PURE_FUNCTION
  83. void *memcchr(void const *data, int c, size_t data_len);
  84. /**
  85. * memeq - Are two byte arrays equal?
  86. * @a: first array
  87. * @al: bytes in first array
  88. * @b: second array
  89. * @bl: bytes in second array
  90. *
  91. * Example:
  92. * if (memeq(somebytes, bytes_len, otherbytes, otherbytes_len)) {
  93. * printf("memory blocks are the same!\n");
  94. * }
  95. */
  96. PURE_FUNCTION
  97. static inline bool memeq(const void *a, size_t al, const void *b, size_t bl)
  98. {
  99. return al == bl && !memcmp(a, b, bl);
  100. }
  101. /**
  102. * memstarts - determine if @data starts with @prefix
  103. * @data: does this begin with @prefix?
  104. * @data_len: bytes in @data
  105. * @prefix: does @data begin with these bytes?
  106. * @prefix_len: bytes in @prefix
  107. *
  108. * Returns true if @data starts with @prefix, otherwise return false.
  109. *
  110. * Example:
  111. * if (memstarts(somebytes, bytes_len, otherbytes, otherbytes_len)) {
  112. * printf("somebytes starts with otherbytes!\n");
  113. * }
  114. */
  115. PURE_FUNCTION
  116. static inline bool memstarts(void const *data, size_t data_len,
  117. void const *prefix, size_t prefix_len)
  118. {
  119. if (prefix_len > data_len)
  120. return false;
  121. return memeq(data, prefix_len, prefix, prefix_len);
  122. }
  123. /**
  124. * memeqstr - Is a byte array equal to a NUL terminated string?
  125. * @data: byte array
  126. * @length: length of @data in bytes
  127. * @string: NUL terminated string
  128. *
  129. * The '\0' byte is ignored when checking if @bytes == @string.
  130. *
  131. * Example:
  132. * if (memeqstr(somebytes, bytes_len, "foo")) {
  133. * printf("somebytes == 'foo'!\n");
  134. * }
  135. */
  136. PURE_FUNCTION
  137. static inline bool memeqstr(const void *data, size_t length, const char *string)
  138. {
  139. return memeq(data, length, string, strlen(string));
  140. }
  141. /**
  142. * memstarts_str - Does this byte array start with a string prefix?
  143. * @a: byte array
  144. * @al: length in bytes
  145. * @s: string prefix
  146. *
  147. * Example:
  148. * if (memstarts_str(somebytes, bytes_len, "It")) {
  149. * printf("somebytes starts with 'It'\n");
  150. * }
  151. */
  152. PURE_FUNCTION
  153. static inline bool memstarts_str(const void *a, size_t al, const char *s)
  154. {
  155. return memstarts(a, al, s, strlen(s));
  156. }
  157. /**
  158. * memends - Does this byte array end with a given byte-array suffix?
  159. * @s: byte array
  160. * @s_len: length in bytes
  161. * @suffix: byte array suffix
  162. * @suffix_len: length of suffix in bytes
  163. *
  164. * Returns true if @suffix appears as a substring at the end of @s,
  165. * false otherwise.
  166. */
  167. PURE_FUNCTION
  168. static inline bool memends(const void *s, size_t s_len, const void *suffix, size_t suffix_len)
  169. {
  170. return (s_len >= suffix_len) && (memcmp((const char *)s + s_len - suffix_len,
  171. suffix, suffix_len) == 0);
  172. }
  173. #endif /* CCAN_MEM_H */