blob: f83ac67c6662b75f58fdada6c4321473543022af [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
Simon Glassff9b9032021-07-24 09:03:30 -060081ulong dectoul(const char *cp, char **endp)
82{
83 return simple_strtoul(cp, endp, 10);
84}
85
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +010086int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
87{
88 char *tail;
89 unsigned long val;
90 size_t len;
91
92 *res = 0;
93 len = strlen(cp);
94 if (len == 0)
95 return -EINVAL;
96
97 val = simple_strtoul(cp, &tail, base);
98 if (tail == cp)
99 return -EINVAL;
100
101 if ((*tail == '\0') ||
102 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
103 *res = val;
104 return 0;
105 }
106
107 return -EINVAL;
108}
109
110long simple_strtol(const char *cp, char **endp, unsigned int base)
111{
112 if (*cp == '-')
113 return -simple_strtoul(cp + 1, endp, base);
114
115 return simple_strtoul(cp, endp, base);
116}
117
118unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
119{
120 unsigned long result = simple_strtoul(cp, endp, base);
Miquel Raynal5823a752018-09-06 09:08:43 +0200121 switch (tolower(**endp)) {
122 case 'g':
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100123 result *= 1024;
124 /* fall through */
Miquel Raynal5823a752018-09-06 09:08:43 +0200125 case 'm':
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100126 result *= 1024;
127 /* fall through */
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100128 case 'k':
129 result *= 1024;
Miquel Raynalb4cf9422018-09-06 09:08:44 +0200130 (*endp)++;
131 if (**endp == 'i')
132 (*endp)++;
133 if (**endp == 'B')
134 (*endp)++;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100135 }
136 return result;
137}
138
139unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
140{
141 unsigned long long result = simple_strtoull(cp, endp, base);
Miquel Raynal5823a752018-09-06 09:08:43 +0200142 switch (tolower(**endp)) {
143 case 'g':
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100144 result *= 1024;
145 /* fall through */
Miquel Raynal5823a752018-09-06 09:08:43 +0200146 case 'm':
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100147 result *= 1024;
148 /* fall through */
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100149 case 'k':
150 result *= 1024;
Miquel Raynalb4cf9422018-09-06 09:08:44 +0200151 (*endp)++;
152 if (**endp == 'i')
153 (*endp)++;
154 if (**endp == 'B')
155 (*endp)++;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100156 }
157 return result;
158}
159
160unsigned long long simple_strtoull(const char *cp, char **endp,
161 unsigned int base)
162{
Simon Glassfe2622c2021-07-24 09:03:35 -0600163 unsigned long long result = 0;
164 uint value;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100165
Rob Clark365a3ef2017-09-11 16:53:08 -0400166 cp = _parse_integer_fixup_radix(cp, &base);
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100167
Simon Glassfe2622c2021-07-24 09:03:35 -0600168 while (value = decode_digit(*cp), value < base) {
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100169 result = result * base + value;
170 cp++;
171 }
172
173 if (endp)
174 *endp = (char *) cp;
175
176 return result;
177}
178
Roland Gaudigfd8b0342021-07-23 12:29:18 +0000179long long simple_strtoll(const char *cp, char **endp, unsigned int base)
180{
181 if (*cp == '-')
182 return -simple_strtoull(cp + 1, endp, base);
183
184 return simple_strtoull(cp, endp, base);
185}
186
Simon Glass4bf2fc12022-04-24 23:30:58 -0600187long trailing_strtoln_end(const char *str, const char *end, char const **endp)
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100188{
189 const char *p;
190
191 if (!end)
192 end = str + strlen(str);
Simon Glass20888f32022-04-24 23:30:57 -0600193 p = end - 1;
194 if (p > str && isdigit(*p)) {
195 do {
Simon Glass4bf2fc12022-04-24 23:30:58 -0600196 if (!isdigit(p[-1])) {
197 if (endp)
198 *endp = p;
Simon Glass20888f32022-04-24 23:30:57 -0600199 return dectoul(p, NULL);
Simon Glass4bf2fc12022-04-24 23:30:58 -0600200 }
Simon Glass20888f32022-04-24 23:30:57 -0600201 } while (--p > str);
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100202 }
Simon Glass4bf2fc12022-04-24 23:30:58 -0600203 if (endp)
204 *endp = end;
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100205
206 return -1;
207}
208
Simon Glass4bf2fc12022-04-24 23:30:58 -0600209long trailing_strtoln(const char *str, const char *end)
210{
211 return trailing_strtoln_end(str, end, NULL);
212}
213
Sjoerd Simonsfe4f1792015-12-04 23:27:39 +0100214long trailing_strtol(const char *str)
215{
216 return trailing_strtoln(str, NULL);
217}
Simon Glass811a2602020-04-08 08:32:56 -0600218
219void str_to_upper(const char *in, char *out, size_t len)
220{
221 for (; len > 0 && *in; len--)
222 *out++ = toupper(*in++);
223 if (len)
224 *out = '\0';
225}
Simon Glass6a9de552023-01-17 10:47:14 -0700226
227const char **str_to_list(const char *instr)
228{
229 const char **ptr;
230 char *str, *p;
231 int count, i;
232
233 /* don't allocate if the string is empty */
234 str = *instr ? strdup(instr) : (char *)instr;
235 if (!str)
236 return NULL;
237
238 /* count the number of space-separated strings */
Simon Glass6667c012024-07-30 08:39:36 -0600239 for (count = 0, p = str; *p; p++) {
Simon Glass6a9de552023-01-17 10:47:14 -0700240 if (*p == ' ') {
241 count++;
242 *p = '\0';
243 }
244 }
Simon Glass6667c012024-07-30 08:39:36 -0600245 if (p != str && p[-1])
246 count++;
Simon Glass6a9de552023-01-17 10:47:14 -0700247
248 /* allocate the pointer array, allowing for a NULL terminator */
249 ptr = calloc(count + 1, sizeof(char *));
250 if (!ptr) {
251 if (*str)
252 free(str);
253 return NULL;
254 }
255
256 for (i = 0, p = str; i < count; p += strlen(p) + 1, i++)
257 ptr[i] = p;
258
259 return ptr;
260}
261
262void str_free_list(const char **ptr)
263{
264 if (ptr)
265 free((char *)ptr[0]);
266 free(ptr);
267}