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