2 * Copyright (c) 2005-2014 Rich Felker, et al.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
37 typedef struct FFFILE {
40 unsigned char *rpos, *rend;
44 size_t (*read)(struct FFFILE *, unsigned char *, size_t);
54 #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
56 static int fftoread(FFFILE *f)
58 f->rpos = f->rend = f->buf + f->buf_size;
62 static size_t ffstring_read(FFFILE *f, unsigned char *buf, size_t len)
64 char *src = f->cookie;
66 char *end = memchr(src, 0, k);
70 memcpy(buf, src, len);
71 f->rpos = (void *)(src+len);
72 f->rend = (void *)(src+k);
78 static int ffuflow(FFFILE *f)
81 if (!fftoread(f) && f->read(f, &c, 1)==1) return c;
85 static void ffshlim(FFFILE *f, off_t 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;
96 static int ffshgetc(FFFILE *f)
100 if (f->shlim && cnt >= f->shlim || (c=ffuflow(f)) < 0) {
101 f->shcnt = f->buf - f->rpos + cnt;
106 if (f->shlim && f->rend - f->rpos > f->shlim - cnt)
107 f->shend = f->rpos + (f->shlim - cnt);
110 f->shcnt = f->buf - f->rpos + cnt;
111 if (f->rpos[-1] != c) f->rpos[-1] = c;
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)
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,
138 static unsigned long long ffintscan(FFFILE *f, unsigned base, int pok, unsigned long long lim)
140 const unsigned char *val = table+1;
143 unsigned long long y;
144 if (base > 36 || base == 1) {
148 while (av_isspace((c=shgetc(f))));
149 if (c=='+' || c=='-') {
153 if ((base == 0 || base == 16) && c=='0') {
164 } else if (base == 0) {
168 if (base == 0) base = 10;
169 if (val[c] >= base) {
177 for (x=0; c-'0'<10U && x<=UINT_MAX/10-1; c=shgetc(f))
179 for (y=x; c-'0'<10U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(c-'0'); c=shgetc(f))
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))
186 for (y=x; val[c]<base && y<=ULLONG_MAX>>bs; c=shgetc(f))
189 for (x=0; val[c]<base && x<=UINT_MAX/36-1; c=shgetc(f))
191 for (y=x; val[c]<base && y<=ULLONG_MAX/base && base*y<=ULLONG_MAX-val[c]; c=shgetc(f))
195 for (; val[c]<base; c=shgetc(f));
203 if (!(lim&1) && !neg) {
214 static long long scanexp(FFFILE *f, int pok)
222 if (c=='+' || c=='-') {
225 if (c-'0'>=10U && pok) shunget(f);
231 for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
233 for (y=x; c-'0'<10U && y<LLONG_MAX/100; c = shgetc(f))
235 for (; c-'0'<10U; c = shgetc(f));
241 #define LD_B1B_MAX 9007199, 254740991
243 #define MASK (KMAX-1)
244 #define CONCAT2(x,y) x ## y
245 #define CONCAT(x,y) CONCAT2(x,y)
247 static double decfloat(FFFILE *f, int c, int bits, int emin, int sign, int pok)
250 static const uint32_t th[] = { LD_B1B_MAX };
252 long long lrp=0, dc=0;
255 int gotdig = 0, gotrad = 0;
258 int emax = -emin-bits+3;
263 static const int p10s[] = { 10, 100, 1000, 10000,
264 100000, 1000000, 10000000, 100000000 };
269 /* Don't let leading zeros consume buffer space */
270 for (; c=='0'; c = shgetc(f)) gotdig=1;
273 for (c = shgetc(f); c=='0'; c = shgetc(f)) gotdig=1, lrp--;
277 for (; c-'0'<10U || c=='.'; c = shgetc(f)) {
282 } else if (k < KMAX-3) {
284 if (c!='0') lnz = dc;
285 if (j) x[k] = x[k]*10 + c-'0';
302 if (gotdig && (c|32)=='e') {
303 e10 = scanexp(f, pok);
304 if (e10 == LLONG_MIN) {
323 /* Handle zero specially to avoid nasty special cases later */
324 if (!x[0]) return sign * 0.0;
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];
331 return sign * DBL_MAX * DBL_MAX;
333 if (lrp < emin-2*DBL_MANT_DIG) {
335 return sign * DBL_MIN * DBL_MIN;
338 /* Align incomplete final B1B digit */
340 for (; j<9; j++) x[k]*=10;
350 /* Optimize small to mid-size integers (even in exp. notation) */
351 if (lnz<9 && lnz<=rp && rp < 18) {
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];
360 /* Drop trailing zeros */
361 for (; !x[z-1]; z--);
363 /* Align radix point to B1B digit boundary */
365 int rpm9 = rp>=0 ? rp%9 : rp%9+9;
366 int p10 = p10s[8-rpm9];
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;
377 if (carry) x[z++] = carry;
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])) {
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;
394 if (k==(z-1 & MASK) && k!=a && !x[k]) z = k;
402 x[z-1 & MASK] |= x[z];
408 /* Downscale until exactly number of bits are left of radix point */
412 for (i=0; i<LD_B1B_DIG; i++) {
414 if (k == z || x[k] < th[i]) {
418 if (x[a+i & MASK] > th[i]) break;
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;
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;
435 if ((z+1 & MASK) != a) {
438 } else x[z-1 & MASK] |= 1;
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];
450 /* Limit precision for denormal results */
451 if (bits > DBL_MANT_DIG+e2-emin) {
452 bits = DBL_MANT_DIG+e2-emin;
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));
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))
470 else if (t > 500000000)
472 else if (t == 500000000) {
473 if ((a+i+1 & MASK) == z)
478 if (DBL_MANT_DIG-bits >= 2 && !fmodl(frac, 1))
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)
492 if (e2+DBL_MANT_DIG>emax || (denormal && frac))
496 return scalbnl(y, e2);
499 static double hexfloat(FFFILE *f, int bits, int emin, int sign, int pok)
505 int gottail = 0, gotrad = 0, gotdig = 0;
514 /* Skip leading zeros */
515 for (; c=='0'; c = shgetc(f))
521 /* Count zeros after the radix point before significand */
522 for (rp=0; c=='0'; c = shgetc(f), rp--) gotdig = 1;
525 for (; c-'0'<10U || (c|32)-'a'<6U || c=='.'; c = shgetc(f)) {
532 if (c > '9') d = (c|32)+10-'a';
536 } else if (dc < DBL_MANT_DIG/4+1) {
538 } else if (d && !gottail) {
549 if (gotrad) shunget(f);
555 if (!gotrad) rp = dc;
556 while (dc<8) x *= 16, dc++;
558 e2 = scanexp(f, pok);
559 if (e2 == LLONG_MIN) {
573 if (!x) return sign * 0.0;
576 return sign * DBL_MAX * DBL_MAX;
578 if (e2 < emin-2*DBL_MANT_DIG) {
580 return sign * DBL_MIN * DBL_MIN;
583 while (x < 0x80000000) {
594 if (bits > 32+e2-emin) {
599 if (bits < DBL_MANT_DIG)
600 bias = copysignl(scalbn(1, 32+DBL_MANT_DIG-bits-1), sign);
602 if (bits<32 && y && !(x&1)) x++, y=0;
604 y = bias + sign*(double)x + sign*y;
607 if (!y) errno = ERANGE;
609 return scalbnl(y, e2);
612 static double fffloatscan(FFFILE *f, int prec, int pok)
623 emin = FLT_MIN_EXP-bits;
627 emin = DBL_MIN_EXP-bits;
631 emin = DBL_MIN_EXP-bits;
637 while (av_isspace((c = shgetc(f))));
639 if (c=='+' || c=='-') {
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)) {
649 if (pok) for (; i>3; i--) shunget(f);
651 return sign * INFINITY;
653 if (!i) for (i=0; i<3 && (c|32)=="nan"[i]; i++)
654 if (i<2) c = shgetc(f);
656 if (shgetc(f) != '(') {
662 if (c-'0'<10U || c-'A'<26U || c-'a'<26U || c=='_')
664 if (c==')') return NAN;
671 while (i--) shunget(f);
687 return hexfloat(f, bits, emin, sign, pok);
692 return decfloat(f, c, bits, emin, sign, pok);
695 static void *arg_n(va_list ap, unsigned int n)
701 for (i=n; i>1; i--) va_arg(ap2, void *);
702 p = va_arg(ap2, void *);
707 static void store_int(void *dest, int size, unsigned long long i)
724 *(long long *)dest = i;
729 static int ff_vfscanf(FFFILE *f, const char *fmt, va_list ap)
734 const unsigned char *p;
740 unsigned long long x;
743 unsigned char scanset[257];
746 for (p=(const unsigned char *)fmt; *p; p++) {
748 if (av_isspace(*p)) {
749 while (av_isspace(p[1])) p++;
751 while (av_isspace(shgetc(f)));
756 if (*p != '%' || p[1] == '%') {
760 while (av_isspace((c=shgetc(f))));
766 if (c<0) goto input_fail;
776 } else if (av_isdigit(*p) && p[1]=='$') {
777 dest = arg_n(ap, *p-'0'); p+=2;
779 dest = va_arg(ap, void *);
782 for (width=0; av_isdigit(*p); p++) {
783 width = 10*width + *p - '0';
794 if (*p == 'h') p++, size = SIZE_hh;
798 if (*p == 'l') p++, size = SIZE_ll;
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 '[':
833 if (width < 1) width = 1;
837 store_int(dest, size, pos);
838 /* do not increment match count, etc! */
842 while (av_isspace(shgetc(f)));
848 if (shgetc(f) < 0) goto input_fail;
855 if (t == 'c' || t == 's') {
856 memset(scanset, -1, sizeof scanset);
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;
867 if (*++p == '^') p++, invert = 1;
869 memset(scanset, invert, sizeof scanset);
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;
884 while (scanset[(c=shgetc(f))+1])
887 while (scanset[(c=shgetc(f))+1]);
890 if (!shcnt(f)) goto match_fail;
891 if (t == 'c' && shcnt(f) != width) goto match_fail;
911 x = ffintscan(f, base, 0, ULLONG_MAX);
915 *(void **)dest = (void *)(uintptr_t)x;
917 store_int(dest, size, x);
923 y = fffloatscan(f, size, 0);
948 if (!matches) matches--;
954 static int ff_vsscanf(const char *s, const char *fmt, va_list ap)
957 .buf = (void *)s, .cookie = (void *)s,
958 .read = ffstring_read,
961 return ff_vfscanf(&f, fmt, ap);
964 int av_sscanf(const char *string, const char *format, ...)
968 va_start(ap, format);
969 ret = ff_vsscanf(string, format, ap);