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