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 | |
Md Sadre Alam | 9cdab51 | 2018-04-26 16:09:45 +0530 | [diff] [blame] | 178 | #ifndef __HAVE_ARCH_STRLCAT |
| 179 | /** |
| 180 | * strlcat - Append a length-limited, %NUL-terminated string to another |
| 181 | * @dest: The string to be appended to |
| 182 | * @src: The string to append to it |
| 183 | * @count: The size of the destination buffer. |
| 184 | * |
| 185 | */ |
| 186 | size_t strlcat(char *dest, const char *src, size_t count) |
| 187 | { |
| 188 | size_t dsize = strlen(dest); |
| 189 | size_t len = strlen(src); |
| 190 | size_t res = dsize + len; |
| 191 | |
| 192 | /* This would be a bug */ |
| 193 | BUG_ON(dsize >= count); |
| 194 | |
| 195 | dest += dsize; |
| 196 | count -= dsize; |
| 197 | if (len >= count) |
| 198 | len = count-1; |
| 199 | memcpy(dest, src, len); |
| 200 | dest[len] = 0; |
| 201 | return res; |
| 202 | } |
Md Sadre Alam | 9cdab51 | 2018-04-26 16:09:45 +0530 | [diff] [blame] | 203 | #endif |
| 204 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 205 | #ifndef __HAVE_ARCH_STRCMP |
| 206 | /** |
| 207 | * strcmp - Compare two strings |
| 208 | * @cs: One string |
| 209 | * @ct: Another string |
| 210 | */ |
| 211 | int strcmp(const char * cs,const char * ct) |
| 212 | { |
| 213 | register signed char __res; |
| 214 | |
| 215 | while (1) { |
| 216 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | return __res; |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | #ifndef __HAVE_ARCH_STRNCMP |
| 225 | /** |
| 226 | * strncmp - Compare two length-limited strings |
| 227 | * @cs: One string |
| 228 | * @ct: Another string |
| 229 | * @count: The maximum number of bytes to compare |
| 230 | */ |
| 231 | int strncmp(const char * cs,const char * ct,size_t count) |
| 232 | { |
| 233 | register signed char __res = 0; |
| 234 | |
| 235 | while (count) { |
| 236 | if ((__res = *cs - *ct++) != 0 || !*cs++) |
| 237 | break; |
| 238 | count--; |
| 239 | } |
| 240 | |
| 241 | return __res; |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | #ifndef __HAVE_ARCH_STRCHR |
| 246 | /** |
| 247 | * strchr - Find the first occurrence of a character in a string |
| 248 | * @s: The string to be searched |
| 249 | * @c: The character to search for |
| 250 | */ |
| 251 | char * strchr(const char * s, int c) |
| 252 | { |
| 253 | for(; *s != (char) c; ++s) |
| 254 | if (*s == '\0') |
| 255 | return NULL; |
| 256 | return (char *) s; |
| 257 | } |
| 258 | #endif |
| 259 | |
| 260 | #ifndef __HAVE_ARCH_STRRCHR |
| 261 | /** |
| 262 | * strrchr - Find the last occurrence of a character in a string |
| 263 | * @s: The string to be searched |
| 264 | * @c: The character to search for |
| 265 | */ |
| 266 | char * strrchr(const char * s, int c) |
| 267 | { |
| 268 | const char *p = s + strlen(s); |
| 269 | do { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 270 | if (*p == (char)c) |
| 271 | return (char *)p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 272 | } while (--p >= s); |
| 273 | return NULL; |
| 274 | } |
| 275 | #endif |
| 276 | |
| 277 | #ifndef __HAVE_ARCH_STRLEN |
| 278 | /** |
| 279 | * strlen - Find the length of a string |
| 280 | * @s: The string to be sized |
| 281 | */ |
| 282 | size_t strlen(const char * s) |
| 283 | { |
| 284 | const char *sc; |
| 285 | |
| 286 | for (sc = s; *sc != '\0'; ++sc) |
| 287 | /* nothing */; |
| 288 | return sc - s; |
| 289 | } |
| 290 | #endif |
| 291 | |
| 292 | #ifndef __HAVE_ARCH_STRNLEN |
| 293 | /** |
| 294 | * strnlen - Find the length of a length-limited string |
| 295 | * @s: The string to be sized |
| 296 | * @count: The maximum number of bytes to search |
| 297 | */ |
| 298 | size_t strnlen(const char * s, size_t count) |
| 299 | { |
| 300 | const char *sc; |
| 301 | |
| 302 | for (sc = s; count-- && *sc != '\0'; ++sc) |
| 303 | /* nothing */; |
| 304 | return sc - s; |
| 305 | } |
| 306 | #endif |
| 307 | |
| 308 | #ifndef __HAVE_ARCH_STRDUP |
| 309 | char * strdup(const char *s) |
| 310 | { |
| 311 | char *new; |
| 312 | |
| 313 | if ((s == NULL) || |
| 314 | ((new = malloc (strlen(s) + 1)) == NULL) ) { |
| 315 | return NULL; |
| 316 | } |
| 317 | |
| 318 | strcpy (new, s); |
| 319 | return new; |
| 320 | } |
| 321 | #endif |
| 322 | |
| 323 | #ifndef __HAVE_ARCH_STRSPN |
| 324 | /** |
| 325 | * strspn - Calculate the length of the initial substring of @s which only |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 326 | * contain letters in @accept |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 327 | * @s: The string to be searched |
| 328 | * @accept: The string to search for |
| 329 | */ |
| 330 | size_t strspn(const char *s, const char *accept) |
| 331 | { |
| 332 | const char *p; |
| 333 | const char *a; |
| 334 | size_t count = 0; |
| 335 | |
| 336 | for (p = s; *p != '\0'; ++p) { |
| 337 | for (a = accept; *a != '\0'; ++a) { |
| 338 | if (*p == *a) |
| 339 | break; |
| 340 | } |
| 341 | if (*a == '\0') |
| 342 | return count; |
| 343 | ++count; |
| 344 | } |
| 345 | |
| 346 | return count; |
| 347 | } |
| 348 | #endif |
| 349 | |
| 350 | #ifndef __HAVE_ARCH_STRPBRK |
| 351 | /** |
| 352 | * strpbrk - Find the first occurrence of a set of characters |
| 353 | * @cs: The string to be searched |
| 354 | * @ct: The characters to search for |
| 355 | */ |
| 356 | char * strpbrk(const char * cs,const char * ct) |
| 357 | { |
| 358 | const char *sc1,*sc2; |
| 359 | |
| 360 | for( sc1 = cs; *sc1 != '\0'; ++sc1) { |
| 361 | for( sc2 = ct; *sc2 != '\0'; ++sc2) { |
| 362 | if (*sc1 == *sc2) |
| 363 | return (char *) sc1; |
| 364 | } |
| 365 | } |
| 366 | return NULL; |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | #ifndef __HAVE_ARCH_STRTOK |
| 371 | /** |
| 372 | * strtok - Split a string into tokens |
| 373 | * @s: The string to be searched |
| 374 | * @ct: The characters to search for |
| 375 | * |
| 376 | * WARNING: strtok is deprecated, use strsep instead. |
| 377 | */ |
| 378 | char * strtok(char * s,const char * ct) |
| 379 | { |
| 380 | char *sbegin, *send; |
| 381 | |
| 382 | sbegin = s ? s : ___strtok; |
| 383 | if (!sbegin) { |
| 384 | return NULL; |
| 385 | } |
| 386 | sbegin += strspn(sbegin,ct); |
| 387 | if (*sbegin == '\0') { |
| 388 | ___strtok = NULL; |
| 389 | return( NULL ); |
| 390 | } |
| 391 | send = strpbrk( sbegin, ct); |
| 392 | if (send && *send != '\0') |
| 393 | *send++ = '\0'; |
| 394 | ___strtok = send; |
| 395 | return (sbegin); |
| 396 | } |
| 397 | #endif |
| 398 | |
| 399 | #ifndef __HAVE_ARCH_STRSEP |
| 400 | /** |
| 401 | * strsep - Split a string into tokens |
| 402 | * @s: The string to be searched |
| 403 | * @ct: The characters to search for |
| 404 | * |
| 405 | * strsep() updates @s to point after the token, ready for the next call. |
| 406 | * |
| 407 | * It returns empty tokens, too, behaving exactly like the libc function |
| 408 | * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. |
| 409 | * Same semantics, slimmer shape. ;) |
| 410 | */ |
| 411 | char * strsep(char **s, const char *ct) |
| 412 | { |
| 413 | char *sbegin = *s, *end; |
| 414 | |
| 415 | if (sbegin == NULL) |
| 416 | return NULL; |
| 417 | |
| 418 | end = strpbrk(sbegin, ct); |
| 419 | if (end) |
| 420 | *end++ = '\0'; |
| 421 | *s = end; |
| 422 | |
| 423 | return sbegin; |
| 424 | } |
| 425 | #endif |
| 426 | |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 427 | #ifndef __HAVE_ARCH_STRSWAB |
| 428 | /** |
| 429 | * strswab - swap adjacent even and odd bytes in %NUL-terminated string |
| 430 | * s: address of the string |
| 431 | * |
| 432 | * returns the address of the swapped string or NULL on error. If |
| 433 | * string length is odd, last byte is untouched. |
| 434 | */ |
| 435 | char *strswab(const char *s) |
| 436 | { |
Wolfgang Denk | 389db1f | 2005-09-25 16:15:17 +0200 | [diff] [blame] | 437 | char *p, *q; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 438 | |
| 439 | if ((NULL == s) || ('\0' == *s)) { |
| 440 | return (NULL); |
| 441 | } |
| 442 | |
Wolfgang Denk | e5e98ed | 2005-10-04 23:38:07 +0200 | [diff] [blame] | 443 | 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] | 444 | char tmp; |
Wolfgang Denk | 389db1f | 2005-09-25 16:15:17 +0200 | [diff] [blame] | 445 | |
| 446 | tmp = *p; |
| 447 | *p = *q; |
| 448 | *q = tmp; |
wdenk | c3f9d49 | 2004-03-14 00:59:59 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | return (char *) s; |
| 452 | } |
| 453 | #endif |
| 454 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 455 | #ifndef __HAVE_ARCH_MEMSET |
| 456 | /** |
| 457 | * memset - Fill a region of memory with the given value |
| 458 | * @s: Pointer to the start of the area. |
| 459 | * @c: The byte to fill the area with |
| 460 | * @count: The size of the area. |
| 461 | * |
| 462 | * Do not use memset() to access IO space, use memset_io() instead. |
| 463 | */ |
| 464 | void * memset(void * s,int c,size_t count) |
| 465 | { |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 466 | unsigned long *sl = (unsigned long *) s; |
| 467 | unsigned long cl = 0; |
| 468 | char *s8; |
| 469 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 470 | |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 471 | /* do it one word at a time (32 bits or 64 bits) while possible */ |
| 472 | if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) { |
| 473 | for (i = 0; i < sizeof(*sl); i++) { |
| 474 | cl <<= 8; |
| 475 | cl |= c & 0xff; |
| 476 | } |
| 477 | while (count >= sizeof(*sl)) { |
| 478 | *sl++ = cl; |
| 479 | count -= sizeof(*sl); |
| 480 | } |
| 481 | } |
| 482 | /* fill 8 bits at a time */ |
| 483 | s8 = (char *)sl; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 484 | while (count--) |
Alessandro Rubini | e3ea948 | 2009-10-10 11:51:16 +0200 | [diff] [blame] | 485 | *s8++ = c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 486 | |
| 487 | return s; |
| 488 | } |
| 489 | #endif |
| 490 | |
| 491 | #ifndef __HAVE_ARCH_BCOPY |
| 492 | /** |
| 493 | * bcopy - Copy one area of memory to another |
| 494 | * @src: Where to copy from |
| 495 | * @dest: Where to copy to |
| 496 | * @count: The size of the area. |
| 497 | * |
| 498 | * Note that this is the same as memcpy(), with the arguments reversed. |
| 499 | * memcpy() is the standard, bcopy() is a legacy BSD function. |
| 500 | * |
| 501 | * You should not use this function to access IO space, use memcpy_toio() |
| 502 | * or memcpy_fromio() instead. |
| 503 | */ |
| 504 | char * bcopy(const char * src, char * dest, int count) |
| 505 | { |
| 506 | char *tmp = dest; |
| 507 | |
| 508 | while (count--) |
| 509 | *tmp++ = *src++; |
| 510 | |
| 511 | return dest; |
| 512 | } |
| 513 | #endif |
| 514 | |
| 515 | #ifndef __HAVE_ARCH_MEMCPY |
| 516 | /** |
| 517 | * memcpy - Copy one area of memory to another |
| 518 | * @dest: Where to copy to |
| 519 | * @src: Where to copy from |
| 520 | * @count: The size of the area. |
| 521 | * |
| 522 | * You should not use this function to access IO space, use memcpy_toio() |
| 523 | * or memcpy_fromio() instead. |
| 524 | */ |
Alessandro Rubini | ecd830b | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 525 | void * memcpy(void *dest, const void *src, size_t count) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 526 | { |
Alessandro Rubini | ecd830b | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 527 | unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src; |
| 528 | char *d8, *s8; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 529 | |
Matthias Weisser | b038db8 | 2011-05-22 23:03:55 +0000 | [diff] [blame] | 530 | if (src == dest) |
| 531 | return dest; |
| 532 | |
Alessandro Rubini | ecd830b | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 533 | /* while all data is aligned (common case), copy a word at a time */ |
| 534 | if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) { |
| 535 | while (count >= sizeof(*dl)) { |
| 536 | *dl++ = *sl++; |
| 537 | count -= sizeof(*dl); |
| 538 | } |
| 539 | } |
| 540 | /* copy the reset one byte at a time */ |
| 541 | d8 = (char *)dl; |
| 542 | s8 = (char *)sl; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 543 | while (count--) |
Alessandro Rubini | ecd830b | 2009-10-10 11:51:05 +0200 | [diff] [blame] | 544 | *d8++ = *s8++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 545 | |
| 546 | return dest; |
| 547 | } |
| 548 | #endif |
| 549 | |
Md Sadre Alam | 94028ba | 2018-04-26 16:51:43 +0530 | [diff] [blame] | 550 | #ifndef __HAVE_ARCH_MEMSCPY |
| 551 | /** |
| 552 | * memscpy - Copy one area of memory to another |
| 553 | * @dest: Where to copy to |
| 554 | * @src: Where to copy from |
| 555 | * @dst_size: The size of destination buffer area |
| 556 | * @copy_size: The size of source buffer area |
| 557 | * |
| 558 | * You should not use this function where memcpy |
| 559 | * was used to copy null terminated buffer, the |
| 560 | * replacement function is strlcpy() and strlcat() |
| 561 | * depending on situation. |
| 562 | * |
| 563 | * The aim of memscpy() is to prevent buffer overflow |
| 564 | * by taking both destination buffer size and source |
| 565 | * buffer size. |
| 566 | */ |
| 567 | size_t memscpy(void *dest, size_t dst_size, const void *src, size_t copy_size) |
| 568 | { |
Md Sadre Alam | 9cdab51 | 2018-04-26 16:09:45 +0530 | [diff] [blame] | 569 | size_t min_size = dst_size < copy_size ? dst_size : copy_size; |
| 570 | memcpy(dest, src, min_size); |
| 571 | return min_size; |
| 572 | } |
Md Sadre Alam | 94028ba | 2018-04-26 16:51:43 +0530 | [diff] [blame] | 573 | #endif |
Md Sadre Alam | 9cdab51 | 2018-04-26 16:09:45 +0530 | [diff] [blame] | 574 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 575 | #ifndef __HAVE_ARCH_MEMMOVE |
| 576 | /** |
| 577 | * memmove - Copy one area of memory to another |
| 578 | * @dest: Where to copy to |
| 579 | * @src: Where to copy from |
| 580 | * @count: The size of the area. |
| 581 | * |
| 582 | * Unlike memcpy(), memmove() copes with overlapping areas. |
| 583 | */ |
| 584 | void * memmove(void * dest,const void *src,size_t count) |
| 585 | { |
| 586 | char *tmp, *s; |
| 587 | |
Matthias Weisser | b038db8 | 2011-05-22 23:03:55 +0000 | [diff] [blame] | 588 | if (src == dest) |
| 589 | return dest; |
| 590 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 591 | if (dest <= src) { |
| 592 | tmp = (char *) dest; |
| 593 | s = (char *) src; |
| 594 | while (count--) |
| 595 | *tmp++ = *s++; |
| 596 | } |
| 597 | else { |
| 598 | tmp = (char *) dest + count; |
| 599 | s = (char *) src + count; |
| 600 | while (count--) |
| 601 | *--tmp = *--s; |
| 602 | } |
| 603 | |
| 604 | return dest; |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | #ifndef __HAVE_ARCH_MEMCMP |
| 609 | /** |
| 610 | * memcmp - Compare two areas of memory |
| 611 | * @cs: One area of memory |
| 612 | * @ct: Another area of memory |
| 613 | * @count: The size of the area. |
| 614 | */ |
| 615 | int memcmp(const void * cs,const void * ct,size_t count) |
| 616 | { |
| 617 | const unsigned char *su1, *su2; |
| 618 | int res = 0; |
| 619 | |
| 620 | for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) |
| 621 | if ((res = *su1 - *su2) != 0) |
| 622 | break; |
| 623 | return res; |
| 624 | } |
| 625 | #endif |
| 626 | |
| 627 | #ifndef __HAVE_ARCH_MEMSCAN |
| 628 | /** |
| 629 | * memscan - Find a character in an area of memory. |
| 630 | * @addr: The memory area |
| 631 | * @c: The byte to search for |
| 632 | * @size: The size of the area. |
| 633 | * |
| 634 | * returns the address of the first occurrence of @c, or 1 byte past |
| 635 | * the area if @c is not found |
| 636 | */ |
| 637 | void * memscan(void * addr, int c, size_t size) |
| 638 | { |
| 639 | unsigned char * p = (unsigned char *) addr; |
| 640 | |
| 641 | while (size) { |
| 642 | if (*p == c) |
| 643 | return (void *) p; |
| 644 | p++; |
| 645 | size--; |
| 646 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 647 | return (void *) p; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 648 | } |
| 649 | #endif |
| 650 | |
| 651 | #ifndef __HAVE_ARCH_STRSTR |
| 652 | /** |
| 653 | * strstr - Find the first substring in a %NUL terminated string |
| 654 | * @s1: The string to be searched |
| 655 | * @s2: The string to search for |
| 656 | */ |
| 657 | char * strstr(const char * s1,const char * s2) |
| 658 | { |
| 659 | int l1, l2; |
| 660 | |
| 661 | l2 = strlen(s2); |
| 662 | if (!l2) |
| 663 | return (char *) s1; |
| 664 | l1 = strlen(s1); |
| 665 | while (l1 >= l2) { |
| 666 | l1--; |
| 667 | if (!memcmp(s1,s2,l2)) |
| 668 | return (char *) s1; |
| 669 | s1++; |
| 670 | } |
| 671 | return NULL; |
| 672 | } |
| 673 | #endif |
| 674 | |
| 675 | #ifndef __HAVE_ARCH_MEMCHR |
| 676 | /** |
| 677 | * memchr - Find a character in an area of memory. |
| 678 | * @s: The memory area |
| 679 | * @c: The byte to search for |
| 680 | * @n: The size of the area. |
| 681 | * |
| 682 | * returns the address of the first occurrence of @c, or %NULL |
| 683 | * if @c is not found |
| 684 | */ |
| 685 | void *memchr(const void *s, int c, size_t n) |
| 686 | { |
| 687 | const unsigned char *p = s; |
| 688 | while (n-- != 0) { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 689 | if ((unsigned char)c == *p++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 690 | return (void *)(p-1); |
| 691 | } |
| 692 | } |
| 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | #endif |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 697 | #ifndef __HAVE_ARCH_MEMCHR_INV |
| 698 | static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes) |
| 699 | { |
| 700 | while (bytes) { |
| 701 | if (*start != value) |
| 702 | return (void *)start; |
| 703 | start++; |
| 704 | bytes--; |
| 705 | } |
| 706 | return NULL; |
| 707 | } |
| 708 | /** |
| 709 | * memchr_inv - Find an unmatching character in an area of memory. |
| 710 | * @start: The memory area |
| 711 | * @c: Find a character other than c |
| 712 | * @bytes: The size of the area. |
| 713 | * |
| 714 | * returns the address of the first character other than @c, or %NULL |
| 715 | * if the whole buffer contains just @c. |
| 716 | */ |
| 717 | void *memchr_inv(const void *start, int c, size_t bytes) |
| 718 | { |
| 719 | u8 value = c; |
| 720 | u64 value64; |
| 721 | unsigned int words, prefix; |
| 722 | |
| 723 | if (bytes <= 16) |
| 724 | return check_bytes8(start, value, bytes); |
| 725 | |
| 726 | value64 = value; |
| 727 | value64 |= value64 << 8; |
| 728 | value64 |= value64 << 16; |
| 729 | value64 |= value64 << 32; |
| 730 | |
| 731 | prefix = (unsigned long)start % 8; |
| 732 | if (prefix) { |
| 733 | u8 *r; |
| 734 | |
| 735 | prefix = 8 - prefix; |
| 736 | r = check_bytes8(start, value, prefix); |
| 737 | if (r) |
| 738 | return r; |
| 739 | start += prefix; |
| 740 | bytes -= prefix; |
| 741 | } |
| 742 | |
| 743 | words = bytes / 8; |
| 744 | |
| 745 | while (words) { |
| 746 | if (*(u64 *)start != value64) |
| 747 | return check_bytes8(start, value, 8); |
| 748 | start += 8; |
| 749 | words--; |
| 750 | } |
| 751 | |
| 752 | return check_bytes8(start, value, bytes % 8); |
| 753 | } |
| 754 | #endif |