Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef _LINUX_STRING_H_ |
3 | #define _LINUX_STRING_H_ | |
4 | ||
1da177e4 LT |
5 | #include <linux/compiler.h> /* for inline */ |
6 | #include <linux/types.h> /* for size_t */ | |
7 | #include <linux/stddef.h> /* for NULL */ | |
33e56a59 | 8 | #include <linux/errno.h> /* for E2BIG */ |
c0891ac1 | 9 | #include <linux/stdarg.h> |
607ca46e | 10 | #include <uapi/linux/string.h> |
1da177e4 | 11 | |
96840aa0 | 12 | extern char *strndup_user(const char __user *, long); |
610a77e0 | 13 | extern void *memdup_user(const void __user *, size_t); |
50fd2f29 | 14 | extern void *vmemdup_user(const void __user *, size_t); |
e9d408e1 | 15 | extern void *memdup_user_nul(const void __user *, size_t); |
96840aa0 | 16 | |
1da177e4 LT |
17 | /* |
18 | * Include machine specific inline routines | |
19 | */ | |
20 | #include <asm/string.h> | |
21 | ||
22 | #ifndef __HAVE_ARCH_STRCPY | |
23 | extern char * strcpy(char *,const char *); | |
24 | #endif | |
25 | #ifndef __HAVE_ARCH_STRNCPY | |
26 | extern char * strncpy(char *,const char *, __kernel_size_t); | |
27 | #endif | |
28 | #ifndef __HAVE_ARCH_STRLCPY | |
29 | size_t strlcpy(char *, const char *, size_t); | |
30 | #endif | |
30035e45 | 31 | #ifndef __HAVE_ARCH_STRSCPY |
08a77676 | 32 | ssize_t strscpy(char *, const char *, size_t); |
30035e45 | 33 | #endif |
458a3bf8 TH |
34 | |
35 | /* Wraps calls to strscpy()/memset(), no arch specific code required */ | |
36 | ssize_t strscpy_pad(char *dest, const char *src, size_t count); | |
37 | ||
1da177e4 LT |
38 | #ifndef __HAVE_ARCH_STRCAT |
39 | extern char * strcat(char *, const char *); | |
40 | #endif | |
41 | #ifndef __HAVE_ARCH_STRNCAT | |
42 | extern char * strncat(char *, const char *, __kernel_size_t); | |
43 | #endif | |
44 | #ifndef __HAVE_ARCH_STRLCAT | |
45 | extern size_t strlcat(char *, const char *, __kernel_size_t); | |
46 | #endif | |
47 | #ifndef __HAVE_ARCH_STRCMP | |
48 | extern int strcmp(const char *,const char *); | |
49 | #endif | |
50 | #ifndef __HAVE_ARCH_STRNCMP | |
51 | extern int strncmp(const char *,const char *,__kernel_size_t); | |
52 | #endif | |
ded220bd DM |
53 | #ifndef __HAVE_ARCH_STRCASECMP |
54 | extern int strcasecmp(const char *s1, const char *s2); | |
55 | #endif | |
56 | #ifndef __HAVE_ARCH_STRNCASECMP | |
57 | extern int strncasecmp(const char *s1, const char *s2, size_t n); | |
58 | #endif | |
1da177e4 LT |
59 | #ifndef __HAVE_ARCH_STRCHR |
60 | extern char * strchr(const char *,int); | |
61 | #endif | |
11d200e9 GL |
62 | #ifndef __HAVE_ARCH_STRCHRNUL |
63 | extern char * strchrnul(const char *,int); | |
64 | #endif | |
0bee0cec | 65 | extern char * strnchrnul(const char *, size_t, int); |
1da177e4 LT |
66 | #ifndef __HAVE_ARCH_STRNCHR |
67 | extern char * strnchr(const char *, size_t, int); | |
68 | #endif | |
69 | #ifndef __HAVE_ARCH_STRRCHR | |
70 | extern char * strrchr(const char *,int); | |
71 | #endif | |
f653398c | 72 | extern char * __must_check skip_spaces(const char *); |
ca54cb8c KM |
73 | |
74 | extern char *strim(char *); | |
75 | ||
76 | static inline __must_check char *strstrip(char *str) | |
77 | { | |
78 | return strim(str); | |
79 | } | |
80 | ||
1da177e4 | 81 | #ifndef __HAVE_ARCH_STRSTR |
d5f1fb53 LZ |
82 | extern char * strstr(const char *, const char *); |
83 | #endif | |
84 | #ifndef __HAVE_ARCH_STRNSTR | |
85 | extern char * strnstr(const char *, const char *, size_t); | |
1da177e4 LT |
86 | #endif |
87 | #ifndef __HAVE_ARCH_STRLEN | |
88 | extern __kernel_size_t strlen(const char *); | |
89 | #endif | |
90 | #ifndef __HAVE_ARCH_STRNLEN | |
91 | extern __kernel_size_t strnlen(const char *,__kernel_size_t); | |
92 | #endif | |
8833d328 KM |
93 | #ifndef __HAVE_ARCH_STRPBRK |
94 | extern char * strpbrk(const char *,const char *); | |
95 | #endif | |
96 | #ifndef __HAVE_ARCH_STRSEP | |
97 | extern char * strsep(char **,const char *); | |
98 | #endif | |
99 | #ifndef __HAVE_ARCH_STRSPN | |
100 | extern __kernel_size_t strspn(const char *,const char *); | |
101 | #endif | |
102 | #ifndef __HAVE_ARCH_STRCSPN | |
103 | extern __kernel_size_t strcspn(const char *,const char *); | |
104 | #endif | |
1da177e4 LT |
105 | |
106 | #ifndef __HAVE_ARCH_MEMSET | |
107 | extern void * memset(void *,int,__kernel_size_t); | |
108 | #endif | |
3b3c4bab MW |
109 | |
110 | #ifndef __HAVE_ARCH_MEMSET16 | |
111 | extern void *memset16(uint16_t *, uint16_t, __kernel_size_t); | |
112 | #endif | |
113 | ||
114 | #ifndef __HAVE_ARCH_MEMSET32 | |
115 | extern void *memset32(uint32_t *, uint32_t, __kernel_size_t); | |
116 | #endif | |
117 | ||
118 | #ifndef __HAVE_ARCH_MEMSET64 | |
119 | extern void *memset64(uint64_t *, uint64_t, __kernel_size_t); | |
120 | #endif | |
121 | ||
122 | static inline void *memset_l(unsigned long *p, unsigned long v, | |
123 | __kernel_size_t n) | |
124 | { | |
125 | if (BITS_PER_LONG == 32) | |
126 | return memset32((uint32_t *)p, v, n); | |
127 | else | |
128 | return memset64((uint64_t *)p, v, n); | |
129 | } | |
130 | ||
131 | static inline void *memset_p(void **p, void *v, __kernel_size_t n) | |
132 | { | |
133 | if (BITS_PER_LONG == 32) | |
134 | return memset32((uint32_t *)p, (uintptr_t)v, n); | |
135 | else | |
136 | return memset64((uint64_t *)p, (uintptr_t)v, n); | |
137 | } | |
138 | ||
ce76d938 AS |
139 | extern void **__memcat_p(void **a, void **b); |
140 | #define memcat_p(a, b) ({ \ | |
141 | BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \ | |
142 | "type mismatch in memcat_p()"); \ | |
143 | (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \ | |
144 | }) | |
145 | ||
1da177e4 LT |
146 | #ifndef __HAVE_ARCH_MEMCPY |
147 | extern void * memcpy(void *,const void *,__kernel_size_t); | |
148 | #endif | |
149 | #ifndef __HAVE_ARCH_MEMMOVE | |
150 | extern void * memmove(void *,const void *,__kernel_size_t); | |
151 | #endif | |
152 | #ifndef __HAVE_ARCH_MEMSCAN | |
153 | extern void * memscan(void *,int,__kernel_size_t); | |
154 | #endif | |
155 | #ifndef __HAVE_ARCH_MEMCMP | |
156 | extern int memcmp(const void *,const void *,__kernel_size_t); | |
157 | #endif | |
5f074f3e ND |
158 | #ifndef __HAVE_ARCH_BCMP |
159 | extern int bcmp(const void *,const void *,__kernel_size_t); | |
160 | #endif | |
1da177e4 LT |
161 | #ifndef __HAVE_ARCH_MEMCHR |
162 | extern void * memchr(const void *,int,__kernel_size_t); | |
163 | #endif | |
0aed55af DW |
164 | #ifndef __HAVE_ARCH_MEMCPY_FLUSHCACHE |
165 | static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt) | |
166 | { | |
167 | memcpy(dst, src, cnt); | |
168 | } | |
169 | #endif | |
ec6347bb | 170 | |
79824820 | 171 | void *memchr_inv(const void *s, int c, size_t n); |
94df2904 | 172 | char *strreplace(char *s, char old, char new); |
1da177e4 | 173 | |
a4bb1e43 AH |
174 | extern void kfree_const(const void *x); |
175 | ||
48a27055 | 176 | extern char *kstrdup(const char *s, gfp_t gfp) __malloc; |
a4bb1e43 | 177 | extern const char *kstrdup_const(const char *s, gfp_t gfp); |
1e66df3e | 178 | extern char *kstrndup(const char *s, size_t len, gfp_t gfp); |
9e4a6177 | 179 | extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2); |
0b7b8704 | 180 | extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2); |
f3515741 | 181 | extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); |
543537bd | 182 | |
d84d1cc7 JF |
183 | extern char **argv_split(gfp_t gfp, const char *str, int *argcp); |
184 | extern void argv_free(char **argv); | |
185 | ||
34990cf7 | 186 | extern bool sysfs_streq(const char *s1, const char *s2); |
56b06081 | 187 | int match_string(const char * const *array, size_t n, const char *string); |
e1fe7b6a HK |
188 | int __sysfs_match_string(const char * const *array, size_t n, const char *s); |
189 | ||
190 | /** | |
191 | * sysfs_match_string - matches given string in an array | |
192 | * @_a: array of strings | |
193 | * @_s: string to match with | |
194 | * | |
195 | * Helper for __sysfs_match_string(). Calculates the size of @a automatically. | |
196 | */ | |
197 | #define sysfs_match_string(_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s) | |
56b06081 | 198 | |
4370aa4a LJ |
199 | #ifdef CONFIG_BINARY_PRINTF |
200 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args); | |
201 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf); | |
202 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4); | |
203 | #endif | |
204 | ||
e108526e | 205 | extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, |
d4c5efdb | 206 | const void *from, size_t available); |
e108526e | 207 | |
e4dcad20 JFG |
208 | int ptr_to_hashval(const void *ptr, unsigned long *hashval_out); |
209 | ||
66f92cf9 RR |
210 | /** |
211 | * strstarts - does @str start with @prefix? | |
212 | * @str: string to examine | |
213 | * @prefix: prefix to look for. | |
214 | */ | |
215 | static inline bool strstarts(const char *str, const char *prefix) | |
216 | { | |
217 | return strncmp(str, prefix, strlen(prefix)) == 0; | |
218 | } | |
639b9e34 | 219 | |
d4c5efdb | 220 | size_t memweight(const void *ptr, size_t bytes); |
bec50077 AS |
221 | |
222 | /** | |
223 | * memzero_explicit - Fill a region of memory (e.g. sensitive | |
224 | * keying data) with 0s. | |
225 | * @s: Pointer to the start of the area. | |
226 | * @count: The size of the area. | |
227 | * | |
228 | * Note: usually using memset() is just fine (!), but in cases | |
229 | * where clearing out _local_ data at the end of a scope is | |
230 | * necessary, memzero_explicit() should be used instead in | |
231 | * order to prevent the compiler from optimising away zeroing. | |
232 | * | |
233 | * memzero_explicit() doesn't need an arch-specific version as | |
234 | * it just invokes the one of memset() implicitly. | |
235 | */ | |
236 | static inline void memzero_explicit(void *s, size_t count) | |
237 | { | |
238 | memset(s, 0, count); | |
239 | barrier_data(s); | |
240 | } | |
639b9e34 | 241 | |
b18888ab AS |
242 | /** |
243 | * kbasename - return the last part of a pathname. | |
244 | * | |
245 | * @path: path to extract the filename from. | |
246 | */ | |
247 | static inline const char *kbasename(const char *path) | |
248 | { | |
249 | const char *tail = strrchr(path, '/'); | |
250 | return tail ? tail + 1 : path; | |
251 | } | |
252 | ||
6974f0c4 | 253 | #if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE) |
a28a6e86 | 254 | #include <linux/fortify-string.h> |
6974f0c4 | 255 | #endif |
43213dae KC |
256 | #ifndef unsafe_memcpy |
257 | #define unsafe_memcpy(dst, src, bytes, justification) \ | |
258 | memcpy(dst, src, bytes) | |
259 | #endif | |
6974f0c4 | 260 | |
5c4e0a21 GR |
261 | void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count, |
262 | int pad); | |
01f33c33 | 263 | |
dfbafa70 KC |
264 | /** |
265 | * strtomem_pad - Copy NUL-terminated string to non-NUL-terminated buffer | |
266 | * | |
267 | * @dest: Pointer of destination character array (marked as __nonstring) | |
268 | * @src: Pointer to NUL-terminated string | |
269 | * @pad: Padding character to fill any remaining bytes of @dest after copy | |
270 | * | |
271 | * This is a replacement for strncpy() uses where the destination is not | |
272 | * a NUL-terminated string, but with bounds checking on the source size, and | |
273 | * an explicit padding character. If padding is not required, use strtomem(). | |
274 | * | |
275 | * Note that the size of @dest is not an argument, as the length of @dest | |
276 | * must be discoverable by the compiler. | |
277 | */ | |
278 | #define strtomem_pad(dest, src, pad) do { \ | |
279 | const size_t _dest_len = __builtin_object_size(dest, 1); \ | |
280 | \ | |
281 | BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \ | |
282 | _dest_len == (size_t)-1); \ | |
283 | memcpy_and_pad(dest, _dest_len, src, strnlen(src, _dest_len), pad); \ | |
284 | } while (0) | |
285 | ||
286 | /** | |
287 | * strtomem - Copy NUL-terminated string to non-NUL-terminated buffer | |
288 | * | |
289 | * @dest: Pointer of destination character array (marked as __nonstring) | |
290 | * @src: Pointer to NUL-terminated string | |
291 | * | |
292 | * This is a replacement for strncpy() uses where the destination is not | |
293 | * a NUL-terminated string, but with bounds checking on the source size, and | |
294 | * without trailing padding. If padding is required, use strtomem_pad(). | |
295 | * | |
296 | * Note that the size of @dest is not an argument, as the length of @dest | |
297 | * must be discoverable by the compiler. | |
298 | */ | |
299 | #define strtomem(dest, src) do { \ | |
300 | const size_t _dest_len = __builtin_object_size(dest, 1); \ | |
301 | \ | |
302 | BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \ | |
303 | _dest_len == (size_t)-1); \ | |
304 | memcpy(dest, src, min(_dest_len, strnlen(src, _dest_len))); \ | |
305 | } while (0) | |
306 | ||
4797632f KC |
307 | /** |
308 | * memset_after - Set a value after a struct member to the end of a struct | |
309 | * | |
310 | * @obj: Address of target struct instance | |
311 | * @v: Byte value to repeatedly write | |
312 | * @member: after which struct member to start writing bytes | |
313 | * | |
314 | * This is good for clearing padding following the given member. | |
315 | */ | |
316 | #define memset_after(obj, v, member) \ | |
317 | ({ \ | |
318 | u8 *__ptr = (u8 *)(obj); \ | |
319 | typeof(v) __val = (v); \ | |
320 | memset(__ptr + offsetofend(typeof(*(obj)), member), __val, \ | |
321 | sizeof(*(obj)) - offsetofend(typeof(*(obj)), member)); \ | |
322 | }) | |
323 | ||
6dbefad4 KC |
324 | /** |
325 | * memset_startat - Set a value starting at a member to the end of a struct | |
326 | * | |
327 | * @obj: Address of target struct instance | |
328 | * @v: Byte value to repeatedly write | |
329 | * @member: struct member to start writing at | |
330 | * | |
331 | * Note that if there is padding between the prior member and the target | |
332 | * member, memset_after() should be used to clear the prior padding. | |
333 | */ | |
334 | #define memset_startat(obj, v, member) \ | |
335 | ({ \ | |
336 | u8 *__ptr = (u8 *)(obj); \ | |
337 | typeof(v) __val = (v); \ | |
338 | memset(__ptr + offsetof(typeof(*(obj)), member), __val, \ | |
339 | sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \ | |
340 | }) | |
341 | ||
72921427 SRV |
342 | /** |
343 | * str_has_prefix - Test if a string has a given prefix | |
344 | * @str: The string to test | |
345 | * @prefix: The string to see if @str starts with | |
346 | * | |
347 | * A common way to test a prefix of a string is to do: | |
348 | * strncmp(str, prefix, sizeof(prefix) - 1) | |
349 | * | |
350 | * But this can lead to bugs due to typos, or if prefix is a pointer | |
351 | * and not a constant. Instead use str_has_prefix(). | |
352 | * | |
917cda27 JP |
353 | * Returns: |
354 | * * strlen(@prefix) if @str starts with @prefix | |
355 | * * 0 if @str does not start with @prefix | |
72921427 SRV |
356 | */ |
357 | static __always_inline size_t str_has_prefix(const char *str, const char *prefix) | |
358 | { | |
359 | size_t len = strlen(prefix); | |
360 | return strncmp(str, prefix, len) == 0 ? len : 0; | |
361 | } | |
362 | ||
1da177e4 | 363 | #endif /* _LINUX_STRING_H_ */ |