1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * linux/lib/string.c 3 * linux/lib/string.c 4 * 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 6 */ 7 7 8 /* 8 /* 9 * This file should be used only for "library" !! 9 * stupid library routines.. The optimized versions should generally be found 10 * alternative implementations on specific arc !! 10 * as inline code in <asm-xx/string.h> 11 * found in <asm-xx/string.h>), or get overloa << 12 * (Specifically, this file is built with __NO << 13 * 11 * 14 * Other helper functions should live in strin !! 12 * These are buggy as well.. >> 13 * >> 14 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de> >> 15 * - Added strsep() which will replace strtok() soon (because strsep() is >> 16 * reentrant and should be faster). Use only strsep() in new code, please. >> 17 * >> 18 * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>, >> 19 * Matthew Hawkins <matt@mh.dropbear.id.au> >> 20 * - Kissed strtok() goodbye 15 */ 21 */ 16 22 17 #define __NO_FORTIFY !! 23 #include <linux/types.h> 18 #include <linux/bits.h> !! 24 #include <linux/string.h> 19 #include <linux/bug.h> << 20 #include <linux/ctype.h> 25 #include <linux/ctype.h> >> 26 #include <linux/kernel.h> >> 27 #include <linux/export.h> >> 28 #include <linux/bug.h> 21 #include <linux/errno.h> 29 #include <linux/errno.h> 22 #include <linux/limits.h> !! 30 #include <linux/slab.h> 23 #include <linux/linkage.h> << 24 #include <linux/stddef.h> << 25 #include <linux/string.h> << 26 #include <linux/types.h> << 27 31 28 #include <asm/page.h> !! 32 #include <asm/byteorder.h> 29 #include <asm/rwonce.h> << 30 #include <linux/unaligned.h> << 31 #include <asm/word-at-a-time.h> 33 #include <asm/word-at-a-time.h> >> 34 #include <asm/page.h> 32 35 33 #ifndef __HAVE_ARCH_STRNCASECMP 36 #ifndef __HAVE_ARCH_STRNCASECMP 34 /** 37 /** 35 * strncasecmp - Case insensitive, length-limi 38 * strncasecmp - Case insensitive, length-limited string comparison 36 * @s1: One string 39 * @s1: One string 37 * @s2: The other string 40 * @s2: The other string 38 * @len: the maximum number of characters to c 41 * @len: the maximum number of characters to compare 39 */ 42 */ 40 int strncasecmp(const char *s1, const char *s2 43 int strncasecmp(const char *s1, const char *s2, size_t len) 41 { 44 { 42 /* Yes, Virginia, it had better be uns 45 /* Yes, Virginia, it had better be unsigned */ 43 unsigned char c1, c2; 46 unsigned char c1, c2; 44 47 45 if (!len) 48 if (!len) 46 return 0; 49 return 0; 47 50 48 do { 51 do { 49 c1 = *s1++; 52 c1 = *s1++; 50 c2 = *s2++; 53 c2 = *s2++; 51 if (!c1 || !c2) 54 if (!c1 || !c2) 52 break; 55 break; 53 if (c1 == c2) 56 if (c1 == c2) 54 continue; 57 continue; 55 c1 = tolower(c1); 58 c1 = tolower(c1); 56 c2 = tolower(c2); 59 c2 = tolower(c2); 57 if (c1 != c2) 60 if (c1 != c2) 58 break; 61 break; 59 } while (--len); 62 } while (--len); 60 return (int)c1 - (int)c2; 63 return (int)c1 - (int)c2; 61 } 64 } 62 EXPORT_SYMBOL(strncasecmp); 65 EXPORT_SYMBOL(strncasecmp); 63 #endif 66 #endif 64 67 65 #ifndef __HAVE_ARCH_STRCASECMP 68 #ifndef __HAVE_ARCH_STRCASECMP 66 int strcasecmp(const char *s1, const char *s2) 69 int strcasecmp(const char *s1, const char *s2) 67 { 70 { 68 int c1, c2; 71 int c1, c2; 69 72 70 do { 73 do { 71 c1 = tolower(*s1++); 74 c1 = tolower(*s1++); 72 c2 = tolower(*s2++); 75 c2 = tolower(*s2++); 73 } while (c1 == c2 && c1 != 0); 76 } while (c1 == c2 && c1 != 0); 74 return c1 - c2; 77 return c1 - c2; 75 } 78 } 76 EXPORT_SYMBOL(strcasecmp); 79 EXPORT_SYMBOL(strcasecmp); 77 #endif 80 #endif 78 81 79 #ifndef __HAVE_ARCH_STRCPY 82 #ifndef __HAVE_ARCH_STRCPY >> 83 /** >> 84 * strcpy - Copy a %NUL terminated string >> 85 * @dest: Where to copy the string to >> 86 * @src: Where to copy the string from >> 87 */ >> 88 #undef strcpy 80 char *strcpy(char *dest, const char *src) 89 char *strcpy(char *dest, const char *src) 81 { 90 { 82 char *tmp = dest; 91 char *tmp = dest; 83 92 84 while ((*dest++ = *src++) != '\0') 93 while ((*dest++ = *src++) != '\0') 85 /* nothing */; 94 /* nothing */; 86 return tmp; 95 return tmp; 87 } 96 } 88 EXPORT_SYMBOL(strcpy); 97 EXPORT_SYMBOL(strcpy); 89 #endif 98 #endif 90 99 91 #ifndef __HAVE_ARCH_STRNCPY 100 #ifndef __HAVE_ARCH_STRNCPY >> 101 /** >> 102 * strncpy - Copy a length-limited, C-string >> 103 * @dest: Where to copy the string to >> 104 * @src: Where to copy the string from >> 105 * @count: The maximum number of bytes to copy >> 106 * >> 107 * The result is not %NUL-terminated if the source exceeds >> 108 * @count bytes. >> 109 * >> 110 * In the case where the length of @src is less than that of >> 111 * count, the remainder of @dest will be padded with %NUL. >> 112 * >> 113 */ 92 char *strncpy(char *dest, const char *src, siz 114 char *strncpy(char *dest, const char *src, size_t count) 93 { 115 { 94 char *tmp = dest; 116 char *tmp = dest; 95 117 96 while (count) { 118 while (count) { 97 if ((*tmp = *src) != 0) 119 if ((*tmp = *src) != 0) 98 src++; 120 src++; 99 tmp++; 121 tmp++; 100 count--; 122 count--; 101 } 123 } 102 return dest; 124 return dest; 103 } 125 } 104 EXPORT_SYMBOL(strncpy); 126 EXPORT_SYMBOL(strncpy); 105 #endif 127 #endif 106 128 107 ssize_t sized_strscpy(char *dest, const char * !! 129 #ifndef __HAVE_ARCH_STRLCPY >> 130 /** >> 131 * strlcpy - Copy a C-string into a sized buffer >> 132 * @dest: Where to copy the string to >> 133 * @src: Where to copy the string from >> 134 * @size: size of destination buffer >> 135 * >> 136 * Compatible with ``*BSD``: the result is always a valid >> 137 * NUL-terminated string that fits in the buffer (unless, >> 138 * of course, the buffer size is zero). It does not pad >> 139 * out the result like strncpy() does. >> 140 */ >> 141 size_t strlcpy(char *dest, const char *src, size_t size) >> 142 { >> 143 size_t ret = strlen(src); >> 144 >> 145 if (size) { >> 146 size_t len = (ret >= size) ? size - 1 : ret; >> 147 memcpy(dest, src, len); >> 148 dest[len] = '\0'; >> 149 } >> 150 return ret; >> 151 } >> 152 EXPORT_SYMBOL(strlcpy); >> 153 #endif >> 154 >> 155 #ifndef __HAVE_ARCH_STRSCPY >> 156 /** >> 157 * strscpy - Copy a C-string into a sized buffer >> 158 * @dest: Where to copy the string to >> 159 * @src: Where to copy the string from >> 160 * @count: Size of destination buffer >> 161 * >> 162 * Copy the string, or as much of it as fits, into the dest buffer. >> 163 * The routine returns the number of characters copied (not including >> 164 * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough. >> 165 * The behavior is undefined if the string buffers overlap. >> 166 * The destination buffer is always NUL terminated, unless it's zero-sized. >> 167 * >> 168 * Preferred to strlcpy() since the API doesn't require reading memory >> 169 * from the src string beyond the specified "count" bytes, and since >> 170 * the return value is easier to error-check than strlcpy()'s. >> 171 * In addition, the implementation is robust to the string changing out >> 172 * from underneath it, unlike the current strlcpy() implementation. >> 173 * >> 174 * Preferred to strncpy() since it always returns a valid string, and >> 175 * doesn't unnecessarily force the tail of the destination buffer to be >> 176 * zeroed. If the zeroing is desired, it's likely cleaner to use strscpy() >> 177 * with an overflow test, then just memset() the tail of the dest buffer. >> 178 */ >> 179 ssize_t strscpy(char *dest, const char *src, size_t count) 108 { 180 { 109 const struct word_at_a_time constants 181 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 110 size_t max = count; 182 size_t max = count; 111 long res = 0; 183 long res = 0; 112 184 113 if (count == 0 || WARN_ON_ONCE(count > !! 185 if (count == 0) 114 return -E2BIG; 186 return -E2BIG; 115 187 116 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 188 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 117 /* 189 /* 118 * If src is unaligned, don't cross a 190 * If src is unaligned, don't cross a page boundary, 119 * since we don't know if the next pag 191 * since we don't know if the next page is mapped. 120 */ 192 */ 121 if ((long)src & (sizeof(long) - 1)) { 193 if ((long)src & (sizeof(long) - 1)) { 122 size_t limit = PAGE_SIZE - ((l 194 size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1)); 123 if (limit < max) 195 if (limit < max) 124 max = limit; 196 max = limit; 125 } 197 } 126 #else 198 #else 127 /* If src or dest is unaligned, don't 199 /* If src or dest is unaligned, don't do word-at-a-time. */ 128 if (((long) dest | (long) src) & (size 200 if (((long) dest | (long) src) & (sizeof(long) - 1)) 129 max = 0; 201 max = 0; 130 #endif 202 #endif 131 203 132 /* << 133 * read_word_at_a_time() below may rea << 134 * trailing zero and use them in compa << 135 * under KMSAN to prevent false positi << 136 */ << 137 if (IS_ENABLED(CONFIG_KMSAN)) << 138 max = 0; << 139 << 140 while (max >= sizeof(unsigned long)) { 204 while (max >= sizeof(unsigned long)) { 141 unsigned long c, data; 205 unsigned long c, data; 142 206 143 c = read_word_at_a_time(src+re 207 c = read_word_at_a_time(src+res); 144 if (has_zero(c, &data, &consta 208 if (has_zero(c, &data, &constants)) { 145 data = prep_zero_mask( 209 data = prep_zero_mask(c, data, &constants); 146 data = create_zero_mas 210 data = create_zero_mask(data); 147 *(unsigned long *)(des 211 *(unsigned long *)(dest+res) = c & zero_bytemask(data); 148 return res + find_zero 212 return res + find_zero(data); 149 } 213 } 150 *(unsigned long *)(dest+res) = 214 *(unsigned long *)(dest+res) = c; 151 res += sizeof(unsigned long); 215 res += sizeof(unsigned long); 152 count -= sizeof(unsigned long) 216 count -= sizeof(unsigned long); 153 max -= sizeof(unsigned long); 217 max -= sizeof(unsigned long); 154 } 218 } 155 219 156 while (count) { 220 while (count) { 157 char c; 221 char c; 158 222 159 c = src[res]; 223 c = src[res]; 160 dest[res] = c; 224 dest[res] = c; 161 if (!c) 225 if (!c) 162 return res; 226 return res; 163 res++; 227 res++; 164 count--; 228 count--; 165 } 229 } 166 230 167 /* Hit buffer length without finding a 231 /* Hit buffer length without finding a NUL; force NUL-termination. */ 168 if (res) 232 if (res) 169 dest[res-1] = '\0'; 233 dest[res-1] = '\0'; 170 234 171 return -E2BIG; 235 return -E2BIG; 172 } 236 } 173 EXPORT_SYMBOL(sized_strscpy); !! 237 EXPORT_SYMBOL(strscpy); >> 238 #endif 174 239 >> 240 #ifndef __HAVE_ARCH_STRCAT 175 /** 241 /** 176 * stpcpy - copy a string from src to dest ret !! 242 * strcat - Append one %NUL-terminated string to another 177 * of dest, including src's %NUL-term !! 243 * @dest: The string to be appended to 178 * @dest: pointer to end of string being copie !! 244 * @src: The string to append to it 179 * to receive copy. << 180 * @src: pointer to the beginning of string be << 181 * dest. << 182 * << 183 * stpcpy differs from strcpy in a key way: th << 184 * to the new %NUL-terminating character in @d << 185 * value is a pointer to the start of @dest). << 186 * unsafe as it doesn't perform bounds checkin << 187 * not recommended for usage. Instead, its def << 188 * the compiler lowers other libcalls to stpcp << 189 */ 245 */ 190 char *stpcpy(char *__restrict__ dest, const ch !! 246 #undef strcat 191 char *stpcpy(char *__restrict__ dest, const ch << 192 { << 193 while ((*dest++ = *src++) != '\0') << 194 /* nothing */; << 195 return --dest; << 196 } << 197 EXPORT_SYMBOL(stpcpy); << 198 << 199 #ifndef __HAVE_ARCH_STRCAT << 200 char *strcat(char *dest, const char *src) 247 char *strcat(char *dest, const char *src) 201 { 248 { 202 char *tmp = dest; 249 char *tmp = dest; 203 250 204 while (*dest) 251 while (*dest) 205 dest++; 252 dest++; 206 while ((*dest++ = *src++) != '\0') 253 while ((*dest++ = *src++) != '\0') 207 ; 254 ; 208 return tmp; 255 return tmp; 209 } 256 } 210 EXPORT_SYMBOL(strcat); 257 EXPORT_SYMBOL(strcat); 211 #endif 258 #endif 212 259 213 #ifndef __HAVE_ARCH_STRNCAT 260 #ifndef __HAVE_ARCH_STRNCAT >> 261 /** >> 262 * strncat - Append a length-limited, C-string to another >> 263 * @dest: The string to be appended to >> 264 * @src: The string to append to it >> 265 * @count: The maximum numbers of bytes to copy >> 266 * >> 267 * Note that in contrast to strncpy(), strncat() ensures the result is >> 268 * terminated. >> 269 */ 214 char *strncat(char *dest, const char *src, siz 270 char *strncat(char *dest, const char *src, size_t count) 215 { 271 { 216 char *tmp = dest; 272 char *tmp = dest; 217 273 218 if (count) { 274 if (count) { 219 while (*dest) 275 while (*dest) 220 dest++; 276 dest++; 221 while ((*dest++ = *src++) != 0 277 while ((*dest++ = *src++) != 0) { 222 if (--count == 0) { 278 if (--count == 0) { 223 *dest = '\0'; 279 *dest = '\0'; 224 break; 280 break; 225 } 281 } 226 } 282 } 227 } 283 } 228 return tmp; 284 return tmp; 229 } 285 } 230 EXPORT_SYMBOL(strncat); 286 EXPORT_SYMBOL(strncat); 231 #endif 287 #endif 232 288 233 #ifndef __HAVE_ARCH_STRLCAT 289 #ifndef __HAVE_ARCH_STRLCAT >> 290 /** >> 291 * strlcat - Append a length-limited, C-string to another >> 292 * @dest: The string to be appended to >> 293 * @src: The string to append to it >> 294 * @count: The size of the destination buffer. >> 295 */ 234 size_t strlcat(char *dest, const char *src, si 296 size_t strlcat(char *dest, const char *src, size_t count) 235 { 297 { 236 size_t dsize = strlen(dest); 298 size_t dsize = strlen(dest); 237 size_t len = strlen(src); 299 size_t len = strlen(src); 238 size_t res = dsize + len; 300 size_t res = dsize + len; 239 301 240 /* This would be a bug */ 302 /* This would be a bug */ 241 BUG_ON(dsize >= count); 303 BUG_ON(dsize >= count); 242 304 243 dest += dsize; 305 dest += dsize; 244 count -= dsize; 306 count -= dsize; 245 if (len >= count) 307 if (len >= count) 246 len = count-1; 308 len = count-1; 247 __builtin_memcpy(dest, src, len); !! 309 memcpy(dest, src, len); 248 dest[len] = 0; 310 dest[len] = 0; 249 return res; 311 return res; 250 } 312 } 251 EXPORT_SYMBOL(strlcat); 313 EXPORT_SYMBOL(strlcat); 252 #endif 314 #endif 253 315 254 #ifndef __HAVE_ARCH_STRCMP 316 #ifndef __HAVE_ARCH_STRCMP 255 /** 317 /** 256 * strcmp - Compare two strings 318 * strcmp - Compare two strings 257 * @cs: One string 319 * @cs: One string 258 * @ct: Another string 320 * @ct: Another string 259 */ 321 */ >> 322 #undef strcmp 260 int strcmp(const char *cs, const char *ct) 323 int strcmp(const char *cs, const char *ct) 261 { 324 { 262 unsigned char c1, c2; 325 unsigned char c1, c2; 263 326 264 while (1) { 327 while (1) { 265 c1 = *cs++; 328 c1 = *cs++; 266 c2 = *ct++; 329 c2 = *ct++; 267 if (c1 != c2) 330 if (c1 != c2) 268 return c1 < c2 ? -1 : 331 return c1 < c2 ? -1 : 1; 269 if (!c1) 332 if (!c1) 270 break; 333 break; 271 } 334 } 272 return 0; 335 return 0; 273 } 336 } 274 EXPORT_SYMBOL(strcmp); 337 EXPORT_SYMBOL(strcmp); 275 #endif 338 #endif 276 339 277 #ifndef __HAVE_ARCH_STRNCMP 340 #ifndef __HAVE_ARCH_STRNCMP 278 /** 341 /** 279 * strncmp - Compare two length-limited string 342 * strncmp - Compare two length-limited strings 280 * @cs: One string 343 * @cs: One string 281 * @ct: Another string 344 * @ct: Another string 282 * @count: The maximum number of bytes to comp 345 * @count: The maximum number of bytes to compare 283 */ 346 */ 284 int strncmp(const char *cs, const char *ct, si 347 int strncmp(const char *cs, const char *ct, size_t count) 285 { 348 { 286 unsigned char c1, c2; 349 unsigned char c1, c2; 287 350 288 while (count) { 351 while (count) { 289 c1 = *cs++; 352 c1 = *cs++; 290 c2 = *ct++; 353 c2 = *ct++; 291 if (c1 != c2) 354 if (c1 != c2) 292 return c1 < c2 ? -1 : 355 return c1 < c2 ? -1 : 1; 293 if (!c1) 356 if (!c1) 294 break; 357 break; 295 count--; 358 count--; 296 } 359 } 297 return 0; 360 return 0; 298 } 361 } 299 EXPORT_SYMBOL(strncmp); 362 EXPORT_SYMBOL(strncmp); 300 #endif 363 #endif 301 364 302 #ifndef __HAVE_ARCH_STRCHR 365 #ifndef __HAVE_ARCH_STRCHR 303 /** 366 /** 304 * strchr - Find the first occurrence of a cha 367 * strchr - Find the first occurrence of a character in a string 305 * @s: The string to be searched 368 * @s: The string to be searched 306 * @c: The character to search for 369 * @c: The character to search for 307 * << 308 * Note that the %NUL-terminator is considered << 309 * be searched for. << 310 */ 370 */ 311 char *strchr(const char *s, int c) 371 char *strchr(const char *s, int c) 312 { 372 { 313 for (; *s != (char)c; ++s) 373 for (; *s != (char)c; ++s) 314 if (*s == '\0') 374 if (*s == '\0') 315 return NULL; 375 return NULL; 316 return (char *)s; 376 return (char *)s; 317 } 377 } 318 EXPORT_SYMBOL(strchr); 378 EXPORT_SYMBOL(strchr); 319 #endif 379 #endif 320 380 321 #ifndef __HAVE_ARCH_STRCHRNUL 381 #ifndef __HAVE_ARCH_STRCHRNUL 322 /** 382 /** 323 * strchrnul - Find and return a character in 383 * strchrnul - Find and return a character in a string, or end of string 324 * @s: The string to be searched 384 * @s: The string to be searched 325 * @c: The character to search for 385 * @c: The character to search for 326 * 386 * 327 * Returns pointer to first occurrence of 'c' 387 * Returns pointer to first occurrence of 'c' in s. If c is not found, then 328 * return a pointer to the null byte at the en 388 * return a pointer to the null byte at the end of s. 329 */ 389 */ 330 char *strchrnul(const char *s, int c) 390 char *strchrnul(const char *s, int c) 331 { 391 { 332 while (*s && *s != (char)c) 392 while (*s && *s != (char)c) 333 s++; 393 s++; 334 return (char *)s; 394 return (char *)s; 335 } 395 } 336 EXPORT_SYMBOL(strchrnul); 396 EXPORT_SYMBOL(strchrnul); 337 #endif 397 #endif 338 398 339 /** << 340 * strnchrnul - Find and return a character in << 341 * or end of string << 342 * @s: The string to be searched << 343 * @count: The number of characters to be sear << 344 * @c: The character to search for << 345 * << 346 * Returns pointer to the first occurrence of << 347 * then return a pointer to the last character << 348 */ << 349 char *strnchrnul(const char *s, size_t count, << 350 { << 351 while (count-- && *s && *s != (char)c) << 352 s++; << 353 return (char *)s; << 354 } << 355 << 356 #ifndef __HAVE_ARCH_STRRCHR 399 #ifndef __HAVE_ARCH_STRRCHR 357 /** 400 /** 358 * strrchr - Find the last occurrence of a cha 401 * strrchr - Find the last occurrence of a character in a string 359 * @s: The string to be searched 402 * @s: The string to be searched 360 * @c: The character to search for 403 * @c: The character to search for 361 */ 404 */ 362 char *strrchr(const char *s, int c) 405 char *strrchr(const char *s, int c) 363 { 406 { 364 const char *last = NULL; 407 const char *last = NULL; 365 do { 408 do { 366 if (*s == (char)c) 409 if (*s == (char)c) 367 last = s; 410 last = s; 368 } while (*s++); 411 } while (*s++); 369 return (char *)last; 412 return (char *)last; 370 } 413 } 371 EXPORT_SYMBOL(strrchr); 414 EXPORT_SYMBOL(strrchr); 372 #endif 415 #endif 373 416 374 #ifndef __HAVE_ARCH_STRNCHR 417 #ifndef __HAVE_ARCH_STRNCHR 375 /** 418 /** 376 * strnchr - Find a character in a length limi 419 * strnchr - Find a character in a length limited string 377 * @s: The string to be searched 420 * @s: The string to be searched 378 * @count: The number of characters to be sear 421 * @count: The number of characters to be searched 379 * @c: The character to search for 422 * @c: The character to search for 380 * << 381 * Note that the %NUL-terminator is considered << 382 * be searched for. << 383 */ 423 */ 384 char *strnchr(const char *s, size_t count, int 424 char *strnchr(const char *s, size_t count, int c) 385 { 425 { 386 while (count--) { !! 426 for (; count-- && *s != '\0'; ++s) 387 if (*s == (char)c) 427 if (*s == (char)c) 388 return (char *)s; 428 return (char *)s; 389 if (*s++ == '\0') << 390 break; << 391 } << 392 return NULL; 429 return NULL; 393 } 430 } 394 EXPORT_SYMBOL(strnchr); 431 EXPORT_SYMBOL(strnchr); 395 #endif 432 #endif 396 433 >> 434 /** >> 435 * skip_spaces - Removes leading whitespace from @str. >> 436 * @str: The string to be stripped. >> 437 * >> 438 * Returns a pointer to the first non-whitespace character in @str. >> 439 */ >> 440 char *skip_spaces(const char *str) >> 441 { >> 442 while (isspace(*str)) >> 443 ++str; >> 444 return (char *)str; >> 445 } >> 446 EXPORT_SYMBOL(skip_spaces); >> 447 >> 448 /** >> 449 * strim - Removes leading and trailing whitespace from @s. >> 450 * @s: The string to be stripped. >> 451 * >> 452 * Note that the first trailing whitespace is replaced with a %NUL-terminator >> 453 * in the given string @s. Returns a pointer to the first non-whitespace >> 454 * character in @s. >> 455 */ >> 456 char *strim(char *s) >> 457 { >> 458 size_t size; >> 459 char *end; >> 460 >> 461 size = strlen(s); >> 462 if (!size) >> 463 return s; >> 464 >> 465 end = s + size - 1; >> 466 while (end >= s && isspace(*end)) >> 467 end--; >> 468 *(end + 1) = '\0'; >> 469 >> 470 return skip_spaces(s); >> 471 } >> 472 EXPORT_SYMBOL(strim); >> 473 397 #ifndef __HAVE_ARCH_STRLEN 474 #ifndef __HAVE_ARCH_STRLEN >> 475 /** >> 476 * strlen - Find the length of a string >> 477 * @s: The string to be sized >> 478 */ 398 size_t strlen(const char *s) 479 size_t strlen(const char *s) 399 { 480 { 400 const char *sc; 481 const char *sc; 401 482 402 for (sc = s; *sc != '\0'; ++sc) 483 for (sc = s; *sc != '\0'; ++sc) 403 /* nothing */; 484 /* nothing */; 404 return sc - s; 485 return sc - s; 405 } 486 } 406 EXPORT_SYMBOL(strlen); 487 EXPORT_SYMBOL(strlen); 407 #endif 488 #endif 408 489 409 #ifndef __HAVE_ARCH_STRNLEN 490 #ifndef __HAVE_ARCH_STRNLEN >> 491 /** >> 492 * strnlen - Find the length of a length-limited string >> 493 * @s: The string to be sized >> 494 * @count: The maximum number of bytes to search >> 495 */ 410 size_t strnlen(const char *s, size_t count) 496 size_t strnlen(const char *s, size_t count) 411 { 497 { 412 const char *sc; 498 const char *sc; 413 499 414 for (sc = s; count-- && *sc != '\0'; + 500 for (sc = s; count-- && *sc != '\0'; ++sc) 415 /* nothing */; 501 /* nothing */; 416 return sc - s; 502 return sc - s; 417 } 503 } 418 EXPORT_SYMBOL(strnlen); 504 EXPORT_SYMBOL(strnlen); 419 #endif 505 #endif 420 506 421 #ifndef __HAVE_ARCH_STRSPN 507 #ifndef __HAVE_ARCH_STRSPN 422 /** 508 /** 423 * strspn - Calculate the length of the initia 509 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept 424 * @s: The string to be searched 510 * @s: The string to be searched 425 * @accept: The string to search for 511 * @accept: The string to search for 426 */ 512 */ 427 size_t strspn(const char *s, const char *accep 513 size_t strspn(const char *s, const char *accept) 428 { 514 { 429 const char *p; 515 const char *p; >> 516 const char *a; >> 517 size_t count = 0; 430 518 431 for (p = s; *p != '\0'; ++p) { 519 for (p = s; *p != '\0'; ++p) { 432 if (!strchr(accept, *p)) !! 520 for (a = accept; *a != '\0'; ++a) { 433 break; !! 521 if (*p == *a) >> 522 break; >> 523 } >> 524 if (*a == '\0') >> 525 return count; >> 526 ++count; 434 } 527 } 435 return p - s; !! 528 return count; 436 } 529 } >> 530 437 EXPORT_SYMBOL(strspn); 531 EXPORT_SYMBOL(strspn); 438 #endif 532 #endif 439 533 440 #ifndef __HAVE_ARCH_STRCSPN 534 #ifndef __HAVE_ARCH_STRCSPN 441 /** 535 /** 442 * strcspn - Calculate the length of the initi 536 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject 443 * @s: The string to be searched 537 * @s: The string to be searched 444 * @reject: The string to avoid 538 * @reject: The string to avoid 445 */ 539 */ 446 size_t strcspn(const char *s, const char *reje 540 size_t strcspn(const char *s, const char *reject) 447 { 541 { 448 const char *p; 542 const char *p; >> 543 const char *r; >> 544 size_t count = 0; 449 545 450 for (p = s; *p != '\0'; ++p) { 546 for (p = s; *p != '\0'; ++p) { 451 if (strchr(reject, *p)) !! 547 for (r = reject; *r != '\0'; ++r) { 452 break; !! 548 if (*p == *r) >> 549 return count; >> 550 } >> 551 ++count; 453 } 552 } 454 return p - s; !! 553 return count; 455 } 554 } 456 EXPORT_SYMBOL(strcspn); 555 EXPORT_SYMBOL(strcspn); 457 #endif 556 #endif 458 557 459 #ifndef __HAVE_ARCH_STRPBRK 558 #ifndef __HAVE_ARCH_STRPBRK 460 /** 559 /** 461 * strpbrk - Find the first occurrence of a se 560 * strpbrk - Find the first occurrence of a set of characters 462 * @cs: The string to be searched 561 * @cs: The string to be searched 463 * @ct: The characters to search for 562 * @ct: The characters to search for 464 */ 563 */ 465 char *strpbrk(const char *cs, const char *ct) 564 char *strpbrk(const char *cs, const char *ct) 466 { 565 { 467 const char *sc; !! 566 const char *sc1, *sc2; 468 567 469 for (sc = cs; *sc != '\0'; ++sc) { !! 568 for (sc1 = cs; *sc1 != '\0'; ++sc1) { 470 if (strchr(ct, *sc)) !! 569 for (sc2 = ct; *sc2 != '\0'; ++sc2) { 471 return (char *)sc; !! 570 if (*sc1 == *sc2) >> 571 return (char *)sc1; >> 572 } 472 } 573 } 473 return NULL; 574 return NULL; 474 } 575 } 475 EXPORT_SYMBOL(strpbrk); 576 EXPORT_SYMBOL(strpbrk); 476 #endif 577 #endif 477 578 478 #ifndef __HAVE_ARCH_STRSEP 579 #ifndef __HAVE_ARCH_STRSEP 479 /** 580 /** 480 * strsep - Split a string into tokens 581 * strsep - Split a string into tokens 481 * @s: The string to be searched 582 * @s: The string to be searched 482 * @ct: The characters to search for 583 * @ct: The characters to search for 483 * 584 * 484 * strsep() updates @s to point after the toke 585 * strsep() updates @s to point after the token, ready for the next call. 485 * 586 * 486 * It returns empty tokens, too, behaving exac 587 * It returns empty tokens, too, behaving exactly like the libc function 487 * of that name. In fact, it was stolen from g 588 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. 488 * Same semantics, slimmer shape. ;) 589 * Same semantics, slimmer shape. ;) 489 */ 590 */ 490 char *strsep(char **s, const char *ct) 591 char *strsep(char **s, const char *ct) 491 { 592 { 492 char *sbegin = *s; 593 char *sbegin = *s; 493 char *end; 594 char *end; 494 595 495 if (sbegin == NULL) 596 if (sbegin == NULL) 496 return NULL; 597 return NULL; 497 598 498 end = strpbrk(sbegin, ct); 599 end = strpbrk(sbegin, ct); 499 if (end) 600 if (end) 500 *end++ = '\0'; 601 *end++ = '\0'; 501 *s = end; 602 *s = end; 502 return sbegin; 603 return sbegin; 503 } 604 } 504 EXPORT_SYMBOL(strsep); 605 EXPORT_SYMBOL(strsep); 505 #endif 606 #endif 506 607 >> 608 /** >> 609 * sysfs_streq - return true if strings are equal, modulo trailing newline >> 610 * @s1: one string >> 611 * @s2: another string >> 612 * >> 613 * This routine returns true iff two strings are equal, treating both >> 614 * NUL and newline-then-NUL as equivalent string terminations. It's >> 615 * geared for use with sysfs input strings, which generally terminate >> 616 * with newlines but are compared against values without newlines. >> 617 */ >> 618 bool sysfs_streq(const char *s1, const char *s2) >> 619 { >> 620 while (*s1 && *s1 == *s2) { >> 621 s1++; >> 622 s2++; >> 623 } >> 624 >> 625 if (*s1 == *s2) >> 626 return true; >> 627 if (!*s1 && *s2 == '\n' && !s2[1]) >> 628 return true; >> 629 if (*s1 == '\n' && !s1[1] && !*s2) >> 630 return true; >> 631 return false; >> 632 } >> 633 EXPORT_SYMBOL(sysfs_streq); >> 634 >> 635 /** >> 636 * match_string - matches given string in an array >> 637 * @array: array of strings >> 638 * @n: number of strings in the array or -1 for NULL terminated arrays >> 639 * @string: string to match with >> 640 * >> 641 * Return: >> 642 * index of a @string in the @array if matches, or %-EINVAL otherwise. >> 643 */ >> 644 int match_string(const char * const *array, size_t n, const char *string) >> 645 { >> 646 int index; >> 647 const char *item; >> 648 >> 649 for (index = 0; index < n; index++) { >> 650 item = array[index]; >> 651 if (!item) >> 652 break; >> 653 if (!strcmp(item, string)) >> 654 return index; >> 655 } >> 656 >> 657 return -EINVAL; >> 658 } >> 659 EXPORT_SYMBOL(match_string); >> 660 >> 661 /** >> 662 * __sysfs_match_string - matches given string in an array >> 663 * @array: array of strings >> 664 * @n: number of strings in the array or -1 for NULL terminated arrays >> 665 * @str: string to match with >> 666 * >> 667 * Returns index of @str in the @array or -EINVAL, just like match_string(). >> 668 * Uses sysfs_streq instead of strcmp for matching. >> 669 */ >> 670 int __sysfs_match_string(const char * const *array, size_t n, const char *str) >> 671 { >> 672 const char *item; >> 673 int index; >> 674 >> 675 for (index = 0; index < n; index++) { >> 676 item = array[index]; >> 677 if (!item) >> 678 break; >> 679 if (sysfs_streq(item, str)) >> 680 return index; >> 681 } >> 682 >> 683 return -EINVAL; >> 684 } >> 685 EXPORT_SYMBOL(__sysfs_match_string); >> 686 507 #ifndef __HAVE_ARCH_MEMSET 687 #ifndef __HAVE_ARCH_MEMSET 508 /** 688 /** 509 * memset - Fill a region of memory with the g 689 * memset - Fill a region of memory with the given value 510 * @s: Pointer to the start of the area. 690 * @s: Pointer to the start of the area. 511 * @c: The byte to fill the area with 691 * @c: The byte to fill the area with 512 * @count: The size of the area. 692 * @count: The size of the area. 513 * 693 * 514 * Do not use memset() to access IO space, use 694 * Do not use memset() to access IO space, use memset_io() instead. 515 */ 695 */ 516 void *memset(void *s, int c, size_t count) 696 void *memset(void *s, int c, size_t count) 517 { 697 { 518 char *xs = s; 698 char *xs = s; 519 699 520 while (count--) 700 while (count--) 521 *xs++ = c; 701 *xs++ = c; 522 return s; 702 return s; 523 } 703 } 524 EXPORT_SYMBOL(memset); 704 EXPORT_SYMBOL(memset); 525 #endif 705 #endif 526 706 >> 707 /** >> 708 * memzero_explicit - Fill a region of memory (e.g. sensitive >> 709 * keying data) with 0s. >> 710 * @s: Pointer to the start of the area. >> 711 * @count: The size of the area. >> 712 * >> 713 * Note: usually using memset() is just fine (!), but in cases >> 714 * where clearing out _local_ data at the end of a scope is >> 715 * necessary, memzero_explicit() should be used instead in >> 716 * order to prevent the compiler from optimising away zeroing. >> 717 * >> 718 * memzero_explicit() doesn't need an arch-specific version as >> 719 * it just invokes the one of memset() implicitly. >> 720 */ >> 721 void memzero_explicit(void *s, size_t count) >> 722 { >> 723 memset(s, 0, count); >> 724 barrier_data(s); >> 725 } >> 726 EXPORT_SYMBOL(memzero_explicit); >> 727 527 #ifndef __HAVE_ARCH_MEMSET16 728 #ifndef __HAVE_ARCH_MEMSET16 528 /** 729 /** 529 * memset16() - Fill a memory area with a uint 730 * memset16() - Fill a memory area with a uint16_t 530 * @s: Pointer to the start of the area. 731 * @s: Pointer to the start of the area. 531 * @v: The value to fill the area with 732 * @v: The value to fill the area with 532 * @count: The number of values to store 733 * @count: The number of values to store 533 * 734 * 534 * Differs from memset() in that it fills with 735 * Differs from memset() in that it fills with a uint16_t instead 535 * of a byte. Remember that @count is the num 736 * of a byte. Remember that @count is the number of uint16_ts to 536 * store, not the number of bytes. 737 * store, not the number of bytes. 537 */ 738 */ 538 void *memset16(uint16_t *s, uint16_t v, size_t 739 void *memset16(uint16_t *s, uint16_t v, size_t count) 539 { 740 { 540 uint16_t *xs = s; 741 uint16_t *xs = s; 541 742 542 while (count--) 743 while (count--) 543 *xs++ = v; 744 *xs++ = v; 544 return s; 745 return s; 545 } 746 } 546 EXPORT_SYMBOL(memset16); 747 EXPORT_SYMBOL(memset16); 547 #endif 748 #endif 548 749 549 #ifndef __HAVE_ARCH_MEMSET32 750 #ifndef __HAVE_ARCH_MEMSET32 550 /** 751 /** 551 * memset32() - Fill a memory area with a uint 752 * memset32() - Fill a memory area with a uint32_t 552 * @s: Pointer to the start of the area. 753 * @s: Pointer to the start of the area. 553 * @v: The value to fill the area with 754 * @v: The value to fill the area with 554 * @count: The number of values to store 755 * @count: The number of values to store 555 * 756 * 556 * Differs from memset() in that it fills with 757 * Differs from memset() in that it fills with a uint32_t instead 557 * of a byte. Remember that @count is the num 758 * of a byte. Remember that @count is the number of uint32_ts to 558 * store, not the number of bytes. 759 * store, not the number of bytes. 559 */ 760 */ 560 void *memset32(uint32_t *s, uint32_t v, size_t 761 void *memset32(uint32_t *s, uint32_t v, size_t count) 561 { 762 { 562 uint32_t *xs = s; 763 uint32_t *xs = s; 563 764 564 while (count--) 765 while (count--) 565 *xs++ = v; 766 *xs++ = v; 566 return s; 767 return s; 567 } 768 } 568 EXPORT_SYMBOL(memset32); 769 EXPORT_SYMBOL(memset32); 569 #endif 770 #endif 570 771 571 #ifndef __HAVE_ARCH_MEMSET64 772 #ifndef __HAVE_ARCH_MEMSET64 572 /** 773 /** 573 * memset64() - Fill a memory area with a uint 774 * memset64() - Fill a memory area with a uint64_t 574 * @s: Pointer to the start of the area. 775 * @s: Pointer to the start of the area. 575 * @v: The value to fill the area with 776 * @v: The value to fill the area with 576 * @count: The number of values to store 777 * @count: The number of values to store 577 * 778 * 578 * Differs from memset() in that it fills with 779 * Differs from memset() in that it fills with a uint64_t instead 579 * of a byte. Remember that @count is the num 780 * of a byte. Remember that @count is the number of uint64_ts to 580 * store, not the number of bytes. 781 * store, not the number of bytes. 581 */ 782 */ 582 void *memset64(uint64_t *s, uint64_t v, size_t 783 void *memset64(uint64_t *s, uint64_t v, size_t count) 583 { 784 { 584 uint64_t *xs = s; 785 uint64_t *xs = s; 585 786 586 while (count--) 787 while (count--) 587 *xs++ = v; 788 *xs++ = v; 588 return s; 789 return s; 589 } 790 } 590 EXPORT_SYMBOL(memset64); 791 EXPORT_SYMBOL(memset64); 591 #endif 792 #endif 592 793 593 #ifndef __HAVE_ARCH_MEMCPY 794 #ifndef __HAVE_ARCH_MEMCPY 594 /** 795 /** 595 * memcpy - Copy one area of memory to another 796 * memcpy - Copy one area of memory to another 596 * @dest: Where to copy to 797 * @dest: Where to copy to 597 * @src: Where to copy from 798 * @src: Where to copy from 598 * @count: The size of the area. 799 * @count: The size of the area. 599 * 800 * 600 * You should not use this function to access 801 * You should not use this function to access IO space, use memcpy_toio() 601 * or memcpy_fromio() instead. 802 * or memcpy_fromio() instead. 602 */ 803 */ 603 void *memcpy(void *dest, const void *src, size 804 void *memcpy(void *dest, const void *src, size_t count) 604 { 805 { 605 char *tmp = dest; 806 char *tmp = dest; 606 const char *s = src; 807 const char *s = src; 607 808 608 while (count--) 809 while (count--) 609 *tmp++ = *s++; 810 *tmp++ = *s++; 610 return dest; 811 return dest; 611 } 812 } 612 EXPORT_SYMBOL(memcpy); 813 EXPORT_SYMBOL(memcpy); 613 #endif 814 #endif 614 815 615 #ifndef __HAVE_ARCH_MEMMOVE 816 #ifndef __HAVE_ARCH_MEMMOVE 616 /** 817 /** 617 * memmove - Copy one area of memory to anothe 818 * memmove - Copy one area of memory to another 618 * @dest: Where to copy to 819 * @dest: Where to copy to 619 * @src: Where to copy from 820 * @src: Where to copy from 620 * @count: The size of the area. 821 * @count: The size of the area. 621 * 822 * 622 * Unlike memcpy(), memmove() copes with overl 823 * Unlike memcpy(), memmove() copes with overlapping areas. 623 */ 824 */ 624 void *memmove(void *dest, const void *src, siz 825 void *memmove(void *dest, const void *src, size_t count) 625 { 826 { 626 char *tmp; 827 char *tmp; 627 const char *s; 828 const char *s; 628 829 629 if (dest <= src) { 830 if (dest <= src) { 630 tmp = dest; 831 tmp = dest; 631 s = src; 832 s = src; 632 while (count--) 833 while (count--) 633 *tmp++ = *s++; 834 *tmp++ = *s++; 634 } else { 835 } else { 635 tmp = dest; 836 tmp = dest; 636 tmp += count; 837 tmp += count; 637 s = src; 838 s = src; 638 s += count; 839 s += count; 639 while (count--) 840 while (count--) 640 *--tmp = *--s; 841 *--tmp = *--s; 641 } 842 } 642 return dest; 843 return dest; 643 } 844 } 644 EXPORT_SYMBOL(memmove); 845 EXPORT_SYMBOL(memmove); 645 #endif 846 #endif 646 847 647 #ifndef __HAVE_ARCH_MEMCMP 848 #ifndef __HAVE_ARCH_MEMCMP 648 /** 849 /** 649 * memcmp - Compare two areas of memory 850 * memcmp - Compare two areas of memory 650 * @cs: One area of memory 851 * @cs: One area of memory 651 * @ct: Another area of memory 852 * @ct: Another area of memory 652 * @count: The size of the area. 853 * @count: The size of the area. 653 */ 854 */ 654 #undef memcmp 855 #undef memcmp 655 __visible int memcmp(const void *cs, const voi 856 __visible int memcmp(const void *cs, const void *ct, size_t count) 656 { 857 { 657 const unsigned char *su1, *su2; 858 const unsigned char *su1, *su2; 658 int res = 0; 859 int res = 0; 659 860 660 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS << 661 if (count >= sizeof(unsigned long)) { << 662 const unsigned long *u1 = cs; << 663 const unsigned long *u2 = ct; << 664 do { << 665 if (get_unaligned(u1) << 666 break; << 667 u1++; << 668 u2++; << 669 count -= sizeof(unsign << 670 } while (count >= sizeof(unsig << 671 cs = u1; << 672 ct = u2; << 673 } << 674 #endif << 675 for (su1 = cs, su2 = ct; 0 < count; ++ 861 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) 676 if ((res = *su1 - *su2) != 0) 862 if ((res = *su1 - *su2) != 0) 677 break; 863 break; 678 return res; 864 return res; 679 } 865 } 680 EXPORT_SYMBOL(memcmp); 866 EXPORT_SYMBOL(memcmp); 681 #endif 867 #endif 682 868 683 #ifndef __HAVE_ARCH_BCMP 869 #ifndef __HAVE_ARCH_BCMP 684 /** 870 /** 685 * bcmp - returns 0 if and only if the buffers 871 * bcmp - returns 0 if and only if the buffers have identical contents. 686 * @a: pointer to first buffer. 872 * @a: pointer to first buffer. 687 * @b: pointer to second buffer. 873 * @b: pointer to second buffer. 688 * @len: size of buffers. 874 * @len: size of buffers. 689 * 875 * 690 * The sign or magnitude of a non-zero return 876 * The sign or magnitude of a non-zero return value has no particular 691 * meaning, and architectures may implement th 877 * meaning, and architectures may implement their own more efficient bcmp(). So 692 * while this particular implementation is a s 878 * while this particular implementation is a simple (tail) call to memcmp, do 693 * not rely on anything but whether the return 879 * not rely on anything but whether the return value is zero or non-zero. 694 */ 880 */ >> 881 #undef bcmp 695 int bcmp(const void *a, const void *b, size_t 882 int bcmp(const void *a, const void *b, size_t len) 696 { 883 { 697 return memcmp(a, b, len); 884 return memcmp(a, b, len); 698 } 885 } 699 EXPORT_SYMBOL(bcmp); 886 EXPORT_SYMBOL(bcmp); 700 #endif 887 #endif 701 888 702 #ifndef __HAVE_ARCH_MEMSCAN 889 #ifndef __HAVE_ARCH_MEMSCAN 703 /** 890 /** 704 * memscan - Find a character in an area of me 891 * memscan - Find a character in an area of memory. 705 * @addr: The memory area 892 * @addr: The memory area 706 * @c: The byte to search for 893 * @c: The byte to search for 707 * @size: The size of the area. 894 * @size: The size of the area. 708 * 895 * 709 * returns the address of the first occurrence 896 * returns the address of the first occurrence of @c, or 1 byte past 710 * the area if @c is not found 897 * the area if @c is not found 711 */ 898 */ 712 void *memscan(void *addr, int c, size_t size) 899 void *memscan(void *addr, int c, size_t size) 713 { 900 { 714 unsigned char *p = addr; 901 unsigned char *p = addr; 715 902 716 while (size) { 903 while (size) { 717 if (*p == (unsigned char)c) !! 904 if (*p == c) 718 return (void *)p; 905 return (void *)p; 719 p++; 906 p++; 720 size--; 907 size--; 721 } 908 } 722 return (void *)p; 909 return (void *)p; 723 } 910 } 724 EXPORT_SYMBOL(memscan); 911 EXPORT_SYMBOL(memscan); 725 #endif 912 #endif 726 913 727 #ifndef __HAVE_ARCH_STRSTR 914 #ifndef __HAVE_ARCH_STRSTR 728 /** 915 /** 729 * strstr - Find the first substring in a %NUL 916 * strstr - Find the first substring in a %NUL terminated string 730 * @s1: The string to be searched 917 * @s1: The string to be searched 731 * @s2: The string to search for 918 * @s2: The string to search for 732 */ 919 */ 733 char *strstr(const char *s1, const char *s2) 920 char *strstr(const char *s1, const char *s2) 734 { 921 { 735 size_t l1, l2; 922 size_t l1, l2; 736 923 737 l2 = strlen(s2); 924 l2 = strlen(s2); 738 if (!l2) 925 if (!l2) 739 return (char *)s1; 926 return (char *)s1; 740 l1 = strlen(s1); 927 l1 = strlen(s1); 741 while (l1 >= l2) { 928 while (l1 >= l2) { 742 l1--; 929 l1--; 743 if (!memcmp(s1, s2, l2)) 930 if (!memcmp(s1, s2, l2)) 744 return (char *)s1; 931 return (char *)s1; 745 s1++; 932 s1++; 746 } 933 } 747 return NULL; 934 return NULL; 748 } 935 } 749 EXPORT_SYMBOL(strstr); 936 EXPORT_SYMBOL(strstr); 750 #endif 937 #endif 751 938 752 #ifndef __HAVE_ARCH_STRNSTR 939 #ifndef __HAVE_ARCH_STRNSTR 753 /** 940 /** 754 * strnstr - Find the first substring in a len 941 * strnstr - Find the first substring in a length-limited string 755 * @s1: The string to be searched 942 * @s1: The string to be searched 756 * @s2: The string to search for 943 * @s2: The string to search for 757 * @len: the maximum number of characters to s 944 * @len: the maximum number of characters to search 758 */ 945 */ 759 char *strnstr(const char *s1, const char *s2, 946 char *strnstr(const char *s1, const char *s2, size_t len) 760 { 947 { 761 size_t l2; 948 size_t l2; 762 949 763 l2 = strlen(s2); 950 l2 = strlen(s2); 764 if (!l2) 951 if (!l2) 765 return (char *)s1; 952 return (char *)s1; 766 while (len >= l2) { 953 while (len >= l2) { 767 len--; 954 len--; 768 if (!memcmp(s1, s2, l2)) 955 if (!memcmp(s1, s2, l2)) 769 return (char *)s1; 956 return (char *)s1; 770 s1++; 957 s1++; 771 } 958 } 772 return NULL; 959 return NULL; 773 } 960 } 774 EXPORT_SYMBOL(strnstr); 961 EXPORT_SYMBOL(strnstr); 775 #endif 962 #endif 776 963 777 #ifndef __HAVE_ARCH_MEMCHR 964 #ifndef __HAVE_ARCH_MEMCHR 778 /** 965 /** 779 * memchr - Find a character in an area of mem 966 * memchr - Find a character in an area of memory. 780 * @s: The memory area 967 * @s: The memory area 781 * @c: The byte to search for 968 * @c: The byte to search for 782 * @n: The size of the area. 969 * @n: The size of the area. 783 * 970 * 784 * returns the address of the first occurrence 971 * returns the address of the first occurrence of @c, or %NULL 785 * if @c is not found 972 * if @c is not found 786 */ 973 */ 787 void *memchr(const void *s, int c, size_t n) 974 void *memchr(const void *s, int c, size_t n) 788 { 975 { 789 const unsigned char *p = s; 976 const unsigned char *p = s; 790 while (n-- != 0) { 977 while (n-- != 0) { 791 if ((unsigned char)c == *p++) 978 if ((unsigned char)c == *p++) { 792 return (void *)(p - 1) 979 return (void *)(p - 1); 793 } 980 } 794 } 981 } 795 return NULL; 982 return NULL; 796 } 983 } 797 EXPORT_SYMBOL(memchr); 984 EXPORT_SYMBOL(memchr); 798 #endif 985 #endif 799 986 800 static void *check_bytes8(const u8 *start, u8 987 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) 801 { 988 { 802 while (bytes) { 989 while (bytes) { 803 if (*start != value) 990 if (*start != value) 804 return (void *)start; 991 return (void *)start; 805 start++; 992 start++; 806 bytes--; 993 bytes--; 807 } 994 } 808 return NULL; 995 return NULL; 809 } 996 } 810 997 811 /** 998 /** 812 * memchr_inv - Find an unmatching character i 999 * memchr_inv - Find an unmatching character in an area of memory. 813 * @start: The memory area 1000 * @start: The memory area 814 * @c: Find a character other than c 1001 * @c: Find a character other than c 815 * @bytes: The size of the area. 1002 * @bytes: The size of the area. 816 * 1003 * 817 * returns the address of the first character 1004 * returns the address of the first character other than @c, or %NULL 818 * if the whole buffer contains just @c. 1005 * if the whole buffer contains just @c. 819 */ 1006 */ 820 void *memchr_inv(const void *start, int c, siz 1007 void *memchr_inv(const void *start, int c, size_t bytes) 821 { 1008 { 822 u8 value = c; 1009 u8 value = c; 823 u64 value64; 1010 u64 value64; 824 unsigned int words, prefix; 1011 unsigned int words, prefix; 825 1012 826 if (bytes <= 16) 1013 if (bytes <= 16) 827 return check_bytes8(start, val 1014 return check_bytes8(start, value, bytes); 828 1015 829 value64 = value; 1016 value64 = value; 830 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) & 1017 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64 831 value64 *= 0x0101010101010101ULL; 1018 value64 *= 0x0101010101010101ULL; 832 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) 1019 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) 833 value64 *= 0x01010101; 1020 value64 *= 0x01010101; 834 value64 |= value64 << 32; 1021 value64 |= value64 << 32; 835 #else 1022 #else 836 value64 |= value64 << 8; 1023 value64 |= value64 << 8; 837 value64 |= value64 << 16; 1024 value64 |= value64 << 16; 838 value64 |= value64 << 32; 1025 value64 |= value64 << 32; 839 #endif 1026 #endif 840 1027 841 prefix = (unsigned long)start % 8; 1028 prefix = (unsigned long)start % 8; 842 if (prefix) { 1029 if (prefix) { 843 u8 *r; 1030 u8 *r; 844 1031 845 prefix = 8 - prefix; 1032 prefix = 8 - prefix; 846 r = check_bytes8(start, value, 1033 r = check_bytes8(start, value, prefix); 847 if (r) 1034 if (r) 848 return r; 1035 return r; 849 start += prefix; 1036 start += prefix; 850 bytes -= prefix; 1037 bytes -= prefix; 851 } 1038 } 852 1039 853 words = bytes / 8; 1040 words = bytes / 8; 854 1041 855 while (words) { 1042 while (words) { 856 if (*(u64 *)start != value64) 1043 if (*(u64 *)start != value64) 857 return check_bytes8(st 1044 return check_bytes8(start, value, 8); 858 start += 8; 1045 start += 8; 859 words--; 1046 words--; 860 } 1047 } 861 1048 862 return check_bytes8(start, value, byte 1049 return check_bytes8(start, value, bytes % 8); 863 } 1050 } 864 EXPORT_SYMBOL(memchr_inv); 1051 EXPORT_SYMBOL(memchr_inv); >> 1052 >> 1053 /** >> 1054 * strreplace - Replace all occurrences of character in string. >> 1055 * @s: The string to operate on. >> 1056 * @old: The character being replaced. >> 1057 * @new: The character @old is replaced with. >> 1058 * >> 1059 * Returns pointer to the nul byte at the end of @s. >> 1060 */ >> 1061 char *strreplace(char *s, char old, char new) >> 1062 { >> 1063 for (; *s; ++s) >> 1064 if (*s == old) >> 1065 *s = new; >> 1066 return s; >> 1067 } >> 1068 EXPORT_SYMBOL(strreplace); >> 1069 >> 1070 void fortify_panic(const char *name) >> 1071 { >> 1072 pr_emerg("detected buffer overflow in %s\n", name); >> 1073 BUG(); >> 1074 } >> 1075 EXPORT_SYMBOL(fortify_panic); 865 1076
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.