blob: cfd83eb2f926c74622f46ed931bb1c58277df49f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_STRING_H_
3#define _LINUX_STRING_H_
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6#include <linux/compiler.h> /* for inline */
7#include <linux/types.h> /* for size_t */
8#include <linux/stddef.h> /* for NULL */
Lai Jiangshan4370aa42009-03-06 17:21:46 +01009#include <stdarg.h>
David Howells607ca462012-10-13 10:46:48 +010010#include <uapi/linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Davi Arnaut96840aa2006-03-24 03:18:42 -080012extern char *strndup_user(const char __user *, long);
Li Zefan610a77e2009-03-31 15:23:16 -070013extern void *memdup_user(const void __user *, size_t);
Al Viroe9d408e2015-12-24 00:06:05 -050014extern void *memdup_user_nul(const void __user *, size_t);
Davi Arnaut96840aa2006-03-24 03:18:42 -080015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/*
17 * Include machine specific inline routines
18 */
19#include <asm/string.h>
20
21#ifndef __HAVE_ARCH_STRCPY
22extern char * strcpy(char *,const char *);
23#endif
24#ifndef __HAVE_ARCH_STRNCPY
25extern char * strncpy(char *,const char *, __kernel_size_t);
26#endif
27#ifndef __HAVE_ARCH_STRLCPY
28size_t strlcpy(char *, const char *, size_t);
29#endif
Chris Metcalf30035e42015-04-29 12:52:04 -040030#ifndef __HAVE_ARCH_STRSCPY
31ssize_t __must_check strscpy(char *, const char *, size_t);
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#ifndef __HAVE_ARCH_STRCAT
34extern char * strcat(char *, const char *);
35#endif
36#ifndef __HAVE_ARCH_STRNCAT
37extern char * strncat(char *, const char *, __kernel_size_t);
38#endif
39#ifndef __HAVE_ARCH_STRLCAT
40extern size_t strlcat(char *, const char *, __kernel_size_t);
41#endif
42#ifndef __HAVE_ARCH_STRCMP
43extern int strcmp(const char *,const char *);
44#endif
45#ifndef __HAVE_ARCH_STRNCMP
46extern int strncmp(const char *,const char *,__kernel_size_t);
47#endif
David S. Millerded220b2007-03-29 01:18:42 -070048#ifndef __HAVE_ARCH_STRCASECMP
49extern int strcasecmp(const char *s1, const char *s2);
50#endif
51#ifndef __HAVE_ARCH_STRNCASECMP
52extern int strncasecmp(const char *s1, const char *s2, size_t n);
53#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#ifndef __HAVE_ARCH_STRCHR
55extern char * strchr(const char *,int);
56#endif
Grant Likely11d200e2014-03-14 17:00:14 +000057#ifndef __HAVE_ARCH_STRCHRNUL
58extern char * strchrnul(const char *,int);
59#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifndef __HAVE_ARCH_STRNCHR
61extern char * strnchr(const char *, size_t, int);
62#endif
63#ifndef __HAVE_ARCH_STRRCHR
64extern char * strrchr(const char *,int);
65#endif
André Goddard Rosaf6533982009-12-14 18:01:04 -080066extern char * __must_check skip_spaces(const char *);
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -080067
68extern char *strim(char *);
69
70static inline __must_check char *strstrip(char *str)
71{
72 return strim(str);
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#ifndef __HAVE_ARCH_STRSTR
Li Zefand5f1fb52010-01-14 10:53:55 +080076extern char * strstr(const char *, const char *);
77#endif
78#ifndef __HAVE_ARCH_STRNSTR
79extern char * strnstr(const char *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#endif
81#ifndef __HAVE_ARCH_STRLEN
82extern __kernel_size_t strlen(const char *);
83#endif
84#ifndef __HAVE_ARCH_STRNLEN
85extern __kernel_size_t strnlen(const char *,__kernel_size_t);
86#endif
Kyle McMartin8833d322006-04-10 22:53:57 -070087#ifndef __HAVE_ARCH_STRPBRK
88extern char * strpbrk(const char *,const char *);
89#endif
90#ifndef __HAVE_ARCH_STRSEP
91extern char * strsep(char **,const char *);
92#endif
93#ifndef __HAVE_ARCH_STRSPN
94extern __kernel_size_t strspn(const char *,const char *);
95#endif
96#ifndef __HAVE_ARCH_STRCSPN
97extern __kernel_size_t strcspn(const char *,const char *);
98#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#ifndef __HAVE_ARCH_MEMSET
101extern void * memset(void *,int,__kernel_size_t);
102#endif
Matthew Wilcox3b3c4ba2017-09-08 16:13:48 -0700103
104#ifndef __HAVE_ARCH_MEMSET16
105extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
106#endif
107
108#ifndef __HAVE_ARCH_MEMSET32
109extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
110#endif
111
112#ifndef __HAVE_ARCH_MEMSET64
113extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
114#endif
115
116static inline void *memset_l(unsigned long *p, unsigned long v,
117 __kernel_size_t n)
118{
119 if (BITS_PER_LONG == 32)
120 return memset32((uint32_t *)p, v, n);
121 else
122 return memset64((uint64_t *)p, v, n);
123}
124
125static inline void *memset_p(void **p, void *v, __kernel_size_t n)
126{
127 if (BITS_PER_LONG == 32)
128 return memset32((uint32_t *)p, (uintptr_t)v, n);
129 else
130 return memset64((uint64_t *)p, (uintptr_t)v, n);
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#ifndef __HAVE_ARCH_MEMCPY
134extern void * memcpy(void *,const void *,__kernel_size_t);
135#endif
136#ifndef __HAVE_ARCH_MEMMOVE
137extern void * memmove(void *,const void *,__kernel_size_t);
138#endif
139#ifndef __HAVE_ARCH_MEMSCAN
140extern void * memscan(void *,int,__kernel_size_t);
141#endif
142#ifndef __HAVE_ARCH_MEMCMP
143extern int memcmp(const void *,const void *,__kernel_size_t);
144#endif
145#ifndef __HAVE_ARCH_MEMCHR
146extern void * memchr(const void *,int,__kernel_size_t);
147#endif
Dan Williams6abccd12017-01-13 14:14:23 -0800148#ifndef __HAVE_ARCH_MEMCPY_MCSAFE
149static inline __must_check int memcpy_mcsafe(void *dst, const void *src,
150 size_t cnt)
151{
152 memcpy(dst, src, cnt);
153 return 0;
154}
155#endif
Dan Williams0aed55a2017-05-29 12:22:50 -0700156#ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
157static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
158{
159 memcpy(dst, src, cnt);
160}
161#endif
Akinobu Mita798248202011-10-31 17:08:07 -0700162void *memchr_inv(const void *s, int c, size_t n);
Rasmus Villemoes94df2902015-06-25 15:02:22 -0700163char *strreplace(char *s, char old, char new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Andrzej Hajdaa4bb1e42015-02-13 14:36:24 -0800165extern void kfree_const(const void *x);
166
Rasmus Villemoes48a27052016-05-19 17:10:55 -0700167extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
Andrzej Hajdaa4bb1e42015-02-13 14:36:24 -0800168extern const char *kstrdup_const(const char *s, gfp_t gfp);
Jeremy Fitzhardinge1e66df32007-07-17 18:37:02 -0700169extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
Alexey Dobriyan1a2f67b2006-09-30 23:27:20 -0700170extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
David Howellsf3515742017-07-04 17:25:02 +0100171extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
Paulo Marques543537b2005-06-23 00:09:02 -0700172
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -0700173extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
174extern void argv_free(char **argv);
175
David Brownell34990cf2008-05-01 04:34:42 -0700176extern bool sysfs_streq(const char *s1, const char *s2);
Kees Cookef951592016-03-17 14:22:50 -0700177extern int kstrtobool(const char *s, bool *res);
178static inline int strtobool(const char *s, bool *res)
179{
180 return kstrtobool(s, res);
181}
David Brownell34990cf2008-05-01 04:34:42 -0700182
Andy Shevchenko56b06082016-03-17 14:22:14 -0700183int match_string(const char * const *array, size_t n, const char *string);
Heikki Krogeruse1fe7b62017-03-21 13:56:46 +0200184int __sysfs_match_string(const char * const *array, size_t n, const char *s);
185
186/**
187 * sysfs_match_string - matches given string in an array
188 * @_a: array of strings
189 * @_s: string to match with
190 *
191 * Helper for __sysfs_match_string(). Calculates the size of @a automatically.
192 */
193#define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
Andy Shevchenko56b06082016-03-17 14:22:14 -0700194
Lai Jiangshan4370aa42009-03-06 17:21:46 +0100195#ifdef CONFIG_BINARY_PRINTF
196int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
197int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
198int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
199#endif
200
Akinobu Mitae108526e2008-07-23 21:26:44 -0700201extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
Daniel Borkmannd4c5efd2014-08-26 23:16:35 -0400202 const void *from, size_t available);
Akinobu Mitae108526e2008-07-23 21:26:44 -0700203
Rusty Russell66f92cf2009-03-31 13:05:36 -0600204/**
205 * strstarts - does @str start with @prefix?
206 * @str: string to examine
207 * @prefix: prefix to look for.
208 */
209static inline bool strstarts(const char *str, const char *prefix)
210{
211 return strncmp(str, prefix, strlen(prefix)) == 0;
212}
Akinobu Mita639b9e32012-07-30 14:40:55 -0700213
Daniel Borkmannd4c5efd2014-08-26 23:16:35 -0400214size_t memweight(const void *ptr, size_t bytes);
215void memzero_explicit(void *s, size_t count);
Akinobu Mita639b9e32012-07-30 14:40:55 -0700216
Andy Shevchenkob18888a2012-12-17 16:01:18 -0800217/**
218 * kbasename - return the last part of a pathname.
219 *
220 * @path: path to extract the filename from.
221 */
222static inline const char *kbasename(const char *path)
223{
224 const char *tail = strrchr(path, '/');
225 return tail ? tail + 1 : path;
226}
227
Daniel Micay6974f0c2017-07-12 14:36:10 -0700228#define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
229#define __RENAME(x) __asm__(#x)
230
231void fortify_panic(const char *name) __noreturn __cold;
232void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
233void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
Martin Wilck01f33c32017-08-14 22:12:38 +0200234void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
Daniel Micay6974f0c2017-07-12 14:36:10 -0700235void __write_overflow(void) __compiletime_error("detected write beyond size of object passed as 1st parameter");
236
237#if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
Daniel Micay6974f0c2017-07-12 14:36:10 -0700238__FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
239{
240 size_t p_size = __builtin_object_size(p, 0);
241 if (__builtin_constant_p(size) && p_size < size)
242 __write_overflow();
243 if (p_size < size)
244 fortify_panic(__func__);
245 return __builtin_strncpy(p, q, size);
246}
247
248__FORTIFY_INLINE char *strcat(char *p, const char *q)
249{
250 size_t p_size = __builtin_object_size(p, 0);
251 if (p_size == (size_t)-1)
252 return __builtin_strcat(p, q);
253 if (strlcat(p, q, p_size) >= p_size)
254 fortify_panic(__func__);
255 return p;
256}
257
258__FORTIFY_INLINE __kernel_size_t strlen(const char *p)
259{
260 __kernel_size_t ret;
261 size_t p_size = __builtin_object_size(p, 0);
Arnd Bergmann146734b2017-12-14 15:32:34 -0800262
263 /* Work around gcc excess stack consumption issue */
264 if (p_size == (size_t)-1 ||
265 (__builtin_constant_p(p[p_size - 1]) && p[p_size - 1] == '\0'))
Daniel Micay6974f0c2017-07-12 14:36:10 -0700266 return __builtin_strlen(p);
267 ret = strnlen(p, p_size);
268 if (p_size <= ret)
269 fortify_panic(__func__);
270 return ret;
271}
272
273extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
274__FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
275{
276 size_t p_size = __builtin_object_size(p, 0);
277 __kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
278 if (p_size <= ret && maxlen != ret)
279 fortify_panic(__func__);
280 return ret;
281}
282
283/* defined after fortified strlen to reuse it */
284extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
285__FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
286{
287 size_t ret;
288 size_t p_size = __builtin_object_size(p, 0);
289 size_t q_size = __builtin_object_size(q, 0);
290 if (p_size == (size_t)-1 && q_size == (size_t)-1)
291 return __real_strlcpy(p, q, size);
292 ret = strlen(q);
293 if (size) {
294 size_t len = (ret >= size) ? size - 1 : ret;
295 if (__builtin_constant_p(len) && len >= p_size)
296 __write_overflow();
297 if (len >= p_size)
298 fortify_panic(__func__);
299 __builtin_memcpy(p, q, len);
300 p[len] = '\0';
301 }
302 return ret;
303}
304
305/* defined after fortified strlen and strnlen to reuse them */
306__FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
307{
308 size_t p_len, copy_len;
309 size_t p_size = __builtin_object_size(p, 0);
310 size_t q_size = __builtin_object_size(q, 0);
311 if (p_size == (size_t)-1 && q_size == (size_t)-1)
312 return __builtin_strncat(p, q, count);
313 p_len = strlen(p);
314 copy_len = strnlen(q, count);
315 if (p_size < p_len + copy_len + 1)
316 fortify_panic(__func__);
317 __builtin_memcpy(p + p_len, q, copy_len);
318 p[p_len + copy_len] = '\0';
319 return p;
320}
321
322__FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size)
323{
324 size_t p_size = __builtin_object_size(p, 0);
325 if (__builtin_constant_p(size) && p_size < size)
326 __write_overflow();
327 if (p_size < size)
328 fortify_panic(__func__);
329 return __builtin_memset(p, c, size);
330}
331
332__FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size)
333{
334 size_t p_size = __builtin_object_size(p, 0);
335 size_t q_size = __builtin_object_size(q, 0);
336 if (__builtin_constant_p(size)) {
337 if (p_size < size)
338 __write_overflow();
339 if (q_size < size)
340 __read_overflow2();
341 }
342 if (p_size < size || q_size < size)
343 fortify_panic(__func__);
344 return __builtin_memcpy(p, q, size);
345}
346
347__FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size)
348{
349 size_t p_size = __builtin_object_size(p, 0);
350 size_t q_size = __builtin_object_size(q, 0);
351 if (__builtin_constant_p(size)) {
352 if (p_size < size)
353 __write_overflow();
354 if (q_size < size)
355 __read_overflow2();
356 }
357 if (p_size < size || q_size < size)
358 fortify_panic(__func__);
359 return __builtin_memmove(p, q, size);
360}
361
362extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
363__FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)
364{
365 size_t p_size = __builtin_object_size(p, 0);
366 if (__builtin_constant_p(size) && p_size < size)
367 __read_overflow();
368 if (p_size < size)
369 fortify_panic(__func__);
370 return __real_memscan(p, c, size);
371}
372
373__FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size)
374{
375 size_t p_size = __builtin_object_size(p, 0);
376 size_t q_size = __builtin_object_size(q, 0);
377 if (__builtin_constant_p(size)) {
378 if (p_size < size)
379 __read_overflow();
380 if (q_size < size)
381 __read_overflow2();
382 }
383 if (p_size < size || q_size < size)
384 fortify_panic(__func__);
385 return __builtin_memcmp(p, q, size);
386}
387
388__FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size)
389{
390 size_t p_size = __builtin_object_size(p, 0);
391 if (__builtin_constant_p(size) && p_size < size)
392 __read_overflow();
393 if (p_size < size)
394 fortify_panic(__func__);
395 return __builtin_memchr(p, c, size);
396}
397
398void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
399__FORTIFY_INLINE void *memchr_inv(const void *p, int c, size_t size)
400{
401 size_t p_size = __builtin_object_size(p, 0);
402 if (__builtin_constant_p(size) && p_size < size)
403 __read_overflow();
404 if (p_size < size)
405 fortify_panic(__func__);
406 return __real_memchr_inv(p, c, size);
407}
408
409extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
410__FORTIFY_INLINE void *kmemdup(const void *p, size_t size, gfp_t gfp)
411{
412 size_t p_size = __builtin_object_size(p, 0);
413 if (__builtin_constant_p(size) && p_size < size)
414 __read_overflow();
415 if (p_size < size)
416 fortify_panic(__func__);
417 return __real_kmemdup(p, size, gfp);
418}
Daniel Micay077d2ba2017-07-14 17:28:12 -0400419
420/* defined after fortified strlen and memcpy to reuse them */
421__FORTIFY_INLINE char *strcpy(char *p, const char *q)
422{
423 size_t p_size = __builtin_object_size(p, 0);
424 size_t q_size = __builtin_object_size(q, 0);
425 if (p_size == (size_t)-1 && q_size == (size_t)-1)
426 return __builtin_strcpy(p, q);
427 memcpy(p, q, strlen(q) + 1);
428 return p;
429}
430
Daniel Micay6974f0c2017-07-12 14:36:10 -0700431#endif
432
Martin Wilck01f33c32017-08-14 22:12:38 +0200433/**
434 * memcpy_and_pad - Copy one buffer to another with padding
435 * @dest: Where to copy to
436 * @dest_len: The destination buffer size
437 * @src: Where to copy from
438 * @count: The number of bytes to copy
439 * @pad: Character to use for padding if space is left in destination.
440 */
Martin Wilck13597982017-09-06 14:36:57 +0200441static inline void memcpy_and_pad(void *dest, size_t dest_len,
442 const void *src, size_t count, int pad)
Martin Wilck01f33c32017-08-14 22:12:38 +0200443{
Martin Wilck01f33c32017-08-14 22:12:38 +0200444 if (dest_len > count) {
445 memcpy(dest, src, count);
446 memset(dest + count, pad, dest_len - count);
447 } else
448 memcpy(dest, src, dest_len);
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#endif /* _LINUX_STRING_H_ */