1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STRING_H_
3 #define _LINUX_STRING_H_
6 #include <linux/compiler.h> /* for inline */
7 #include <linux/types.h> /* for size_t */
8 #include <linux/stddef.h> /* for NULL */
10 #include <uapi/linux/string.h>
12 extern char *strndup_user(const char __user
*, long);
13 extern void *memdup_user(const void __user
*, size_t);
14 extern void *vmemdup_user(const void __user
*, size_t);
15 extern void *memdup_user_nul(const void __user
*, size_t);
18 * Include machine specific inline routines
20 #include <asm/string.h>
22 #ifndef __HAVE_ARCH_STRCPY
23 extern char * strcpy(char *,const char *);
25 #ifndef __HAVE_ARCH_STRNCPY
26 extern char * strncpy(char *,const char *, __kernel_size_t
);
28 #ifndef __HAVE_ARCH_STRLCPY
29 size_t strlcpy(char *, const char *, size_t);
31 #ifndef __HAVE_ARCH_STRSCPY
32 ssize_t
strscpy(char *, const char *, size_t);
34 #ifndef __HAVE_ARCH_STRCAT
35 extern char * strcat(char *, const char *);
37 #ifndef __HAVE_ARCH_STRNCAT
38 extern char * strncat(char *, const char *, __kernel_size_t
);
40 #ifndef __HAVE_ARCH_STRLCAT
41 extern size_t strlcat(char *, const char *, __kernel_size_t
);
43 #ifndef __HAVE_ARCH_STRCMP
44 extern int strcmp(const char *,const char *);
46 #ifndef __HAVE_ARCH_STRNCMP
47 extern int strncmp(const char *,const char *,__kernel_size_t
);
49 #ifndef __HAVE_ARCH_STRCASECMP
50 extern int strcasecmp(const char *s1
, const char *s2
);
52 #ifndef __HAVE_ARCH_STRNCASECMP
53 extern int strncasecmp(const char *s1
, const char *s2
, size_t n
);
55 #ifndef __HAVE_ARCH_STRCHR
56 extern char * strchr(const char *,int);
58 #ifndef __HAVE_ARCH_STRCHRNUL
59 extern char * strchrnul(const char *,int);
61 #ifndef __HAVE_ARCH_STRNCHR
62 extern char * strnchr(const char *, size_t, int);
64 #ifndef __HAVE_ARCH_STRRCHR
65 extern char * strrchr(const char *,int);
67 extern char * __must_check
skip_spaces(const char *);
69 extern char *strim(char *);
71 static inline __must_check
char *strstrip(char *str
)
76 #ifndef __HAVE_ARCH_STRSTR
77 extern char * strstr(const char *, const char *);
79 #ifndef __HAVE_ARCH_STRNSTR
80 extern char * strnstr(const char *, const char *, size_t);
82 #ifndef __HAVE_ARCH_STRLEN
83 extern __kernel_size_t
strlen(const char *);
85 #ifndef __HAVE_ARCH_STRNLEN
86 extern __kernel_size_t
strnlen(const char *,__kernel_size_t
);
88 #ifndef __HAVE_ARCH_STRPBRK
89 extern char * strpbrk(const char *,const char *);
91 #ifndef __HAVE_ARCH_STRSEP
92 extern char * strsep(char **,const char *);
94 #ifndef __HAVE_ARCH_STRSPN
95 extern __kernel_size_t
strspn(const char *,const char *);
97 #ifndef __HAVE_ARCH_STRCSPN
98 extern __kernel_size_t
strcspn(const char *,const char *);
101 #ifndef __HAVE_ARCH_MEMSET
102 extern void * memset(void *,int,__kernel_size_t
);
105 #ifndef __HAVE_ARCH_MEMSET16
106 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t
);
109 #ifndef __HAVE_ARCH_MEMSET32
110 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t
);
113 #ifndef __HAVE_ARCH_MEMSET64
114 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t
);
117 static inline void *memset_l(unsigned long *p
, unsigned long v
,
120 if (BITS_PER_LONG
== 32)
121 return memset32((uint32_t *)p
, v
, n
);
123 return memset64((uint64_t *)p
, v
, n
);
126 static inline void *memset_p(void **p
, void *v
, __kernel_size_t n
)
128 if (BITS_PER_LONG
== 32)
129 return memset32((uint32_t *)p
, (uintptr_t)v
, n
);
131 return memset64((uint64_t *)p
, (uintptr_t)v
, n
);
134 #ifndef __HAVE_ARCH_MEMCPY
135 extern void * memcpy(void *,const void *,__kernel_size_t
);
137 #ifndef __HAVE_ARCH_MEMMOVE
138 extern void * memmove(void *,const void *,__kernel_size_t
);
140 #ifndef __HAVE_ARCH_MEMSCAN
141 extern void * memscan(void *,int,__kernel_size_t
);
143 #ifndef __HAVE_ARCH_MEMCMP
144 extern int memcmp(const void *,const void *,__kernel_size_t
);
146 #ifndef __HAVE_ARCH_MEMCHR
147 extern void * memchr(const void *,int,__kernel_size_t
);
149 #ifndef __HAVE_ARCH_MEMCPY_MCSAFE
150 static inline __must_check
int memcpy_mcsafe(void *dst
, const void *src
,
153 memcpy(dst
, src
, cnt
);
157 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
158 static inline void memcpy_flushcache(void *dst
, const void *src
, size_t cnt
)
160 memcpy(dst
, src
, cnt
);
163 void *memchr_inv(const void *s
, int c
, size_t n
);
164 char *strreplace(char *s
, char old
, char new);
166 extern void kfree_const(const void *x
);
168 extern char *kstrdup(const char *s
, gfp_t gfp
) __malloc
;
169 extern const char *kstrdup_const(const char *s
, gfp_t gfp
);
170 extern char *kstrndup(const char *s
, size_t len
, gfp_t gfp
);
171 extern void *kmemdup(const void *src
, size_t len
, gfp_t gfp
);
172 extern char *kmemdup_nul(const char *s
, size_t len
, gfp_t gfp
);
174 extern char **argv_split(gfp_t gfp
, const char *str
, int *argcp
);
175 extern void argv_free(char **argv
);
177 extern bool sysfs_streq(const char *s1
, const char *s2
);
178 extern int kstrtobool(const char *s
, bool *res
);
179 static inline int strtobool(const char *s
, bool *res
)
181 return kstrtobool(s
, res
);
184 int match_string(const char * const *array
, size_t n
, const char *string
);
185 int __sysfs_match_string(const char * const *array
, size_t n
, const char *s
);
188 * sysfs_match_string - matches given string in an array
189 * @_a: array of strings
190 * @_s: string to match with
192 * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
194 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
196 #ifdef CONFIG_BINARY_PRINTF
197 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
);
198 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
);
199 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...) __printf(3, 4);
202 extern ssize_t
memory_read_from_buffer(void *to
, size_t count
, loff_t
*ppos
,
203 const void *from
, size_t available
);
206 * strstarts - does @str start with @prefix?
207 * @str: string to examine
208 * @prefix: prefix to look for.
210 static inline bool strstarts(const char *str
, const char *prefix
)
212 return strncmp(str
, prefix
, strlen(prefix
)) == 0;
215 size_t memweight(const void *ptr
, size_t bytes
);
216 void memzero_explicit(void *s
, size_t count
);
219 * kbasename - return the last part of a pathname.
221 * @path: path to extract the filename from.
223 static inline const char *kbasename(const char *path
)
225 const char *tail
= strrchr(path
, '/');
226 return tail
? tail
+ 1 : path
;
229 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
230 #define __RENAME(x) __asm__(#x)
232 void fortify_panic(const char *name
) __noreturn __cold
;
233 void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
234 void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
235 void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
236 void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
238 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
239 __FORTIFY_INLINE
char *strncpy(char *p
, const char *q
, __kernel_size_t size
)
241 size_t p_size
= __builtin_object_size(p
, 0);
242 if (__builtin_constant_p(size
) && p_size
< size
)
245 fortify_panic(__func__
);
246 return __builtin_strncpy(p
, q
, size
);
249 __FORTIFY_INLINE
char *strcat(char *p
, const char *q
)
251 size_t p_size
= __builtin_object_size(p
, 0);
252 if (p_size
== (size_t)-1)
253 return __builtin_strcat(p
, q
);
254 if (strlcat(p
, q
, p_size
) >= p_size
)
255 fortify_panic(__func__
);
259 __FORTIFY_INLINE __kernel_size_t
strlen(const char *p
)
262 size_t p_size
= __builtin_object_size(p
, 0);
264 /* Work around gcc excess stack consumption issue */
265 if (p_size
== (size_t)-1 ||
266 (__builtin_constant_p(p
[p_size
- 1]) && p
[p_size
- 1] == '\0'))
267 return __builtin_strlen(p
);
268 ret
= strnlen(p
, p_size
);
270 fortify_panic(__func__
);
274 extern __kernel_size_t
__real_strnlen(const char *, __kernel_size_t
) __RENAME(strnlen
);
275 __FORTIFY_INLINE __kernel_size_t
strnlen(const char *p
, __kernel_size_t maxlen
)
277 size_t p_size
= __builtin_object_size(p
, 0);
278 __kernel_size_t ret
= __real_strnlen(p
, maxlen
< p_size
? maxlen
: p_size
);
279 if (p_size
<= ret
&& maxlen
!= ret
)
280 fortify_panic(__func__
);
284 /* defined after fortified strlen to reuse it */
285 extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy
);
286 __FORTIFY_INLINE
size_t strlcpy(char *p
, const char *q
, size_t size
)
289 size_t p_size
= __builtin_object_size(p
, 0);
290 size_t q_size
= __builtin_object_size(q
, 0);
291 if (p_size
== (size_t)-1 && q_size
== (size_t)-1)
292 return __real_strlcpy(p
, q
, size
);
295 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
296 if (__builtin_constant_p(len
) && len
>= p_size
)
299 fortify_panic(__func__
);
300 __builtin_memcpy(p
, q
, len
);
306 /* defined after fortified strlen and strnlen to reuse them */
307 __FORTIFY_INLINE
char *strncat(char *p
, const char *q
, __kernel_size_t count
)
309 size_t p_len
, copy_len
;
310 size_t p_size
= __builtin_object_size(p
, 0);
311 size_t q_size
= __builtin_object_size(q
, 0);
312 if (p_size
== (size_t)-1 && q_size
== (size_t)-1)
313 return __builtin_strncat(p
, q
, count
);
315 copy_len
= strnlen(q
, count
);
316 if (p_size
< p_len
+ copy_len
+ 1)
317 fortify_panic(__func__
);
318 __builtin_memcpy(p
+ p_len
, q
, copy_len
);
319 p
[p_len
+ copy_len
] = '\0';
323 __FORTIFY_INLINE
void *memset(void *p
, int c
, __kernel_size_t size
)
325 size_t p_size
= __builtin_object_size(p
, 0);
326 if (__builtin_constant_p(size
) && p_size
< size
)
329 fortify_panic(__func__
);
330 return __builtin_memset(p
, c
, size
);
333 __FORTIFY_INLINE
void *memcpy(void *p
, const void *q
, __kernel_size_t size
)
335 size_t p_size
= __builtin_object_size(p
, 0);
336 size_t q_size
= __builtin_object_size(q
, 0);
337 if (__builtin_constant_p(size
)) {
343 if (p_size
< size
|| q_size
< size
)
344 fortify_panic(__func__
);
345 return __builtin_memcpy(p
, q
, size
);
348 __FORTIFY_INLINE
void *memmove(void *p
, const void *q
, __kernel_size_t size
)
350 size_t p_size
= __builtin_object_size(p
, 0);
351 size_t q_size
= __builtin_object_size(q
, 0);
352 if (__builtin_constant_p(size
)) {
358 if (p_size
< size
|| q_size
< size
)
359 fortify_panic(__func__
);
360 return __builtin_memmove(p
, q
, size
);
363 extern void *__real_memscan(void *, int, __kernel_size_t
) __RENAME(memscan
);
364 __FORTIFY_INLINE
void *memscan(void *p
, int c
, __kernel_size_t size
)
366 size_t p_size
= __builtin_object_size(p
, 0);
367 if (__builtin_constant_p(size
) && p_size
< size
)
370 fortify_panic(__func__
);
371 return __real_memscan(p
, c
, size
);
374 __FORTIFY_INLINE
int memcmp(const void *p
, const void *q
, __kernel_size_t size
)
376 size_t p_size
= __builtin_object_size(p
, 0);
377 size_t q_size
= __builtin_object_size(q
, 0);
378 if (__builtin_constant_p(size
)) {
384 if (p_size
< size
|| q_size
< size
)
385 fortify_panic(__func__
);
386 return __builtin_memcmp(p
, q
, size
);
389 __FORTIFY_INLINE
void *memchr(const void *p
, int c
, __kernel_size_t size
)
391 size_t p_size
= __builtin_object_size(p
, 0);
392 if (__builtin_constant_p(size
) && p_size
< size
)
395 fortify_panic(__func__
);
396 return __builtin_memchr(p
, c
, size
);
399 void *__real_memchr_inv(const void *s
, int c
, size_t n
) __RENAME(memchr_inv
);
400 __FORTIFY_INLINE
void *memchr_inv(const void *p
, int c
, size_t size
)
402 size_t p_size
= __builtin_object_size(p
, 0);
403 if (__builtin_constant_p(size
) && p_size
< size
)
406 fortify_panic(__func__
);
407 return __real_memchr_inv(p
, c
, size
);
410 extern void *__real_kmemdup(const void *src
, size_t len
, gfp_t gfp
) __RENAME(kmemdup
);
411 __FORTIFY_INLINE
void *kmemdup(const void *p
, size_t size
, gfp_t gfp
)
413 size_t p_size
= __builtin_object_size(p
, 0);
414 if (__builtin_constant_p(size
) && p_size
< size
)
417 fortify_panic(__func__
);
418 return __real_kmemdup(p
, size
, gfp
);
421 /* defined after fortified strlen and memcpy to reuse them */
422 __FORTIFY_INLINE
char *strcpy(char *p
, const char *q
)
424 size_t p_size
= __builtin_object_size(p
, 0);
425 size_t q_size
= __builtin_object_size(q
, 0);
426 if (p_size
== (size_t)-1 && q_size
== (size_t)-1)
427 return __builtin_strcpy(p
, q
);
428 memcpy(p
, q
, strlen(q
) + 1);
435 * memcpy_and_pad - Copy one buffer to another with padding
436 * @dest: Where to copy to
437 * @dest_len: The destination buffer size
438 * @src: Where to copy from
439 * @count: The number of bytes to copy
440 * @pad: Character to use for padding if space is left in destination.
442 static inline void memcpy_and_pad(void *dest
, size_t dest_len
,
443 const void *src
, size_t count
, int pad
)
445 if (dest_len
> count
) {
446 memcpy(dest
, src
, count
);
447 memset(dest
+ count
, pad
, dest_len
- count
);
449 memcpy(dest
, src
, dest_len
);
452 #endif /* _LINUX_STRING_H_ */