xref: /linux/arch/x86/boot/string.c (revision bb29b957eaaa15be33243550d9ee576c2d601669)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* -*- linux-c -*- ------------------------------------------------------- *
3  *
4  *   Copyright (C) 1991, 1992 Linus Torvalds
5  *   Copyright 2007 rPath, Inc. - All Rights Reserved
6  *
7  * ----------------------------------------------------------------------- */
8 
9 /*
10  * Very basic string functions
11  */
12 
13 #include <linux/types.h>
14 #include <linux/compiler.h>
15 #include <linux/errno.h>
16 #include <linux/limits.h>
17 #include <asm/asm.h>
18 #include <asm/shared/string.h>
19 #include "ctype.h"
20 #include "string.h"
21 
22 #define KSTRTOX_OVERFLOW       (1U << 31)
23 
24 /*
25  * Undef these macros so that the functions that we provide
26  * here will have the correct names regardless of how string.h
27  * may have chosen to #define them.
28  */
29 #undef memcpy
30 #undef memset
31 #undef memcmp
32 
33 int memcmp(const void *s1, const void *s2, size_t len)
34 {
35 	return __inline_memcmp(s1, s2, len);
36 }
37 
38 /*
39  * Clang may lower `memcmp == 0` to `bcmp == 0`.
40  */
41 int bcmp(const void *s1, const void *s2, size_t len)
42 {
43 	return memcmp(s1, s2, len);
44 }
45 
46 int strcmp(const char *str1, const char *str2)
47 {
48 	const unsigned char *s1 = (const unsigned char *)str1;
49 	const unsigned char *s2 = (const unsigned char *)str2;
50 	int delta;
51 
52 	while (*s1 || *s2) {
53 		delta = *s1 - *s2;
54 		if (delta)
55 			return delta;
56 		s1++;
57 		s2++;
58 	}
59 	return 0;
60 }
61 
62 int strncmp(const char *cs, const char *ct, size_t count)
63 {
64 	unsigned char c1, c2;
65 
66 	while (count) {
67 		c1 = *cs++;
68 		c2 = *ct++;
69 		if (c1 != c2)
70 			return c1 < c2 ? -1 : 1;
71 		if (!c1)
72 			break;
73 		count--;
74 	}
75 	return 0;
76 }
77 
78 size_t strnlen(const char *s, size_t maxlen)
79 {
80 	const char *es = s;
81 	while (*es && maxlen) {
82 		es++;
83 		maxlen--;
84 	}
85 
86 	return (es - s);
87 }
88 
89 /* Works only for digits and letters, but small and fast */
90 #define TOLOWER(x) ((x) | 0x20)
91 
92 static unsigned int simple_guess_base(const char *cp)
93 {
94 	if (cp[0] == '0') {
95 		if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
96 			return 16;
97 		else
98 			return 8;
99 	} else {
100 		return 10;
101 	}
102 }
103 
104 /**
105  * simple_strtoull - convert a string to an unsigned long long
106  * @cp: The start of the string
107  * @endp: A pointer to the end of the parsed string will be placed here
108  * @base: The number base to use
109  */
110 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
111 {
112 	unsigned long long result = 0;
113 
114 	if (!base)
115 		base = simple_guess_base(cp);
116 
117 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
118 		cp += 2;
119 
120 	while (isxdigit(*cp)) {
121 		unsigned int value;
122 
123 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
124 		if (value >= base)
125 			break;
126 		result = result * base + value;
127 		cp++;
128 	}
129 	if (endp)
130 		*endp = (char *)cp;
131 
132 	return result;
133 }
134 
135 long simple_strtol(const char *cp, char **endp, unsigned int base)
136 {
137 	if (*cp == '-')
138 		return -simple_strtoull(cp + 1, endp, base);
139 
140 	return simple_strtoull(cp, endp, base);
141 }
142 
143 /**
144  * strlen - Find the length of a string
145  * @s: The string to be sized
146  */
147 size_t strlen(const char *s)
148 {
149 	const char *sc;
150 
151 	for (sc = s; *sc != '\0'; ++sc)
152 		/* nothing */;
153 	return sc - s;
154 }
155 
156 /**
157  * strstr - Find the first substring in a %NUL terminated string
158  * @s1: The string to be searched
159  * @s2: The string to search for
160  */
161 char *strstr(const char *s1, const char *s2)
162 {
163 	size_t l1, l2;
164 
165 	l2 = strlen(s2);
166 	if (!l2)
167 		return (char *)s1;
168 	l1 = strlen(s1);
169 	while (l1 >= l2) {
170 		l1--;
171 		if (!memcmp(s1, s2, l2))
172 			return (char *)s1;
173 		s1++;
174 	}
175 	return NULL;
176 }
177 
178 /**
179  * strchr - Find the first occurrence of the character c in the string s.
180  * @s: the string to be searched
181  * @c: the character to search for
182  */
183 char *strchr(const char *s, int c)
184 {
185 	while (*s != (char)c)
186 		if (*s++ == '\0')
187 			return NULL;
188 	return (char *)s;
189 }
190 
191 static inline u64 __div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
192 {
193 	union {
194 		u64 v64;
195 		u32 v32[2];
196 	} d = { dividend };
197 	u32 upper;
198 
199 	upper = d.v32[1];
200 	d.v32[1] = 0;
201 	if (upper >= divisor) {
202 		d.v32[1] = upper / divisor;
203 		upper %= divisor;
204 	}
205 	asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
206 		"rm" (divisor), "0" (d.v32[0]), "1" (upper));
207 	return d.v64;
208 }
209 
210 static inline u64 __div_u64(u64 dividend, u32 divisor)
211 {
212 	u32 remainder;
213 
214 	return __div_u64_rem(dividend, divisor, &remainder);
215 }
216 
217 static inline char _tolower(const char c)
218 {
219 	return c | 0x20;
220 }
221 
222 static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
223 {
224 	if (*base == 0) {
225 		if (s[0] == '0') {
226 			if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
227 				*base = 16;
228 			else
229 				*base = 8;
230 		} else
231 			*base = 10;
232 	}
233 	if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
234 		s += 2;
235 	return s;
236 }
237 
238 /*
239  * Convert non-negative integer string representation in explicitly given radix
240  * to an integer.
241  * Return number of characters consumed maybe or-ed with overflow bit.
242  * If overflow occurs, result integer (incorrect) is still returned.
243  *
244  * Don't you dare use this function.
245  */
246 static unsigned int _parse_integer(const char *s,
247 				   unsigned int base,
248 				   unsigned long long *p)
249 {
250 	unsigned long long res;
251 	unsigned int rv;
252 
253 	res = 0;
254 	rv = 0;
255 	while (1) {
256 		unsigned int c = *s;
257 		unsigned int lc = c | 0x20; /* don't tolower() this line */
258 		unsigned int val;
259 
260 		if ('0' <= c && c <= '9')
261 			val = c - '0';
262 		else if ('a' <= lc && lc <= 'f')
263 			val = lc - 'a' + 10;
264 		else
265 			break;
266 
267 		if (val >= base)
268 			break;
269 		/*
270 		 * Check for overflow only if we are within range of
271 		 * it in the max base we support (16)
272 		 */
273 		if (unlikely(res & (~0ull << 60))) {
274 			if (res > __div_u64(ULLONG_MAX - val, base))
275 				rv |= KSTRTOX_OVERFLOW;
276 		}
277 		res = res * base + val;
278 		rv++;
279 		s++;
280 	}
281 	*p = res;
282 	return rv;
283 }
284 
285 static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
286 {
287 	unsigned long long _res;
288 	unsigned int rv;
289 
290 	if (s[0] == '+')
291 		s++;
292 
293 	s = _parse_integer_fixup_radix(s, &base);
294 	rv = _parse_integer(s, base, &_res);
295 	if (rv & KSTRTOX_OVERFLOW)
296 		return -ERANGE;
297 	if (rv == 0)
298 		return -EINVAL;
299 	s += rv;
300 	if (*s == '\n')
301 		s++;
302 	if (*s)
303 		return -EINVAL;
304 	*res = _res;
305 	return 0;
306 }
307 
308 static int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
309 {
310 	unsigned long long tmp;
311 	int rv;
312 
313 	rv = _kstrtoull(s, base, &tmp);
314 	if (rv < 0)
315 		return rv;
316 	if (tmp != (unsigned long)tmp)
317 		return -ERANGE;
318 	*res = tmp;
319 	return 0;
320 }
321 
322 /**
323  * boot_kstrtoul - convert a string to an unsigned long
324  * @s: The start of the string. The string must be null-terminated, and may also
325  *  include a single newline before its terminating null. The first character
326  *  may also be a plus sign, but not a minus sign.
327  * @base: The number base to use. The maximum supported base is 16. If base is
328  *  given as 0, then the base of the string is automatically detected with the
329  *  conventional semantics - If it begins with 0x the number will be parsed as a
330  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
331  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
332  * @res: Where to write the result of the conversion on success.
333  *
334  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
335  * Used as a replacement for the simple_strtoull.
336  */
337 int boot_kstrtoul(const char *s, unsigned int base, unsigned long *res)
338 {
339 	/*
340 	 * We want to shortcut function call, but
341 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
342 	 */
343 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
344 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
345 		return _kstrtoull(s, base, (unsigned long long *)res);
346 	else
347 		return _kstrtoul(s, base, res);
348 }
349