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