~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/string.h

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0 */
  2 #ifndef _LINUX_STRING_H_
  3 #define _LINUX_STRING_H_
  4 
  5 
  6 #include <linux/compiler.h>     /* for inline */
  7 #include <linux/types.h>        /* for size_t */
  8 #include <linux/stddef.h>       /* for NULL */
  9 #include <stdarg.h>
 10 #include <uapi/linux/string.h>
 11 
 12 extern char *strndup_user(const char __user *, long);
 13 extern void *memdup_user(const void __user *, size_t);
 14 extern void *memdup_user_nul(const void __user *, size_t);
 15 
 16 /*
 17  * Include machine specific inline routines
 18  */
 19 #include <asm/string.h>
 20 
 21 #ifndef __HAVE_ARCH_STRCPY
 22 extern char * strcpy(char *,const char *);
 23 #endif
 24 #ifndef __HAVE_ARCH_STRNCPY
 25 extern char * strncpy(char *,const char *, __kernel_size_t);
 26 #endif
 27 #ifndef __HAVE_ARCH_STRLCPY
 28 size_t strlcpy(char *, const char *, size_t);
 29 #endif
 30 #ifndef __HAVE_ARCH_STRSCPY
 31 ssize_t __must_check strscpy(char *, const char *, size_t);
 32 #endif
 33 #ifndef __HAVE_ARCH_STRCAT
 34 extern char * strcat(char *, const char *);
 35 #endif
 36 #ifndef __HAVE_ARCH_STRNCAT
 37 extern char * strncat(char *, const char *, __kernel_size_t);
 38 #endif
 39 #ifndef __HAVE_ARCH_STRLCAT
 40 extern size_t strlcat(char *, const char *, __kernel_size_t);
 41 #endif
 42 #ifndef __HAVE_ARCH_STRCMP
 43 extern int strcmp(const char *,const char *);
 44 #endif
 45 #ifndef __HAVE_ARCH_STRNCMP
 46 extern int strncmp(const char *,const char *,__kernel_size_t);
 47 #endif
 48 #ifndef __HAVE_ARCH_STRCASECMP
 49 extern int strcasecmp(const char *s1, const char *s2);
 50 #endif
 51 #ifndef __HAVE_ARCH_STRNCASECMP
 52 extern int strncasecmp(const char *s1, const char *s2, size_t n);
 53 #endif
 54 #ifndef __HAVE_ARCH_STRCHR
 55 extern char * strchr(const char *,int);
 56 #endif
 57 #ifndef __HAVE_ARCH_STRCHRNUL
 58 extern char * strchrnul(const char *,int);
 59 #endif
 60 #ifndef __HAVE_ARCH_STRNCHR
 61 extern char * strnchr(const char *, size_t, int);
 62 #endif
 63 #ifndef __HAVE_ARCH_STRRCHR
 64 extern char * strrchr(const char *,int);
 65 #endif
 66 extern char * __must_check skip_spaces(const char *);
 67 
 68 extern char *strim(char *);
 69 
 70 static inline __must_check char *strstrip(char *str)
 71 {
 72         return strim(str);
 73 }
 74 
 75 #ifndef __HAVE_ARCH_STRSTR
 76 extern char * strstr(const char *, const char *);
 77 #endif
 78 #ifndef __HAVE_ARCH_STRNSTR
 79 extern char * strnstr(const char *, const char *, size_t);
 80 #endif
 81 #ifndef __HAVE_ARCH_STRLEN
 82 extern __kernel_size_t strlen(const char *);
 83 #endif
 84 #ifndef __HAVE_ARCH_STRNLEN
 85 extern __kernel_size_t strnlen(const char *,__kernel_size_t);
 86 #endif
 87 #ifndef __HAVE_ARCH_STRPBRK
 88 extern char * strpbrk(const char *,const char *);
 89 #endif
 90 #ifndef __HAVE_ARCH_STRSEP
 91 extern char * strsep(char **,const char *);
 92 #endif
 93 #ifndef __HAVE_ARCH_STRSPN
 94 extern __kernel_size_t strspn(const char *,const char *);
 95 #endif
 96 #ifndef __HAVE_ARCH_STRCSPN
 97 extern __kernel_size_t strcspn(const char *,const char *);
 98 #endif
 99 
100 #ifndef __HAVE_ARCH_MEMSET
101 extern void * memset(void *,int,__kernel_size_t);
102 #endif
103 
104 #ifndef __HAVE_ARCH_MEMSET16
105 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
106 #endif
107 
108 #ifndef __HAVE_ARCH_MEMSET32
109 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
110 #endif
111 
112 #ifndef __HAVE_ARCH_MEMSET64
113 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
114 #endif
115 
116 static 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 
125 static 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 
133 #ifndef __HAVE_ARCH_MEMCPY
134 extern void * memcpy(void *,const void *,__kernel_size_t);
135 #endif
136 #ifndef __HAVE_ARCH_MEMMOVE
137 extern void * memmove(void *,const void *,__kernel_size_t);
138 #endif
139 #ifndef __HAVE_ARCH_MEMSCAN
140 extern void * memscan(void *,int,__kernel_size_t);
141 #endif
142 #ifndef __HAVE_ARCH_MEMCMP
143 extern int memcmp(const void *,const void *,__kernel_size_t);
144 #endif
145 #ifndef __HAVE_ARCH_MEMCHR
146 extern void * memchr(const void *,int,__kernel_size_t);
147 #endif
148 #ifndef __HAVE_ARCH_MEMCPY_MCSAFE
149 static 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
156 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
157 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
158 {
159         memcpy(dst, src, cnt);
160 }
161 #endif
162 void *memchr_inv(const void *s, int c, size_t n);
163 char *strreplace(char *s, char old, char new);
164 
165 extern void kfree_const(const void *x);
166 
167 extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
168 extern const char *kstrdup_const(const char *s, gfp_t gfp);
169 extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
170 extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
171 extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
172 
173 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
174 extern void argv_free(char **argv);
175 
176 extern bool sysfs_streq(const char *s1, const char *s2);
177 extern int kstrtobool(const char *s, bool *res);
178 static inline int strtobool(const char *s, bool *res)
179 {
180         return kstrtobool(s, res);
181 }
182 
183 int match_string(const char * const *array, size_t n, const char *string);
184 int __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)
194 
195 #ifdef CONFIG_BINARY_PRINTF
196 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
197 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
198 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
199 #endif
200 
201 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
202                                        const void *from, size_t available);
203 
204 /**
205  * strstarts - does @str start with @prefix?
206  * @str: string to examine
207  * @prefix: prefix to look for.
208  */
209 static inline bool strstarts(const char *str, const char *prefix)
210 {
211         return strncmp(str, prefix, strlen(prefix)) == 0;
212 }
213 
214 size_t memweight(const void *ptr, size_t bytes);
215 void memzero_explicit(void *s, size_t count);
216 
217 /**
218  * kbasename - return the last part of a pathname.
219  *
220  * @path: path to extract the filename from.
221  */
222 static inline const char *kbasename(const char *path)
223 {
224         const char *tail = strrchr(path, '/');
225         return tail ? tail + 1 : path;
226 }
227 
228 #define __FORTIFY_INLINE extern __always_inline __attribute__((gnu_inline))
229 #define __RENAME(x) __asm__(#x)
230 
231 void fortify_panic(const char *name) __noreturn __cold;
232 void __read_overflow(void) __compiletime_error("detected read beyond size of object passed as 1st parameter");
233 void __read_overflow2(void) __compiletime_error("detected read beyond size of object passed as 2nd parameter");
234 void __read_overflow3(void) __compiletime_error("detected read beyond size of object passed as 3rd parameter");
235 void __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)
238 __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);
262 
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'))
266                 return __builtin_strlen(p);
267         ret = strnlen(p, p_size);
268         if (p_size <= ret)
269                 fortify_panic(__func__);
270         return ret;
271 }
272 
273 extern __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 */
284 extern 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 
362 extern 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 
398 void *__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 
409 extern 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 }
419 
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 
431 #endif
432 
433 /**
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  */
441 static inline void memcpy_and_pad(void *dest, size_t dest_len,
442                                   const void *src, size_t count, int pad)
443 {
444         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 
451 #endif /* _LINUX_STRING_H_ */
452 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp