Debian Codegraph @

lib/xstrtol.c

raw
1 /* A more useful interface to strtol.
2
3 Copyright (C) 1995-1996, 1998-2001, 2003-2007, 2009-2026 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 if (!strchr (valid_suffixes, **p))
121 {
122 *val = tmp;
123 return err | LONGINT_INVALID_SUFFIX_CHAR;
124 }
125
126 int xbase = 1024;
127 int suffixes = 1;
128 switch (**p)
129 {
130 case 'E': case 'G': case 'g': case 'k': case 'K': case 'M': case 'm':
131 case 'P': case 'Q': case 'R': case 'T': case 't': case 'Y': case 'Z':
132
133 /* The "valid suffix" '0' is a special flag meaning that
134 an optional second suffix is allowed, which can change
135 the base. A suffix "B" (e.g. "100MB") stands for a power
136 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
137 a power of 1024. If no suffix (e.g. "100M"), assume
138 power-of-1024. */
139
140 if (strchr (valid_suffixes, '0'))
141 switch (p[0][1])
142 {
143 case 'i':
144 if (p[0][2] == 'B')
145 suffixes += 2;
146 break;
147
148 case 'B':
149 case 'D': /* 'D' is obsolescent */
150 xbase = 1000;
151 suffixes++;
152 break;
153 }
154 }
155
156 strtol_error overflow;
157 switch (**p)
158 {
159 case 'b':
160 overflow = bkm_scale (&tmp, 512);
161 break;
162
163 case 'B':
164 /* This obsolescent first suffix is distinct from the 'B'
165 second suffix above. E.g., 'tar -L 1000B' means change
166 the tape after writing 1000 KiB of data. */
167 overflow = bkm_scale (&tmp, 1024);
168 break;
169
170 case 'c':
171 overflow = LONGINT_OK;
172 break;
173
174 case 'E': /* exa or exbi */
175 overflow = bkm_scale_by_power (&tmp, xbase, 6);
176 break;
177
178 case 'G': /* giga or gibi */
179 case 'g': /* 'g' is undocumented; for compatibility only */
180 overflow = bkm_scale_by_power (&tmp, xbase, 3);
181 break;
182
183 case 'k': /* kilo */
184 case 'K': /* kibi */
185 overflow = bkm_scale_by_power (&tmp, xbase, 1);
186 break;
187
188 case 'M': /* mega or mebi */
189 case 'm': /* 'm' is undocumented; for compatibility only */
190 overflow = bkm_scale_by_power (&tmp, xbase, 2);
191 break;
192
193 case 'P': /* peta or pebi */
194 overflow = bkm_scale_by_power (&tmp, xbase, 5);
195 break;
196
197 case 'Q': /* quetta or 2**100 */
198 overflow = bkm_scale_by_power (&tmp, xbase, 10);
199 break;
200
201 case 'R': /* ronna or 2**90 */
202 overflow = bkm_scale_by_power (&tmp, xbase, 9);
203 break;
204
205 case 'T': /* tera or tebi */
206 case 't': /* 't' is undocumented; for compatibility only */
207 overflow = bkm_scale_by_power (&tmp, xbase, 4);
208 break;
209
210 case 'w':
211 overflow = bkm_scale (&tmp, 2);
212 break;
213
214 case 'Y': /* yotta or 2**80 */
215 overflow = bkm_scale_by_power (&tmp, xbase, 8);
216 break;
217
218 case 'Z': /* zetta or 2**70 */
219 overflow = bkm_scale_by_power (&tmp, xbase, 7);
220 break;
221
222 default:
223 *val = tmp;
224 return err | LONGINT_INVALID_SUFFIX_CHAR;
225 }
226
227 err |= overflow;
228 *p += suffixes;
229 if (**p)
230 err |= LONGINT_INVALID_SUFFIX_CHAR;
231 }
232
233 *val = tmp;
234 return err;
235 }