blob: 206d1e918479354a8d62065d91ea321fccb041b2 [file] [log] [blame]
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +01001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +010012#include <errno.h>
Simon Glass6a9de552023-01-17 10:47:14 -070013#include <malloc.h>
Tom Rinia877ce12023-12-14 13:16:58 -050014#include <vsprintf.h>
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +010015#include <linux/ctype.h>
16
Rob Clark365a3ef2017-09-11 16:53:08 -040017/* from lib/kstrtox.c */
Simon Glass90877bb2021-07-24 09:03:38 -060018static const char *_parse_integer_fixup_radix(const char *s, uint *basep)
Rob Clark365a3ef2017-09-11 16:53:08 -040019{
Simon Glass90877bb2021-07-24 09:03:38 -060020 /* Look for a 0x prefix */
21 if (s[0] == '0') {
22 int ch = tolower(s[1]);
23
24 if (ch == 'x') {
25 *basep = 16;
26 s += 2;
27 } else if (!*basep) {
28 /* Only select octal if we don't have a base */
29 *basep = 8;
30 }
Rob Clark365a3ef2017-09-11 16:53:08 -040031 }
Simon Glass90877bb2021-07-24 09:03:38 -060032
33 /* Use decimal by default */
34 if (!*basep)
35 *basep = 10;
36
Rob Clark365a3ef2017-09-11 16:53:08 -040037 return s;
38}
39
Simon Glassfe2622c2021-07-24 09:03:35 -060040/**
41 * decode_digit() - Decode a single character into its numeric digit value
42 *
43 * This ignore case
44 *
45 * @ch: Character to convert (expects '0'..'9', 'a'..'f' or 'A'..'F')
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010046 * Return: value of digit (0..0xf) or 255 if the character is invalid
Simon Glassfe2622c2021-07-24 09:03:35 -060047 */
48static uint decode_digit(int ch)
49{
50 if (!isxdigit(ch))
51 return 256;
52
53 ch = tolower(ch);
54
55 return ch <= '9' ? ch - '0' : ch - 'a' + 0xa;
56}
57
Simon Glass3ff49ec2021-07-24 09:03:29 -060058ulong simple_strtoul(const char *cp, char **endp, uint base)
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +010059{
Simon Glass3ff49ec2021-07-24 09:03:29 -060060 ulong result = 0;
Simon Glassfe2622c2021-07-24 09:03:35 -060061 uint value;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +010062
Rob Clark365a3ef2017-09-11 16:53:08 -040063 cp = _parse_integer_fixup_radix(cp, &base);
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +010064
Simon Glassfe2622c2021-07-24 09:03:35 -060065 while (value = decode_digit(*cp), value < base) {
66 result = result * base + value;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +010067 cp++;
68 }
69
70 if (endp)
71 *endp = (char *)cp;
72
73 return result;
74}
75
Simon Glass3ff49ec2021-07-24 09:03:29 -060076ulong hextoul(const char *cp, char **endp)
77{
78 return simple_strtoul(cp, endp, 16);
79}
80
Heinrich Schuchardt7fac39a2024-11-03 23:42:20 +010081unsigned long long hextoull(const char *cp, char **endp)
82{
83 return simple_strtoull(cp, endp, 16);
84}
85
Simon Glassff9b9032021-07-24 09:03:30 -060086ulong dectoul(const char *cp, char **endp)
87{
88 return simple_strtoul(cp, endp, 10);
89}
90
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +010091int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
92{
93 char *tail;
94 unsigned long val;
95 size_t len;
96
97 *res = 0;
98 len = strlen(cp);
99 if (len == 0)
100 return -EINVAL;
101
102 val = simple_strtoul(cp, &tail, base);
103 if (tail == cp)
104 return -EINVAL;
105
106 if ((*tail == '\0') ||
107 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
108 *res = val;
109 return 0;
110 }
111
112 return -EINVAL;
113}
114
115long simple_strtol(const char *cp, char **endp, unsigned int base)
116{
117 if (*cp == '-')
118 return -simple_strtoul(cp + 1, endp, base);
119
120 return simple_strtoul(cp, endp, base);
121}
122
123unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
124{
125 unsigned long result = simple_strtoul(cp, endp, base);
Miquel Raynal5823a752018-09-06 09:08:43 +0200126 switch (tolower(**endp)) {
127 case 'g':
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100128 result *= 1024;
129 /* fall through */
Miquel Raynal5823a752018-09-06 09:08:43 +0200130 case 'm':
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100131 result *= 1024;
132 /* fall through */
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100133 case 'k':
134 result *= 1024;
Miquel Raynalb4cf9422018-09-06 09:08:44 +0200135 (*endp)++;
136 if (**endp == 'i')
137 (*endp)++;
138 if (**endp == 'B')
139 (*endp)++;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100140 }
141 return result;
142}
143
144unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
145{
146 unsigned long long result = simple_strtoull(cp, endp, base);
Miquel Raynal5823a752018-09-06 09:08:43 +0200147 switch (tolower(**endp)) {
148 case 'g':
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100149 result *= 1024;
150 /* fall through */
Miquel Raynal5823a752018-09-06 09:08:43 +0200151 case 'm':
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100152 result *= 1024;
153 /* fall through */
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100154 case 'k':
155 result *= 1024;
Miquel Raynalb4cf9422018-09-06 09:08:44 +0200156 (*endp)++;
157 if (**endp == 'i')
158 (*endp)++;
159 if (**endp == 'B')
160 (*endp)++;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100161 }
162 return result;
163}
164
165unsigned long long simple_strtoull(const char *cp, char **endp,
166 unsigned int base)
167{
Simon Glassfe2622c2021-07-24 09:03:35 -0600168 unsigned long long result = 0;
169 uint value;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100170
Rob Clark365a3ef2017-09-11 16:53:08 -0400171 cp = _parse_integer_fixup_radix(cp, &base);
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100172
Simon Glassfe2622c2021-07-24 09:03:35 -0600173 while (value = decode_digit(*cp), value < base) {
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100174 result = result * base + value;
175 cp++;
176 }
177
178 if (endp)
179 *endp = (char *) cp;
180
181 return result;
182}
183
Roland Gaudigfd8b0342021-07-23 12:29:18 +0000184long long simple_strtoll(const char *cp, char **endp, unsigned int base)
185{
186 if (*cp == '-')
187 return -simple_strtoull(cp + 1, endp, base);
188
189 return simple_strtoull(cp, endp, base);
190}
191
Simon Glass4bf2fc12022-04-24 23:30:58 -0600192long trailing_strtoln_end(const char *str, const char *end, char const **endp)
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100193{
194 const char *p;
195
196 if (!end)
197 end = str + strlen(str);
Simon Glass20888f32022-04-24 23:30:57 -0600198 p = end - 1;
199 if (p > str && isdigit(*p)) {
200 do {
Simon Glass4bf2fc12022-04-24 23:30:58 -0600201 if (!isdigit(p[-1])) {
202 if (endp)
203 *endp = p;
Simon Glass20888f32022-04-24 23:30:57 -0600204 return dectoul(p, NULL);
Simon Glass4bf2fc12022-04-24 23:30:58 -0600205 }
Simon Glass20888f32022-04-24 23:30:57 -0600206 } while (--p > str);
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100207 }
Simon Glass4bf2fc12022-04-24 23:30:58 -0600208 if (endp)
209 *endp = end;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100210
211 return -1;
212}
213
Simon Glass4bf2fc12022-04-24 23:30:58 -0600214long trailing_strtoln(const char *str, const char *end)
215{
216 return trailing_strtoln_end(str, end, NULL);
217}
218
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100219long trailing_strtol(const char *str)
220{
221 return trailing_strtoln(str, NULL);
222}
Simon Glass811a2602020-04-08 08:32:56 -0600223
224void str_to_upper(const char *in, char *out, size_t len)
225{
226 for (; len > 0 && *in; len--)
227 *out++ = toupper(*in++);
228 if (len)
229 *out = '\0';
230}
Simon Glass6a9de552023-01-17 10:47:14 -0700231
232const char **str_to_list(const char *instr)
233{
234 const char **ptr;
235 char *str, *p;
236 int count, i;
237
238 /* don't allocate if the string is empty */
239 str = *instr ? strdup(instr) : (char *)instr;
240 if (!str)
241 return NULL;
242
243 /* count the number of space-separated strings */
Simon Glass6667c012024-07-30 08:39:36 -0600244 for (count = 0, p = str; *p; p++) {
Simon Glass6a9de552023-01-17 10:47:14 -0700245 if (*p == ' ') {
246 count++;
247 *p = '\0';
248 }
249 }
Simon Glass6667c012024-07-30 08:39:36 -0600250 if (p != str && p[-1])
251 count++;
Simon Glass6a9de552023-01-17 10:47:14 -0700252
253 /* allocate the pointer array, allowing for a NULL terminator */
254 ptr = calloc(count + 1, sizeof(char *));
255 if (!ptr) {
256 if (*str)
257 free(str);
258 return NULL;
259 }
260
261 for (i = 0, p = str; i < count; p += strlen(p) + 1, i++)
262 ptr[i] = p;
263
264 return ptr;
265}
266
267void str_free_list(const char **ptr)
268{
269 if (ptr)
270 free((char *)ptr[0]);
271 free(ptr);
272}