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 | |
| 12 | #include <common.h> |
| 13 | #include <errno.h> |
| 14 | #include <linux/ctype.h> |
| 15 | |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 16 | /* from lib/kstrtox.c */ |
| 17 | static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) |
| 18 | { |
| 19 | if (*base == 0) { |
| 20 | if (s[0] == '0') { |
| 21 | if (tolower(s[1]) == 'x' && isxdigit(s[2])) |
| 22 | *base = 16; |
| 23 | else |
| 24 | *base = 8; |
Sean Anderson | e980ae1 | 2020-06-07 01:36:45 -0400 | [diff] [blame] | 25 | } else |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 26 | *base = 10; |
| 27 | } |
| 28 | if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x') |
| 29 | s += 2; |
| 30 | return s; |
| 31 | } |
| 32 | |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 33 | unsigned long simple_strtoul(const char *cp, char **endp, |
| 34 | unsigned int base) |
| 35 | { |
| 36 | unsigned long result = 0; |
| 37 | unsigned long value; |
| 38 | |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 39 | cp = _parse_integer_fixup_radix(cp, &base); |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 40 | |
| 41 | while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp) |
| 42 | ? toupper(*cp) : *cp)-'A'+10) < base) { |
| 43 | result = result*base + value; |
| 44 | cp++; |
| 45 | } |
| 46 | |
| 47 | if (endp) |
| 48 | *endp = (char *)cp; |
| 49 | |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) |
| 54 | { |
| 55 | char *tail; |
| 56 | unsigned long val; |
| 57 | size_t len; |
| 58 | |
| 59 | *res = 0; |
| 60 | len = strlen(cp); |
| 61 | if (len == 0) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | val = simple_strtoul(cp, &tail, base); |
| 65 | if (tail == cp) |
| 66 | return -EINVAL; |
| 67 | |
| 68 | if ((*tail == '\0') || |
| 69 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { |
| 70 | *res = val; |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | return -EINVAL; |
| 75 | } |
| 76 | |
| 77 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
| 78 | { |
| 79 | if (*cp == '-') |
| 80 | return -simple_strtoul(cp + 1, endp, base); |
| 81 | |
| 82 | return simple_strtoul(cp, endp, base); |
| 83 | } |
| 84 | |
| 85 | unsigned long ustrtoul(const char *cp, char **endp, unsigned int base) |
| 86 | { |
| 87 | unsigned long result = simple_strtoul(cp, endp, base); |
Miquel Raynal | 5823a75 | 2018-09-06 09:08:43 +0200 | [diff] [blame] | 88 | switch (tolower(**endp)) { |
| 89 | case 'g': |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 90 | result *= 1024; |
| 91 | /* fall through */ |
Miquel Raynal | 5823a75 | 2018-09-06 09:08:43 +0200 | [diff] [blame] | 92 | case 'm': |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 93 | result *= 1024; |
| 94 | /* fall through */ |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 95 | case 'k': |
| 96 | result *= 1024; |
Miquel Raynal | b4cf942 | 2018-09-06 09:08:44 +0200 | [diff] [blame] | 97 | (*endp)++; |
| 98 | if (**endp == 'i') |
| 99 | (*endp)++; |
| 100 | if (**endp == 'B') |
| 101 | (*endp)++; |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 102 | } |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base) |
| 107 | { |
| 108 | unsigned long long result = simple_strtoull(cp, endp, base); |
Miquel Raynal | 5823a75 | 2018-09-06 09:08:43 +0200 | [diff] [blame] | 109 | switch (tolower(**endp)) { |
| 110 | case 'g': |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 111 | result *= 1024; |
| 112 | /* fall through */ |
Miquel Raynal | 5823a75 | 2018-09-06 09:08:43 +0200 | [diff] [blame] | 113 | case 'm': |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 114 | result *= 1024; |
| 115 | /* fall through */ |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 116 | case 'k': |
| 117 | result *= 1024; |
Miquel Raynal | b4cf942 | 2018-09-06 09:08:44 +0200 | [diff] [blame] | 118 | (*endp)++; |
| 119 | if (**endp == 'i') |
| 120 | (*endp)++; |
| 121 | if (**endp == 'B') |
| 122 | (*endp)++; |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 123 | } |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | unsigned long long simple_strtoull(const char *cp, char **endp, |
| 128 | unsigned int base) |
| 129 | { |
| 130 | unsigned long long result = 0, value; |
| 131 | |
Rob Clark | 365a3ef | 2017-09-11 16:53:08 -0400 | [diff] [blame] | 132 | cp = _parse_integer_fixup_radix(cp, &base); |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 133 | |
| 134 | while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0' |
| 135 | : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) { |
| 136 | result = result * base + value; |
| 137 | cp++; |
| 138 | } |
| 139 | |
| 140 | if (endp) |
| 141 | *endp = (char *) cp; |
| 142 | |
| 143 | return result; |
| 144 | } |
| 145 | |
Roland Gaudig | fd8b034 | 2021-07-23 12:29:18 +0000 | [diff] [blame] | 146 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
| 147 | { |
| 148 | if (*cp == '-') |
| 149 | return -simple_strtoull(cp + 1, endp, base); |
| 150 | |
| 151 | return simple_strtoull(cp, endp, base); |
| 152 | } |
| 153 | |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 154 | long trailing_strtoln(const char *str, const char *end) |
| 155 | { |
| 156 | const char *p; |
| 157 | |
| 158 | if (!end) |
| 159 | end = str + strlen(str); |
Simon Glass | e9ee762 | 2016-10-05 20:42:11 -0600 | [diff] [blame] | 160 | if (isdigit(end[-1])) { |
| 161 | for (p = end - 1; p > str; p--) { |
| 162 | if (!isdigit(*p)) |
| 163 | return simple_strtoul(p + 1, NULL, 10); |
| 164 | } |
Sjoerd Simons | fe4f179 | 2015-12-04 23:27:39 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | long trailing_strtol(const char *str) |
| 171 | { |
| 172 | return trailing_strtoln(str, NULL); |
| 173 | } |
Simon Glass | 811a260 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 174 | |
| 175 | void str_to_upper(const char *in, char *out, size_t len) |
| 176 | { |
| 177 | for (; len > 0 && *in; len--) |
| 178 | *out++ = toupper(*in++); |
| 179 | if (len) |
| 180 | *out = '\0'; |
| 181 | } |