string.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_MEMCPY
  94. extern void * memcpy(void *,const void *,__kernel_size_t);
  95. #endif
  96. #ifndef __HAVE_ARCH_MEMMOVE
  97. extern void * memmove(void *,const void *,__kernel_size_t);
  98. #endif
  99. #ifndef __HAVE_ARCH_MEMSCAN
  100. extern void * memscan(void *,int,__kernel_size_t);
  101. #endif
  102. #ifndef __HAVE_ARCH_MEMCMP
  103. extern int memcmp(const void *,const void *,__kernel_size_t);
  104. #endif
  105. #ifndef __HAVE_ARCH_MEMCHR
  106. extern void * memchr(const void *,int,__kernel_size_t);
  107. #endif
  108. #ifndef __HAVE_ARCH_MEMCPY_MCSAFE
  109. static inline __must_check int memcpy_mcsafe(void *dst, const void *src,
  110. size_t cnt)
  111. {
  112. memcpy(dst, src, cnt);
  113. return 0;
  114. }
  115. #endif
  116. #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
  117. static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
  118. {
  119. memcpy(dst, src, cnt);
  120. }
  121. #endif
  122. void *memchr_inv(const void *s, int c, size_t n);
  123. char *strreplace(char *s, char old, char new);
  124. extern void kfree_const(const void *x);
  125. extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
  126. extern const char *kstrdup_const(const char *s, gfp_t gfp);
  127. extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
  128. extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
  129. extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
  130. extern void argv_free(char **argv);
  131. extern bool sysfs_streq(const char *s1, const char *s2);
  132. extern int kstrtobool(const char *s, bool *res);
  133. static inline int strtobool(const char *s, bool *res)
  134. {
  135. return kstrtobool(s, res);
  136. }
  137. int match_string(const char * const *array, size_t n, const char *string);
  138. int __sysfs_match_string(const char * const *array, size_t n, const char *s);
  139. /**
  140. * sysfs_match_string - matches given string in an array
  141. * @_a: array of strings
  142. * @_s: string to match with
  143. *
  144. * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
  145. */
  146. #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
  147. #ifdef CONFIG_BINARY_PRINTF
  148. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
  149. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
  150. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
  151. #endif
  152. extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
  153. const void *from, size_t available);
  154. /**
  155. * strstarts - does @str start with @prefix?
  156. * @str: string to examine
  157. * @prefix: prefix to look for.
  158. */
  159. static inline bool strstarts(const char *str, const char *prefix)
  160. {
  161. return strncmp(str, prefix, strlen(prefix)) == 0;
  162. }
  163. size_t memweight(const void *ptr, size_t bytes);
  164. void memzero_explicit(void *s, size_t count);
  165. /**
  166. * kbasename - return the last part of a pathname.
  167. *
  168. * @path: path to extract the filename from.
  169. */
  170. static inline const char *kbasename(const char *path)
  171. {
  172. const char *tail = strrchr(path, '/');
  173. return tail ? tail + 1 : path;
  174. }
  175. #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
  176. #define __RENAME(x) __asm__(#x)
  177. void fortify_panic(const char *name) __noreturn __cold;
  178. void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
  179. void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
  180. void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
  181. #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
  182. __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
  183. {
  184. size_t p_size = __builtin_object_size(p, 0);
  185. if (__builtin_constant_p(size) && p_size < size)
  186. __write_overflow();
  187. if (p_size < size)
  188. fortify_panic(__func__);
  189. return __builtin_strncpy(p, q, size);
  190. }
  191. __FORTIFY_INLINE char *strcat(char *p, const char *q)
  192. {
  193. size_t p_size = __builtin_object_size(p, 0);
  194. if (p_size == (size_t)-1)
  195. return __builtin_strcat(p, q);
  196. if (strlcat(p, q, p_size) >= p_size)
  197. fortify_panic(__func__);
  198. return p;
  199. }
  200. __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
  201. {
  202. __kernel_size_t ret;
  203. size_t p_size = __builtin_object_size(p, 0);
  204. if (p_size == (size_t)-1)
  205. return __builtin_strlen(p);
  206. ret = strnlen(p, p_size);
  207. if (p_size <= ret)
  208. fortify_panic(__func__);
  209. return ret;
  210. }
  211. extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
  212. __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
  213. {
  214. size_t p_size = __builtin_object_size(p, 0);
  215. __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
  216. if (p_size <= ret && maxlen != ret)
  217. fortify_panic(__func__);
  218. return ret;
  219. }
  220. /* defined after fortified strlen to reuse it */
  221. extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
  222. __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
  223. {
  224. size_t ret;
  225. size_t p_size = __builtin_object_size(p, 0);
  226. size_t q_size = __builtin_object_size(q, 0);
  227. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  228. return __real_strlcpy(p, q, size);
  229. ret = strlen(q);
  230. if (size) {
  231. size_t len = (ret >= size) ? size - 1 : ret;
  232. if (__builtin_constant_p(len) && len >= p_size)
  233. __write_overflow();
  234. if (len >= p_size)
  235. fortify_panic(__func__);
  236. __builtin_memcpy(p, q, len);
  237. p[len] = '\0';
  238. }
  239. return ret;
  240. }
  241. /* defined after fortified strlen and strnlen to reuse them */
  242. __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
  243. {
  244. size_t p_len, copy_len;
  245. size_t p_size = __builtin_object_size(p, 0);
  246. size_t q_size = __builtin_object_size(q, 0);
  247. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  248. return __builtin_strncat(p, q, count);
  249. p_len = strlen(p);
  250. copy_len = strnlen(q, count);
  251. if (p_size < p_len + copy_len + 1)
  252. fortify_panic(__func__);
  253. __builtin_memcpy(p + p_len, q, copy_len);
  254. p[p_len + copy_len] = '\0';
  255. return p;
  256. }
  257. __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
  258. {
  259. size_t p_size = __builtin_object_size(p, 0);
  260. if (__builtin_constant_p(size) && p_size < size)
  261. __write_overflow();
  262. if (p_size < size)
  263. fortify_panic(__func__);
  264. return __builtin_memset(p, c, size);
  265. }
  266. __FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
  267. {
  268. size_t p_size = __builtin_object_size(p, 0);
  269. size_t q_size = __builtin_object_size(q, 0);
  270. if (__builtin_constant_p(size)) {
  271. if (p_size < size)
  272. __write_overflow();
  273. if (q_size < size)
  274. __read_overflow2();
  275. }
  276. if (p_size < size || q_size < size)
  277. fortify_panic(__func__);
  278. return __builtin_memcpy(p, q, size);
  279. }
  280. __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
  281. {
  282. size_t p_size = __builtin_object_size(p, 0);
  283. size_t q_size = __builtin_object_size(q, 0);
  284. if (__builtin_constant_p(size)) {
  285. if (p_size < size)
  286. __write_overflow();
  287. if (q_size < size)
  288. __read_overflow2();
  289. }
  290. if (p_size < size || q_size < size)
  291. fortify_panic(__func__);
  292. return __builtin_memmove(p, q, size);
  293. }
  294. extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
  295. __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
  296. {
  297. size_t p_size = __builtin_object_size(p, 0);
  298. if (__builtin_constant_p(size) && p_size < size)
  299. __read_overflow();
  300. if (p_size < size)
  301. fortify_panic(__func__);
  302. return __real_memscan(p, c, size);
  303. }
  304. __FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
  305. {
  306. size_t p_size = __builtin_object_size(p, 0);
  307. size_t q_size = __builtin_object_size(q, 0);
  308. if (__builtin_constant_p(size)) {
  309. if (p_size < size)
  310. __read_overflow();
  311. if (q_size < size)
  312. __read_overflow2();
  313. }
  314. if (p_size < size || q_size < size)
  315. fortify_panic(__func__);
  316. return __builtin_memcmp(p, q, size);
  317. }
  318. __FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
  319. {
  320. size_t p_size = __builtin_object_size(p, 0);
  321. if (__builtin_constant_p(size) && p_size < size)
  322. __read_overflow();
  323. if (p_size < size)
  324. fortify_panic(__func__);
  325. return __builtin_memchr(p, c, size);
  326. }
  327. void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
  328. __FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
  329. {
  330. size_t p_size = __builtin_object_size(p, 0);
  331. if (__builtin_constant_p(size) && p_size < size)
  332. __read_overflow();
  333. if (p_size < size)
  334. fortify_panic(__func__);
  335. return __real_memchr_inv(p, c, size);
  336. }
  337. extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
  338. __FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
  339. {
  340. size_t p_size = __builtin_object_size(p, 0);
  341. if (__builtin_constant_p(size) && p_size < size)
  342. __read_overflow();
  343. if (p_size < size)
  344. fortify_panic(__func__);
  345. return __real_kmemdup(p, size, gfp);
  346. }
  347. /* defined after fortified strlen and memcpy to reuse them */
  348. __FORTIFY_INLINE char *strcpy(char *p, const char *q)
  349. {
  350. size_t p_size = __builtin_object_size(p, 0);
  351. size_t q_size = __builtin_object_size(q, 0);
  352. if (p_size == (size_t)-1 && q_size == (size_t)-1)
  353. return __builtin_strcpy(p, q);
  354. memcpy(p, q, strlen(q) + 1);
  355. return p;
  356. }
  357. #endif
  358. #endif /* _LINUX_STRING_H_ */