Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
32int memcmp(const void *s1, const void *s2, size_t len)
33{
34 bool diff;
35 asm("repe; cmpsb" CC_SET(nz)
36 : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len));
37 return diff;
38}
39
40int strcmp(const char *str1, const char *str2)
41{
42 const unsigned char *s1 = (const unsigned char *)str1;
43 const unsigned char *s2 = (const unsigned char *)str2;
44 int delta = 0;
45
46 while (*s1 || *s2) {
47 delta = *s1 - *s2;
48 if (delta)
49 return delta;
50 s1++;
51 s2++;
52 }
53 return 0;
54}
55
56int strncmp(const char *cs, const char *ct, size_t count)
57{
58 unsigned char c1, c2;
59
60 while (count) {
61 c1 = *cs++;
62 c2 = *ct++;
63 if (c1 != c2)
64 return c1 < c2 ? -1 : 1;
65 if (!c1)
66 break;
67 count--;
68 }
69 return 0;
70}
71
72size_t strnlen(const char *s, size_t maxlen)
73{
74 const char *es = s;
75 while (*es && maxlen) {
76 es++;
77 maxlen--;
78 }
79
80 return (es - s);
81}
82
83unsigned int atou(const char *s)
84{
85 unsigned int i = 0;
86 while (isdigit(*s))
87 i = i * 10 + (*s++ - '0');
88 return i;
89}
90
91/* Works only for digits and letters, but small and fast */
92#define TOLOWER(x) ((x) | 0x20)
93
94static unsigned int simple_guess_base(const char *cp)
95{
96 if (cp[0] == '0') {
97 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
98 return 16;
99 else
100 return 8;
101 } else {
102 return 10;
103 }
104}
105
106/**
107 * simple_strtoull - convert a string to an unsigned long long
108 * @cp: The start of the string
109 * @endp: A pointer to the end of the parsed string will be placed here
110 * @base: The number base to use
111 */
112
113unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
114{
115 unsigned long long result = 0;
116
117 if (!base)
118 base = simple_guess_base(cp);
119
120 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
121 cp += 2;
122
123 while (isxdigit(*cp)) {
124 unsigned int value;
125
126 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
127 if (value >= base)
128 break;
129 result = result * base + value;
130 cp++;
131 }
132 if (endp)
133 *endp = (char *)cp;
134
135 return result;
136}
137
138long simple_strtol(const char *cp, char **endp, unsigned int base)
139{
140 if (*cp == '-')
141 return -simple_strtoull(cp + 1, endp, base);
142
143 return simple_strtoull(cp, endp, base);
144}
145
146/**
147 * strlen - Find the length of a string
148 * @s: The string to be sized
149 */
150size_t strlen(const char *s)
151{
152 const char *sc;
153
154 for (sc = s; *sc != '\0'; ++sc)
155 /* nothing */;
156 return sc - s;
157}
158
159/**
160 * strstr - Find the first substring in a %NUL terminated string
161 * @s1: The string to be searched
162 * @s2: The string to search for
163 */
164char *strstr(const char *s1, const char *s2)
165{
166 size_t l1, l2;
167
168 l2 = strlen(s2);
169 if (!l2)
170 return (char *)s1;
171 l1 = strlen(s1);
172 while (l1 >= l2) {
173 l1--;
174 if (!memcmp(s1, s2, l2))
175 return (char *)s1;
176 s1++;
177 }
178 return NULL;
179}
180
181/**
182 * strchr - Find the first occurrence of the character c in the string s.
183 * @s: the string to be searched
184 * @c: the character to search for
185 */
186char *strchr(const char *s, int c)
187{
188 while (*s != (char)c)
189 if (*s++ == '\0')
190 return NULL;
191 return (char *)s;
192}
193
194static inline u64 __div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
195{
196 union {
197 u64 v64;
198 u32 v32[2];
199 } d = { dividend };
200 u32 upper;
201
202 upper = d.v32[1];
203 d.v32[1] = 0;
204 if (upper >= divisor) {
205 d.v32[1] = upper / divisor;
206 upper %= divisor;
207 }
208 asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
209 "rm" (divisor), "0" (d.v32[0]), "1" (upper));
210 return d.v64;
211}
212
213static inline u64 __div_u64(u64 dividend, u32 divisor)
214{
215 u32 remainder;
216
217 return __div_u64_rem(dividend, divisor, &remainder);
218}
219
220static inline char _tolower(const char c)
221{
222 return c | 0x20;
223}
224
225static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
226{
227 if (*base == 0) {
228 if (s[0] == '0') {
229 if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
230 *base = 16;
231 else
232 *base = 8;
233 } else
234 *base = 10;
235 }
236 if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
237 s += 2;
238 return s;
239}
240
241/*
242 * Convert non-negative integer string representation in explicitly given radix
243 * to an integer.
244 * Return number of characters consumed maybe or-ed with overflow bit.
245 * If overflow occurs, result integer (incorrect) is still returned.
246 *
247 * Don't you dare use this function.
248 */
249static unsigned int _parse_integer(const char *s,
250 unsigned int base,
251 unsigned long long *p)
252{
253 unsigned long long res;
254 unsigned int rv;
255
256 res = 0;
257 rv = 0;
258 while (1) {
259 unsigned int c = *s;
260 unsigned int lc = c | 0x20; /* don't tolower() this line */
261 unsigned int val;
262
263 if ('0' <= c && c <= '9')
264 val = c - '0';
265 else if ('a' <= lc && lc <= 'f')
266 val = lc - 'a' + 10;
267 else
268 break;
269
270 if (val >= base)
271 break;
272 /*
273 * Check for overflow only if we are within range of
274 * it in the max base we support (16)
275 */
276 if (unlikely(res & (~0ull << 60))) {
277 if (res > __div_u64(ULLONG_MAX - val, base))
278 rv |= KSTRTOX_OVERFLOW;
279 }
280 res = res * base + val;
281 rv++;
282 s++;
283 }
284 *p = res;
285 return rv;
286}
287
288static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
289{
290 unsigned long long _res;
291 unsigned int rv;
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/**
309 * kstrtoull - convert a string to an unsigned long long
310 * @s: The start of the string. The string must be null-terminated, and may also
311 * include a single newline before its terminating null. The first character
312 * may also be a plus sign, but not a minus sign.
313 * @base: The number base to use. The maximum supported base is 16. If base is
314 * given as 0, then the base of the string is automatically detected with the
315 * conventional semantics - If it begins with 0x the number will be parsed as a
316 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
317 * parsed as an octal number. Otherwise it will be parsed as a decimal.
318 * @res: Where to write the result of the conversion on success.
319 *
320 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
321 * Used as a replacement for the obsolete simple_strtoull. Return code must
322 * be checked.
323 */
324int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
325{
326 if (s[0] == '+')
327 s++;
328 return _kstrtoull(s, base, res);
329}