forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoi.c
277 lines (258 loc) · 7.16 KB
/
atoi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* @file
* Parse a number in a string
*
* @authors
* Copyright (C) 2021 Pietro Cerutti <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page mutt_atoi Parse a number in a string
*
* Parse a number in a string.
*/
#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
/**
* str_atol_clamp - Convert ASCII string to a long and clamp
* @param[in] str String to read
* @param[in] lmin Lower bound
* @param[in] lmax Upper bound
* @param[out] dst Store the result
* @retval endptr
*
* endptr == NULL -> no conversion happened, or overflow
* endptr[0] == '\0' -> str was fully converted
* endptr[0] != '\0' -> endptr points to first non converted char in str
*
* This is a strtol() wrapper with range checking.
* errno may be set on error, e.g. ERANGE
*/
static const char *str_atol_clamp(const char *str, long *dst, long lmin, long lmax)
{
if (dst)
{
*dst = 0;
}
if (!str || (*str == '\0'))
{
return NULL;
}
char *e = NULL;
errno = 0;
long res = strtol(str, &e, 10);
if ((e == str) || (((res == LONG_MIN) || (res == LONG_MAX)) && (errno == ERANGE)) ||
(res < lmin) || (res > lmax))
{
return NULL;
}
if (dst)
{
*dst = res;
}
return e;
}
/**
* str_atoull_clamp - Convert ASCII string to an unsigned long long and clamp
* @param[in] str String to read
* @param[in] ullmax Upper bound
* @param[out] dst Store the result
* @retval endptr
*
* endptr == NULL -> no conversion happened, or overflow
* endptr[0] == '\0' -> str was fully converted
* endptr[0] != '\0' -> endptr points to first non converted char in str
*
* This is a strtoull() wrapper with range checking.
* errno may be set on error, e.g. ERANGE
*/
static const char *str_atoull_clamp(const char *str, unsigned long long *dst,
unsigned long long ullmax)
{
if (dst)
{
*dst = 0;
}
if (!str || (*str == '\0'))
{
return str;
}
char *e = NULL;
errno = 0;
unsigned long long res = strtoull(str, &e, 10);
if ((e == str) || ((res == ULLONG_MAX) && (errno == ERANGE)) || (res > ullmax))
{
return NULL;
}
if (dst)
{
*dst = res;
}
return e;
}
/**
* mutt_str_atol - Convert ASCII string to a long
* @param[in] str String to read
* @param[out] dst Store the result
* @retval endptr
*
* endptr == NULL -> no conversion happened, or overflow
* endptr[0] == '\0' -> str was fully converted
* endptr[0] != '\0' -> endptr points to first non converted char in str
*
* This is a strtol() wrapper with range checking.
* errno may be set on error, e.g. ERANGE
*/
const char *mutt_str_atol(const char *str, long *dst)
{
return str_atol_clamp(str, dst, LONG_MIN, LONG_MAX);
}
/**
* mutt_str_atos - Convert ASCII string to a short
* @param[in] str String to read
* @param[out] dst Store the result
* @retval 0 Success
* @retval -1 Error
* @retval -2 Error, overflow
*
* This is a strtol() wrapper with range checking.
* If @a dst is NULL, the string will be tested only (without conversion).
*
* errno may be set on error, e.g. ERANGE
*/
const char *mutt_str_atos(const char *str, short *dst)
{
long l;
const char *res = str_atol_clamp(str, &l, SHRT_MIN, SHRT_MAX);
if (dst)
{
*dst = res ? l : 0;
}
return res;
}
/**
* mutt_str_atoi - Convert ASCII string to an integer
* @param[in] str String to read
* @param[out] dst Store the result
* @retval endptr
*
* endptr == NULL -> no conversion happened, or overflow
* endptr[0] == '\0' -> str was fully converted
* endptr[0] != '\0' -> endptr points to first non converted char in str
*
* This is a strtol() wrapper with range checking.
* If @a dst is NULL, the string will be tested only (without conversion).
* errno may be set on error, e.g. ERANGE
*/
const char *mutt_str_atoi(const char *str, int *dst)
{
long l;
const char *res = str_atol_clamp(str, &l, INT_MIN, INT_MAX);
if (dst)
{
*dst = res ? l : 0;
}
return res;
}
/**
* mutt_str_atoui - Convert ASCII string to an unsigned integer
* @param[in] str String to read
* @param[out] dst Store the result
* @retval endptr
*
* endptr == NULL -> no conversion happened, or overflow
* endptr[0] == '\0' -> str was fully converted
* endptr[0] != '\0' -> endptr points to first non converted char in str
*
* @note This function's return value differs from the other functions.
* They return -1 if there is input beyond the number.
*/
const char *mutt_str_atoui(const char *str, unsigned int *dst)
{
unsigned long long l;
const char *res = str_atoull_clamp(str, &l, UINT_MAX);
if (dst)
{
*dst = res ? l : 0;
}
return res;
}
/**
* mutt_str_atoul - Convert ASCII string to an unsigned long
* @param[in] str String to read
* @param[out] dst Store the result
* @retval endptr
*
* endptr == NULL -> no conversion happened, or overflow
* endptr[0] == '\0' -> str was fully converted
* endptr[0] != '\0' -> endptr points to first non converted char in str
*
* @note This function's return value differs from the other functions.
* They return -1 if there is input beyond the number.
*/
const char *mutt_str_atoul(const char *str, unsigned long *dst)
{
unsigned long long l;
const char *res = str_atoull_clamp(str, &l, ULONG_MAX);
if (dst)
{
*dst = res ? l : 0;
}
return res;
}
/**
* mutt_str_atous - Convert ASCII string to an unsigned short
* @param[in] str String to read
* @param[out] dst Store the result
* @retval endptr
*
* endptr == NULL -> no conversion happened, or overflow
* endptr[0] == '\0' -> str was fully converted
* endptr[0] != '\0' -> endptr points to first non converted char in str
*
* @note This function's return value differs from the other functions.
* They return -1 if there is input beyond the number.
*/
const char *mutt_str_atous(const char *str, unsigned short *dst)
{
unsigned long long l;
const char *res = str_atoull_clamp(str, &l, USHRT_MAX);
if (dst)
{
*dst = res ? l : 0;
}
return res;
}
/**
* mutt_str_atoull - Convert ASCII string to an unsigned long long
* @param[in] str String to read
* @param[out] dst Store the result
* @retval endptr
*
* endptr == NULL -> no conversion happened, or overflow
* endptr[0] == '\0' -> str was fully converted
* endptr[0] != '\0' -> endptr points to first non converted char in str
*
* @note This function's return value differs from the other functions.
* They return -1 if there is input beyond the number.
*/
const char *mutt_str_atoull(const char *str, unsigned long long *dst)
{
return str_atoull_clamp(str, dst, ULLONG_MAX);
}