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