mem.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /**
  174. * memends_str - Does this byte array end with a string suffix?
  175. * @a: byte array
  176. * @al: length in bytes
  177. * @s: string suffix
  178. *
  179. * Example:
  180. * if (memends_str(somebytes, bytes_len, "It")) {
  181. * printf("somebytes ends with with 'It'\n");
  182. * }
  183. */
  184. PURE_FUNCTION
  185. static inline bool memends_str(const void *a, size_t al, const char *s)
  186. {
  187. return memends(a, al, s, strlen(s));
  188. }
  189. /**
  190. * memoverlaps - Do two memory ranges overlap?
  191. * @a: pointer to first memory range
  192. * @al: length of first memory range
  193. * @b: pointer to second memory range
  194. * @al: length of second memory range
  195. */
  196. CONST_FUNCTION
  197. static inline bool memoverlaps(const void *a_, size_t al,
  198. const void *b_, size_t bl)
  199. {
  200. const char *a = a_;
  201. const char *b = b_;
  202. return (a < (b + bl)) && (b < (a + al));
  203. }
  204. /*
  205. * memswap - Exchange two memory regions
  206. * @a: first region
  207. * @b: second region
  208. * @n: length of the regions
  209. *
  210. * Undefined results if the two memory regions overlap.
  211. */
  212. void memswap(void *a, void *b, size_t n);
  213. #if HAVE_VALGRIND_MEMCHECK_H
  214. #include <valgrind/memcheck.h>
  215. static inline void *memcheck_(const void *data, size_t len)
  216. {
  217. VALGRIND_CHECK_MEM_IS_DEFINED(data, len);
  218. return (void *)data;
  219. }
  220. #else
  221. static inline void *memcheck_(const void *data, size_t len)
  222. {
  223. return (void *)data;
  224. }
  225. #endif
  226. #if HAVE_TYPEOF
  227. /**
  228. * memcheck - check that a memory region is initialized
  229. * @data: start of region
  230. * @len: length in bytes
  231. *
  232. * When running under valgrind, this causes an error to be printed
  233. * if the entire region is not defined. Otherwise valgrind only
  234. * reports an error when an undefined value is used for a branch, or
  235. * written out.
  236. *
  237. * Example:
  238. * // Search for space, but make sure it's all initialized.
  239. * if (memchr(memcheck(somebytes, bytes_len), ' ', bytes_len)) {
  240. * printf("space was found!\n");
  241. * }
  242. */
  243. #define memcheck(data, len) ((__typeof__((data)+0))memcheck_((data), (len)))
  244. #else
  245. #define memcheck(data, len) memcheck_((data), (len))
  246. #endif
  247. #endif /* CCAN_MEM_H */