blob: 9f4a98cd47e1c65082508263044a887fdf962178 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
18 * Matthew Hawkins <matt@mh.dropbear.id.au>
19 * - Kissed strtok() goodbye
20 */
21
22#include <linux/types.h>
23#include <linux/string.h>
24#include <linux/ctype.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050025#include <linux/kernel.h>
26#include <linux/export.h>
Paul Gortmaker50af5ea2012-01-20 18:35:53 -050027#include <linux/bug.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050028#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Chris Metcalf30035e42015-04-29 12:52:04 -040030#include <asm/byteorder.h>
31#include <asm/word-at-a-time.h>
32#include <asm/page.h>
33
Rasmus Villemoescd514e72014-10-13 15:54:25 -070034#ifndef __HAVE_ARCH_STRNCASECMP
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/**
Rasmus Villemoescd514e72014-10-13 15:54:25 -070036 * strncasecmp - Case insensitive, length-limited string comparison
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * @s1: One string
38 * @s2: The other string
39 * @len: the maximum number of characters to compare
40 */
Rasmus Villemoescd514e72014-10-13 15:54:25 -070041int strncasecmp(const char *s1, const char *s2, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 /* Yes, Virginia, it had better be unsigned */
44 unsigned char c1, c2;
45
André Goddard Rosaa11d2b62010-03-05 13:43:11 -080046 if (!len)
47 return 0;
48
49 do {
50 c1 = *s1++;
51 c2 = *s2++;
52 if (!c1 || !c2)
53 break;
54 if (c1 == c2)
55 continue;
56 c1 = tolower(c1);
57 c2 = tolower(c2);
58 if (c1 != c2)
59 break;
60 } while (--len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return (int)c1 - (int)c2;
62}
Rasmus Villemoescd514e72014-10-13 15:54:25 -070063EXPORT_SYMBOL(strncasecmp);
64#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
David S. Millerded220b2007-03-29 01:18:42 -070066#ifndef __HAVE_ARCH_STRCASECMP
67int strcasecmp(const char *s1, const char *s2)
68{
69 int c1, c2;
70
71 do {
72 c1 = tolower(*s1++);
73 c2 = tolower(*s2++);
74 } while (c1 == c2 && c1 != 0);
75 return c1 - c2;
76}
77EXPORT_SYMBOL(strcasecmp);
78#endif
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#ifndef __HAVE_ARCH_STRCPY
81/**
82 * strcpy - Copy a %NUL terminated string
83 * @dest: Where to copy the string to
84 * @src: Where to copy the string from
85 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -070086#undef strcpy
Jesper Juhl51a0f0f2005-10-30 15:02:11 -080087char *strcpy(char *dest, const char *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 char *tmp = dest;
90
91 while ((*dest++ = *src++) != '\0')
92 /* nothing */;
93 return tmp;
94}
95EXPORT_SYMBOL(strcpy);
96#endif
97
98#ifndef __HAVE_ARCH_STRNCPY
99/**
Dan Carpenter0046dd92014-06-04 16:11:47 -0700100 * strncpy - Copy a length-limited, C-string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * @dest: Where to copy the string to
102 * @src: Where to copy the string from
103 * @count: The maximum number of bytes to copy
104 *
105 * The result is not %NUL-terminated if the source exceeds
106 * @count bytes.
walter harms25279522005-05-05 16:16:20 -0700107 *
108 * In the case where the length of @src is less than that of
109 * count, the remainder of @dest will be padded with %NUL.
110 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800112char *strncpy(char *dest, const char *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 char *tmp = dest;
115
116 while (count) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800117 if ((*tmp = *src) != 0)
118 src++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 tmp++;
120 count--;
121 }
122 return dest;
123}
124EXPORT_SYMBOL(strncpy);
125#endif
126
127#ifndef __HAVE_ARCH_STRLCPY
128/**
Dan Carpenter0046dd92014-06-04 16:11:47 -0700129 * strlcpy - Copy a C-string into a sized buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * @dest: Where to copy the string to
131 * @src: Where to copy the string from
132 * @size: size of destination buffer
133 *
134 * Compatible with *BSD: the result is always a valid
135 * NUL-terminated string that fits in the buffer (unless,
136 * of course, the buffer size is zero). It does not pad
137 * out the result like strncpy() does.
138 */
139size_t strlcpy(char *dest, const char *src, size_t size)
140{
141 size_t ret = strlen(src);
142
143 if (size) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800144 size_t len = (ret >= size) ? size - 1 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 memcpy(dest, src, len);
146 dest[len] = '\0';
147 }
148 return ret;
149}
150EXPORT_SYMBOL(strlcpy);
151#endif
152
Chris Metcalf30035e42015-04-29 12:52:04 -0400153#ifndef __HAVE_ARCH_STRSCPY
154/**
155 * strscpy - Copy a C-string into a sized buffer
156 * @dest: Where to copy the string to
157 * @src: Where to copy the string from
158 * @count: Size of destination buffer
159 *
Tobin C. Harding316c6cc2019-04-05 12:58:58 +1100160 * Copy the string, or as much of it as fits, into the dest buffer. The
161 * behavior is undefined if the string buffers overlap. The destination
162 * buffer is always NUL terminated, unless it's zero-sized.
Chris Metcalf30035e42015-04-29 12:52:04 -0400163 *
164 * Preferred to strlcpy() since the API doesn't require reading memory
165 * from the src string beyond the specified "count" bytes, and since
166 * the return value is easier to error-check than strlcpy()'s.
167 * In addition, the implementation is robust to the string changing out
168 * from underneath it, unlike the current strlcpy() implementation.
169 *
170 * Preferred to strncpy() since it always returns a valid string, and
171 * doesn't unnecessarily force the tail of the destination buffer to be
Tobin C. Harding316c6cc2019-04-05 12:58:58 +1100172 * zeroed. If zeroing is desired please use strscpy_pad().
173 *
174 * Return: The number of characters copied (not including the trailing
175 * %NUL) or -E2BIG if the destination buffer wasn't big enough.
Chris Metcalf30035e42015-04-29 12:52:04 -0400176 */
177ssize_t strscpy(char *dest, const char *src, size_t count)
178{
179 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
180 size_t max = count;
181 long res = 0;
182
183 if (count == 0)
184 return -E2BIG;
185
186#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
187 /*
188 * If src is unaligned, don't cross a page boundary,
189 * since we don't know if the next page is mapped.
190 */
191 if ((long)src & (sizeof(long) - 1)) {
192 size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
193 if (limit < max)
194 max = limit;
195 }
196#else
197 /* If src or dest is unaligned, don't do word-at-a-time. */
198 if (((long) dest | (long) src) & (sizeof(long) - 1))
199 max = 0;
200#endif
201
202 while (max >= sizeof(unsigned long)) {
203 unsigned long c, data;
204
Andrey Ryabinin8dd8b4d2018-02-01 21:00:50 +0300205 c = read_word_at_a_time(src+res);
Chris Metcalf30035e42015-04-29 12:52:04 -0400206 if (has_zero(c, &data, &constants)) {
207 data = prep_zero_mask(c, data, &constants);
208 data = create_zero_mask(data);
Chris Metcalf990486c2015-10-06 12:37:41 -0400209 *(unsigned long *)(dest+res) = c & zero_bytemask(data);
Chris Metcalf30035e42015-04-29 12:52:04 -0400210 return res + find_zero(data);
211 }
Chris Metcalf990486c2015-10-06 12:37:41 -0400212 *(unsigned long *)(dest+res) = c;
Chris Metcalf30035e42015-04-29 12:52:04 -0400213 res += sizeof(unsigned long);
214 count -= sizeof(unsigned long);
215 max -= sizeof(unsigned long);
216 }
217
218 while (count) {
219 char c;
220
221 c = src[res];
222 dest[res] = c;
223 if (!c)
224 return res;
225 res++;
226 count--;
227 }
228
229 /* Hit buffer length without finding a NUL; force NUL-termination. */
230 if (res)
231 dest[res-1] = '\0';
232
233 return -E2BIG;
234}
235EXPORT_SYMBOL(strscpy);
236#endif
237
Nick Desaulniers586d6f172020-09-25 21:19:18 -0700238/**
239 * stpcpy - copy a string from src to dest returning a pointer to the new end
240 * of dest, including src's %NUL-terminator. May overrun dest.
241 * @dest: pointer to end of string being copied into. Must be large enough
242 * to receive copy.
243 * @src: pointer to the beginning of string being copied from. Must not overlap
244 * dest.
245 *
246 * stpcpy differs from strcpy in a key way: the return value is a pointer
247 * to the new %NUL-terminating character in @dest. (For strcpy, the return
248 * value is a pointer to the start of @dest). This interface is considered
249 * unsafe as it doesn't perform bounds checking of the inputs. As such it's
250 * not recommended for usage. Instead, its definition is provided in case
251 * the compiler lowers other libcalls to stpcpy.
252 */
253char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
254char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
255{
256 while ((*dest++ = *src++) != '\0')
257 /* nothing */;
258 return --dest;
259}
260EXPORT_SYMBOL(stpcpy);
261
Tobin C. Harding316c6cc2019-04-05 12:58:58 +1100262/**
263 * strscpy_pad() - Copy a C-string into a sized buffer
264 * @dest: Where to copy the string to
265 * @src: Where to copy the string from
266 * @count: Size of destination buffer
267 *
268 * Copy the string, or as much of it as fits, into the dest buffer. The
269 * behavior is undefined if the string buffers overlap. The destination
270 * buffer is always %NUL terminated, unless it's zero-sized.
271 *
272 * If the source string is shorter than the destination buffer, zeros
273 * the tail of the destination buffer.
274 *
275 * For full explanation of why you may want to consider using the
276 * 'strscpy' functions please see the function docstring for strscpy().
277 *
278 * Return: The number of characters copied (not including the trailing
279 * %NUL) or -E2BIG if the destination buffer wasn't big enough.
280 */
281ssize_t strscpy_pad(char *dest, const char *src, size_t count)
282{
283 ssize_t written;
284
285 written = strscpy(dest, src, count);
286 if (written < 0 || written == count - 1)
287 return written;
288
289 memset(dest + written + 1, 0, count - written - 1);
290
291 return written;
292}
293EXPORT_SYMBOL(strscpy_pad);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#ifndef __HAVE_ARCH_STRCAT
296/**
297 * strcat - Append one %NUL-terminated string to another
298 * @dest: The string to be appended to
299 * @src: The string to append to it
300 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700301#undef strcat
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800302char *strcat(char *dest, const char *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 char *tmp = dest;
305
306 while (*dest)
307 dest++;
308 while ((*dest++ = *src++) != '\0')
309 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return tmp;
311}
312EXPORT_SYMBOL(strcat);
313#endif
314
315#ifndef __HAVE_ARCH_STRNCAT
316/**
Dan Carpenter0046dd92014-06-04 16:11:47 -0700317 * strncat - Append a length-limited, C-string to another
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * @dest: The string to be appended to
319 * @src: The string to append to it
320 * @count: The maximum numbers of bytes to copy
321 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800322 * Note that in contrast to strncpy(), strncat() ensures the result is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * terminated.
324 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800325char *strncat(char *dest, const char *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 char *tmp = dest;
328
329 if (count) {
330 while (*dest)
331 dest++;
332 while ((*dest++ = *src++) != 0) {
333 if (--count == 0) {
334 *dest = '\0';
335 break;
336 }
337 }
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return tmp;
340}
341EXPORT_SYMBOL(strncat);
342#endif
343
344#ifndef __HAVE_ARCH_STRLCAT
345/**
Dan Carpenter0046dd92014-06-04 16:11:47 -0700346 * strlcat - Append a length-limited, C-string to another
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * @dest: The string to be appended to
348 * @src: The string to append to it
349 * @count: The size of the destination buffer.
350 */
351size_t strlcat(char *dest, const char *src, size_t count)
352{
353 size_t dsize = strlen(dest);
354 size_t len = strlen(src);
355 size_t res = dsize + len;
356
357 /* This would be a bug */
358 BUG_ON(dsize >= count);
359
360 dest += dsize;
361 count -= dsize;
362 if (len >= count)
363 len = count-1;
364 memcpy(dest, src, len);
365 dest[len] = 0;
366 return res;
367}
368EXPORT_SYMBOL(strlcat);
369#endif
370
371#ifndef __HAVE_ARCH_STRCMP
372/**
373 * strcmp - Compare two strings
374 * @cs: One string
375 * @ct: Another string
376 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700377#undef strcmp
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800378int strcmp(const char *cs, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Linus Torvaldsa414f012009-11-18 22:31:52 +0100380 unsigned char c1, c2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 while (1) {
Linus Torvaldsa414f012009-11-18 22:31:52 +0100383 c1 = *cs++;
384 c2 = *ct++;
385 if (c1 != c2)
386 return c1 < c2 ? -1 : 1;
387 if (!c1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 break;
389 }
Linus Torvaldsa414f012009-11-18 22:31:52 +0100390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392EXPORT_SYMBOL(strcmp);
393#endif
394
395#ifndef __HAVE_ARCH_STRNCMP
396/**
397 * strncmp - Compare two length-limited strings
398 * @cs: One string
399 * @ct: Another string
400 * @count: The maximum number of bytes to compare
401 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800402int strncmp(const char *cs, const char *ct, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Linus Torvaldsa414f012009-11-18 22:31:52 +0100404 unsigned char c1, c2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 while (count) {
Linus Torvaldsa414f012009-11-18 22:31:52 +0100407 c1 = *cs++;
408 c2 = *ct++;
409 if (c1 != c2)
410 return c1 < c2 ? -1 : 1;
411 if (!c1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
413 count--;
414 }
Linus Torvaldsa414f012009-11-18 22:31:52 +0100415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417EXPORT_SYMBOL(strncmp);
418#endif
419
420#ifndef __HAVE_ARCH_STRCHR
421/**
422 * strchr - Find the first occurrence of a character in a string
423 * @s: The string to be searched
424 * @c: The character to search for
425 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800426char *strchr(const char *s, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800428 for (; *s != (char)c; ++s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (*s == '\0')
430 return NULL;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800431 return (char *)s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433EXPORT_SYMBOL(strchr);
434#endif
435
Grant Likely11d200e2014-03-14 17:00:14 +0000436#ifndef __HAVE_ARCH_STRCHRNUL
437/**
438 * strchrnul - Find and return a character in a string, or end of string
439 * @s: The string to be searched
440 * @c: The character to search for
441 *
442 * Returns pointer to first occurrence of 'c' in s. If c is not found, then
443 * return a pointer to the null byte at the end of s.
444 */
445char *strchrnul(const char *s, int c)
446{
447 while (*s && *s != (char)c)
448 s++;
449 return (char *)s;
450}
451EXPORT_SYMBOL(strchrnul);
452#endif
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#ifndef __HAVE_ARCH_STRRCHR
455/**
456 * strrchr - Find the last occurrence of a character in a string
457 * @s: The string to be searched
458 * @c: The character to search for
459 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800460char *strrchr(const char *s, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Rasmus Villemoes8da53d42015-02-13 14:36:44 -0800462 const char *last = NULL;
463 do {
464 if (*s == (char)c)
465 last = s;
466 } while (*s++);
467 return (char *)last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469EXPORT_SYMBOL(strrchr);
470#endif
471
472#ifndef __HAVE_ARCH_STRNCHR
473/**
474 * strnchr - Find a character in a length limited string
475 * @s: The string to be searched
476 * @count: The number of characters to be searched
477 * @c: The character to search for
478 */
479char *strnchr(const char *s, size_t count, int c)
480{
481 for (; count-- && *s != '\0'; ++s)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800482 if (*s == (char)c)
483 return (char *)s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return NULL;
485}
486EXPORT_SYMBOL(strnchr);
487#endif
488
Pekka Enberg481fad42006-06-23 02:05:44 -0700489/**
Randy Dunlapa6cd13f2009-12-21 14:37:22 -0800490 * skip_spaces - Removes leading whitespace from @str.
491 * @str: The string to be stripped.
André Goddard Rosaf6533982009-12-14 18:01:04 -0800492 *
Randy Dunlapa6cd13f2009-12-21 14:37:22 -0800493 * Returns a pointer to the first non-whitespace character in @str.
André Goddard Rosaf6533982009-12-14 18:01:04 -0800494 */
495char *skip_spaces(const char *str)
496{
497 while (isspace(*str))
498 ++str;
499 return (char *)str;
500}
501EXPORT_SYMBOL(skip_spaces);
502
503/**
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -0800504 * strim - Removes leading and trailing whitespace from @s.
Pekka Enberg481fad42006-06-23 02:05:44 -0700505 * @s: The string to be stripped.
506 *
507 * Note that the first trailing whitespace is replaced with a %NUL-terminator
508 * in the given string @s. Returns a pointer to the first non-whitespace
509 * character in @s.
510 */
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -0800511char *strim(char *s)
Pekka Enberg481fad42006-06-23 02:05:44 -0700512{
513 size_t size;
514 char *end;
515
516 size = strlen(s);
Pekka Enberg481fad42006-06-23 02:05:44 -0700517 if (!size)
518 return s;
519
520 end = s + size - 1;
Michael Holzheu6e6d9fa2006-10-28 10:38:47 -0700521 while (end >= s && isspace(*end))
Pekka Enberg481fad42006-06-23 02:05:44 -0700522 end--;
523 *(end + 1) = '\0';
524
Michael Holzheu66f69582011-10-31 17:12:37 -0700525 return skip_spaces(s);
Pekka Enberg481fad42006-06-23 02:05:44 -0700526}
KOSAKI Motohiroca54cb82009-12-14 18:01:15 -0800527EXPORT_SYMBOL(strim);
Pekka Enberg481fad42006-06-23 02:05:44 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#ifndef __HAVE_ARCH_STRLEN
530/**
531 * strlen - Find the length of a string
532 * @s: The string to be sized
533 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800534size_t strlen(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 const char *sc;
537
538 for (sc = s; *sc != '\0'; ++sc)
539 /* nothing */;
540 return sc - s;
541}
542EXPORT_SYMBOL(strlen);
543#endif
544
545#ifndef __HAVE_ARCH_STRNLEN
546/**
547 * strnlen - Find the length of a length-limited string
548 * @s: The string to be sized
549 * @count: The maximum number of bytes to search
550 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800551size_t strnlen(const char *s, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 const char *sc;
554
555 for (sc = s; count-- && *sc != '\0'; ++sc)
556 /* nothing */;
557 return sc - s;
558}
559EXPORT_SYMBOL(strnlen);
560#endif
561
562#ifndef __HAVE_ARCH_STRSPN
563/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800564 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * @s: The string to be searched
566 * @accept: The string to search for
567 */
568size_t strspn(const char *s, const char *accept)
569{
570 const char *p;
571 const char *a;
572 size_t count = 0;
573
574 for (p = s; *p != '\0'; ++p) {
575 for (a = accept; *a != '\0'; ++a) {
576 if (*p == *a)
577 break;
578 }
579 if (*a == '\0')
580 return count;
581 ++count;
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return count;
584}
585
586EXPORT_SYMBOL(strspn);
587#endif
588
Kyle McMartin8833d322006-04-10 22:53:57 -0700589#ifndef __HAVE_ARCH_STRCSPN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800591 * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * @s: The string to be searched
593 * @reject: The string to avoid
594 */
595size_t strcspn(const char *s, const char *reject)
596{
597 const char *p;
598 const char *r;
599 size_t count = 0;
600
601 for (p = s; *p != '\0'; ++p) {
602 for (r = reject; *r != '\0'; ++r) {
603 if (*p == *r)
604 return count;
605 }
606 ++count;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return count;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800609}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610EXPORT_SYMBOL(strcspn);
Kyle McMartin8833d322006-04-10 22:53:57 -0700611#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613#ifndef __HAVE_ARCH_STRPBRK
614/**
615 * strpbrk - Find the first occurrence of a set of characters
616 * @cs: The string to be searched
617 * @ct: The characters to search for
618 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800619char *strpbrk(const char *cs, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800621 const char *sc1, *sc2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800623 for (sc1 = cs; *sc1 != '\0'; ++sc1) {
624 for (sc2 = ct; *sc2 != '\0'; ++sc2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (*sc1 == *sc2)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800626 return (char *)sc1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 }
629 return NULL;
630}
Kyle McMartin894b5772006-04-10 22:53:56 -0700631EXPORT_SYMBOL(strpbrk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#endif
633
634#ifndef __HAVE_ARCH_STRSEP
635/**
636 * strsep - Split a string into tokens
637 * @s: The string to be searched
638 * @ct: The characters to search for
639 *
640 * strsep() updates @s to point after the token, ready for the next call.
641 *
642 * It returns empty tokens, too, behaving exactly like the libc function
643 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
644 * Same semantics, slimmer shape. ;)
645 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800646char *strsep(char **s, const char *ct)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800648 char *sbegin = *s;
649 char *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (sbegin == NULL)
652 return NULL;
653
654 end = strpbrk(sbegin, ct);
655 if (end)
656 *end++ = '\0';
657 *s = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return sbegin;
659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660EXPORT_SYMBOL(strsep);
661#endif
662
David Brownell34990cf2008-05-01 04:34:42 -0700663/**
664 * sysfs_streq - return true if strings are equal, modulo trailing newline
665 * @s1: one string
666 * @s2: another string
667 *
668 * This routine returns true iff two strings are equal, treating both
669 * NUL and newline-then-NUL as equivalent string terminations. It's
670 * geared for use with sysfs input strings, which generally terminate
671 * with newlines but are compared against values without newlines.
672 */
673bool sysfs_streq(const char *s1, const char *s2)
674{
675 while (*s1 && *s1 == *s2) {
676 s1++;
677 s2++;
678 }
679
680 if (*s1 == *s2)
681 return true;
682 if (!*s1 && *s2 == '\n' && !s2[1])
683 return true;
684 if (*s1 == '\n' && !s1[1] && !*s2)
685 return true;
686 return false;
687}
688EXPORT_SYMBOL(sysfs_streq);
689
Jonathan Camerond0f1fed2011-04-19 12:43:45 +0100690/**
Andy Shevchenko56b06082016-03-17 14:22:14 -0700691 * match_string - matches given string in an array
692 * @array: array of strings
693 * @n: number of strings in the array or -1 for NULL terminated arrays
694 * @string: string to match with
695 *
696 * Return:
697 * index of a @string in the @array if matches, or %-EINVAL otherwise.
698 */
699int match_string(const char * const *array, size_t n, const char *string)
700{
701 int index;
702 const char *item;
703
704 for (index = 0; index < n; index++) {
705 item = array[index];
706 if (!item)
707 break;
708 if (!strcmp(item, string))
709 return index;
710 }
711
712 return -EINVAL;
713}
714EXPORT_SYMBOL(match_string);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716#ifndef __HAVE_ARCH_MEMSET
717/**
718 * memset - Fill a region of memory with the given value
719 * @s: Pointer to the start of the area.
720 * @c: The byte to fill the area with
721 * @count: The size of the area.
722 *
723 * Do not use memset() to access IO space, use memset_io() instead.
724 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800725void *memset(void *s, int c, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Jesper Juhl850b9242005-10-30 15:02:13 -0800727 char *xs = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 while (count--)
730 *xs++ = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return s;
732}
733EXPORT_SYMBOL(memset);
734#endif
735
Daniel Borkmannd4c5efd2014-08-26 23:16:35 -0400736/**
737 * memzero_explicit - Fill a region of memory (e.g. sensitive
738 * keying data) with 0s.
739 * @s: Pointer to the start of the area.
740 * @count: The size of the area.
741 *
Daniel Borkmann81553302015-01-06 00:27:45 +0100742 * Note: usually using memset() is just fine (!), but in cases
743 * where clearing out _local_ data at the end of a scope is
744 * necessary, memzero_explicit() should be used instead in
745 * order to prevent the compiler from optimising away zeroing.
746 *
Daniel Borkmannd4c5efd2014-08-26 23:16:35 -0400747 * memzero_explicit() doesn't need an arch-specific version as
748 * it just invokes the one of memset() implicitly.
749 */
750void memzero_explicit(void *s, size_t count)
751{
752 memset(s, 0, count);
Daniel Borkmann7829fb02015-04-30 04:13:52 +0200753 barrier_data(s);
Daniel Borkmannd4c5efd2014-08-26 23:16:35 -0400754}
755EXPORT_SYMBOL(memzero_explicit);
756
Matthew Wilcoxfe0bc3f2017-09-08 16:13:48 -0700757#ifndef __HAVE_ARCH_MEMSET16
758/**
759 * memset16() - Fill a memory area with a uint16_t
760 * @s: Pointer to the start of the area.
761 * @v: The value to fill the area with
762 * @count: The number of values to store
763 *
764 * Differs from memset() in that it fills with a uint16_t instead
765 * of a byte. Remember that @count is the number of uint16_ts to
766 * store, not the number of bytes.
767 */
768void *memset16(uint16_t *s, uint16_t v, size_t count)
769{
770 uint16_t *xs = s;
771
772 while (count--)
773 *xs++ = v;
774 return s;
775}
776EXPORT_SYMBOL(memset16);
777#endif
778
779#ifndef __HAVE_ARCH_MEMSET32
780/**
781 * memset32() - Fill a memory area with a uint32_t
782 * @s: Pointer to the start of the area.
783 * @v: The value to fill the area with
784 * @count: The number of values to store
785 *
786 * Differs from memset() in that it fills with a uint32_t instead
787 * of a byte. Remember that @count is the number of uint32_ts to
788 * store, not the number of bytes.
789 */
790void *memset32(uint32_t *s, uint32_t v, size_t count)
791{
792 uint32_t *xs = s;
793
794 while (count--)
795 *xs++ = v;
796 return s;
797}
798EXPORT_SYMBOL(memset32);
799#endif
800
801#ifndef __HAVE_ARCH_MEMSET64
802/**
803 * memset64() - Fill a memory area with a uint64_t
804 * @s: Pointer to the start of the area.
805 * @v: The value to fill the area with
806 * @count: The number of values to store
807 *
808 * Differs from memset() in that it fills with a uint64_t instead
809 * of a byte. Remember that @count is the number of uint64_ts to
810 * store, not the number of bytes.
811 */
812void *memset64(uint64_t *s, uint64_t v, size_t count)
813{
814 uint64_t *xs = s;
815
816 while (count--)
817 *xs++ = v;
818 return s;
819}
820EXPORT_SYMBOL(memset64);
821#endif
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#ifndef __HAVE_ARCH_MEMCPY
824/**
825 * memcpy - Copy one area of memory to another
826 * @dest: Where to copy to
827 * @src: Where to copy from
828 * @count: The size of the area.
829 *
830 * You should not use this function to access IO space, use memcpy_toio()
831 * or memcpy_fromio() instead.
832 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800833void *memcpy(void *dest, const void *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Jesper Juhl850b9242005-10-30 15:02:13 -0800835 char *tmp = dest;
Jan-Benedict Glaw4c416ab2006-04-10 22:54:09 -0700836 const char *s = src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 while (count--)
839 *tmp++ = *s++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return dest;
841}
842EXPORT_SYMBOL(memcpy);
843#endif
844
845#ifndef __HAVE_ARCH_MEMMOVE
846/**
847 * memmove - Copy one area of memory to another
848 * @dest: Where to copy to
849 * @src: Where to copy from
850 * @count: The size of the area.
851 *
852 * Unlike memcpy(), memmove() copes with overlapping areas.
853 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800854void *memmove(void *dest, const void *src, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Paul Jackson82da2c32005-10-30 15:03:19 -0800856 char *tmp;
857 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 if (dest <= src) {
Jesper Juhl850b9242005-10-30 15:02:13 -0800860 tmp = dest;
861 s = src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 while (count--)
863 *tmp++ = *s++;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800864 } else {
Jesper Juhl850b9242005-10-30 15:02:13 -0800865 tmp = dest;
866 tmp += count;
867 s = src;
868 s += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 while (count--)
870 *--tmp = *--s;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return dest;
873}
874EXPORT_SYMBOL(memmove);
875#endif
876
877#ifndef __HAVE_ARCH_MEMCMP
878/**
879 * memcmp - Compare two areas of memory
880 * @cs: One area of memory
881 * @ct: Another area of memory
882 * @count: The size of the area.
883 */
Paolo 'Blaisorblade' Giarrusso0c281302005-05-05 16:15:17 -0700884#undef memcmp
Andi Kleena7330c92014-02-08 08:52:06 +0100885__visible int memcmp(const void *cs, const void *ct, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 const unsigned char *su1, *su2;
888 int res = 0;
889
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800890 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if ((res = *su1 - *su2) != 0)
892 break;
893 return res;
894}
895EXPORT_SYMBOL(memcmp);
896#endif
897
Nick Desaulniers7eceaf52019-04-05 18:38:45 -0700898#ifndef __HAVE_ARCH_BCMP
899/**
900 * bcmp - returns 0 if and only if the buffers have identical contents.
901 * @a: pointer to first buffer.
902 * @b: pointer to second buffer.
903 * @len: size of buffers.
904 *
905 * The sign or magnitude of a non-zero return value has no particular
906 * meaning, and architectures may implement their own more efficient bcmp(). So
907 * while this particular implementation is a simple (tail) call to memcmp, do
908 * not rely on anything but whether the return value is zero or non-zero.
909 */
910#undef bcmp
911int bcmp(const void *a, const void *b, size_t len)
912{
913 return memcmp(a, b, len);
914}
915EXPORT_SYMBOL(bcmp);
916#endif
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918#ifndef __HAVE_ARCH_MEMSCAN
919/**
920 * memscan - Find a character in an area of memory.
921 * @addr: The memory area
922 * @c: The byte to search for
923 * @size: The size of the area.
924 *
925 * returns the address of the first occurrence of @c, or 1 byte past
926 * the area if @c is not found
927 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800928void *memscan(void *addr, int c, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Jesper Juhl850b9242005-10-30 15:02:13 -0800930 unsigned char *p = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 while (size) {
933 if (*p == c)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800934 return (void *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 p++;
936 size--;
937 }
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800938 return (void *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940EXPORT_SYMBOL(memscan);
941#endif
942
943#ifndef __HAVE_ARCH_STRSTR
944/**
945 * strstr - Find the first substring in a %NUL terminated string
946 * @s1: The string to be searched
947 * @s2: The string to search for
948 */
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800949char *strstr(const char *s1, const char *s2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Li Zefand5f1fb52010-01-14 10:53:55 +0800951 size_t l1, l2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 l2 = strlen(s2);
954 if (!l2)
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800955 return (char *)s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 l1 = strlen(s1);
957 while (l1 >= l2) {
958 l1--;
Jesper Juhl51a0f0f2005-10-30 15:02:11 -0800959 if (!memcmp(s1, s2, l2))
960 return (char *)s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 s1++;
962 }
963 return NULL;
964}
965EXPORT_SYMBOL(strstr);
966#endif
967
Li Zefand5f1fb52010-01-14 10:53:55 +0800968#ifndef __HAVE_ARCH_STRNSTR
969/**
970 * strnstr - Find the first substring in a length-limited string
971 * @s1: The string to be searched
972 * @s2: The string to search for
973 * @len: the maximum number of characters to search
974 */
975char *strnstr(const char *s1, const char *s2, size_t len)
976{
André Goddard Rosad6a2eed2010-03-05 13:43:12 -0800977 size_t l2;
Li Zefand5f1fb52010-01-14 10:53:55 +0800978
979 l2 = strlen(s2);
980 if (!l2)
981 return (char *)s1;
André Goddard Rosad6a2eed2010-03-05 13:43:12 -0800982 while (len >= l2) {
983 len--;
Li Zefand5f1fb52010-01-14 10:53:55 +0800984 if (!memcmp(s1, s2, l2))
985 return (char *)s1;
986 s1++;
987 }
988 return NULL;
989}
990EXPORT_SYMBOL(strnstr);
991#endif
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#ifndef __HAVE_ARCH_MEMCHR
994/**
995 * memchr - Find a character in an area of memory.
996 * @s: The memory area
997 * @c: The byte to search for
998 * @n: The size of the area.
999 *
1000 * returns the address of the first occurrence of @c, or %NULL
1001 * if @c is not found
1002 */
1003void *memchr(const void *s, int c, size_t n)
1004{
1005 const unsigned char *p = s;
1006 while (n-- != 0) {
1007 if ((unsigned char)c == *p++) {
Jesper Juhl51a0f0f2005-10-30 15:02:11 -08001008 return (void *)(p - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 }
1011 return NULL;
1012}
1013EXPORT_SYMBOL(memchr);
1014#endif
Akinobu Mita798248202011-10-31 17:08:07 -07001015
1016static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
1017{
1018 while (bytes) {
1019 if (*start != value)
1020 return (void *)start;
1021 start++;
1022 bytes--;
1023 }
1024 return NULL;
1025}
1026
1027/**
1028 * memchr_inv - Find an unmatching character in an area of memory.
1029 * @start: The memory area
1030 * @c: Find a character other than c
1031 * @bytes: The size of the area.
1032 *
1033 * returns the address of the first character other than @c, or %NULL
1034 * if the whole buffer contains just @c.
1035 */
1036void *memchr_inv(const void *start, int c, size_t bytes)
1037{
1038 u8 value = c;
1039 u64 value64;
1040 unsigned int words, prefix;
1041
1042 if (bytes <= 16)
1043 return check_bytes8(start, value, bytes);
1044
Akinobu Mitaf43804b2012-03-23 15:02:14 -07001045 value64 = value;
Linus Torvalds72d93102014-09-13 11:14:53 -07001046#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
Andy Shevchenko3368e8fb2015-11-10 14:45:14 -08001047 value64 *= 0x0101010101010101ULL;
Linus Torvalds72d93102014-09-13 11:14:53 -07001048#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
Akinobu Mitaf43804b2012-03-23 15:02:14 -07001049 value64 *= 0x01010101;
1050 value64 |= value64 << 32;
1051#else
1052 value64 |= value64 << 8;
1053 value64 |= value64 << 16;
1054 value64 |= value64 << 32;
1055#endif
Akinobu Mita798248202011-10-31 17:08:07 -07001056
Akinobu Mitaf43804b2012-03-23 15:02:14 -07001057 prefix = (unsigned long)start % 8;
Akinobu Mita798248202011-10-31 17:08:07 -07001058 if (prefix) {
Akinobu Mitaf43804b2012-03-23 15:02:14 -07001059 u8 *r;
1060
1061 prefix = 8 - prefix;
1062 r = check_bytes8(start, value, prefix);
Akinobu Mita798248202011-10-31 17:08:07 -07001063 if (r)
1064 return r;
1065 start += prefix;
1066 bytes -= prefix;
1067 }
1068
1069 words = bytes / 8;
1070
1071 while (words) {
1072 if (*(u64 *)start != value64)
1073 return check_bytes8(start, value, 8);
1074 start += 8;
1075 words--;
1076 }
1077
1078 return check_bytes8(start, value, bytes % 8);
1079}
1080EXPORT_SYMBOL(memchr_inv);
Rasmus Villemoes94df2902015-06-25 15:02:22 -07001081
1082/**
1083 * strreplace - Replace all occurrences of character in string.
1084 * @s: The string to operate on.
1085 * @old: The character being replaced.
1086 * @new: The character @old is replaced with.
1087 *
1088 * Returns pointer to the nul byte at the end of @s.
1089 */
1090char *strreplace(char *s, char old, char new)
1091{
1092 for (; *s; ++s)
1093 if (*s == old)
1094 *s = new;
1095 return s;
1096}
1097EXPORT_SYMBOL(strreplace);
Daniel Micay0f513102017-07-12 14:36:10 -07001098
1099void fortify_panic(const char *name)
1100{
1101 pr_emerg("detected buffer overflow in %s\n", name);
1102 BUG();
1103}
1104EXPORT_SYMBOL(fortify_panic);