Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 1 | /* |
| 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 Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | 6a9de55 | 2023-01-17 10:47:14 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Tom Rini | a877ce1 | 2023-12-14 13:16:58 -0500 | [diff] [blame] | 14 | #include <vsprintf.h> |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 15 | #include <linux/ctype.h> |
| 16 | |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 17 | /* from lib/kstrtox.c */ |
Simon Glass | 90877bb | 2021-07-24 09:03:38 -0600 | [diff] [blame] | 18 | static const char *_parse_integer_fixup_radix(const char *s, uint *basep) |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 19 | { |
Simon Glass | 90877bb | 2021-07-24 09:03:38 -0600 | [diff] [blame] | 20 | /* 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 Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 31 | } |
Simon Glass | 90877bb | 2021-07-24 09:03:38 -0600 | [diff] [blame] | 32 | |
| 33 | /* Use decimal by default */ |
| 34 | if (!*basep) |
| 35 | *basep = 10; |
| 36 | |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 37 | return s; |
| 38 | } |
| 39 | |
Simon Glass | fe2622c | 2021-07-24 09:03:35 -0600 | [diff] [blame] | 40 | /** |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 46 | * Return: value of digit (0..0xf) or 255 if the character is invalid |
Simon Glass | fe2622c | 2021-07-24 09:03:35 -0600 | [diff] [blame] | 47 | */ |
| 48 | static 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 Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 58 | ulong simple_strtoul(const char *cp, char **endp, uint base) |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 59 | { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 60 | ulong result = 0; |
Simon Glass | fe2622c | 2021-07-24 09:03:35 -0600 | [diff] [blame] | 61 | uint value; |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 62 | |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 63 | cp = _parse_integer_fixup_radix(cp, &base); |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 64 | |
Simon Glass | fe2622c | 2021-07-24 09:03:35 -0600 | [diff] [blame] | 65 | while (value = decode_digit(*cp), value < base) { |
| 66 | result = result * base + value; |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 67 | cp++; |
| 68 | } |
| 69 | |
| 70 | if (endp) |
| 71 | *endp = (char *)cp; |
| 72 | |
| 73 | return result; |
| 74 | } |
| 75 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 76 | ulong hextoul(const char *cp, char **endp) |
| 77 | { |
| 78 | return simple_strtoul(cp, endp, 16); |
| 79 | } |
| 80 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 81 | ulong dectoul(const char *cp, char **endp) |
| 82 | { |
| 83 | return simple_strtoul(cp, endp, 10); |
| 84 | } |
| 85 | |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 86 | int 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 | |
| 110 | long 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 | |
| 118 | unsigned long ustrtoul(const char *cp, char **endp, unsigned int base) |
| 119 | { |
| 120 | unsigned long result = simple_strtoul(cp, endp, base); |
Miquel Raynal | 5823a75 | 2018-09-06 09:08:43 +0200 | [diff] [blame] | 121 | switch (tolower(**endp)) { |
| 122 | case 'g': |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 123 | result *= 1024; |
| 124 | /* fall through */ |
Miquel Raynal | 5823a75 | 2018-09-06 09:08:43 +0200 | [diff] [blame] | 125 | case 'm': |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 126 | result *= 1024; |
| 127 | /* fall through */ |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 128 | case 'k': |
| 129 | result *= 1024; |
Miquel Raynal | b4cf942 | 2018-09-06 09:08:44 +0200 | [diff] [blame] | 130 | (*endp)++; |
| 131 | if (**endp == 'i') |
| 132 | (*endp)++; |
| 133 | if (**endp == 'B') |
| 134 | (*endp)++; |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base) |
| 140 | { |
| 141 | unsigned long long result = simple_strtoull(cp, endp, base); |
Miquel Raynal | 5823a75 | 2018-09-06 09:08:43 +0200 | [diff] [blame] | 142 | switch (tolower(**endp)) { |
| 143 | case 'g': |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 144 | result *= 1024; |
| 145 | /* fall through */ |
Miquel Raynal | 5823a75 | 2018-09-06 09:08:43 +0200 | [diff] [blame] | 146 | case 'm': |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 147 | result *= 1024; |
| 148 | /* fall through */ |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 149 | case 'k': |
| 150 | result *= 1024; |
Miquel Raynal | b4cf942 | 2018-09-06 09:08:44 +0200 | [diff] [blame] | 151 | (*endp)++; |
| 152 | if (**endp == 'i') |
| 153 | (*endp)++; |
| 154 | if (**endp == 'B') |
| 155 | (*endp)++; |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 156 | } |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | unsigned long long simple_strtoull(const char *cp, char **endp, |
| 161 | unsigned int base) |
| 162 | { |
Simon Glass | fe2622c | 2021-07-24 09:03:35 -0600 | [diff] [blame] | 163 | unsigned long long result = 0; |
| 164 | uint value; |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 165 | |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 166 | cp = _parse_integer_fixup_radix(cp, &base); |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 167 | |
Simon Glass | fe2622c | 2021-07-24 09:03:35 -0600 | [diff] [blame] | 168 | while (value = decode_digit(*cp), value < base) { |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 169 | result = result * base + value; |
| 170 | cp++; |
| 171 | } |
| 172 | |
| 173 | if (endp) |
| 174 | *endp = (char *) cp; |
| 175 | |
| 176 | return result; |
| 177 | } |
| 178 | |
Roland Gaudig | fd8b034 | 2021-07-23 12:29:18 +0000 | [diff] [blame] | 179 | long 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 Glass | 4bf2fc1 | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 187 | long trailing_strtoln_end(const char *str, const char *end, char const **endp) |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 188 | { |
| 189 | const char *p; |
| 190 | |
| 191 | if (!end) |
| 192 | end = str + strlen(str); |
Simon Glass | 20888f3 | 2022-04-24 23:30:57 -0600 | [diff] [blame] | 193 | p = end - 1; |
| 194 | if (p > str && isdigit(*p)) { |
| 195 | do { |
Simon Glass | 4bf2fc1 | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 196 | if (!isdigit(p[-1])) { |
| 197 | if (endp) |
| 198 | *endp = p; |
Simon Glass | 20888f3 | 2022-04-24 23:30:57 -0600 | [diff] [blame] | 199 | return dectoul(p, NULL); |
Simon Glass | 4bf2fc1 | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 200 | } |
Simon Glass | 20888f3 | 2022-04-24 23:30:57 -0600 | [diff] [blame] | 201 | } while (--p > str); |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 202 | } |
Simon Glass | 4bf2fc1 | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 203 | if (endp) |
| 204 | *endp = end; |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 205 | |
| 206 | return -1; |
| 207 | } |
| 208 | |
Simon Glass | 4bf2fc1 | 2022-04-24 23:30:58 -0600 | [diff] [blame] | 209 | long trailing_strtoln(const char *str, const char *end) |
| 210 | { |
| 211 | return trailing_strtoln_end(str, end, NULL); |
| 212 | } |
| 213 | |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 214 | long trailing_strtol(const char *str) |
| 215 | { |
| 216 | return trailing_strtoln(str, NULL); |
| 217 | } |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 218 | |
| 219 | void 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 Glass | 6a9de55 | 2023-01-17 10:47:14 -0700 | [diff] [blame] | 226 | |
| 227 | const 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 Glass | 6667c01 | 2024-07-30 08:39:36 -0600 | [diff] [blame] | 239 | for (count = 0, p = str; *p; p++) { |
Simon Glass | 6a9de55 | 2023-01-17 10:47:14 -0700 | [diff] [blame] | 240 | if (*p == ' ') { |
| 241 | count++; |
| 242 | *p = '\0'; |
| 243 | } |
| 244 | } |
Simon Glass | 6667c01 | 2024-07-30 08:39:36 -0600 | [diff] [blame] | 245 | if (p != str && p[-1]) |
| 246 | count++; |
Simon Glass | 6a9de55 | 2023-01-17 10:47:14 -0700 | [diff] [blame] | 247 | |
| 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 | |
| 262 | void str_free_list(const char **ptr) |
| 263 | { |
| 264 | if (ptr) |
| 265 | free((char *)ptr[0]); |
| 266 | free(ptr); |
| 267 | } |