Debian Codegraph @

lib/xstrtol.c

raw
1 /* A more useful interface to strtol.
2
3 Copyright (C) 1995-1996, 1998-2001, 2003-2007, 2009-2025 Free Software
4 Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19 /* Written by Jim Meyering. */
20
21 #ifndef __strtol
22 # define __strtol strtol
23 # define __strtol_t long int
24 # define __xstrtol xstrtol
25 # define STRTOL_T_MINIMUM LONG_MIN
26 # define STRTOL_T_MAXIMUM LONG_MAX
27 #endif
28
29 #include <config.h>
30
31 #include "xstrtol.h"
32
33 #include <ctype.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdckdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #if XSTRTOL_INCLUDE_INTTYPES_H
41 # include <inttypes.h>
42 #endif
43
44 #include "intprops.h"
45
46 static strtol_error
47 bkm_scale (__strtol_t *x, int scale_factor)
48 {
49 __strtol_t scaled;
50 if (ckd_mul (&scaled, *x, scale_factor))
51 {
52 *x = *x < 0 ? TYPE_MINIMUM (__strtol_t) : TYPE_MAXIMUM (__strtol_t);
53 return LONGINT_OVERFLOW;
54 }
55
56 *x = scaled;
57
58 return LONGINT_OK;
59 }
60
61 static strtol_error
62 bkm_scale_by_power (__strtol_t *x, int base, int power)
63 {
64 strtol_error err = LONGINT_OK;
65 while (power--)
66 err |= bkm_scale (x, base);
67 return err;
68 }
69
70 strtol_error
71 __xstrtol (char const *nptr, char **endptr, int base,
72 __strtol_t *val, char const *valid_suffixes)
73 {
74 char *t_ptr;
75 char **p = endptr ? endptr : &t_ptr;
76
77 if (! TYPE_SIGNED (__strtol_t))
78 {
79 char const *q = nptr;
80 unsigned char ch = *q;
81 while (isspace (ch))
82 ch = *++q;
83 if (ch == '-')
84 {
85 *p = (char *) nptr;
86 return LONGINT_INVALID;
87 }
88 }
89
90 errno = 0;
91 __strtol_t tmp = __strtol (nptr, p, base);
92 strtol_error err = LONGINT_OK;
93
94 if (*p == nptr)
95 {
96 /* If there is no number but there is a valid suffix, assume the
97 number is 1. The string is invalid otherwise. */
98 if (! (valid_suffixes && *nptr && strchr (valid_suffixes, *nptr)))
99 return LONGINT_INVALID;
100 tmp = 1;
101 }
102 else if (errno != 0)
103 {
104 if (errno != ERANGE)
105 return LONGINT_INVALID;
106 err = LONGINT_OVERFLOW;
107 }
108
109 /* Let valid_suffixes == NULL mean "allow any suffix". */
110 /* FIXME: update all callers except the ones that allow suffixes
111 after the number, changing last parameter NULL to "". */
112 if (!valid_suffixes)
113 {
114 *val = tmp;
115 return err;
116 }
117
118 if (**p != '\0')
119 {
120 int xbase = 1024;
121 int suffixes = 1;
122 strtol_error overflow;
123
124 if (!strchr (valid_suffixes, **p))
125 {
126 *val = tmp;
127 return err | LONGINT_INVALID_SUFFIX_CHAR;
128 }
129
130 switch (**p)
131 {
132 case 'E': case 'G': case 'g': case 'k': case 'K': case 'M': case 'm':
133 case 'P': case 'Q': case 'R': case 'T': case 't': case 'Y': case 'Z':
134
135 /* The "valid suffix" '0' is a special flag meaning that
136 an optional second suffix is allowed, which can change
137 the base. A suffix "B" (e.g. "100MB") stands for a power
138 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
139 a power of 1024. If no suffix (e.g. "100M"), assume
140 power-of-1024. */
141
142 if (strchr (valid_suffixes, '0'))
143 switch (p[0][1])
144 {
145 case 'i':
146 if (p[0][2] == 'B')
147 suffixes += 2;
148 break;
149
150 case 'B':
151 case 'D': /* 'D' is obsolescent */
152 xbase = 1000;
153 suffixes++;
154 break;
155 }
156 }
157
158 switch (**p)
159 {
160 case 'b':
161 overflow = bkm_scale (&tmp, 512);
162 break;
163
164 case 'B':
165 /* This obsolescent first suffix is distinct from the 'B'
166 second suffix above. E.g., 'tar -L 1000B' means change
167 the tape after writing 1000 KiB of data. */
168 overflow = bkm_scale (&tmp, 1024);
169 break;
170
171 case 'c':
172 overflow = LONGINT_OK;
173 break;
174
175 case 'E': /* exa or exbi */
176 overflow = bkm_scale_by_power (&tmp, xbase, 6);
177 break;
178
179 case 'G': /* giga or gibi */
180 case 'g': /* 'g' is undocumented; for compatibility only */
181 overflow = bkm_scale_by_power (&tmp, xbase, 3);
182 break;
183
184 case 'k': /* kilo */
185 case 'K': /* kibi */
186 overflow = bkm_scale_by_power (&tmp, xbase, 1);
187 break;
188
189 case 'M': /* mega or mebi */
190 case 'm': /* 'm' is undocumented; for compatibility only */
191 overflow = bkm_scale_by_power (&tmp, xbase, 2);
192 break;
193
194 case 'P': /* peta or pebi */
195 overflow = bkm_scale_by_power (&tmp, xbase, 5);
196 break;
197
198 case 'Q': /* quetta or 2**100 */
199 overflow = bkm_scale_by_power (&tmp, xbase, 10);
200 break;
201
202 case 'R': /* ronna or 2**90 */
203 overflow = bkm_scale_by_power (&tmp, xbase, 9);
204 break;
205
206 case 'T': /* tera or tebi */
207 case 't': /* 't' is undocumented; for compatibility only */
208 overflow = bkm_scale_by_power (&tmp, xbase, 4);
209 break;
210
211 case 'w':
212 overflow = bkm_scale (&tmp, 2);
213 break;
214
215 case 'Y': /* yotta or 2**80 */
216 overflow = bkm_scale_by_power (&tmp, xbase, 8);
217 break;
218
219 case 'Z': /* zetta or 2**70 */
220 overflow = bkm_scale_by_power (&tmp, xbase, 7);
221 break;
222
223 default:
224 *val = tmp;
225 return err | LONGINT_INVALID_SUFFIX_CHAR;
226 }
227
228 err |= overflow;
229 *p += suffixes;
230 if (**p)
231 err |= LONGINT_INVALID_SUFFIX_CHAR;
232 }
233
234 *val = tmp;
235 return err;
236 }