1 // SPDX-License-Identifier: GPL-2.0
6 * Copyright (C) 1991, 1992 Linus Torvalds
9 #include <linux/ctype.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
14 #ifndef EFI_HAVE_STRLEN
16 * strlen - Find the length of a string
17 * @s: The string to be sized
19 size_t strlen(const char *s)
23 for (sc = s; *sc != '\0'; ++sc)
29 #ifndef EFI_HAVE_STRNLEN
31 * strnlen - Find the length of a length-limited string
32 * @s: The string to be sized
33 * @count: The maximum number of bytes to search
35 size_t strnlen(const char *s, size_t count)
39 for (sc = s; count-- && *sc != '\0'; ++sc)
46 * strstr - Find the first substring in a %NUL terminated string
47 * @s1: The string to be searched
48 * @s2: The string to search for
50 char *strstr(const char *s1, const char *s2)
60 if (!memcmp(s1, s2, l2))
67 #ifndef EFI_HAVE_STRCMP
69 * strcmp - Compare two strings
73 int strcmp(const char *cs, const char *ct)
81 return c1 < c2 ? -1 : 1;
90 * strncmp - Compare two length-limited strings
93 * @count: The maximum number of bytes to compare
95 int strncmp(const char *cs, const char *ct, size_t count)
103 return c1 < c2 ? -1 : 1;
111 /* Works only for digits and letters, but small and fast */
112 #define TOLOWER(x) ((x) | 0x20)
114 static unsigned int simple_guess_base(const char *cp)
117 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
127 * simple_strtoull - convert a string to an unsigned long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
133 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
135 unsigned long long result = 0;
138 base = simple_guess_base(cp);
140 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
143 while (isxdigit(*cp)) {
146 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
149 result = result * base + value;
158 long simple_strtol(const char *cp, char **endp, unsigned int base)
161 return -simple_strtoull(cp + 1, endp, base);
163 return simple_strtoull(cp, endp, base);
166 #ifdef CONFIG_EFI_PARAMS_FROM_FDT
167 #ifndef EFI_HAVE_STRRCHR
169 * strrchr - Find the last occurrence of a character in a string
170 * @s: The string to be searched
171 * @c: The character to search for
173 char *strrchr(const char *s, int c)
175 const char *last = NULL;
183 #ifndef EFI_HAVE_MEMCHR
185 * memchr - Find a character in an area of memory.
186 * @s: The memory area
187 * @c: The byte to search for
188 * @n: The size of the area.
190 * returns the address of the first occurrence of @c, or %NULL
193 void *memchr(const void *s, int c, size_t n)
195 const unsigned char *p = s;
197 if ((unsigned char)c == *p++) {
198 return (void *)(p - 1);