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