1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STRING_H_
3 #define _LINUX_STRING_H_
5 #include <linux/args.h>
6 #include <linux/array_size.h>
7 #include <linux/compiler.h> /* for inline */
8 #include <linux/types.h> /* for size_t */
9 #include <linux/stddef.h> /* for NULL */
10 #include <linux/err.h> /* for ERR_PTR() */
11 #include <linux/errno.h> /* for E2BIG */
12 #include <linux/overflow.h> /* for check_mul_overflow() */
13 #include <linux/stdarg.h>
14 #include <uapi/linux/string.h>
16 extern char *strndup_user(const char __user *, long);
17 extern void *memdup_user(const void __user *, size_t) __realloc_size(2);
18 extern void *vmemdup_user(const void __user *, size_t) __realloc_size(2);
19 extern void *memdup_user_nul(const void __user *, size_t);
22 * memdup_array_user - duplicate array from user space
23 * @src: source address in user space
24 * @n: number of array members to copy
25 * @size: size of one array member
27 * Return: an ERR_PTR() on failure. Result is physically
28 * contiguous, to be freed by kfree().
30 static inline __realloc_size(2, 3)
31 void *memdup_array_user(const void __user *src, size_t n, size_t size)
35 if (check_mul_overflow(n, size, &nbytes))
36 return ERR_PTR(-EOVERFLOW);
38 return memdup_user(src, nbytes);
42 * vmemdup_array_user - duplicate array from user space
43 * @src: source address in user space
44 * @n: number of array members to copy
45 * @size: size of one array member
47 * Return: an ERR_PTR() on failure. Result may be not
48 * physically contiguous. Use kvfree() to free.
50 static inline __realloc_size(2, 3)
51 void *vmemdup_array_user(const void __user *src, size_t n, size_t size)
55 if (check_mul_overflow(n, size, &nbytes))
56 return ERR_PTR(-EOVERFLOW);
58 return vmemdup_user(src, nbytes);
62 * Include machine specific inline routines
64 #include <asm/string.h>
66 #ifndef __HAVE_ARCH_STRCPY
67 extern char * strcpy(char *,const char *);
69 #ifndef __HAVE_ARCH_STRNCPY
70 extern char * strncpy(char *,const char *, __kernel_size_t);
72 ssize_t sized_strscpy(char *, const char *, size_t);
75 * The 2 argument style can only be used when dst is an array with a
78 #define __strscpy0(dst, src, ...) \
79 sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst))
80 #define __strscpy1(dst, src, size) sized_strscpy(dst, src, size)
82 #define __strscpy_pad0(dst, src, ...) \
83 sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst))
84 #define __strscpy_pad1(dst, src, size) sized_strscpy_pad(dst, src, size)
87 * strscpy - Copy a C-string into a sized buffer
88 * @dst: Where to copy the string to
89 * @src: Where to copy the string from
90 * @...: Size of destination buffer (optional)
92 * Copy the source string @src, or as much of it as fits, into the
93 * destination @dst buffer. The behavior is undefined if the string
94 * buffers overlap. The destination @dst buffer is always NUL terminated,
95 * unless it's zero-sized.
97 * The size argument @... is only required when @dst is not an array, or
98 * when the copy needs to be smaller than sizeof(@dst).
100 * Preferred to strncpy() since it always returns a valid string, and
101 * doesn't unnecessarily force the tail of the destination buffer to be
102 * zero padded. If padding is desired please use strscpy_pad().
104 * Returns the number of characters copied in @dst (not including the
105 * trailing %NUL) or -E2BIG if @size is 0 or the copy from @src was
108 #define strscpy(dst, src, ...) \
109 CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
111 #define sized_strscpy_pad(dest, src, count) ({ \
112 char *__dst = (dest); \
113 const char *__src = (src); \
114 const size_t __count = (count); \
117 __wrote = sized_strscpy(__dst, __src, __count); \
118 if (__wrote >= 0 && __wrote < __count) \
119 memset(__dst + __wrote + 1, 0, __count - __wrote - 1); \
124 * strscpy_pad() - Copy a C-string into a sized buffer
125 * @dst: Where to copy the string to
126 * @src: Where to copy the string from
127 * @...: Size of destination buffer
129 * Copy the string, or as much of it as fits, into the dest buffer. The
130 * behavior is undefined if the string buffers overlap. The destination
131 * buffer is always %NUL terminated, unless it's zero-sized.
133 * If the source string is shorter than the destination buffer, the
134 * remaining bytes in the buffer will be filled with %NUL bytes.
136 * For full explanation of why you may want to consider using the
137 * 'strscpy' functions please see the function docstring for strscpy().
140 * * The number of characters copied (not including the trailing %NULs)
141 * * -E2BIG if count is 0 or @src was truncated.
143 #define strscpy_pad(dst, src, ...) \
144 CONCATENATE(__strscpy_pad, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
146 #ifndef __HAVE_ARCH_STRCAT
147 extern char * strcat(char *, const char *);
149 #ifndef __HAVE_ARCH_STRNCAT
150 extern char * strncat(char *, const char *, __kernel_size_t);
152 #ifndef __HAVE_ARCH_STRLCAT
153 extern size_t strlcat(char *, const char *, __kernel_size_t);
155 #ifndef __HAVE_ARCH_STRCMP
156 extern int strcmp(const char *,const char *);
158 #ifndef __HAVE_ARCH_STRNCMP
159 extern int strncmp(const char *,const char *,__kernel_size_t);
161 #ifndef __HAVE_ARCH_STRCASECMP
162 extern int strcasecmp(const char *s1, const char *s2);
164 #ifndef __HAVE_ARCH_STRNCASECMP
165 extern int strncasecmp(const char *s1, const char *s2, size_t n);
167 #ifndef __HAVE_ARCH_STRCHR
168 extern char * strchr(const char *,int);
170 #ifndef __HAVE_ARCH_STRCHRNUL
171 extern char * strchrnul(const char *,int);
173 extern char * strnchrnul(const char *, size_t, int);
174 #ifndef __HAVE_ARCH_STRNCHR
175 extern char * strnchr(const char *, size_t, int);
177 #ifndef __HAVE_ARCH_STRRCHR
178 extern char * strrchr(const char *,int);
180 extern char * __must_check skip_spaces(const char *);
182 extern char *strim(char *);
184 static inline __must_check char *strstrip(char *str)
189 #ifndef __HAVE_ARCH_STRSTR
190 extern char * strstr(const char *, const char *);
192 #ifndef __HAVE_ARCH_STRNSTR
193 extern char * strnstr(const char *, const char *, size_t);
195 #ifndef __HAVE_ARCH_STRLEN
196 extern __kernel_size_t strlen(const char *);
198 #ifndef __HAVE_ARCH_STRNLEN
199 extern __kernel_size_t strnlen(const char *,__kernel_size_t);
201 #ifndef __HAVE_ARCH_STRPBRK
202 extern char * strpbrk(const char *,const char *);
204 #ifndef __HAVE_ARCH_STRSEP
205 extern char * strsep(char **,const char *);
207 #ifndef __HAVE_ARCH_STRSPN
208 extern __kernel_size_t strspn(const char *,const char *);
210 #ifndef __HAVE_ARCH_STRCSPN
211 extern __kernel_size_t strcspn(const char *,const char *);
214 #ifndef __HAVE_ARCH_MEMSET
215 extern void * memset(void *,int,__kernel_size_t);
218 #ifndef __HAVE_ARCH_MEMSET16
219 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
222 #ifndef __HAVE_ARCH_MEMSET32
223 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
226 #ifndef __HAVE_ARCH_MEMSET64
227 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
230 static inline void *memset_l(unsigned long *p, unsigned long v,
233 if (BITS_PER_LONG == 32)
234 return memset32((uint32_t *)p, v, n);
236 return memset64((uint64_t *)p, v, n);
239 static inline void *memset_p(void **p, void *v, __kernel_size_t n)
241 if (BITS_PER_LONG == 32)
242 return memset32((uint32_t *)p, (uintptr_t)v, n);
244 return memset64((uint64_t *)p, (uintptr_t)v, n);
247 extern void **__memcat_p(void **a, void **b);
248 #define memcat_p(a, b) ({ \
249 BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \
250 "type mismatch in memcat_p()"); \
251 (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \
254 #ifndef __HAVE_ARCH_MEMCPY
255 extern void * memcpy(void *,const void *,__kernel_size_t);
257 #ifndef __HAVE_ARCH_MEMMOVE
258 extern void * memmove(void *,const void *,__kernel_size_t);
260 #ifndef __HAVE_ARCH_MEMSCAN
261 extern void * memscan(void *,int,__kernel_size_t);
263 #ifndef __HAVE_ARCH_MEMCMP
264 extern int memcmp(const void *,const void *,__kernel_size_t);
266 #ifndef __HAVE_ARCH_BCMP
267 extern int bcmp(const void *,const void *,__kernel_size_t);
269 #ifndef __HAVE_ARCH_MEMCHR
270 extern void * memchr(const void *,int,__kernel_size_t);
272 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
273 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
275 memcpy(dst, src, cnt);
279 void *memchr_inv(const void *s, int c, size_t n);
280 char *strreplace(char *str, char old, char new);
282 extern void kfree_const(const void *x);
284 extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
285 extern const char *kstrdup_const(const char *s, gfp_t gfp);
286 extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
287 extern void *kmemdup_noprof(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
288 #define kmemdup(...) alloc_hooks(kmemdup_noprof(__VA_ARGS__))
290 extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
291 extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
292 extern void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp)
293 __realloc_size(2, 3);
295 /* lib/argv_split.c */
296 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
297 extern void argv_free(char **argv);
300 extern int get_option(char **str, int *pint);
301 extern char *get_options(const char *str, int nints, int *ints);
302 extern unsigned long long memparse(const char *ptr, char **retptr);
303 extern bool parse_option_str(const char *str, const char *option);
304 extern char *next_arg(char *args, char **param, char **val);
306 extern bool sysfs_streq(const char *s1, const char *s2);
307 int match_string(const char * const *array, size_t n, const char *string);
308 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
311 * sysfs_match_string - matches given string in an array
312 * @_a: array of strings
313 * @_s: string to match with
315 * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
317 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
319 #ifdef CONFIG_BINARY_PRINTF
320 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
321 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
322 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
325 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
326 const void *from, size_t available);
328 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
331 * strstarts - does @str start with @prefix?
332 * @str: string to examine
333 * @prefix: prefix to look for.
335 static inline bool strstarts(const char *str, const char *prefix)
337 return strncmp(str, prefix, strlen(prefix)) == 0;
340 size_t memweight(const void *ptr, size_t bytes);
343 * memzero_explicit - Fill a region of memory (e.g. sensitive
344 * keying data) with 0s.
345 * @s: Pointer to the start of the area.
346 * @count: The size of the area.
348 * Note: usually using memset() is just fine (!), but in cases
349 * where clearing out _local_ data at the end of a scope is
350 * necessary, memzero_explicit() should be used instead in
351 * order to prevent the compiler from optimising away zeroing.
353 * memzero_explicit() doesn't need an arch-specific version as
354 * it just invokes the one of memset() implicitly.
356 static inline void memzero_explicit(void *s, size_t count)
363 * kbasename - return the last part of a pathname.
365 * @path: path to extract the filename from.
367 static inline const char *kbasename(const char *path)
369 const char *tail = strrchr(path, '/');
370 return tail ? tail + 1 : path;
373 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
374 #include <linux/fortify-string.h>
376 #ifndef unsafe_memcpy
377 #define unsafe_memcpy(dst, src, bytes, justification) \
378 memcpy(dst, src, bytes)
381 void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
385 * strtomem_pad - Copy NUL-terminated string to non-NUL-terminated buffer
387 * @dest: Pointer of destination character array (marked as __nonstring)
388 * @src: Pointer to NUL-terminated string
389 * @pad: Padding character to fill any remaining bytes of @dest after copy
391 * This is a replacement for strncpy() uses where the destination is not
392 * a NUL-terminated string, but with bounds checking on the source size, and
393 * an explicit padding character. If padding is not required, use strtomem().
395 * Note that the size of @dest is not an argument, as the length of @dest
396 * must be discoverable by the compiler.
398 #define strtomem_pad(dest, src, pad) do { \
399 const size_t _dest_len = __builtin_object_size(dest, 1); \
400 const size_t _src_len = __builtin_object_size(src, 1); \
402 BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
403 _dest_len == (size_t)-1); \
404 memcpy_and_pad(dest, _dest_len, src, \
405 strnlen(src, min(_src_len, _dest_len)), pad); \
409 * strtomem - Copy NUL-terminated string to non-NUL-terminated buffer
411 * @dest: Pointer of destination character array (marked as __nonstring)
412 * @src: Pointer to NUL-terminated string
414 * This is a replacement for strncpy() uses where the destination is not
415 * a NUL-terminated string, but with bounds checking on the source size, and
416 * without trailing padding. If padding is required, use strtomem_pad().
418 * Note that the size of @dest is not an argument, as the length of @dest
419 * must be discoverable by the compiler.
421 #define strtomem(dest, src) do { \
422 const size_t _dest_len = __builtin_object_size(dest, 1); \
423 const size_t _src_len = __builtin_object_size(src, 1); \
425 BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
426 _dest_len == (size_t)-1); \
427 memcpy(dest, src, strnlen(src, min(_src_len, _dest_len))); \
431 * memtostr - Copy a possibly non-NUL-term string to a NUL-term string
432 * @dest: Pointer to destination NUL-terminates string
433 * @src: Pointer to character array (likely marked as __nonstring)
435 * This is a replacement for strncpy() uses where the source is not
436 * a NUL-terminated string.
438 * Note that sizes of @dest and @src must be known at compile-time.
440 #define memtostr(dest, src) do { \
441 const size_t _dest_len = __builtin_object_size(dest, 1); \
442 const size_t _src_len = __builtin_object_size(src, 1); \
443 const size_t _src_chars = strnlen(src, _src_len); \
444 const size_t _copy_len = min(_dest_len - 1, _src_chars); \
446 BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
447 !__builtin_constant_p(_src_len) || \
448 _dest_len == 0 || _dest_len == (size_t)-1 || \
449 _src_len == 0 || _src_len == (size_t)-1); \
450 memcpy(dest, src, _copy_len); \
451 dest[_copy_len] = '\0'; \
455 * memtostr_pad - Copy a possibly non-NUL-term string to a NUL-term string
456 * with NUL padding in the destination
457 * @dest: Pointer to destination NUL-terminates string
458 * @src: Pointer to character array (likely marked as __nonstring)
460 * This is a replacement for strncpy() uses where the source is not
461 * a NUL-terminated string.
463 * Note that sizes of @dest and @src must be known at compile-time.
465 #define memtostr_pad(dest, src) do { \
466 const size_t _dest_len = __builtin_object_size(dest, 1); \
467 const size_t _src_len = __builtin_object_size(src, 1); \
468 const size_t _src_chars = strnlen(src, _src_len); \
469 const size_t _copy_len = min(_dest_len - 1, _src_chars); \
471 BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
472 !__builtin_constant_p(_src_len) || \
473 _dest_len == 0 || _dest_len == (size_t)-1 || \
474 _src_len == 0 || _src_len == (size_t)-1); \
475 memcpy(dest, src, _copy_len); \
476 memset(&dest[_copy_len], 0, _dest_len - _copy_len); \
480 * memset_after - Set a value after a struct member to the end of a struct
482 * @obj: Address of target struct instance
483 * @v: Byte value to repeatedly write
484 * @member: after which struct member to start writing bytes
486 * This is good for clearing padding following the given member.
488 #define memset_after(obj, v, member) \
490 u8 *__ptr = (u8 *)(obj); \
491 typeof(v) __val = (v); \
492 memset(__ptr + offsetofend(typeof(*(obj)), member), __val, \
493 sizeof(*(obj)) - offsetofend(typeof(*(obj)), member)); \
497 * memset_startat - Set a value starting at a member to the end of a struct
499 * @obj: Address of target struct instance
500 * @v: Byte value to repeatedly write
501 * @member: struct member to start writing at
503 * Note that if there is padding between the prior member and the target
504 * member, memset_after() should be used to clear the prior padding.
506 #define memset_startat(obj, v, member) \
508 u8 *__ptr = (u8 *)(obj); \
509 typeof(v) __val = (v); \
510 memset(__ptr + offsetof(typeof(*(obj)), member), __val, \
511 sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \
515 * str_has_prefix - Test if a string has a given prefix
516 * @str: The string to test
517 * @prefix: The string to see if @str starts with
519 * A common way to test a prefix of a string is to do:
520 * strncmp(str, prefix, sizeof(prefix) - 1)
522 * But this can lead to bugs due to typos, or if prefix is a pointer
523 * and not a constant. Instead use str_has_prefix().
526 * * strlen(@prefix) if @str starts with @prefix
527 * * 0 if @str does not start with @prefix
529 static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
531 size_t len = strlen(prefix);
532 return strncmp(str, prefix, len) == 0 ? len : 0;
535 #endif /* _LINUX_STRING_H_ */