0001
0002 #ifndef _LINUX_STRING_H_
0003 #define _LINUX_STRING_H_
0004
0005 #include <linux/compiler.h> /* for inline */
0006 #include <linux/types.h> /* for size_t */
0007 #include <linux/stddef.h> /* for NULL */
0008 #include <linux/errno.h> /* for E2BIG */
0009 #include <linux/stdarg.h>
0010 #include <uapi/linux/string.h>
0011
0012 extern char *strndup_user(const char __user *, long);
0013 extern void *memdup_user(const void __user *, size_t);
0014 extern void *vmemdup_user(const void __user *, size_t);
0015 extern void *memdup_user_nul(const void __user *, size_t);
0016
0017
0018
0019
0020 #include <asm/string.h>
0021
0022 #ifndef __HAVE_ARCH_STRCPY
0023 extern char * strcpy(char *,const char *);
0024 #endif
0025 #ifndef __HAVE_ARCH_STRNCPY
0026 extern char * strncpy(char *,const char *, __kernel_size_t);
0027 #endif
0028 #ifndef __HAVE_ARCH_STRLCPY
0029 size_t strlcpy(char *, const char *, size_t);
0030 #endif
0031 #ifndef __HAVE_ARCH_STRSCPY
0032 ssize_t strscpy(char *, const char *, size_t);
0033 #endif
0034
0035
0036 ssize_t strscpy_pad(char *dest, const char *src, size_t count);
0037
0038 #ifndef __HAVE_ARCH_STRCAT
0039 extern char * strcat(char *, const char *);
0040 #endif
0041 #ifndef __HAVE_ARCH_STRNCAT
0042 extern char * strncat(char *, const char *, __kernel_size_t);
0043 #endif
0044 #ifndef __HAVE_ARCH_STRLCAT
0045 extern size_t strlcat(char *, const char *, __kernel_size_t);
0046 #endif
0047 #ifndef __HAVE_ARCH_STRCMP
0048 extern int strcmp(const char *,const char *);
0049 #endif
0050 #ifndef __HAVE_ARCH_STRNCMP
0051 extern int strncmp(const char *,const char *,__kernel_size_t);
0052 #endif
0053 #ifndef __HAVE_ARCH_STRCASECMP
0054 extern int strcasecmp(const char *s1, const char *s2);
0055 #endif
0056 #ifndef __HAVE_ARCH_STRNCASECMP
0057 extern int strncasecmp(const char *s1, const char *s2, size_t n);
0058 #endif
0059 #ifndef __HAVE_ARCH_STRCHR
0060 extern char * strchr(const char *,int);
0061 #endif
0062 #ifndef __HAVE_ARCH_STRCHRNUL
0063 extern char * strchrnul(const char *,int);
0064 #endif
0065 extern char * strnchrnul(const char *, size_t, int);
0066 #ifndef __HAVE_ARCH_STRNCHR
0067 extern char * strnchr(const char *, size_t, int);
0068 #endif
0069 #ifndef __HAVE_ARCH_STRRCHR
0070 extern char * strrchr(const char *,int);
0071 #endif
0072 extern char * __must_check skip_spaces(const char *);
0073
0074 extern char *strim(char *);
0075
0076 static inline __must_check char *strstrip(char *str)
0077 {
0078 return strim(str);
0079 }
0080
0081 #ifndef __HAVE_ARCH_STRSTR
0082 extern char * strstr(const char *, const char *);
0083 #endif
0084 #ifndef __HAVE_ARCH_STRNSTR
0085 extern char * strnstr(const char *, const char *, size_t);
0086 #endif
0087 #ifndef __HAVE_ARCH_STRLEN
0088 extern __kernel_size_t strlen(const char *);
0089 #endif
0090 #ifndef __HAVE_ARCH_STRNLEN
0091 extern __kernel_size_t strnlen(const char *,__kernel_size_t);
0092 #endif
0093 #ifndef __HAVE_ARCH_STRPBRK
0094 extern char * strpbrk(const char *,const char *);
0095 #endif
0096 #ifndef __HAVE_ARCH_STRSEP
0097 extern char * strsep(char **,const char *);
0098 #endif
0099 #ifndef __HAVE_ARCH_STRSPN
0100 extern __kernel_size_t strspn(const char *,const char *);
0101 #endif
0102 #ifndef __HAVE_ARCH_STRCSPN
0103 extern __kernel_size_t strcspn(const char *,const char *);
0104 #endif
0105
0106 #ifndef __HAVE_ARCH_MEMSET
0107 extern void * memset(void *,int,__kernel_size_t);
0108 #endif
0109
0110 #ifndef __HAVE_ARCH_MEMSET16
0111 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
0112 #endif
0113
0114 #ifndef __HAVE_ARCH_MEMSET32
0115 extern void *memset32(uint32_t *, uint32_t, __kernel_size_t);
0116 #endif
0117
0118 #ifndef __HAVE_ARCH_MEMSET64
0119 extern void *memset64(uint64_t *, uint64_t, __kernel_size_t);
0120 #endif
0121
0122 static inline void *memset_l(unsigned long *p, unsigned long v,
0123 __kernel_size_t n)
0124 {
0125 if (BITS_PER_LONG == 32)
0126 return memset32((uint32_t *)p, v, n);
0127 else
0128 return memset64((uint64_t *)p, v, n);
0129 }
0130
0131 static inline void *memset_p(void **p, void *v, __kernel_size_t n)
0132 {
0133 if (BITS_PER_LONG == 32)
0134 return memset32((uint32_t *)p, (uintptr_t)v, n);
0135 else
0136 return memset64((uint64_t *)p, (uintptr_t)v, n);
0137 }
0138
0139 extern void **__memcat_p(void **a, void **b);
0140 #define memcat_p(a, b) ({ \
0141 BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \
0142 "type mismatch in memcat_p()"); \
0143 (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \
0144 })
0145
0146 #ifndef __HAVE_ARCH_MEMCPY
0147 extern void * memcpy(void *,const void *,__kernel_size_t);
0148 #endif
0149 #ifndef __HAVE_ARCH_MEMMOVE
0150 extern void * memmove(void *,const void *,__kernel_size_t);
0151 #endif
0152 #ifndef __HAVE_ARCH_MEMSCAN
0153 extern void * memscan(void *,int,__kernel_size_t);
0154 #endif
0155 #ifndef __HAVE_ARCH_MEMCMP
0156 extern int memcmp(const void *,const void *,__kernel_size_t);
0157 #endif
0158 #ifndef __HAVE_ARCH_BCMP
0159 extern int bcmp(const void *,const void *,__kernel_size_t);
0160 #endif
0161 #ifndef __HAVE_ARCH_MEMCHR
0162 extern void * memchr(const void *,int,__kernel_size_t);
0163 #endif
0164 #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE
0165 static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
0166 {
0167 memcpy(dst, src, cnt);
0168 }
0169 #endif
0170
0171 void *memchr_inv(const void *s, int c, size_t n);
0172 char *strreplace(char *s, char old, char new);
0173
0174 extern void kfree_const(const void *x);
0175
0176 extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
0177 extern const char *kstrdup_const(const char *s, gfp_t gfp);
0178 extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
0179 extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
0180 extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
0181
0182 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
0183 extern void argv_free(char **argv);
0184
0185 extern bool sysfs_streq(const char *s1, const char *s2);
0186 int match_string(const char * const *array, size_t n, const char *string);
0187 int __sysfs_match_string(const char * const *array, size_t n, const char *s);
0188
0189
0190
0191
0192
0193
0194
0195
0196 #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)
0197
0198 #ifdef CONFIG_BINARY_PRINTF
0199 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
0200 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
0201 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
0202 #endif
0203
0204 extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
0205 const void *from, size_t available);
0206
0207 int ptr_to_hashval(const void *ptr, unsigned long *hashval_out);
0208
0209
0210
0211
0212
0213
0214 static inline bool strstarts(const char *str, const char *prefix)
0215 {
0216 return strncmp(str, prefix, strlen(prefix)) == 0;
0217 }
0218
0219 size_t memweight(const void *ptr, size_t bytes);
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235 static inline void memzero_explicit(void *s, size_t count)
0236 {
0237 memset(s, 0, count);
0238 barrier_data(s);
0239 }
0240
0241
0242
0243
0244
0245
0246 static inline const char *kbasename(const char *path)
0247 {
0248 const char *tail = strrchr(path, '/');
0249 return tail ? tail + 1 : path;
0250 }
0251
0252 #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
0253 #include <linux/fortify-string.h>
0254 #endif
0255 #ifndef unsafe_memcpy
0256 #define unsafe_memcpy(dst, src, bytes, justification) \
0257 memcpy(dst, src, bytes)
0258 #endif
0259
0260 void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
0261 int pad);
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272 #define memset_after(obj, v, member) \
0273 ({ \
0274 u8 *__ptr = (u8 *)(obj); \
0275 typeof(v) __val = (v); \
0276 memset(__ptr + offsetofend(typeof(*(obj)), member), __val, \
0277 sizeof(*(obj)) - offsetofend(typeof(*(obj)), member)); \
0278 })
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290 #define memset_startat(obj, v, member) \
0291 ({ \
0292 u8 *__ptr = (u8 *)(obj); \
0293 typeof(v) __val = (v); \
0294 memset(__ptr + offsetof(typeof(*(obj)), member), __val, \
0295 sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \
0296 })
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313 static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
0314 {
0315 size_t len = strlen(prefix);
0316 return strncmp(str, prefix, len) == 0 ? len : 0;
0317 }
0318
0319 #endif