1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STRING_H_
3 #define _LINUX_STRING_H_
5 #include <linux/array_size.h>
6 #include <linux/compiler.h> /* for inline */
7 #include <linux/types.h> /* for size_t */
8 #include <linux/stddef.h> /* for NULL */
9 #include <linux/err.h> /* for ERR_PTR() */
10 #include <linux/errno.h> /* for E2BIG */
11 #include <linux/overflow.h> /* for check_mul_overflow() */
12 #include <linux/stdarg.h>
13 #include <uapi/linux/string.h>
15 extern char *strndup_user(const char __user *, long);
16 extern void *memdup_user(const void __user *, size_t);
17 extern void *vmemdup_user(const void __user *, size_t);
18 extern void *memdup_user_nul(const void __user *, size_t);
21 * memdup_array_user - duplicate array from user space
22 * @src: source address in user space
23 * @n: number of array members to copy
24 * @size: size of one array member
26 * Return: an ERR_PTR() on failure. Result is physically
27 * contiguous, to be freed by kfree().
29 static inline void *memdup_array_user(const void __user *src, size_t n, size_t size)
33 if (check_mul_overflow(n, size, &nbytes))
34 return ERR_PTR(-EOVERFLOW);
36 return memdup_user(src, nbytes);
40 * vmemdup_array_user - duplicate array from user space
41 * @src: source address in user space
42 * @n: number of array members to copy
43 * @size: size of one array member
45 * Return: an ERR_PTR() on failure. Result may be not
46 * physically contiguous. Use kvfree() to free.
48 static inline void *vmemdup_array_user(const void __user *src, size_t n, size_t size)
52 if (check_mul_overflow(n, size, &nbytes))
53 return ERR_PTR(-EOVERFLOW);
55 return vmemdup_user(src, nbytes);
59 * Include machine specific inline routines
61 #include <asm/string.h>
63 #ifndef __HAVE_ARCH_STRCPY
64 extern char * strcpy(char *,const char *);
66 #ifndef __HAVE_ARCH_STRNCPY
67 extern char * strncpy(char *,const char *, __kernel_size_t);
69 #ifndef __HAVE_ARCH_STRLCPY
70 size_t strlcpy(char *, const char *, size_t);
72 #ifndef __HAVE_ARCH_STRSCPY
73 ssize_t strscpy(char *, const char *, size_t);
76 /* Wraps calls to strscpy()/memset(), no arch specific code required */
77 ssize_t strscpy_pad(char *dest, const char *src, size_t count);
79 #ifndef __HAVE_ARCH_STRCAT
80 extern char * strcat(char *, const char *);
82 #ifndef __HAVE_ARCH_STRNCAT
83 extern char * strncat(char *, const char *, __kernel_size_t);
85 #ifndef __HAVE_ARCH_STRLCAT
86 extern size_t strlcat(char *, const char *, __kernel_size_t);
88 #ifndef __HAVE_ARCH_STRCMP
89 extern int strcmp(const char *,const char *);
91 #ifndef __HAVE_ARCH_STRNCMP
92 extern int strncmp(const char *,const char *,__kernel_size_t);
94 #ifndef __HAVE_ARCH_STRCASECMP
95 extern int strcasecmp(const char *s1, const char *s2);
97 #ifndef __HAVE_ARCH_STRNCASECMP
98 extern int strncasecmp(const char *s1, const char *s2, size_t n);
100 #ifndef __HAVE_ARCH_STRCHR
101 extern char * strchr(const char *,int);
103 #ifndef __HAVE_ARCH_STRCHRNUL
104 extern char * strchrnul(const char *,int);
106 extern char * strnchrnul(const char *, size_t, int);
107 #ifndef __HAVE_ARCH_STRNCHR
108 extern char * strnchr(const char *, size_t, int);
110 #ifndef __HAVE_ARCH_STRRCHR
111 extern char * strrchr(const char *,int);
113 extern char * __must_check skip_spaces(const char *);
115 extern char *strim(char *);
117 static inline __must_check char *strstrip(char *str)
122 #ifndef __HAVE_ARCH_STRSTR
123 extern char * strstr(const char *, const char *);
125 #ifndef __HAVE_ARCH_STRNSTR
126 extern char * strnstr(const char *, const char *, size_t);
128 #ifndef __HAVE_ARCH_STRLEN
129 extern __kernel_size_t strlen(const char *);
131 #ifndef __HAVE_ARCH_STRNLEN
132 extern __kernel_size_t strnlen(const char *,__kernel_size_t);
134 #ifndef __HAVE_ARCH_STRPBRK
135 extern char * strpbrk(const char *,const char *);
137 #ifndef __HAVE_ARCH_STRSEP
138 extern char * strsep(char **,const char *);
140 #ifndef __HAVE_ARCH_STRSPN
141 extern __kernel_size_t strspn(const char *,const char *);
143 #ifndef __HAVE_ARCH_STRCSPN
144 extern __kernel_size_t strcspn(const char *,const char *);
147 #ifndef __HAVE_ARCH_MEMSET
148 extern void * memset(void *,int,__kernel_size_t);
151 #ifndef __HAVE_ARCH_MEMSET16
152 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
155 #ifndef __HAVE_ARCH_MEMSET32
156 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
159 #ifndef __HAVE_ARCH_MEMSET64
160 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
163 static inline void *memset_l(unsigned long *p, unsigned long v,
166 if (BITS_PER_LONG == 32)
167 return memset32((uint32_t *)p, v, n);
169 return memset64((uint64_t *)p, v, n);
172 static inline void *memset_p(void **p, void *v, __kernel_size_t n)
174 if (BITS_PER_LONG == 32)
175 return memset32((uint32_t *)p, (uintptr_t)v, n);
177 return memset64((uint64_t *)p, (uintptr_t)v, n);
180 extern void **__memcat_p(void **a, void **b);
181 #define memcat_p(a, b) ({ \
182 BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \
183 "type mismatch in memcat_p()"); \
184 (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \
187 #ifndef __HAVE_ARCH_MEMCPY
188 extern void * memcpy(void *,const void *,__kernel_size_t);
190 #ifndef __HAVE_ARCH_MEMMOVE
191 extern void * memmove(void *,const void *,__kernel_size_t);
193 #ifndef __HAVE_ARCH_MEMSCAN
194 extern void * memscan(void *,int,__kernel_size_t);
196 #ifndef __HAVE_ARCH_MEMCMP
197 extern int memcmp(const void *,const void *,__kernel_size_t);
199 #ifndef __HAVE_ARCH_BCMP
200 extern int bcmp(const void *,const void *,__kernel_size_t);
202 #ifndef __HAVE_ARCH_MEMCHR
203 extern void * memchr(const void *,int,__kernel_size_t);
205 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
206 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
208 memcpy(dst, src, cnt);
212 void *memchr_inv(const void *s, int c, size_t n);
213 char *strreplace(char *str, char old, char new);
215 extern void kfree_const(const void *x);
217 extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
218 extern const char *kstrdup_const(const char *s, gfp_t gfp);
219 extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
220 extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
221 extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
222 extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
224 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
225 extern void argv_free(char **argv);
227 extern bool sysfs_streq(const char *s1, const char *s2);
228 int match_string(const char * const *array, size_t n, const char *string);
229 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
232 * sysfs_match_string - matches given string in an array
233 * @_a: array of strings
234 * @_s: string to match with
236 * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
238 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
240 #ifdef CONFIG_BINARY_PRINTF
241 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
242 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
243 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
246 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
247 const void *from, size_t available);
249 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
252 * strstarts - does @str start with @prefix?
253 * @str: string to examine
254 * @prefix: prefix to look for.
256 static inline bool strstarts(const char *str, const char *prefix)
258 return strncmp(str, prefix, strlen(prefix)) == 0;
261 size_t memweight(const void *ptr, size_t bytes);
264 * memzero_explicit - Fill a region of memory (e.g. sensitive
265 * keying data) with 0s.
266 * @s: Pointer to the start of the area.
267 * @count: The size of the area.
269 * Note: usually using memset() is just fine (!), but in cases
270 * where clearing out _local_ data at the end of a scope is
271 * necessary, memzero_explicit() should be used instead in
272 * order to prevent the compiler from optimising away zeroing.
274 * memzero_explicit() doesn't need an arch-specific version as
275 * it just invokes the one of memset() implicitly.
277 static inline void memzero_explicit(void *s, size_t count)
284 * kbasename - return the last part of a pathname.
286 * @path: path to extract the filename from.
288 static inline const char *kbasename(const char *path)
290 const char *tail = strrchr(path, '/');
291 return tail ? tail + 1 : path;
294 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
295 #include <linux/fortify-string.h>
297 #ifndef unsafe_memcpy
298 #define unsafe_memcpy(dst, src, bytes, justification) \
299 memcpy(dst, src, bytes)
302 void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
306 * strtomem_pad - Copy NUL-terminated string to non-NUL-terminated buffer
308 * @dest: Pointer of destination character array (marked as __nonstring)
309 * @src: Pointer to NUL-terminated string
310 * @pad: Padding character to fill any remaining bytes of @dest after copy
312 * This is a replacement for strncpy() uses where the destination is not
313 * a NUL-terminated string, but with bounds checking on the source size, and
314 * an explicit padding character. If padding is not required, use strtomem().
316 * Note that the size of @dest is not an argument, as the length of @dest
317 * must be discoverable by the compiler.
319 #define strtomem_pad(dest, src, pad) do { \
320 const size_t _dest_len = __builtin_object_size(dest, 1); \
321 const size_t _src_len = __builtin_object_size(src, 1); \
323 BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
324 _dest_len == (size_t)-1); \
325 memcpy_and_pad(dest, _dest_len, src, \
326 strnlen(src, min(_src_len, _dest_len)), pad); \
330 * strtomem - Copy NUL-terminated string to non-NUL-terminated buffer
332 * @dest: Pointer of destination character array (marked as __nonstring)
333 * @src: Pointer to NUL-terminated string
335 * This is a replacement for strncpy() uses where the destination is not
336 * a NUL-terminated string, but with bounds checking on the source size, and
337 * without trailing padding. If padding is required, use strtomem_pad().
339 * Note that the size of @dest is not an argument, as the length of @dest
340 * must be discoverable by the compiler.
342 #define strtomem(dest, src) do { \
343 const size_t _dest_len = __builtin_object_size(dest, 1); \
344 const size_t _src_len = __builtin_object_size(src, 1); \
346 BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
347 _dest_len == (size_t)-1); \
348 memcpy(dest, src, strnlen(src, min(_src_len, _dest_len))); \
352 * memset_after - Set a value after a struct member to the end of a struct
354 * @obj: Address of target struct instance
355 * @v: Byte value to repeatedly write
356 * @member: after which struct member to start writing bytes
358 * This is good for clearing padding following the given member.
360 #define memset_after(obj, v, member) \
362 u8 *__ptr = (u8 *)(obj); \
363 typeof(v) __val = (v); \
364 memset(__ptr + offsetofend(typeof(*(obj)), member), __val, \
365 sizeof(*(obj)) - offsetofend(typeof(*(obj)), member)); \
369 * memset_startat - Set a value starting at a member to the end of a struct
371 * @obj: Address of target struct instance
372 * @v: Byte value to repeatedly write
373 * @member: struct member to start writing at
375 * Note that if there is padding between the prior member and the target
376 * member, memset_after() should be used to clear the prior padding.
378 #define memset_startat(obj, v, member) \
380 u8 *__ptr = (u8 *)(obj); \
381 typeof(v) __val = (v); \
382 memset(__ptr + offsetof(typeof(*(obj)), member), __val, \
383 sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \
387 * str_has_prefix - Test if a string has a given prefix
388 * @str: The string to test
389 * @prefix: The string to see if @str starts with
391 * A common way to test a prefix of a string is to do:
392 * strncmp(str, prefix, sizeof(prefix) - 1)
394 * But this can lead to bugs due to typos, or if prefix is a pointer
395 * and not a constant. Instead use str_has_prefix().
398 * * strlen(@prefix) if @str starts with @prefix
399 * * 0 if @str does not start with @prefix
401 static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
403 size_t len = strlen(prefix);
404 return strncmp(str, prefix, len) == 0 ? len : 0;
407 #endif /* _LINUX_STRING_H_ */