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