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