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