wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * linux/lib/string.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * stupid library routines.. The optimized versions should generally be found |
| 9 | * as inline code in <asm-xx/string.h> |
| 10 | * |
| 11 | * These are buggy as well.. |
| 12 | * |
| 13 | * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de> |
| 14 | * - Added strsep() which will replace strtok() soon (because strsep() is |
| 15 | * reentrant and should be faster). Use only strsep() in new code, please. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <malloc.h> |
| 22 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 23 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | /** |
Simon Glass | b1f17bf | 2012-12-05 14:46:35 +0000 | [diff] [blame] | 25 | * strncasecmp - Case insensitive, length-limited string comparison |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 26 | * @s1: One string |
| 27 | * @s2: The other string |
| 28 | * @len: the maximum number of characters to compare |
| 29 | */ |
Simon Glass | b1f17bf | 2012-12-05 14:46:35 +0000 | [diff] [blame] | 30 | int strncasecmp(const char *s1, const char *s2, size_t len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 31 | { |
| 32 | /* Yes, Virginia, it had better be unsigned */ |
| 33 | unsigned char c1, c2; |
| 34 | |
| 35 | c1 = 0; c2 = 0; |
| 36 | if (len) { |
| 37 | do { |
| 38 | c1 = *s1; c2 = *s2; |
| 39 | s1++; s2++; |
| 40 | if (!c1) |
| 41 | break; |
| 42 | if (!c2) |
| 43 | break; |
| 44 | if (c1 == c2) |
| 45 | continue; |
| 46 | c1 = tolower(c1); |
| 47 | c2 = tolower(c2); |
| 48 | if (c1 != c2) |
| 49 | break; |
| 50 | } while (--len); |
| 51 | } |
| 52 | return (int)c1 - (int)c2; |
| 53 | } |
Simon Glass | b1f17bf | 2012-12-05 14:46:35 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * strcasecmp - Case insensitive string comparison |
| 57 | * @s1: One string |
| 58 | * @s2: The other string |
| 59 | */ |
| 60 | int strcasecmp(const char *s1, const char *s2) |
| 61 | { |
| 62 | return strncasecmp(s1, s2, -1U); |
| 63 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 64 | |
| 65 | char * ___strtok; |
| 66 | |
| 67 | #ifndef __HAVE_ARCH_STRCPY |
| 68 | /** |
| 69 | * strcpy - Copy a %NUL terminated string |
| 70 | * @dest: Where to copy the string to |
| 71 | * @src: Where to copy the string from |
| 72 | */ |
| 73 | char * strcpy(char * dest,const char *src) |
| 74 | { |
| 75 | char *tmp = dest; |
| 76 | |
| 77 | while ((*dest++ = *src++) != '\0') |
| 78 | /* nothing */; |
| 79 | return tmp; |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | #ifndef __HAVE_ARCH_STRNCPY |
| 84 | /** |
| 85 | * strncpy - Copy a length-limited, %NUL-terminated string |
| 86 | * @dest: Where to copy the string to |
| 87 | * @src: Where to copy the string from |
| 88 | * @count: The maximum number of bytes to copy |
| 89 | * |
| 90 | * Note that unlike userspace strncpy, this does not %NUL-pad the buffer. |
| 91 | * However, the result is not %NUL-terminated if the source exceeds |
| 92 | * @count bytes. |
| 93 | */ |
| 94 | char * strncpy(char * dest,const char *src,size_t count) |
| 95 | { |
| 96 | char *tmp = dest; |
| 97 | |
| 98 | while (count-- && (*dest++ = *src++) != '\0') |
| 99 | /* nothing */; |
| 100 | |
| 101 | return tmp; |
| 102 | } |
| 103 | #endif |
| 104 | |
Masahiro Yamada | 80d9ef8 | 2014-11-20 21:20:32 +0900 | [diff] [blame] | 105 | #ifndef __HAVE_ARCH_STRLCPY |
| 106 | /** |
| 107 | * strlcpy - Copy a C-string into a sized buffer |
| 108 | * @dest: Where to copy the string to |
| 109 | * @src: Where to copy the string from |
| 110 | * @size: size of destination buffer |
| 111 | * |
| 112 | * Compatible with *BSD: the result is always a valid |
| 113 | * NUL-terminated string that fits in the buffer (unless, |
| 114 | * of course, the buffer size is zero). It does not pad |
| 115 | * out the result like strncpy() does. |
| 116 | */ |
| 117 | size_t strlcpy(char *dest, const char *src, size_t size) |
| 118 | { |
| 119 | size_t ret = strlen(src); |
| 120 | |
| 121 | if (size) { |
| 122 | size_t len = (ret >= size) ? size - 1 : ret; |
| 123 | memcpy(dest, src, len); |
| 124 | dest[len] = '\0'; |
| 125 | } |
| 126 | return ret; |
| 127 | } |
| 128 | #endif |
| 129 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 130 | #ifndef __HAVE_ARCH_STRCAT |
| 131 | /** |
| 132 | * strcat - Append one %NUL-terminated string to another |
| 133 | * @dest: The string to be appended to |
| 134 | * @src: The string to append to it |
| 135 | */ |
| 136 | char * strcat(char * dest, const char * src) |
| 137 | { |
| 138 | char *tmp = dest; |
| 139 | |
| 140 | while (*dest) |
| 141 | dest++; |
| 142 | while ((*dest++ = *src++) != '\0') |
| 143 | ; |
| 144 | |
| 145 | return tmp; |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | #ifndef __HAVE_ARCH_STRNCAT |
| 150 | /** |
| 151 | * strncat - Append a length-limited, %NUL-terminated string to another |
| 152 | * @dest: The string to be appended to |
| 153 | * @src: The string to append to it |
| 154 | * @count: The maximum numbers of bytes to copy |
| 155 | * |
| 156 | * Note that in contrast to strncpy, strncat ensures the result is |
| 157 | * terminated. |
| 158 | */ |
| 159 | char * strncat(char *dest, const char *src, size_t count) |
| 160 | { |
| 161 | char *tmp = dest; |
| 162 | |
| 163 | if (count) { |
| 164 | while (*dest) |
| 165 | dest++; |
| 166 | while ((*dest++ = *src++)) { |
| 167 | if (--count == 0) { |
| 168 | *dest = '\0'; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return tmp; |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | #ifndef __HAVE_ARCH_STRCMP |
| 179 | /** |
| 180 | * strcmp - Compare two strings |
| 181 | * @cs: One string |
| 182 | * @ct: Another string |
| 183 | */ |
| 184 | int strcmp(const char * cs,const char * ct) |
| 185 | { |
| 186 | register signed char __res; |
| 187 | |
| 188 | while (1) { |
| 189 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | return __res; |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | #ifndef __HAVE_ARCH_STRNCMP |
| 198 | /** |
| 199 | * strncmp - Compare two length-limited strings |
| 200 | * @cs: One string |
| 201 | * @ct: Another string |
| 202 | * @count: The maximum number of bytes to compare |
| 203 | */ |
| 204 | int strncmp(const char * cs,const char * ct,size_t count) |
| 205 | { |
| 206 | register signed char __res = 0; |
| 207 | |
| 208 | while (count) { |
| 209 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 210 | break; |
| 211 | count--; |
| 212 | } |
| 213 | |
| 214 | return __res; |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | #ifndef __HAVE_ARCH_STRCHR |
| 219 | /** |
| 220 | * strchr - Find the first occurrence of a character in a string |
| 221 | * @s: The string to be searched |
| 222 | * @c: The character to search for |
| 223 | */ |
| 224 | char * strchr(const char * s, int c) |
| 225 | { |
| 226 | for(; *s != (char) c; ++s) |
| 227 | if (*s == '\0') |
| 228 | return NULL; |
| 229 | return (char *) s; |
| 230 | } |
| 231 | #endif |
| 232 | |
Simon Glass | 6b45ba4 | 2017-05-18 20:09:28 -0600 | [diff] [blame] | 233 | const char *strchrnul(const char *s, int c) |
| 234 | { |
| 235 | for (; *s != (char)c; ++s) |
| 236 | if (*s == '\0') |
| 237 | break; |
| 238 | return s; |
| 239 | } |
| 240 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | #ifndef __HAVE_ARCH_STRRCHR |
| 242 | /** |
| 243 | * strrchr - Find the last occurrence of a character in a string |
| 244 | * @s: The string to be searched |
| 245 | * @c: The character to search for |
| 246 | */ |
| 247 | char * strrchr(const char * s, int c) |
| 248 | { |
| 249 | const char *p = s + strlen(s); |
| 250 | do { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 251 | if (*p == (char)c) |
| 252 | return (char *)p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 253 | } while (--p >= s); |
| 254 | return NULL; |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | #ifndef __HAVE_ARCH_STRLEN |
| 259 | /** |
| 260 | * strlen - Find the length of a string |
| 261 | * @s: The string to be sized |
| 262 | */ |
| 263 | size_t strlen(const char * s) |
| 264 | { |
| 265 | const char *sc; |
| 266 | |
| 267 | for (sc = s; *sc != '\0'; ++sc) |
| 268 | /* nothing */; |
| 269 | return sc - s; |
| 270 | } |
| 271 | #endif |
| 272 | |
| 273 | #ifndef __HAVE_ARCH_STRNLEN |
| 274 | /** |
| 275 | * strnlen - Find the length of a length-limited string |
| 276 | * @s: The string to be sized |
| 277 | * @count: The maximum number of bytes to search |
| 278 | */ |
| 279 | size_t strnlen(const char * s, size_t count) |
| 280 | { |
| 281 | const char *sc; |
| 282 | |
| 283 | for (sc = s; count-- && *sc != '\0'; ++sc) |
| 284 | /* nothing */; |
| 285 | return sc - s; |
| 286 | } |
| 287 | #endif |
| 288 | |
Simon Glass | a7d0021 | 2017-05-18 20:09:29 -0600 | [diff] [blame] | 289 | #ifndef __HAVE_ARCH_STRCSPN |
| 290 | /** |
| 291 | * strcspn - Calculate the length of the initial substring of @s which does |
| 292 | * not contain letters in @reject |
| 293 | * @s: The string to be searched |
| 294 | * @reject: The string to avoid |
| 295 | */ |
| 296 | size_t strcspn(const char *s, const char *reject) |
| 297 | { |
| 298 | const char *p; |
| 299 | const char *r; |
| 300 | size_t count = 0; |
| 301 | |
| 302 | for (p = s; *p != '\0'; ++p) { |
| 303 | for (r = reject; *r != '\0'; ++r) { |
| 304 | if (*p == *r) |
| 305 | return count; |
| 306 | } |
| 307 | ++count; |
| 308 | } |
| 309 | return count; |
| 310 | } |
| 311 | #endif |
| 312 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 313 | #ifndef __HAVE_ARCH_STRDUP |
| 314 | char * strdup(const char *s) |
| 315 | { |
| 316 | char *new; |
| 317 | |
| 318 | if ((s == NULL) || |
| 319 | ((new = malloc (strlen(s) + 1)) == NULL) ) { |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | strcpy (new, s); |
| 324 | return new; |
| 325 | } |
| 326 | #endif |
| 327 | |
| 328 | #ifndef __HAVE_ARCH_STRSPN |
| 329 | /** |
| 330 | * strspn - Calculate the length of the initial substring of @s which only |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 331 | * contain letters in @accept |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 332 | * @s: The string to be searched |
| 333 | * @accept: The string to search for |
| 334 | */ |
| 335 | size_t strspn(const char *s, const char *accept) |
| 336 | { |
| 337 | const char *p; |
| 338 | const char *a; |
| 339 | size_t count = 0; |
| 340 | |
| 341 | for (p = s; *p != '\0'; ++p) { |
| 342 | for (a = accept; *a != '\0'; ++a) { |
| 343 | if (*p == *a) |
| 344 | break; |
| 345 | } |
| 346 | if (*a == '\0') |
| 347 | return count; |
| 348 | ++count; |
| 349 | } |
| 350 | |
| 351 | return count; |
| 352 | } |
| 353 | #endif |
| 354 | |
| 355 | #ifndef __HAVE_ARCH_STRPBRK |
| 356 | /** |
| 357 | * strpbrk - Find the first occurrence of a set of characters |
| 358 | * @cs: The string to be searched |
| 359 | * @ct: The characters to search for |
| 360 | */ |
| 361 | char * strpbrk(const char * cs,const char * ct) |
| 362 | { |
| 363 | const char *sc1,*sc2; |
| 364 | |
| 365 | for( sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 366 | for( sc2 = ct; *sc2 != '\0'; ++sc2) { |
| 367 | if (*sc1 == *sc2) |
| 368 | return (char *) sc1; |
| 369 | } |
| 370 | } |
| 371 | return NULL; |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | #ifndef __HAVE_ARCH_STRTOK |
| 376 | /** |
| 377 | * strtok - Split a string into tokens |
| 378 | * @s: The string to be searched |
| 379 | * @ct: The characters to search for |
| 380 | * |
| 381 | * WARNING: strtok is deprecated, use strsep instead. |
| 382 | */ |
| 383 | char * strtok(char * s,const char * ct) |
| 384 | { |
| 385 | char *sbegin, *send; |
| 386 | |
| 387 | sbegin = s ? s : ___strtok; |
| 388 | if (!sbegin) { |
| 389 | return NULL; |
| 390 | } |
| 391 | sbegin += strspn(sbegin,ct); |
| 392 | if (*sbegin == '\0') { |
| 393 | ___strtok = NULL; |
| 394 | return( NULL ); |
| 395 | } |
| 396 | send = strpbrk( sbegin, ct); |
| 397 | if (send && *send != '\0') |
| 398 | *send++ = '\0'; |
| 399 | ___strtok = send; |
| 400 | return (sbegin); |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | #ifndef __HAVE_ARCH_STRSEP |
| 405 | /** |
| 406 | * strsep - Split a string into tokens |
| 407 | * @s: The string to be searched |
| 408 | * @ct: The characters to search for |
| 409 | * |
| 410 | * strsep() updates @s to point after the token, ready for the next call. |
| 411 | * |
| 412 | * It returns empty tokens, too, behaving exactly like the libc function |
| 413 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
| 414 | * Same semantics, slimmer shape. ;) |
| 415 | */ |
| 416 | char * strsep(char **s, const char *ct) |
| 417 | { |
| 418 | char *sbegin = *s, *end; |
| 419 | |
| 420 | if (sbegin == NULL) |
| 421 | return NULL; |
| 422 | |
| 423 | end = strpbrk(sbegin, ct); |
| 424 | if (end) |
| 425 | *end++ = '\0'; |
| 426 | *s = end; |
| 427 | |
| 428 | return sbegin; |
| 429 | } |
| 430 | #endif |
| 431 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 432 | #ifndef __HAVE_ARCH_STRSWAB |
| 433 | /** |
| 434 | * strswab - swap adjacent even and odd bytes in %NUL-terminated string |
| 435 | * s: address of the string |
| 436 | * |
| 437 | * returns the address of the swapped string or NULL on error. If |
| 438 | * string length is odd, last byte is untouched. |
| 439 | */ |
| 440 | char *strswab(const char *s) |
| 441 | { |
Wolfgang Denk | 389db1f | 2005-09-25 16:15:17 +0200 | [diff] [blame] | 442 | char *p, *q; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 443 | |
| 444 | if ((NULL == s) || ('\0' == *s)) { |
| 445 | return (NULL); |
| 446 | } |
| 447 | |
Wolfgang Denk | e5e98ed | 2005-10-04 23:38:07 +0200 | [diff] [blame] | 448 | for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) { |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 449 | char tmp; |
Wolfgang Denk | 389db1f | 2005-09-25 16:15:17 +0200 | [diff] [blame] | 450 | |
| 451 | tmp = *p; |
| 452 | *p = *q; |
| 453 | *q = tmp; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return (char *) s; |
| 457 | } |
| 458 | #endif |
| 459 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 460 | #ifndef __HAVE_ARCH_MEMSET |
| 461 | /** |
| 462 | * memset - Fill a region of memory with the given value |
| 463 | * @s: Pointer to the start of the area. |
| 464 | * @c: The byte to fill the area with |
| 465 | * @count: The size of the area. |
| 466 | * |
| 467 | * Do not use memset() to access IO space, use memset_io() instead. |
| 468 | */ |
| 469 | void * memset(void * s,int c,size_t count) |
| 470 | { |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 471 | unsigned long *sl = (unsigned long *) s; |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 472 | char *s8; |
Simon Glass | ab4458b | 2017-04-02 09:50:28 -0600 | [diff] [blame] | 473 | |
| 474 | #if !CONFIG_IS_ENABLED(TINY_MEMSET) |
| 475 | unsigned long cl = 0; |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 476 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 477 | |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 478 | /* do it one word at a time (32 bits or 64 bits) while possible */ |
| 479 | if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) { |
| 480 | for (i = 0; i < sizeof(*sl); i++) { |
| 481 | cl <<= 8; |
| 482 | cl |= c & 0xff; |
| 483 | } |
| 484 | while (count >= sizeof(*sl)) { |
| 485 | *sl++ = cl; |
| 486 | count -= sizeof(*sl); |
| 487 | } |
| 488 | } |
Simon Glass | ab4458b | 2017-04-02 09:50:28 -0600 | [diff] [blame] | 489 | #endif /* fill 8 bits at a time */ |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 490 | s8 = (char *)sl; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 491 | while (count--) |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 492 | *s8++ = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 493 | |
| 494 | return s; |
| 495 | } |
| 496 | #endif |
| 497 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 498 | #ifndef __HAVE_ARCH_MEMCPY |
| 499 | /** |
| 500 | * memcpy - Copy one area of memory to another |
| 501 | * @dest: Where to copy to |
| 502 | * @src: Where to copy from |
| 503 | * @count: The size of the area. |
| 504 | * |
| 505 | * You should not use this function to access IO space, use memcpy_toio() |
| 506 | * or memcpy_fromio() instead. |
| 507 | */ |
Alessandro Rubini | ecd830b | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 508 | void * memcpy(void *dest, const void *src, size_t count) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 509 | { |
Alessandro Rubini | ecd830b | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 510 | unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src; |
| 511 | char *d8, *s8; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 512 | |
Matthias Weisser | b038db8 | 2011-05-22 23:03:55 +0000 | [diff] [blame] | 513 | if (src == dest) |
| 514 | return dest; |
| 515 | |
Alessandro Rubini | ecd830b | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 516 | /* while all data is aligned (common case), copy a word at a time */ |
| 517 | if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) { |
| 518 | while (count >= sizeof(*dl)) { |
| 519 | *dl++ = *sl++; |
| 520 | count -= sizeof(*dl); |
| 521 | } |
| 522 | } |
| 523 | /* copy the reset one byte at a time */ |
| 524 | d8 = (char *)dl; |
| 525 | s8 = (char *)sl; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 526 | while (count--) |
Alessandro Rubini | ecd830b | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 527 | *d8++ = *s8++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 528 | |
| 529 | return dest; |
| 530 | } |
| 531 | #endif |
| 532 | |
| 533 | #ifndef __HAVE_ARCH_MEMMOVE |
| 534 | /** |
| 535 | * memmove - Copy one area of memory to another |
| 536 | * @dest: Where to copy to |
| 537 | * @src: Where to copy from |
| 538 | * @count: The size of the area. |
| 539 | * |
| 540 | * Unlike memcpy(), memmove() copes with overlapping areas. |
| 541 | */ |
| 542 | void * memmove(void * dest,const void *src,size_t count) |
| 543 | { |
| 544 | char *tmp, *s; |
| 545 | |
| 546 | if (dest <= src) { |
Simon Glass | cb0eae8 | 2017-04-05 16:23:31 -0600 | [diff] [blame] | 547 | memcpy(dest, src, count); |
| 548 | } else { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 549 | tmp = (char *) dest + count; |
| 550 | s = (char *) src + count; |
| 551 | while (count--) |
| 552 | *--tmp = *--s; |
| 553 | } |
| 554 | |
| 555 | return dest; |
| 556 | } |
| 557 | #endif |
| 558 | |
| 559 | #ifndef __HAVE_ARCH_MEMCMP |
| 560 | /** |
| 561 | * memcmp - Compare two areas of memory |
| 562 | * @cs: One area of memory |
| 563 | * @ct: Another area of memory |
| 564 | * @count: The size of the area. |
| 565 | */ |
| 566 | int memcmp(const void * cs,const void * ct,size_t count) |
| 567 | { |
| 568 | const unsigned char *su1, *su2; |
| 569 | int res = 0; |
| 570 | |
| 571 | for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
| 572 | if ((res = *su1 - *su2) != 0) |
| 573 | break; |
| 574 | return res; |
| 575 | } |
| 576 | #endif |
| 577 | |
| 578 | #ifndef __HAVE_ARCH_MEMSCAN |
| 579 | /** |
| 580 | * memscan - Find a character in an area of memory. |
| 581 | * @addr: The memory area |
| 582 | * @c: The byte to search for |
| 583 | * @size: The size of the area. |
| 584 | * |
| 585 | * returns the address of the first occurrence of @c, or 1 byte past |
| 586 | * the area if @c is not found |
| 587 | */ |
| 588 | void * memscan(void * addr, int c, size_t size) |
| 589 | { |
| 590 | unsigned char * p = (unsigned char *) addr; |
| 591 | |
| 592 | while (size) { |
| 593 | if (*p == c) |
| 594 | return (void *) p; |
| 595 | p++; |
| 596 | size--; |
| 597 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 598 | return (void *) p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 599 | } |
| 600 | #endif |
| 601 | |
| 602 | #ifndef __HAVE_ARCH_STRSTR |
| 603 | /** |
| 604 | * strstr - Find the first substring in a %NUL terminated string |
| 605 | * @s1: The string to be searched |
| 606 | * @s2: The string to search for |
| 607 | */ |
| 608 | char * strstr(const char * s1,const char * s2) |
| 609 | { |
| 610 | int l1, l2; |
| 611 | |
| 612 | l2 = strlen(s2); |
| 613 | if (!l2) |
| 614 | return (char *) s1; |
| 615 | l1 = strlen(s1); |
| 616 | while (l1 >= l2) { |
| 617 | l1--; |
| 618 | if (!memcmp(s1,s2,l2)) |
| 619 | return (char *) s1; |
| 620 | s1++; |
| 621 | } |
| 622 | return NULL; |
| 623 | } |
| 624 | #endif |
| 625 | |
| 626 | #ifndef __HAVE_ARCH_MEMCHR |
| 627 | /** |
| 628 | * memchr - Find a character in an area of memory. |
| 629 | * @s: The memory area |
| 630 | * @c: The byte to search for |
| 631 | * @n: The size of the area. |
| 632 | * |
| 633 | * returns the address of the first occurrence of @c, or %NULL |
| 634 | * if @c is not found |
| 635 | */ |
| 636 | void *memchr(const void *s, int c, size_t n) |
| 637 | { |
| 638 | const unsigned char *p = s; |
| 639 | while (n-- != 0) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 640 | if ((unsigned char)c == *p++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 641 | return (void *)(p-1); |
| 642 | } |
| 643 | } |
| 644 | return NULL; |
| 645 | } |
| 646 | |
| 647 | #endif |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 648 | #ifndef __HAVE_ARCH_MEMCHR_INV |
| 649 | static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) |
| 650 | { |
| 651 | while (bytes) { |
| 652 | if (*start != value) |
| 653 | return (void *)start; |
| 654 | start++; |
| 655 | bytes--; |
| 656 | } |
| 657 | return NULL; |
| 658 | } |
| 659 | /** |
| 660 | * memchr_inv - Find an unmatching character in an area of memory. |
| 661 | * @start: The memory area |
| 662 | * @c: Find a character other than c |
| 663 | * @bytes: The size of the area. |
| 664 | * |
| 665 | * returns the address of the first character other than @c, or %NULL |
| 666 | * if the whole buffer contains just @c. |
| 667 | */ |
| 668 | void *memchr_inv(const void *start, int c, size_t bytes) |
| 669 | { |
| 670 | u8 value = c; |
| 671 | u64 value64; |
| 672 | unsigned int words, prefix; |
| 673 | |
| 674 | if (bytes <= 16) |
| 675 | return check_bytes8(start, value, bytes); |
| 676 | |
| 677 | value64 = value; |
| 678 | value64 |= value64 << 8; |
| 679 | value64 |= value64 << 16; |
| 680 | value64 |= value64 << 32; |
| 681 | |
| 682 | prefix = (unsigned long)start % 8; |
| 683 | if (prefix) { |
| 684 | u8 *r; |
| 685 | |
| 686 | prefix = 8 - prefix; |
| 687 | r = check_bytes8(start, value, prefix); |
| 688 | if (r) |
| 689 | return r; |
| 690 | start += prefix; |
| 691 | bytes -= prefix; |
| 692 | } |
| 693 | |
| 694 | words = bytes / 8; |
| 695 | |
| 696 | while (words) { |
| 697 | if (*(u64 *)start != value64) |
| 698 | return check_bytes8(start, value, 8); |
| 699 | start += 8; |
| 700 | words--; |
| 701 | } |
| 702 | |
| 703 | return check_bytes8(start, value, bytes % 8); |
| 704 | } |
| 705 | #endif |