string.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #ifndef _LINUX_STRING_H_
  2. #define _LINUX_STRING_H_
  3. #include <linux/compiler.h> /* for inline */
  4. #include <linux/types.h> /* for size_t */
  5. #include <linux/stddef.h> /* for NULL */
  6. #include <stdarg.h>
  7. #include <uapi/linux/string.h>
  8. extern char *strndup_user(const char __user *, long);
  9. extern void *memdup_user(const void __user *, size_t);
  10. extern void *memdup_user_nul(const void __user *, size_t);
  11. /*
  12. * Include machine specific inline routines
  13. */
  14. #include <asm/string.h>
  15. #ifndef __HAVE_ARCH_STRCPY
  16. extern char * strcpy(char *,const char *);
  17. #endif
  18. #ifndef __HAVE_ARCH_STRNCPY
  19. extern char * strncpy(char *,const char *, __kernel_size_t);
  20. #endif
  21. #ifndef __HAVE_ARCH_STRLCPY
  22. size_t strlcpy(char *, const char *, size_t);
  23. #endif
  24. #ifndef __HAVE_ARCH_STRSCPY
  25. ssize_t __must_check strscpy(char *, const char *, size_t);
  26. #endif
  27. #ifndef __HAVE_ARCH_STRCAT
  28. extern char * strcat(char *, const char *);
  29. #endif
  30. #ifndef __HAVE_ARCH_STRNCAT
  31. extern char * strncat(char *, const char *, __kernel_size_t);
  32. #endif
  33. #ifndef __HAVE_ARCH_STRLCAT
  34. extern size_t strlcat(char *, const char *, __kernel_size_t);
  35. #endif
  36. #ifndef __HAVE_ARCH_STRCMP
  37. extern int strcmp(const char *,const char *);
  38. #endif
  39. #ifndef __HAVE_ARCH_STRNCMP
  40. extern int strncmp(const char *,const char *,__kernel_size_t);
  41. #endif
  42. #ifndef __HAVE_ARCH_STRCASECMP
  43. extern int strcasecmp(const char *s1, const char *s2);
  44. #endif
  45. #ifndef __HAVE_ARCH_STRNCASECMP
  46. extern int strncasecmp(const char *s1, const char *s2, size_t n);
  47. #endif
  48. #ifndef __HAVE_ARCH_STRCHR
  49. extern char * strchr(const char *,int);
  50. #endif
  51. #ifndef __HAVE_ARCH_STRCHRNUL
  52. extern char * strchrnul(const char *,int);
  53. #endif
  54. #ifndef __HAVE_ARCH_STRNCHR
  55. extern char * strnchr(const char *, size_t, int);
  56. #endif
  57. #ifndef __HAVE_ARCH_STRRCHR
  58. extern char * strrchr(const char *,int);
  59. #endif
  60. extern char * __must_check skip_spaces(const char *);
  61. extern char *strim(char *);
  62. static inline __must_check char *strstrip(char *str)
  63. {
  64. return strim(str);
  65. }
  66. #ifndef __HAVE_ARCH_STRSTR
  67. extern char * strstr(const char *, const char *);
  68. #endif
  69. #ifndef __HAVE_ARCH_STRNSTR
  70. extern char * strnstr(const char *, const char *, size_t);
  71. #endif
  72. #ifndef __HAVE_ARCH_STRLEN
  73. extern __kernel_size_t strlen(const char *);
  74. #endif
  75. #ifndef __HAVE_ARCH_STRNLEN
  76. extern __kernel_size_t strnlen(const char *,__kernel_size_t);
  77. #endif
  78. #ifndef __HAVE_ARCH_STRPBRK
  79. extern char * strpbrk(const char *,const char *);
  80. #endif
  81. #ifndef __HAVE_ARCH_STRSEP
  82. extern char * strsep(char **,const char *);
  83. #endif
  84. #ifndef __HAVE_ARCH_STRSPN
  85. extern __kernel_size_t strspn(const char *,const char *);
  86. #endif
  87. #ifndef __HAVE_ARCH_STRCSPN
  88. extern __kernel_size_t strcspn(const char *,const char *);
  89. #endif
  90. #ifndef __HAVE_ARCH_MEMSET
  91. extern void * memset(void *,int,__kernel_size_t);
  92. #endif
  93. #ifndef __HAVE_ARCH_MEMSET16
  94. extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
  95. #endif
  96. #ifndef __HAVE_ARCH_MEMSET32
  97. extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
  98. #endif
  99. #ifndef __HAVE_ARCH_MEMSET64
  100. extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
  101. #endif
  102. static inline void *memset_l(unsigned long *p, unsigned long v,
  103. __kernel_size_t n)
  104. {
  105. if (BITS_PER_LONG == 32)
  106. return memset32((uint32_t *)p, v, n);
  107. else
  108. return memset64((uint64_t *)p, v, n);
  109. }
  110. static inline void *memset_p(void **p, void *v, __kernel_size_t n)
  111. {
  112. if (BITS_PER_LONG == 32)
  113. return memset32((uint32_t *)p, (uintptr_t)v, n);
  114. else
  115. return memset64((uint64_t *)p, (uintptr_t)v, n);
  116. }
  117. #ifndef __HAVE_ARCH_MEMCPY
  118. extern void * memcpy(void *,const void *,__kernel_size_t);
  119. #endif
  120. #ifndef __HAVE_ARCH_MEMMOVE
  121. extern void * memmove(void *,const void *,__kernel_size_t);
  122. #endif
  123. #ifndef __HAVE_ARCH_MEMSCAN
  124. extern void * memscan(void *,int,__kernel_size_t);
  125. #endif
  126. #ifndef __HAVE_ARCH_MEMCMP
  127. extern int memcmp(const void *,const void *,__kernel_size_t);
  128. #endif
  129. #ifndef __HAVE_ARCH_MEMCHR
  130. extern void * memchr(const void *,int,__kernel_size_t);
  131. #endif
  132. #ifndef __HAVE_ARCH_MEMCPY_MCSAFE
  133. static inline __must_check int memcpy_mcsafe(void *dst, const void *src,
  134. size_t cnt)
  135. {
  136. memcpy(dst, src, cnt);
  137. return 0;
  138. }
  139. #endif
  140. #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
  141. static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
  142. {
  143. memcpy(dst, src, cnt);
  144. }
  145. #endif
  146. void *memchr_inv(const void *s, int c, size_t n);
  147. char *strreplace(char *s, char old, char new);
  148. extern void kfree_const(const void *x);
  149. extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
  150. extern const char *kstrdup_const(const char *s, gfp_t gfp);
  151. extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
  152. extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
  153. extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
  154. extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
  155. extern void argv_free(char **argv);
  156. extern bool sysfs_streq(const char *s1, const char *s2);
  157. extern int kstrtobool(const char *s, bool *res);
  158. static inline int strtobool(const char *s, bool *res)
  159. {
  160. return kstrtobool(s, res);
  161. }
  162. int match_string(const char * const *array, size_t n, const char *string);
  163. int __sysfs_match_string(const char * const *array, size_t n, const char *s);
  164. /**
  165. * sysfs_match_string - matches given string in an array
  166. * @_a: array of strings
  167. * @_s: string to match with
  168. *
  169. * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
  170. */
  171. #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
  172. #ifdef CONFIG_BINARY_PRINTF
  173. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
  174. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
  175. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
  176. #endif
  177. extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  178. const void *from, size_t available);
  179. /**
  180. * strstarts - does @str start with @prefix?
  181. * @str: string to examine
  182. * @prefix: prefix to look for.
  183. */
  184. static inline bool strstarts(const char *str, const char *prefix)
  185. {
  186. return strncmp(str, prefix, strlen(prefix)) == 0;
  187. }
  188. size_t memweight(const void *ptr, size_t bytes);
  189. void memzero_explicit(void *s, size_t count);
  190. /**
  191. * kbasename - return the last part of a pathname.
  192. *
  193. * @path: path to extract the filename from.
  194. */
  195. static inline const char *kbasename(const char *path)
  196. {
  197. const char *tail = strrchr(path, '/');
  198. return tail ? tail + 1 : path;
  199. }
  200. #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
  201. #define __RENAME(x) __asm__(#x)
  202. void fortify_panic(const char *name) __noreturn __cold;
  203. void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
  204. void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
  205. void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
  206. #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
  207. __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
  208. {
  209. size_t p_size = __builtin_object_size(p, 0);
  210. if (__builtin_constant_p(size) && p_size < size)
  211. __write_overflow();
  212. if (p_size < size)
  213. fortify_panic(__func__);
  214. return __builtin_strncpy(p, q, size);
  215. }
  216. __FORTIFY_INLINE char *strcat(char *p, const char *q)
  217. {
  218. size_t p_size = __builtin_object_size(p, 0);
  219. if (p_size == (size_t)-1)
  220. return __builtin_strcat(p, q);
  221. if (strlcat(p, q, p_size) >= p_size)
  222. fortify_panic(__func__);
  223. return p;
  224. }
  225. __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
  226. {
  227. __kernel_size_t ret;
  228. size_t p_size = __builtin_object_size(p, 0);
  229. if (p_size == (size_t)-1)
  230. return __builtin_strlen(p);
  231. ret = strnlen(p, p_size);
  232. if (p_size <= ret)
  233. fortify_panic(__func__);
  234. return ret;
  235. }
  236. extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
  237. __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
  238. {
  239. size_t p_size = __builtin_object_size(p, 0);
  240. __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
  241. if (p_size <= ret && maxlen != ret)
  242. fortify_panic(__func__);
  243. return ret;
  244. }
  245. /* defined after fortified strlen to reuse it */
  246. extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
  247. __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
  248. {
  249. size_t ret;
  250. size_t p_size = __builtin_object_size(p, 0);
  251. size_t q_size = __builtin_object_size(q, 0);
  252. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  253. return __real_strlcpy(p, q, size);
  254. ret = strlen(q);
  255. if (size) {
  256. size_t len = (ret >= size) ? size - 1 : ret;
  257. if (__builtin_constant_p(len) && len >= p_size)
  258. __write_overflow();
  259. if (len >= p_size)
  260. fortify_panic(__func__);
  261. __builtin_memcpy(p, q, len);
  262. p[len] = '\0';
  263. }
  264. return ret;
  265. }
  266. /* defined after fortified strlen and strnlen to reuse them */
  267. __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
  268. {
  269. size_t p_len, copy_len;
  270. size_t p_size = __builtin_object_size(p, 0);
  271. size_t q_size = __builtin_object_size(q, 0);
  272. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  273. return __builtin_strncat(p, q, count);
  274. p_len = strlen(p);
  275. copy_len = strnlen(q, count);
  276. if (p_size < p_len + copy_len + 1)
  277. fortify_panic(__func__);
  278. __builtin_memcpy(p + p_len, q, copy_len);
  279. p[p_len + copy_len] = '\0';
  280. return p;
  281. }
  282. __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
  283. {
  284. size_t p_size = __builtin_object_size(p, 0);
  285. if (__builtin_constant_p(size) && p_size < size)
  286. __write_overflow();
  287. if (p_size < size)
  288. fortify_panic(__func__);
  289. return __builtin_memset(p, c, size);
  290. }
  291. __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
  292. {
  293. size_t p_size = __builtin_object_size(p, 0);
  294. size_t q_size = __builtin_object_size(q, 0);
  295. if (__builtin_constant_p(size)) {
  296. if (p_size < size)
  297. __write_overflow();
  298. if (q_size < size)
  299. __read_overflow2();
  300. }
  301. if (p_size < size || q_size < size)
  302. fortify_panic(__func__);
  303. return __builtin_memcpy(p, q, size);
  304. }
  305. __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
  306. {
  307. size_t p_size = __builtin_object_size(p, 0);
  308. size_t q_size = __builtin_object_size(q, 0);
  309. if (__builtin_constant_p(size)) {
  310. if (p_size < size)
  311. __write_overflow();
  312. if (q_size < size)
  313. __read_overflow2();
  314. }
  315. if (p_size < size || q_size < size)
  316. fortify_panic(__func__);
  317. return __builtin_memmove(p, q, size);
  318. }
  319. extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
  320. __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
  321. {
  322. size_t p_size = __builtin_object_size(p, 0);
  323. if (__builtin_constant_p(size) && p_size < size)
  324. __read_overflow();
  325. if (p_size < size)
  326. fortify_panic(__func__);
  327. return __real_memscan(p, c, size);
  328. }
  329. __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
  330. {
  331. size_t p_size = __builtin_object_size(p, 0);
  332. size_t q_size = __builtin_object_size(q, 0);
  333. if (__builtin_constant_p(size)) {
  334. if (p_size < size)
  335. __read_overflow();
  336. if (q_size < size)
  337. __read_overflow2();
  338. }
  339. if (p_size < size || q_size < size)
  340. fortify_panic(__func__);
  341. return __builtin_memcmp(p, q, size);
  342. }
  343. __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
  344. {
  345. size_t p_size = __builtin_object_size(p, 0);
  346. if (__builtin_constant_p(size) && p_size < size)
  347. __read_overflow();
  348. if (p_size < size)
  349. fortify_panic(__func__);
  350. return __builtin_memchr(p, c, size);
  351. }
  352. void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
  353. __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
  354. {
  355. size_t p_size = __builtin_object_size(p, 0);
  356. if (__builtin_constant_p(size) && p_size < size)
  357. __read_overflow();
  358. if (p_size < size)
  359. fortify_panic(__func__);
  360. return __real_memchr_inv(p, c, size);
  361. }
  362. extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
  363. __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
  364. {
  365. size_t p_size = __builtin_object_size(p, 0);
  366. if (__builtin_constant_p(size) && p_size < size)
  367. __read_overflow();
  368. if (p_size < size)
  369. fortify_panic(__func__);
  370. return __real_kmemdup(p, size, gfp);
  371. }
  372. /* defined after fortified strlen and memcpy to reuse them */
  373. __FORTIFY_INLINE char *strcpy(char *p, const char *q)
  374. {
  375. size_t p_size = __builtin_object_size(p, 0);
  376. size_t q_size = __builtin_object_size(q, 0);
  377. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  378. return __builtin_strcpy(p, q);
  379. memcpy(p, q, strlen(q) + 1);
  380. return p;
  381. }
  382. #endif
  383. #endif /* _LINUX_STRING_H_ */