/src/ffmpeg/libavutil/avsscanf.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2005-2014 Rich Felker, et al. |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining |
5 | | * a copy of this software and associated documentation files (the |
6 | | * "Software"), to deal in the Software without restriction, including |
7 | | * without limitation the rights to use, copy, modify, merge, publish, |
8 | | * distribute, sublicense, and/or sell copies of the Software, and to |
9 | | * permit persons to whom the Software is furnished to do so, subject to |
10 | | * the following conditions: |
11 | | * |
12 | | * The above copyright notice and this permission notice shall be |
13 | | * included in all copies or substantial portions of the Software. |
14 | | * |
15 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
18 | | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
19 | | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
20 | | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
21 | | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22 | | */ |
23 | | |
24 | | #include <errno.h> |
25 | | #include <limits.h> |
26 | | #include <math.h> |
27 | | #include <stdarg.h> |
28 | | #include <stddef.h> |
29 | | #include <stdint.h> |
30 | | #include <stdio.h> |
31 | | #include <string.h> |
32 | | #include <float.h> |
33 | | |
34 | | #include "avstring.h" |
35 | | #include "libm.h" |
36 | | |
37 | | typedef struct FFFILE { |
38 | | size_t buf_size; |
39 | | unsigned char *buf; |
40 | | unsigned char *rpos, *rend; |
41 | | unsigned char *shend; |
42 | | ptrdiff_t shlim, shcnt; |
43 | | void *cookie; |
44 | | size_t (*read)(struct FFFILE *, unsigned char *, size_t); |
45 | | } FFFILE; |
46 | | |
47 | 0 | #define SIZE_hh -2 |
48 | 0 | #define SIZE_h -1 |
49 | 0 | #define SIZE_def 0 |
50 | 0 | #define SIZE_l 1 |
51 | 0 | #define SIZE_L 2 |
52 | 0 | #define SIZE_ll 3 |
53 | | |
54 | 0 | #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf)) |
55 | | |
56 | | static int fftoread(FFFILE *f) |
57 | 0 | { |
58 | 0 | f->rpos = f->rend = f->buf + f->buf_size; |
59 | 0 | return 0; |
60 | 0 | } |
61 | | |
62 | | static size_t ffstring_read(FFFILE *f, unsigned char *buf, size_t len) |
63 | 0 | { |
64 | 0 | char *src = f->cookie; |
65 | 0 | size_t k = len+256; |
66 | 0 | char *end = memchr(src, 0, k); |
67 | |
|
68 | 0 | if (end) k = end-src; |
69 | 0 | if (k < len) len = k; |
70 | 0 | memcpy(buf, src, len); |
71 | 0 | f->rpos = (void *)(src+len); |
72 | 0 | f->rend = (void *)(src+k); |
73 | 0 | f->cookie = src+k; |
74 | |
|
75 | 0 | return len; |
76 | 0 | } |
77 | | |
78 | | static int ffuflow(FFFILE *f) |
79 | 0 | { |
80 | 0 | unsigned char c; |
81 | 0 | if (!fftoread(f) && f->read(f, &c, 1)==1) return c; |
82 | 0 | return EOF; |
83 | 0 | } |
84 | | |
85 | | static void ffshlim(FFFILE *f, ptrdiff_t lim) |
86 | 0 | { |
87 | 0 | f->shlim = lim; |
88 | 0 | f->shcnt = f->buf - f->rpos; |
89 | | /* If lim is nonzero, rend must be a valid pointer. */ |
90 | 0 | if (lim && f->rend - f->rpos > lim) |
91 | 0 | f->shend = f->rpos + lim; |
92 | 0 | else |
93 | 0 | f->shend = f->rend; |
94 | 0 | } |
95 | | |
96 | | static int ffshgetc(FFFILE *f) |
97 | 0 | { |
98 | 0 | int c; |
99 | 0 | ptrdiff_t cnt = shcnt(f); |
100 | 0 | if (f->shlim && cnt >= f->shlim || (c=ffuflow(f)) < 0) { |
101 | 0 | f->shcnt = f->buf - f->rpos + cnt; |
102 | 0 | f->shend = 0; |
103 | 0 | return EOF; |
104 | 0 | } |
105 | 0 | cnt++; |
106 | 0 | if (f->shlim && f->rend - f->rpos > f->shlim - cnt) |
107 | 0 | f->shend = f->rpos + (f->shlim - cnt); |
108 | 0 | else |
109 | 0 | f->shend = f->rend; |
110 | 0 | f->shcnt = f->buf - f->rpos + cnt; |
111 | 0 | if (f->rpos[-1] != c) f->rpos[-1] = c; |
112 | 0 | return c; |
113 | 0 | } |
114 | | |
115 | 0 | #define shlim(f, lim) ffshlim((f), (lim)) |
116 | 0 | #define shgetc(f) (((f)->rpos < (f)->shend) ? *(f)->rpos++ : ffshgetc(f)) |
117 | 0 | #define shunget(f) ((f)->shend ? (void)(f)->rpos-- : (void)0) |
118 | | |
119 | | static const unsigned char table[] = { -1, |
120 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
121 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
122 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
123 | | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, |
124 | | -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, |
125 | | 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, |
126 | | -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, |
127 | | 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, |
128 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
129 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
130 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
131 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
132 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
133 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
134 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
135 | | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
136 | | }; |
137 | | |
138 | | static unsigned long long ffintscan(FFFILE *f, unsigned base, int pok, unsigned long long lim) |
139 | 0 | { |
140 | 0 | const unsigned char *val = table+1; |
141 | 0 | int c, neg=0; |
142 | 0 | unsigned x; |
143 | 0 | unsigned long long y; |
144 | 0 | if (base > 36 || base == 1) { |
145 | 0 | errno = EINVAL; |
146 | 0 | return 0; |
147 | 0 | } |
148 | 0 | while (av_isspace((c=shgetc(f)))); |
149 | 0 | if (c=='+' || c=='-') { |
150 | 0 | neg = -(c=='-'); |
151 | 0 | c = shgetc(f); |
152 | 0 | } |
153 | 0 | if ((base == 0 || base == 16) && c=='0') { |
154 | 0 | c = shgetc(f); |
155 | 0 | if ((c|32)=='x') { |
156 | 0 | c = shgetc(f); |
157 | 0 | if (val[c]>=16) { |
158 | 0 | shunget(f); |
159 | 0 | if (pok) shunget(f); |
160 | 0 | else shlim(f, 0); |
161 | 0 | return 0; |
162 | 0 | } |
163 | 0 | base = 16; |
164 | 0 | } else if (base == 0) { |
165 | 0 | base = 8; |
166 | 0 | } |
167 | 0 | } else { |
168 | 0 | if (base == 0) base = 10; |
169 | 0 | if (val[c] >= base) { |
170 | 0 | shunget(f); |
171 | 0 | shlim(f, 0); |
172 | 0 | errno = EINVAL; |
173 | 0 | return 0; |
174 | 0 | } |
175 | 0 | } |
176 | 0 | if (base == 10) { |
177 | 0 | for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f)) |
178 | 0 | x = x*10 + (c-'0'); |
179 | 0 | for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f)) |
180 | 0 | y = y*10 + (c-'0'); |
181 | 0 | if (c-'0'>=10U) goto done; |
182 | 0 | } else if (!(base & base-1)) { |
183 | 0 | int bs = "\0\1\2\4\7\3\6\5"[(0x17*base)>>5&7]; |
184 | 0 | for (x=0; val[c]<base && x<=UINT_MAX/32; c=shgetc(f)) |
185 | 0 | x = x<<bs | val[c]; |
186 | 0 | for (y=x; val[c]<base && y<=ULLONG_MAX>>bs; c=shgetc(f)) |
187 | 0 | y = y<<bs | val[c]; |
188 | 0 | } else { |
189 | 0 | for (x=0; val[c]<base && x<=UINT_MAX/36-1; c=shgetc(f)) |
190 | 0 | x = x*base + val[c]; |
191 | 0 | for (y=x; val[c]<base && y<=ULLONG_MAX/base && base*y<=ULLONG_MAX-val[c]; c=shgetc(f)) |
192 | 0 | y = y*base + val[c]; |
193 | 0 | } |
194 | 0 | if (val[c]<base) { |
195 | 0 | for (; val[c]<base; c=shgetc(f)); |
196 | 0 | errno = ERANGE; |
197 | 0 | y = lim; |
198 | 0 | if (lim&1) neg = 0; |
199 | 0 | } |
200 | 0 | done: |
201 | 0 | shunget(f); |
202 | 0 | if (y>=lim) { |
203 | 0 | if (!(lim&1) && !neg) { |
204 | 0 | errno = ERANGE; |
205 | 0 | return lim-1; |
206 | 0 | } else if (y>lim) { |
207 | 0 | errno = ERANGE; |
208 | 0 | return lim; |
209 | 0 | } |
210 | 0 | } |
211 | 0 | return (y^neg)-neg; |
212 | 0 | } |
213 | | |
214 | | static long long scanexp(FFFILE *f, int pok) |
215 | 0 | { |
216 | 0 | int c; |
217 | 0 | int x; |
218 | 0 | long long y; |
219 | 0 | int neg = 0; |
220 | |
|
221 | 0 | c = shgetc(f); |
222 | 0 | if (c=='+' || c=='-') { |
223 | 0 | neg = (c=='-'); |
224 | 0 | c = shgetc(f); |
225 | 0 | if (c-'0'>=10U && pok) shunget(f); |
226 | 0 | } |
227 | 0 | if (c-'0'>=10U) { |
228 | 0 | shunget(f); |
229 | 0 | return LLONG_MIN; |
230 | 0 | } |
231 | 0 | for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f)) |
232 | 0 | x = 10*x + (c-'0'); |
233 | 0 | for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f)) |
234 | 0 | y = 10*y + (c-'0'); |
235 | 0 | for (; c-'0'<10U; c = shgetc(f)); |
236 | 0 | shunget(f); |
237 | 0 | return neg ? -y : y; |
238 | 0 | } |
239 | | |
240 | 0 | #define LD_B1B_DIG 2 |
241 | 0 | #define LD_B1B_MAX 9007199, 254740991 |
242 | 0 | #define KMAX 128 |
243 | 0 | #define MASK (KMAX-1) |
244 | | |
245 | | static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok) |
246 | 0 | { |
247 | 0 | uint32_t x[KMAX]; |
248 | 0 | static const uint32_t th[] = { LD_B1B_MAX }; |
249 | 0 | int i, j, k, a, z; |
250 | 0 | long long lrp=0, dc=0; |
251 | 0 | long long e10=0; |
252 | 0 | int lnz = 0; |
253 | 0 | int gotdig = 0, gotrad = 0; |
254 | 0 | int rp; |
255 | 0 | int e2; |
256 | 0 | int emax = -emin-bits+3; |
257 | 0 | int denormal = 0; |
258 | 0 | double y; |
259 | 0 | double frac=0; |
260 | 0 | double bias=0; |
261 | 0 | static const int p10s[] = { 10, 100, 1000, 10000, |
262 | 0 | 100000, 1000000, 10000000, 100000000 }; |
263 | |
|
264 | 0 | j=0; |
265 | 0 | k=0; |
266 | | |
267 | | /* Don't let leading zeros consume buffer space */ |
268 | 0 | for (; c=='0'; c = shgetc(f)) gotdig=1; |
269 | 0 | if (c=='.') { |
270 | 0 | gotrad = 1; |
271 | 0 | for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--; |
272 | 0 | } |
273 | |
|
274 | 0 | x[0] = 0; |
275 | 0 | for (; c-'0'<10U || c=='.'; c = shgetc(f)) { |
276 | 0 | if (c == '.') { |
277 | 0 | if (gotrad) break; |
278 | 0 | gotrad = 1; |
279 | 0 | lrp = dc; |
280 | 0 | } else if (k < KMAX-3) { |
281 | 0 | dc++; |
282 | 0 | if (c!='0') lnz = dc; |
283 | 0 | if (j) x[k] = x[k]*10 + c-'0'; |
284 | 0 | else x[k] = c-'0'; |
285 | 0 | if (++j==9) { |
286 | 0 | k++; |
287 | 0 | j=0; |
288 | 0 | } |
289 | 0 | gotdig=1; |
290 | 0 | } else { |
291 | 0 | dc++; |
292 | 0 | if (c!='0') { |
293 | 0 | lnz = (KMAX-4)*9; |
294 | 0 | x[KMAX-4] |= 1; |
295 | 0 | } |
296 | 0 | } |
297 | 0 | } |
298 | 0 | if (!gotrad) lrp=dc; |
299 | |
|
300 | 0 | if (gotdig && (c|32)=='e') { |
301 | 0 | e10 = scanexp(f, pok); |
302 | 0 | if (e10 == LLONG_MIN) { |
303 | 0 | if (pok) { |
304 | 0 | shunget(f); |
305 | 0 | } else { |
306 | 0 | shlim(f, 0); |
307 | 0 | return 0; |
308 | 0 | } |
309 | 0 | e10 = 0; |
310 | 0 | } |
311 | 0 | lrp += e10; |
312 | 0 | } else if (c>=0) { |
313 | 0 | shunget(f); |
314 | 0 | } |
315 | 0 | if (!gotdig) { |
316 | 0 | errno = EINVAL; |
317 | 0 | shlim(f, 0); |
318 | 0 | return 0; |
319 | 0 | } |
320 | | |
321 | | /* Handle zero specially to avoid nasty special cases later */ |
322 | 0 | if (!x[0]) return sign * 0.0; |
323 | | |
324 | | /* Optimize small integers (w/no exponent) and over/under-flow */ |
325 | 0 | if (lrp==dc && dc<10 && (bits>30 || x[0]>>bits==0)) |
326 | 0 | return sign * (double)x[0]; |
327 | 0 | if (lrp > -emin/2) { |
328 | 0 | errno = ERANGE; |
329 | 0 | return sign * DBL_MAX * DBL_MAX; |
330 | 0 | } |
331 | 0 | if (lrp < emin-2*DBL_MANT_DIG) { |
332 | 0 | errno = ERANGE; |
333 | 0 | return sign * DBL_MIN * DBL_MIN; |
334 | 0 | } |
335 | | |
336 | | /* Align incomplete final B1B digit */ |
337 | 0 | if (j) { |
338 | 0 | for (; j<9; j++) x[k]*=10; |
339 | 0 | k++; |
340 | 0 | j=0; |
341 | 0 | } |
342 | |
|
343 | 0 | a = 0; |
344 | 0 | z = k; |
345 | 0 | e2 = 0; |
346 | 0 | rp = lrp; |
347 | | |
348 | | /* Optimize small to mid-size integers (even in exp. notation) */ |
349 | 0 | if (lnz<9 && lnz<=rp && rp < 18) { |
350 | 0 | int bitlim; |
351 | 0 | if (rp == 9) return sign * (double)x[0]; |
352 | 0 | if (rp < 9) return sign * (double)x[0] / p10s[8-rp]; |
353 | 0 | bitlim = bits-3*(int)(rp-9); |
354 | 0 | if (bitlim>30 || x[0]>>bitlim==0) |
355 | 0 | return sign * (double)x[0] * p10s[rp-10]; |
356 | 0 | } |
357 | | |
358 | | /* Drop trailing zeros */ |
359 | 0 | for (; !x[z-1]; z--); |
360 | | |
361 | | /* Align radix point to B1B digit boundary */ |
362 | 0 | if (rp % 9) { |
363 | 0 | int rpm9 = rp>=0 ? rp%9 : rp%9+9; |
364 | 0 | int p10 = p10s[8-rpm9]; |
365 | 0 | uint32_t carry = 0; |
366 | 0 | for (k=a; k!=z; k++) { |
367 | 0 | uint32_t tmp = x[k] % p10; |
368 | 0 | x[k] = x[k]/p10 + carry; |
369 | 0 | carry = 1000000000/p10 * tmp; |
370 | 0 | if (k==a && !x[k]) { |
371 | 0 | a = (a+1 & MASK); |
372 | 0 | rp -= 9; |
373 | 0 | } |
374 | 0 | } |
375 | 0 | if (carry) x[z++] = carry; |
376 | 0 | rp += 9-rpm9; |
377 | 0 | } |
378 | | |
379 | | /* Upscale until desired number of bits are left of radix point */ |
380 | 0 | while (rp < 9*LD_B1B_DIG || (rp == 9*LD_B1B_DIG && x[a]<th[0])) { |
381 | 0 | uint32_t carry = 0; |
382 | 0 | e2 -= 29; |
383 | 0 | for (k=(z-1 & MASK); ; k=(k-1 & MASK)) { |
384 | 0 | uint64_t tmp = ((uint64_t)x[k] << 29) + carry; |
385 | 0 | if (tmp > 1000000000) { |
386 | 0 | carry = tmp / 1000000000; |
387 | 0 | x[k] = tmp % 1000000000; |
388 | 0 | } else { |
389 | 0 | carry = 0; |
390 | 0 | x[k] = tmp; |
391 | 0 | } |
392 | 0 | if (k==(z-1 & MASK) && k!=a && !x[k]) z = k; |
393 | 0 | if (k==a) break; |
394 | 0 | } |
395 | 0 | if (carry) { |
396 | 0 | rp += 9; |
397 | 0 | a = (a-1 & MASK); |
398 | 0 | if (a == z) { |
399 | 0 | z = (z-1 & MASK); |
400 | 0 | x[z-1 & MASK] |= x[z]; |
401 | 0 | } |
402 | 0 | x[a] = carry; |
403 | 0 | } |
404 | 0 | } |
405 | | |
406 | | /* Downscale until exactly number of bits are left of radix point */ |
407 | 0 | for (;;) { |
408 | 0 | uint32_t carry = 0; |
409 | 0 | int sh = 1; |
410 | 0 | for (i=0; i<LD_B1B_DIG; i++) { |
411 | 0 | k = (a+i & MASK); |
412 | 0 | if (k == z || x[k] < th[i]) { |
413 | 0 | i=LD_B1B_DIG; |
414 | 0 | break; |
415 | 0 | } |
416 | 0 | if (x[a+i & MASK] > th[i]) break; |
417 | 0 | } |
418 | 0 | if (i==LD_B1B_DIG && rp==9*LD_B1B_DIG) break; |
419 | | /* FIXME: find a way to compute optimal sh */ |
420 | 0 | if (rp > 9+9*LD_B1B_DIG) sh = 9; |
421 | 0 | e2 += sh; |
422 | 0 | for (k=a; k!=z; k=(k+1 & MASK)) { |
423 | 0 | uint32_t tmp = x[k] & (1<<sh)-1; |
424 | 0 | x[k] = (x[k]>>sh) + carry; |
425 | 0 | carry = (1000000000>>sh) * tmp; |
426 | 0 | if (k==a && !x[k]) { |
427 | 0 | a = (a+1 & MASK); |
428 | 0 | i--; |
429 | 0 | rp -= 9; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | if (carry) { |
433 | 0 | if ((z+1 & MASK) != a) { |
434 | 0 | x[z] = carry; |
435 | 0 | z = (z+1 & MASK); |
436 | 0 | } else x[z-1 & MASK] |= 1; |
437 | 0 | } |
438 | 0 | } |
439 | | |
440 | | /* Assemble desired bits into floating point variable */ |
441 | 0 | for (y=i=0; i<LD_B1B_DIG; i++) { |
442 | 0 | if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0; |
443 | 0 | y = 1000000000.0L * y + x[a+i & MASK]; |
444 | 0 | } |
445 | |
|
446 | 0 | y *= sign; |
447 | | |
448 | | /* Limit precision for denormal results */ |
449 | 0 | if (bits > DBL_MANT_DIG+e2-emin) { |
450 | 0 | bits = DBL_MANT_DIG+e2-emin; |
451 | 0 | if (bits<0) bits=0; |
452 | 0 | denormal = 1; |
453 | 0 | } |
454 | | |
455 | | /* Calculate bias term to force rounding, move out lower bits */ |
456 | 0 | if (bits < DBL_MANT_DIG) { |
457 | 0 | bias = copysign(scalbn(1, 2*DBL_MANT_DIG-bits-1), y); |
458 | 0 | frac = fmod(y, scalbn(1, DBL_MANT_DIG-bits)); |
459 | 0 | y -= frac; |
460 | 0 | y += bias; |
461 | 0 | } |
462 | | |
463 | | /* Process tail of decimal input so it can affect rounding */ |
464 | 0 | if ((a+i & MASK) != z) { |
465 | 0 | uint32_t t = x[a+i & MASK]; |
466 | 0 | if (t < 500000000 && (t || (a+i+1 & MASK) != z)) |
467 | 0 | frac += 0.25*sign; |
468 | 0 | else if (t > 500000000) |
469 | 0 | frac += 0.75*sign; |
470 | 0 | else if (t == 500000000) { |
471 | 0 | if ((a+i+1 & MASK) == z) |
472 | 0 | frac += 0.5*sign; |
473 | 0 | else |
474 | 0 | frac += 0.75*sign; |
475 | 0 | } |
476 | 0 | if (DBL_MANT_DIG-bits >= 2 && !fmod(frac, 1)) |
477 | 0 | frac++; |
478 | 0 | } |
479 | |
|
480 | 0 | y += frac; |
481 | 0 | y -= bias; |
482 | |
|
483 | 0 | if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) { |
484 | 0 | if (fabs(y) >= pow(2, DBL_MANT_DIG)) { |
485 | 0 | if (denormal && bits==DBL_MANT_DIG+e2-emin) |
486 | 0 | denormal = 0; |
487 | 0 | y *= 0.5; |
488 | 0 | e2++; |
489 | 0 | } |
490 | 0 | if (e2+DBL_MANT_DIG>emax || (denormal && frac)) |
491 | 0 | errno = ERANGE; |
492 | 0 | } |
493 | |
|
494 | 0 | return scalbn(y, e2); |
495 | 0 | } |
496 | | |
497 | | static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok) |
498 | 0 | { |
499 | 0 | uint32_t x = 0; |
500 | 0 | double y = 0; |
501 | 0 | double scale = 1; |
502 | 0 | double bias = 0; |
503 | 0 | int gottail = 0, gotrad = 0, gotdig = 0; |
504 | 0 | long long rp = 0; |
505 | 0 | long long dc = 0; |
506 | 0 | long long e2 = 0; |
507 | 0 | int d; |
508 | 0 | int c; |
509 | |
|
510 | 0 | c = shgetc(f); |
511 | | |
512 | | /* Skip leading zeros */ |
513 | 0 | for (; c=='0'; c = shgetc(f)) |
514 | 0 | gotdig = 1; |
515 | |
|
516 | 0 | if (c=='.') { |
517 | 0 | gotrad = 1; |
518 | 0 | c = shgetc(f); |
519 | | /* Count zeros after the radix point before significand */ |
520 | 0 | for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1; |
521 | 0 | } |
522 | |
|
523 | 0 | for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) { |
524 | 0 | if (c=='.') { |
525 | 0 | if (gotrad) break; |
526 | 0 | rp = dc; |
527 | 0 | gotrad = 1; |
528 | 0 | } else { |
529 | 0 | gotdig = 1; |
530 | 0 | if (c > '9') d = (c|32)+10-'a'; |
531 | 0 | else d = c-'0'; |
532 | 0 | if (dc<8) { |
533 | 0 | x = x*16 + d; |
534 | 0 | } else if (dc < DBL_MANT_DIG/4+1) { |
535 | 0 | y += d*(scale/=16); |
536 | 0 | } else if (d && !gottail) { |
537 | 0 | y += 0.5*scale; |
538 | 0 | gottail = 1; |
539 | 0 | } |
540 | 0 | dc++; |
541 | 0 | } |
542 | 0 | } |
543 | 0 | if (!gotdig) { |
544 | 0 | shunget(f); |
545 | 0 | if (pok) { |
546 | 0 | shunget(f); |
547 | 0 | if (gotrad) shunget(f); |
548 | 0 | } else { |
549 | 0 | shlim(f, 0); |
550 | 0 | } |
551 | 0 | return sign * 0.0; |
552 | 0 | } |
553 | 0 | if (!gotrad) rp = dc; |
554 | 0 | while (dc<8) x *= 16, dc++; |
555 | 0 | if ((c|32)=='p') { |
556 | 0 | e2 = scanexp(f, pok); |
557 | 0 | if (e2 == LLONG_MIN) { |
558 | 0 | if (pok) { |
559 | 0 | shunget(f); |
560 | 0 | } else { |
561 | 0 | shlim(f, 0); |
562 | 0 | return 0; |
563 | 0 | } |
564 | 0 | e2 = 0; |
565 | 0 | } |
566 | 0 | } else { |
567 | 0 | shunget(f); |
568 | 0 | } |
569 | 0 | e2 += 4*rp - 32; |
570 | |
|
571 | 0 | if (!x) return sign * 0.0; |
572 | 0 | if (e2 > -emin) { |
573 | 0 | errno = ERANGE; |
574 | 0 | return sign * DBL_MAX * DBL_MAX; |
575 | 0 | } |
576 | 0 | if (e2 < emin-2*DBL_MANT_DIG) { |
577 | 0 | errno = ERANGE; |
578 | 0 | return sign * DBL_MIN * DBL_MIN; |
579 | 0 | } |
580 | | |
581 | 0 | while (x < 0x80000000) { |
582 | 0 | if (y>=0.5) { |
583 | 0 | x += x + 1; |
584 | 0 | y += y - 1; |
585 | 0 | } else { |
586 | 0 | x += x; |
587 | 0 | y += y; |
588 | 0 | } |
589 | 0 | e2--; |
590 | 0 | } |
591 | |
|
592 | 0 | if (bits > 32+e2-emin) { |
593 | 0 | bits = 32+e2-emin; |
594 | 0 | if (bits<0) bits=0; |
595 | 0 | } |
596 | |
|
597 | 0 | if (bits < DBL_MANT_DIG) |
598 | 0 | bias = copysign(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign); |
599 | |
|
600 | 0 | if (bits<32 && y && !(x&1)) x++, y=0; |
601 | |
|
602 | 0 | y = bias + sign*(double)x + sign*y; |
603 | 0 | y -= bias; |
604 | |
|
605 | 0 | if (!y) errno = ERANGE; |
606 | |
|
607 | 0 | return scalbn(y, e2); |
608 | 0 | } |
609 | | |
610 | | static double fffloatscan(FFFILE *f, int prec, int pok) |
611 | 0 | { |
612 | 0 | int sign = 1; |
613 | 0 | size_t i; |
614 | 0 | int bits; |
615 | 0 | int emin; |
616 | 0 | int c; |
617 | |
|
618 | 0 | switch (prec) { |
619 | 0 | case 0: |
620 | 0 | bits = FLT_MANT_DIG; |
621 | 0 | emin = FLT_MIN_EXP-bits; |
622 | 0 | break; |
623 | 0 | case 1: |
624 | 0 | bits = DBL_MANT_DIG; |
625 | 0 | emin = DBL_MIN_EXP-bits; |
626 | 0 | break; |
627 | 0 | case 2: |
628 | 0 | bits = DBL_MANT_DIG; |
629 | 0 | emin = DBL_MIN_EXP-bits; |
630 | 0 | break; |
631 | 0 | default: |
632 | 0 | return 0; |
633 | 0 | } |
634 | | |
635 | 0 | while (av_isspace((c = shgetc(f)))); |
636 | |
|
637 | 0 | if (c=='+' || c=='-') { |
638 | 0 | sign -= 2*(c=='-'); |
639 | 0 | c = shgetc(f); |
640 | 0 | } |
641 | |
|
642 | 0 | for (i=0; i<8 && (c|32)=="infinity"[i]; i++) |
643 | 0 | if (i<7) c = shgetc(f); |
644 | 0 | if (i==3 || i==8 || (i>3 && pok)) { |
645 | 0 | if (i!=8) { |
646 | 0 | shunget(f); |
647 | 0 | if (pok) for (; i>3; i--) shunget(f); |
648 | 0 | } |
649 | 0 | return sign * INFINITY; |
650 | 0 | } |
651 | 0 | if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++) |
652 | 0 | if (i<2) c = shgetc(f); |
653 | 0 | if (i==3) { |
654 | 0 | if (shgetc(f) != '(') { |
655 | 0 | shunget(f); |
656 | 0 | return NAN; |
657 | 0 | } |
658 | 0 | for (i=1; ; i++) { |
659 | 0 | c = shgetc(f); |
660 | 0 | if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_') |
661 | 0 | continue; |
662 | 0 | if (c==')') return NAN; |
663 | 0 | shunget(f); |
664 | 0 | if (!pok) { |
665 | 0 | errno = EINVAL; |
666 | 0 | shlim(f, 0); |
667 | 0 | return 0; |
668 | 0 | } |
669 | 0 | while (i--) shunget(f); |
670 | 0 | return NAN; |
671 | 0 | } |
672 | 0 | } |
673 | | |
674 | 0 | if (i) { |
675 | 0 | shunget(f); |
676 | 0 | errno = EINVAL; |
677 | 0 | shlim(f, 0); |
678 | 0 | return 0; |
679 | 0 | } |
680 | | |
681 | 0 | if (c=='0') { |
682 | 0 | c = shgetc(f); |
683 | 0 | if ((c|32) == 'x') |
684 | 0 | return hexfloat(f, bits, emin, sign, pok); |
685 | 0 | shunget(f); |
686 | 0 | c = '0'; |
687 | 0 | } |
688 | | |
689 | 0 | return decfloat(f, c, bits, emin, sign, pok); |
690 | 0 | } |
691 | | |
692 | | static void *arg_n(va_list ap, unsigned int n) |
693 | 0 | { |
694 | 0 | void *p; |
695 | 0 | unsigned int i; |
696 | 0 | va_list ap2; |
697 | 0 | va_copy(ap2, ap); |
698 | 0 | for (i=n; i>1; i--) va_arg(ap2, void *); |
699 | 0 | p = va_arg(ap2, void *); |
700 | 0 | va_end(ap2); |
701 | 0 | return p; |
702 | 0 | } |
703 | | |
704 | | static void store_int(void *dest, int size, unsigned long long i) |
705 | 0 | { |
706 | 0 | if (!dest) return; |
707 | 0 | switch (size) { |
708 | 0 | case SIZE_hh: |
709 | 0 | *(char *)dest = i; |
710 | 0 | break; |
711 | 0 | case SIZE_h: |
712 | 0 | *(short *)dest = i; |
713 | 0 | break; |
714 | 0 | case SIZE_def: |
715 | 0 | *(int *)dest = i; |
716 | 0 | break; |
717 | 0 | case SIZE_l: |
718 | 0 | *(long *)dest = i; |
719 | 0 | break; |
720 | 0 | case SIZE_ll: |
721 | 0 | *(long long *)dest = i; |
722 | 0 | break; |
723 | 0 | } |
724 | 0 | } |
725 | | |
726 | | static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap) |
727 | 0 | { |
728 | 0 | int width; |
729 | 0 | int size; |
730 | 0 | int base; |
731 | 0 | const unsigned char *p; |
732 | 0 | int c, t; |
733 | 0 | char *s; |
734 | 0 | void *dest=NULL; |
735 | 0 | int invert; |
736 | 0 | int matches=0; |
737 | 0 | unsigned long long x; |
738 | 0 | double y; |
739 | 0 | ptrdiff_t pos = 0; |
740 | 0 | unsigned char scanset[257]; |
741 | 0 | size_t i; |
742 | |
|
743 | 0 | for (p=(const unsigned char *)fmt; *p; p++) { |
744 | |
|
745 | 0 | if (av_isspace(*p)) { |
746 | 0 | while (av_isspace(p[1])) p++; |
747 | 0 | shlim(f, 0); |
748 | 0 | while (av_isspace(shgetc(f))); |
749 | 0 | shunget(f); |
750 | 0 | pos += shcnt(f); |
751 | 0 | continue; |
752 | 0 | } |
753 | 0 | if (*p != '%' || p[1] == '%') { |
754 | 0 | shlim(f, 0); |
755 | 0 | if (*p == '%') { |
756 | 0 | p++; |
757 | 0 | while (av_isspace((c=shgetc(f)))); |
758 | 0 | } else { |
759 | 0 | c = shgetc(f); |
760 | 0 | } |
761 | 0 | if (c!=*p) { |
762 | 0 | shunget(f); |
763 | 0 | if (c<0) goto input_fail; |
764 | 0 | goto match_fail; |
765 | 0 | } |
766 | 0 | pos += shcnt(f); |
767 | 0 | continue; |
768 | 0 | } |
769 | | |
770 | 0 | p++; |
771 | 0 | if (*p=='*') { |
772 | 0 | dest = 0; p++; |
773 | 0 | } else if (av_isdigit(*p) && p[1]=='$') { |
774 | 0 | dest = arg_n(ap, *p-'0'); p+=2; |
775 | 0 | } else { |
776 | 0 | dest = va_arg(ap, void *); |
777 | 0 | } |
778 | |
|
779 | 0 | for (width=0; av_isdigit(*p); p++) { |
780 | 0 | width = 10*width + *p - '0'; |
781 | 0 | } |
782 | |
|
783 | 0 | if (*p=='m') { |
784 | 0 | s = 0; |
785 | 0 | p++; |
786 | 0 | } |
787 | |
|
788 | 0 | size = SIZE_def; |
789 | 0 | switch (*p++) { |
790 | 0 | case 'h': |
791 | 0 | if (*p == 'h') p++, size = SIZE_hh; |
792 | 0 | else size = SIZE_h; |
793 | 0 | break; |
794 | 0 | case 'l': |
795 | 0 | if (*p == 'l') p++, size = SIZE_ll; |
796 | 0 | else size = SIZE_l; |
797 | 0 | break; |
798 | 0 | case 'j': |
799 | 0 | size = SIZE_ll; |
800 | 0 | break; |
801 | 0 | case 'z': |
802 | 0 | case 't': |
803 | 0 | size = SIZE_l; |
804 | 0 | break; |
805 | 0 | case 'L': |
806 | 0 | size = SIZE_L; |
807 | 0 | break; |
808 | 0 | case 'd': case 'i': case 'o': case 'u': case 'x': |
809 | 0 | case 'a': case 'e': case 'f': case 'g': |
810 | 0 | case 'A': case 'E': case 'F': case 'G': case 'X': |
811 | 0 | case 's': case 'c': case '[': |
812 | 0 | case 'S': case 'C': |
813 | 0 | case 'p': case 'n': |
814 | 0 | p--; |
815 | 0 | break; |
816 | 0 | default: |
817 | 0 | goto fmt_fail; |
818 | 0 | } |
819 | | |
820 | 0 | t = *p; |
821 | | |
822 | | /* C or S */ |
823 | 0 | if ((t&0x2f) == 3) { |
824 | 0 | t |= 32; |
825 | 0 | size = SIZE_l; |
826 | 0 | } |
827 | |
|
828 | 0 | switch (t) { |
829 | 0 | case 'c': |
830 | 0 | if (width < 1) width = 1; |
831 | 0 | case '[': |
832 | 0 | break; |
833 | 0 | case 'n': |
834 | 0 | store_int(dest, size, pos); |
835 | | /* do not increment match count, etc! */ |
836 | 0 | continue; |
837 | 0 | default: |
838 | 0 | shlim(f, 0); |
839 | 0 | while (av_isspace(shgetc(f))); |
840 | 0 | shunget(f); |
841 | 0 | pos += shcnt(f); |
842 | 0 | } |
843 | | |
844 | 0 | shlim(f, width); |
845 | 0 | if (shgetc(f) < 0) goto input_fail; |
846 | 0 | shunget(f); |
847 | |
|
848 | 0 | switch (t) { |
849 | 0 | case 's': |
850 | 0 | case 'c': |
851 | 0 | case '[': |
852 | 0 | if (t == 'c' || t == 's') { |
853 | 0 | memset(scanset, -1, sizeof scanset); |
854 | 0 | scanset[0] = 0; |
855 | 0 | if (t == 's') { |
856 | 0 | scanset[1 + '\t'] = 0; |
857 | 0 | scanset[1 + '\n'] = 0; |
858 | 0 | scanset[1 + '\v'] = 0; |
859 | 0 | scanset[1 + '\f'] = 0; |
860 | 0 | scanset[1 + '\r'] = 0; |
861 | 0 | scanset[1 + ' ' ] = 0; |
862 | 0 | } |
863 | 0 | } else { |
864 | 0 | if (*++p == '^') p++, invert = 1; |
865 | 0 | else invert = 0; |
866 | 0 | memset(scanset, invert, sizeof scanset); |
867 | 0 | scanset[0] = 0; |
868 | 0 | if (*p == '-') p++, scanset[1+'-'] = 1-invert; |
869 | 0 | else if (*p == ']') p++, scanset[1+']'] = 1-invert; |
870 | 0 | for (; *p != ']'; p++) { |
871 | 0 | if (!*p) goto fmt_fail; |
872 | 0 | if (*p=='-' && p[1] && p[1] != ']') |
873 | 0 | for (c=p++[-1]; c<*p; c++) |
874 | 0 | scanset[1+c] = 1-invert; |
875 | 0 | scanset[1+*p] = 1-invert; |
876 | 0 | } |
877 | 0 | } |
878 | 0 | s = 0; |
879 | 0 | i = 0; |
880 | 0 | if ((s = dest)) { |
881 | 0 | while (scanset[(c=shgetc(f))+1]) |
882 | 0 | s[i++] = c; |
883 | 0 | } else { |
884 | 0 | while (scanset[(c=shgetc(f))+1]); |
885 | 0 | } |
886 | 0 | shunget(f); |
887 | 0 | if (!shcnt(f)) goto match_fail; |
888 | 0 | if (t == 'c' && shcnt(f) != width) goto match_fail; |
889 | 0 | if (t != 'c') { |
890 | 0 | if (s) s[i] = 0; |
891 | 0 | } |
892 | 0 | break; |
893 | 0 | case 'p': |
894 | 0 | case 'X': |
895 | 0 | case 'x': |
896 | 0 | base = 16; |
897 | 0 | goto int_common; |
898 | 0 | case 'o': |
899 | 0 | base = 8; |
900 | 0 | goto int_common; |
901 | 0 | case 'd': |
902 | 0 | case 'u': |
903 | 0 | base = 10; |
904 | 0 | goto int_common; |
905 | 0 | case 'i': |
906 | 0 | base = 0; |
907 | 0 | int_common: |
908 | 0 | x = ffintscan(f, base, 0, ULLONG_MAX); |
909 | 0 | if (!shcnt(f)) |
910 | 0 | goto match_fail; |
911 | 0 | if (t=='p' && dest) |
912 | 0 | *(void **)dest = (void *)(uintptr_t)x; |
913 | 0 | else |
914 | 0 | store_int(dest, size, x); |
915 | 0 | break; |
916 | 0 | case 'a': case 'A': |
917 | 0 | case 'e': case 'E': |
918 | 0 | case 'f': case 'F': |
919 | 0 | case 'g': case 'G': |
920 | 0 | y = fffloatscan(f, size, 0); |
921 | 0 | if (!shcnt(f)) |
922 | 0 | goto match_fail; |
923 | 0 | if (dest) { |
924 | 0 | switch (size) { |
925 | 0 | case SIZE_def: |
926 | 0 | *(float *)dest = y; |
927 | 0 | break; |
928 | 0 | case SIZE_l: |
929 | 0 | *(double *)dest = y; |
930 | 0 | break; |
931 | 0 | case SIZE_L: |
932 | 0 | *(double *)dest = y; |
933 | 0 | break; |
934 | 0 | } |
935 | 0 | } |
936 | 0 | break; |
937 | 0 | } |
938 | | |
939 | 0 | pos += shcnt(f); |
940 | 0 | if (dest) matches++; |
941 | 0 | } |
942 | 0 | if (0) { |
943 | 0 | fmt_fail: |
944 | 0 | input_fail: |
945 | 0 | if (!matches) matches--; |
946 | 0 | } |
947 | 0 | match_fail: |
948 | 0 | return matches; |
949 | 0 | } |
950 | | |
951 | | static int ff_vsscanf(const char *s, const char *fmt, va_list ap) |
952 | 0 | { |
953 | 0 | FFFILE f = { |
954 | 0 | .buf = (void *)s, .cookie = (void *)s, |
955 | 0 | .read = ffstring_read, |
956 | 0 | }; |
957 | |
|
958 | 0 | return ff_vfscanf(&f, fmt, ap); |
959 | 0 | } |
960 | | |
961 | | int av_sscanf(const char *string, const char *format, ...) |
962 | 0 | { |
963 | 0 | int ret; |
964 | 0 | va_list ap; |
965 | 0 | va_start(ap, format); |
966 | 0 | ret = ff_vsscanf(string, format, ap); |
967 | | va_end(ap); |
968 | 0 | return ret; |
969 | 0 | } |