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