string.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_STRING_H_
  3. #define _LINUX_STRING_H_
  4. #include <linux/compiler.h> /* for inline */
  5. #include <linux/types.h> /* for size_t */
  6. #include <linux/stddef.h> /* for NULL */
  7. #include <linux/err.h> /* for ERR_PTR() */
  8. #include <linux/errno.h> /* for E2BIG */
  9. #include <linux/overflow.h> /* for check_mul_overflow() */
  10. #include <linux/stdarg.h>
  11. #include <uapi/linux/string.h>
  12. extern char *strndup_user(const char __user *, long);
  13. extern void *memdup_user(const void __user *, size_t);
  14. extern void *vmemdup_user(const void __user *, size_t);
  15. extern void *memdup_user_nul(const void __user *, size_t);
  16. /**
  17. * memdup_array_user - duplicate array from user space
  18. * @src: source address in user space
  19. * @n: number of array members to copy
  20. * @size: size of one array member
  21. *
  22. * Return: an ERR_PTR() on failure. Result is physically
  23. * contiguous, to be freed by kfree().
  24. */
  25. static inline void *memdup_array_user(const void __user *src, size_t n, size_t size)
  26. {
  27. size_t nbytes;
  28. if (check_mul_overflow(n, size, &nbytes))
  29. return ERR_PTR(-EOVERFLOW);
  30. return memdup_user(src, nbytes);
  31. }
  32. /**
  33. * vmemdup_array_user - duplicate array from user space
  34. * @src: source address in user space
  35. * @n: number of array members to copy
  36. * @size: size of one array member
  37. *
  38. * Return: an ERR_PTR() on failure. Result may be not
  39. * physically contiguous. Use kvfree() to free.
  40. */
  41. static inline void *vmemdup_array_user(const void __user *src, size_t n, size_t size)
  42. {
  43. size_t nbytes;
  44. if (check_mul_overflow(n, size, &nbytes))
  45. return ERR_PTR(-EOVERFLOW);
  46. return vmemdup_user(src, nbytes);
  47. }
  48. /*
  49. * Include machine specific inline routines
  50. */
  51. #include <asm/string.h>
  52. #ifndef __HAVE_ARCH_STRCPY
  53. extern char * strcpy(char *,const char *);
  54. #endif
  55. #ifndef __HAVE_ARCH_STRNCPY
  56. extern char * strncpy(char *,const char *, __kernel_size_t);
  57. #endif
  58. #ifndef __HAVE_ARCH_STRLCPY
  59. size_t strlcpy(char *, const char *, size_t);
  60. #endif
  61. #ifndef __HAVE_ARCH_STRSCPY
  62. ssize_t strscpy(char *, const char *, size_t);
  63. #endif
  64. /* Wraps calls to strscpy()/memset(), no arch specific code required */
  65. ssize_t strscpy_pad(char *dest, const char *src, size_t count);
  66. #ifndef __HAVE_ARCH_STRCAT
  67. extern char * strcat(char *, const char *);
  68. #endif
  69. #ifndef __HAVE_ARCH_STRNCAT
  70. extern char * strncat(char *, const char *, __kernel_size_t);
  71. #endif
  72. #ifndef __HAVE_ARCH_STRLCAT
  73. extern size_t strlcat(char *, const char *, __kernel_size_t);
  74. #endif
  75. #ifndef __HAVE_ARCH_STRCMP
  76. extern int strcmp(const char *,const char *);
  77. #endif
  78. #ifndef __HAVE_ARCH_STRNCMP
  79. extern int strncmp(const char *,const char *,__kernel_size_t);
  80. #endif
  81. #ifndef __HAVE_ARCH_STRCASECMP
  82. extern int strcasecmp(const char *s1, const char *s2);
  83. #endif
  84. #ifndef __HAVE_ARCH_STRNCASECMP
  85. extern int strncasecmp(const char *s1, const char *s2, size_t n);
  86. #endif
  87. #ifndef __HAVE_ARCH_STRCHR
  88. extern char * strchr(const char *,int);
  89. #endif
  90. #ifndef __HAVE_ARCH_STRCHRNUL
  91. extern char * strchrnul(const char *,int);
  92. #endif
  93. extern char * strnchrnul(const char *, size_t, int);
  94. #ifndef __HAVE_ARCH_STRNCHR
  95. extern char * strnchr(const char *, size_t, int);
  96. #endif
  97. #ifndef __HAVE_ARCH_STRRCHR
  98. extern char * strrchr(const char *,int);
  99. #endif
  100. extern char * __must_check skip_spaces(const char *);
  101. extern char *strim(char *);
  102. static inline __must_check char *strstrip(char *str)
  103. {
  104. return strim(str);
  105. }
  106. #ifndef __HAVE_ARCH_STRSTR
  107. extern char * strstr(const char *, const char *);
  108. #endif
  109. #ifndef __HAVE_ARCH_STRNSTR
  110. extern char * strnstr(const char *, const char *, size_t);
  111. #endif
  112. #ifndef __HAVE_ARCH_STRLEN
  113. extern __kernel_size_t strlen(const char *);
  114. #endif
  115. #ifndef __HAVE_ARCH_STRNLEN
  116. extern __kernel_size_t strnlen(const char *,__kernel_size_t);
  117. #endif
  118. #ifndef __HAVE_ARCH_STRPBRK
  119. extern char * strpbrk(const char *,const char *);
  120. #endif
  121. #ifndef __HAVE_ARCH_STRSEP
  122. extern char * strsep(char **,const char *);
  123. #endif
  124. #ifndef __HAVE_ARCH_STRSPN
  125. extern __kernel_size_t strspn(const char *,const char *);
  126. #endif
  127. #ifndef __HAVE_ARCH_STRCSPN
  128. extern __kernel_size_t strcspn(const char *,const char *);
  129. #endif
  130. #ifndef __HAVE_ARCH_MEMSET
  131. extern void * memset(void *,int,__kernel_size_t);
  132. #endif
  133. #ifndef __HAVE_ARCH_MEMSET16
  134. extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
  135. #endif
  136. #ifndef __HAVE_ARCH_MEMSET32
  137. extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
  138. #endif
  139. #ifndef __HAVE_ARCH_MEMSET64
  140. extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
  141. #endif
  142. static inline void *memset_l(unsigned long *p, unsigned long v,
  143. __kernel_size_t n)
  144. {
  145. if (BITS_PER_LONG == 32)
  146. return memset32((uint32_t *)p, v, n);
  147. else
  148. return memset64((uint64_t *)p, v, n);
  149. }
  150. static inline void *memset_p(void **p, void *v, __kernel_size_t n)
  151. {
  152. if (BITS_PER_LONG == 32)
  153. return memset32((uint32_t *)p, (uintptr_t)v, n);
  154. else
  155. return memset64((uint64_t *)p, (uintptr_t)v, n);
  156. }
  157. extern void **__memcat_p(void **a, void **b);
  158. #define memcat_p(a, b) ({ \
  159. BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \
  160. "type mismatch in memcat_p()"); \
  161. (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \
  162. })
  163. #ifndef __HAVE_ARCH_MEMCPY
  164. extern void * memcpy(void *,const void *,__kernel_size_t);
  165. #endif
  166. #ifndef __HAVE_ARCH_MEMMOVE
  167. extern void * memmove(void *,const void *,__kernel_size_t);
  168. #endif
  169. #ifndef __HAVE_ARCH_MEMSCAN
  170. extern void * memscan(void *,int,__kernel_size_t);
  171. #endif
  172. #ifndef __HAVE_ARCH_MEMCMP
  173. extern int memcmp(const void *,const void *,__kernel_size_t);
  174. #endif
  175. #ifndef __HAVE_ARCH_BCMP
  176. extern int bcmp(const void *,const void *,__kernel_size_t);
  177. #endif
  178. #ifndef __HAVE_ARCH_MEMCHR
  179. extern void * memchr(const void *,int,__kernel_size_t);
  180. #endif
  181. #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
  182. static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
  183. {
  184. memcpy(dst, src, cnt);
  185. }
  186. #endif
  187. void *memchr_inv(const void *s, int c, size_t n);
  188. char *strreplace(char *s, char old, char new);
  189. extern void kfree_const(const void *x);
  190. extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
  191. extern const char *kstrdup_const(const char *s, gfp_t gfp);
  192. extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
  193. extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
  194. extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
  195. extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
  196. extern void argv_free(char **argv);
  197. extern bool sysfs_streq(const char *s1, const char *s2);
  198. int match_string(const char * const *array, size_t n, const char *string);
  199. int __sysfs_match_string(const char * const *array, size_t n, const char *s);
  200. /**
  201. * sysfs_match_string - matches given string in an array
  202. * @_a: array of strings
  203. * @_s: string to match with
  204. *
  205. * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
  206. */
  207. #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
  208. #ifdef CONFIG_BINARY_PRINTF
  209. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
  210. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
  211. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
  212. #endif
  213. extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  214. const void *from, size_t available);
  215. int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
  216. /**
  217. * strstarts - does @str start with @prefix?
  218. * @str: string to examine
  219. * @prefix: prefix to look for.
  220. */
  221. static inline bool strstarts(const char *str, const char *prefix)
  222. {
  223. return strncmp(str, prefix, strlen(prefix)) == 0;
  224. }
  225. size_t memweight(const void *ptr, size_t bytes);
  226. /**
  227. * memzero_explicit - Fill a region of memory (e.g. sensitive
  228. * keying data) with 0s.
  229. * @s: Pointer to the start of the area.
  230. * @count: The size of the area.
  231. *
  232. * Note: usually using memset() is just fine (!), but in cases
  233. * where clearing out _local_ data at the end of a scope is
  234. * necessary, memzero_explicit() should be used instead in
  235. * order to prevent the compiler from optimising away zeroing.
  236. *
  237. * memzero_explicit() doesn't need an arch-specific version as
  238. * it just invokes the one of memset() implicitly.
  239. */
  240. static inline void memzero_explicit(void *s, size_t count)
  241. {
  242. memset(s, 0, count);
  243. barrier_data(s);
  244. }
  245. /**
  246. * kbasename - return the last part of a pathname.
  247. *
  248. * @path: path to extract the filename from.
  249. */
  250. static inline const char *kbasename(const char *path)
  251. {
  252. const char *tail = strrchr(path, '/');
  253. return tail ? tail + 1 : path;
  254. }
  255. #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
  256. #include <linux/fortify-string.h>
  257. #endif
  258. #ifndef unsafe_memcpy
  259. #define unsafe_memcpy(dst, src, bytes, justification) \
  260. memcpy(dst, src, bytes)
  261. #endif
  262. void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  263. int pad);
  264. /**
  265. * strtomem_pad - Copy NUL-terminated string to non-NUL-terminated buffer
  266. *
  267. * @dest: Pointer of destination character array (marked as __nonstring)
  268. * @src: Pointer to NUL-terminated string
  269. * @pad: Padding character to fill any remaining bytes of @dest after copy
  270. *
  271. * This is a replacement for strncpy() uses where the destination is not
  272. * a NUL-terminated string, but with bounds checking on the source size, and
  273. * an explicit padding character. If padding is not required, use strtomem().
  274. *
  275. * Note that the size of @dest is not an argument, as the length of @dest
  276. * must be discoverable by the compiler.
  277. */
  278. #define strtomem_pad(dest, src, pad) do { \
  279. const size_t _dest_len = __builtin_object_size(dest, 1); \
  280. const size_t _src_len = __builtin_object_size(src, 1); \
  281. \
  282. BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
  283. _dest_len == (size_t)-1); \
  284. memcpy_and_pad(dest, _dest_len, src, \
  285. strnlen(src, min(_src_len, _dest_len)), pad); \
  286. } while (0)
  287. /**
  288. * strtomem - Copy NUL-terminated string to non-NUL-terminated buffer
  289. *
  290. * @dest: Pointer of destination character array (marked as __nonstring)
  291. * @src: Pointer to NUL-terminated string
  292. *
  293. * This is a replacement for strncpy() uses where the destination is not
  294. * a NUL-terminated string, but with bounds checking on the source size, and
  295. * without trailing padding. If padding is required, use strtomem_pad().
  296. *
  297. * Note that the size of @dest is not an argument, as the length of @dest
  298. * must be discoverable by the compiler.
  299. */
  300. #define strtomem(dest, src) do { \
  301. const size_t _dest_len = __builtin_object_size(dest, 1); \
  302. const size_t _src_len = __builtin_object_size(src, 1); \
  303. \
  304. BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
  305. _dest_len == (size_t)-1); \
  306. memcpy(dest, src, strnlen(src, min(_src_len, _dest_len))); \
  307. } while (0)
  308. /**
  309. * memset_after - Set a value after a struct member to the end of a struct
  310. *
  311. * @obj: Address of target struct instance
  312. * @v: Byte value to repeatedly write
  313. * @member: after which struct member to start writing bytes
  314. *
  315. * This is good for clearing padding following the given member.
  316. */
  317. #define memset_after(obj, v, member) \
  318. ({ \
  319. u8 *__ptr = (u8 *)(obj); \
  320. typeof(v) __val = (v); \
  321. memset(__ptr + offsetofend(typeof(*(obj)), member), __val, \
  322. sizeof(*(obj)) - offsetofend(typeof(*(obj)), member)); \
  323. })
  324. /**
  325. * memset_startat - Set a value starting at a member to the end of a struct
  326. *
  327. * @obj: Address of target struct instance
  328. * @v: Byte value to repeatedly write
  329. * @member: struct member to start writing at
  330. *
  331. * Note that if there is padding between the prior member and the target
  332. * member, memset_after() should be used to clear the prior padding.
  333. */
  334. #define memset_startat(obj, v, member) \
  335. ({ \
  336. u8 *__ptr = (u8 *)(obj); \
  337. typeof(v) __val = (v); \
  338. memset(__ptr + offsetof(typeof(*(obj)), member), __val, \
  339. sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \
  340. })
  341. /**
  342. * str_has_prefix - Test if a string has a given prefix
  343. * @str: The string to test
  344. * @prefix: The string to see if @str starts with
  345. *
  346. * A common way to test a prefix of a string is to do:
  347. * strncmp(str, prefix, sizeof(prefix) - 1)
  348. *
  349. * But this can lead to bugs due to typos, or if prefix is a pointer
  350. * and not a constant. Instead use str_has_prefix().
  351. *
  352. * Returns:
  353. * * strlen(@prefix) if @str starts with @prefix
  354. * * 0 if @str does not start with @prefix
  355. */
  356. static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
  357. {
  358. size_t len = strlen(prefix);
  359. return strncmp(str, prefix, len) == 0 ? len : 0;
  360. }
  361. #endif /* _LINUX_STRING_H_ */