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>
29
30 #include "config.h"
34
42 size_t (*
read)(
struct FFFILE *,
unsigned char *, size_t);
44
51
52 #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
53
55 {
56 f->rpos =
f->rend =
f->buf +
f->buf_size;
57 return 0;
58 }
59
61 {
62 char *
src =
f->cookie;
64 char *end = memchr(
src, 0, k);
65
70 f->rend = (
void *)(
src+k);
72
74 }
75
77 {
80 return EOF;
81 }
82
84 {
86 f->shcnt =
f->buf -
f->rpos;
87 /* If lim is nonzero, rend must be a valid pointer. */
88 if (lim &&
f->rend -
f->rpos > lim)
89 f->shend =
f->rpos + lim;
90 else
92 }
93
95 {
98 if (
f->shlim && cnt >=
f->shlim || (
c=
ffuflow(
f)) < 0) {
99 f->shcnt =
f->buf -
f->rpos + cnt;
101 return EOF;
102 }
103 cnt++;
104 if (
f->shlim &&
f->rend -
f->rpos >
f->shlim - cnt)
105 f->shend =
f->rpos + (
f->shlim - cnt);
106 else
108 f->shcnt =
f->buf -
f->rpos + cnt;
109 if (
f->rpos[-1] !=
c)
f->rpos[-1] =
c;
111 }
112
113 #define shlim(f, lim) ffshlim((f), (lim))
114 #define shgetc(f) (((f)->rpos < (f)->shend) ? *(f)->rpos++ : ffshgetc(f))
115 #define shunget(f) ((f)->shend ? (void)(f)->rpos-- : (void)0)
116
117 static const unsigned char table[] = { -1,
118 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
122 -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
123 25,26,27,28,29,30,31,32,33,34,35,-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,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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 };
135
137 {
140 unsigned x;
141 unsigned long long y;
143 errno = EINVAL;
144 return 0;
145 }
147 if (
c==
'+' ||
c==
'-') {
150 }
151 if ((
base == 0 ||
base == 16) &&
c==
'0') {
159 return 0;
160 }
162 }
else if (
base == 0) {
164 }
165 } else {
170 errno = EINVAL;
171 return 0;
172 }
173 }
175 for (x=0;
c-
'0'<10
U && x<=UINT_MAX/10-1;
c=
shgetc(
f))
177 for (y=x;
c-
'0'<10
U && y<=ULLONG_MAX/10 && 10*y<=ULLONG_MAX-(
c-
'0');
c=
shgetc(
f))
179 if (
c-
'0'>=10
U)
goto done;
181 int bs =
"0円1円2円4円7円3円6円5円"[(0x17*
base)>>5&7];
186 } else {
191 }
194 errno = ERANGE;
195 y = lim;
196 if (lim&1) neg = 0;
197 }
198 done:
200 if (y>=lim) {
201 if (!(lim&1) && !neg) {
202 errno = ERANGE;
203 return lim-1;
204 } else if (y>lim) {
205 errno = ERANGE;
206 return lim;
207 }
208 }
209 return (y^neg)-neg;
210 }
211
213 {
215 int x;
216 long long y;
217 int neg = 0;
218
220 if (
c==
'+' ||
c==
'-') {
224 }
227 return LLONG_MIN;
228 }
229 for (x=0;
c-
'0'<10
U && x<INT_MAX/10;
c =
shgetc(
f))
231 for (y=x;
c-
'0'<10
U && y<LLONG_MAX/100;
c =
shgetc(
f))
235 return neg ? -y : y;
236 }
237
239 #define LD_B1B_MAX 9007199, 254740991
241 #define MASK (KMAX-1)
242
244 {
248 long long lrp=0,
dc=0;
249 long long e10=0;
250 int lnz = 0;
251 int gotdig = 0, gotrad = 0;
252 int rp;
253 int e2;
254 int emax = -emin-
bits+3;
255 int denormal = 0;
256 double y;
257 double frac=0;
258 double bias=0;
259 static const int p10s[] = { 10, 100, 1000, 10000,
260 100000, 1000000, 10000000, 100000000 };
261
262 j=0;
263 k=0;
264
265 /* Don't let leading zeros consume buffer space */
268 gotrad = 1;
270 }
271
272 x[0] = 0;
275 if (gotrad) break;
276 gotrad = 1;
278 }
else if (k <
KMAX-3) {
280 if (
c!=
'0') lnz =
dc;
281 if (j) x[k] = x[k]*10 +
c-
'0';
283 if (++j==9) {
284 k++;
285 j=0;
286 }
287 gotdig=1;
288 } else {
293 }
294 }
295 }
297
298 if (gotdig && (
c|32)==
'e') {
300 if (e10 == LLONG_MIN) {
301 if (pok) {
303 } else {
305 return 0;
306 }
307 e10 = 0;
308 }
309 lrp += e10;
312 }
313 if (!gotdig) {
314 errno = EINVAL;
316 return 0;
317 }
318
319 /* Handle zero specially to avoid nasty special cases later */
320 if (!x[0]) return sign * 0.0;
321
322 /* Optimize small integers (w/no exponent) and over/under-flow */
324 return sign * (double)x[0];
325 if (lrp > -emin/2) {
326 errno = ERANGE;
327 return sign * DBL_MAX * DBL_MAX;
328 }
329 if (lrp < emin-2*DBL_MANT_DIG) {
330 errno = ERANGE;
331 return sign * DBL_MIN * DBL_MIN;
332 }
333
334 /* Align incomplete final B1B digit */
335 if (j) {
336 for (; j<9; j++) x[k]*=10;
337 k++;
338 j=0;
339 }
340
342 z = k;
343 e2 = 0;
344 rp = lrp;
345
346 /* Optimize small to mid-size integers (even in exp. notation) */
347 if (lnz<9 && lnz<=rp && rp < 18) {
348 int bitlim;
349 if (rp == 9) return sign * (double)x[0];
350 if (rp < 9) return sign * (double)x[0] / p10s[8-rp];
352 if (bitlim>30 || x[0]>>bitlim==0)
353 return sign * (double)x[0] * p10s[rp-10];
354 }
355
356 /* Drop trailing zeros */
357 for (; !x[z-1]; z--);
358
359 /* Align radix point to B1B digit boundary */
360 if (rp % 9) {
361 int rpm9 = rp>=0 ? rp%9 : rp%9+9;
362 int p10 = p10s[8-rpm9];
363 uint32_t carry = 0;
364 for (k=
a; k!=z; k++) {
365 uint32_t
tmp = x[k] % p10;
366 x[k] = x[k]/p10 + carry;
367 carry = 1000000000/p10 *
tmp;
370 rp -= 9;
371 }
372 }
373 if (carry) x[z++] = carry;
374 rp += 9-rpm9;
375 }
376
377 /* Upscale until desired number of bits are left of radix point */
379 uint32_t carry = 0;
380 e2 -= 29;
381 for (k=(z-1 &
MASK); ; k=(k-1 &
MASK)) {
382 uint64_t
tmp = ((uint64_t)x[k] << 29) + carry;
383 if (
tmp > 1000000000) {
384 carry =
tmp / 1000000000;
385 x[k] =
tmp % 1000000000;
386 } else {
387 carry = 0;
389 }
390 if (k==(z-1 &
MASK) && k!=
a && !x[k]) z = k;
392 }
393 if (carry) {
394 rp += 9;
398 x[z-1 &
MASK] |= x[z];
399 }
401 }
402 }
403
404 /* Downscale until exactly number of bits are left of radix point */
405 for (;;) {
406 uint32_t carry = 0;
407 int sh = 1;
410 if (k == z || x[k] <
th[
i]) {
412 break;
413 }
415 }
417 /* FIXME: find a way to compute optimal sh */
419 e2 += sh;
420 for (k=
a; k!=z; k=(k+1 &
MASK)) {
421 uint32_t
tmp = x[k] & (1<<sh)-1;
422 x[k] = (x[k]>>sh) + carry;
423 carry = (1000000000>>sh) *
tmp;
427 rp -= 9;
428 }
429 }
430 if (carry) {
431 if ((z+1 &
MASK) !=
a) {
432 x[z] = carry;
434 }
else x[z-1 &
MASK] |= 1;
435 }
436 }
437
438 /* Assemble desired bits into floating point variable */
440 if ((
a+
i &
MASK)==z) x[(z=(z+1 &
MASK))-1] = 0;
441 y = 1000000000.0L * y + x[
a+
i &
MASK];
442 }
443
444 y *= sign;
445
446 /* Limit precision for denormal results */
447 if (
bits > DBL_MANT_DIG+e2-emin) {
448 bits = DBL_MANT_DIG+e2-emin;
450 denormal = 1;
451 }
452
453 /* Calculate bias term to force rounding, move out lower bits */
454 if (
bits < DBL_MANT_DIG) {
456 frac = fmod(y, scalbn(1, DBL_MANT_DIG-
bits));
457 y -= frac;
458 y += bias;
459 }
460
461 /* Process tail of decimal input so it can affect rounding */
463 uint32_t t = x[
a+
i &
MASK];
464 if (t < 500000000 && (t || (
a+
i+1 &
MASK) != z))
465 frac += 0.25*sign;
466 else if (t > 500000000)
467 frac += 0.75*sign;
468 else if (t == 500000000) {
470 frac += 0.5*sign;
471 else
472 frac += 0.75*sign;
473 }
474 if (DBL_MANT_DIG-
bits >= 2 && !fmod(frac, 1))
475 frac++;
476 }
477
478 y += frac;
479 y -= bias;
480
481 if ((e2+DBL_MANT_DIG & INT_MAX) > emax-5) {
482 if (
fabs(y) >= pow(2, DBL_MANT_DIG)) {
483 if (denormal &&
bits==DBL_MANT_DIG+e2-emin)
484 denormal = 0;
485 y *= 0.5;
486 e2++;
487 }
488 if (e2+DBL_MANT_DIG>emax || (denormal && frac))
489 errno = ERANGE;
490 }
491
492 return scalbn(y, e2);
493 }
494
496 {
497 uint32_t x = 0;
498 double y = 0;
500 double bias = 0;
501 int gottail = 0, gotrad = 0, gotdig = 0;
502 long long rp = 0;
504 long long e2 = 0;
507
509
510 /* Skip leading zeros */
512 gotdig = 1;
513
515 gotrad = 1;
517 /* Count zeros after the radix point before significand */
518 for (rp=0;
c==
'0';
c =
shgetc(
f), rp--) gotdig = 1;
519 }
520
521 for (;
c-
'0'<10
U || (
c|32)-
'a'<6
U ||
c==
'.';
c =
shgetc(
f)) {
523 if (gotrad) break;
525 gotrad = 1;
526 } else {
527 gotdig = 1;
528 if (
c >
'9')
d = (
c|32)+10-
'a';
532 }
else if (
dc < DBL_MANT_DIG/4+1) {
534 }
else if (
d && !gottail) {
536 gottail = 1;
537 }
539 }
540 }
541 if (!gotdig) {
543 if (pok) {
546 } else {
548 }
549 return sign * 0.0;
550 }
551 if (!gotrad) rp =
dc;
552 while (
dc<8) x *= 16,
dc++;
555 if (e2 == LLONG_MIN) {
556 if (pok) {
558 } else {
560 return 0;
561 }
562 e2 = 0;
563 }
564 } else {
566 }
567 e2 += 4*rp - 32;
568
569 if (!x) return sign * 0.0;
570 if (e2 > -emin) {
571 errno = ERANGE;
572 return sign * DBL_MAX * DBL_MAX;
573 }
574 if (e2 < emin-2*DBL_MANT_DIG) {
575 errno = ERANGE;
576 return sign * DBL_MIN * DBL_MIN;
577 }
578
579 while (x < 0x80000000) {
580 if (y>=0.5) {
581 x += x + 1;
582 y += y - 1;
583 } else {
584 x += x;
585 y += y;
586 }
587 e2--;
588 }
589
590 if (
bits > 32+e2-emin) {
593 }
594
595 if (
bits < DBL_MANT_DIG)
596 bias =
copysign(scalbn(1, 32+DBL_MANT_DIG-
bits-1), sign);
597
598 if (
bits<32 && y && !(x&1)) x++, y=0;
599
600 y = bias + sign*(double)x + sign*y;
601 y -= bias;
602
603 if (!y) errno = ERANGE;
604
605 return scalbn(y, e2);
606 }
607
609 {
610 int sign = 1;
613 int emin;
615
616 switch (prec) {
617 case 0:
619 emin = FLT_MIN_EXP-
bits;
620 break;
621 case 1:
623 emin = DBL_MIN_EXP-
bits;
624 break;
625 case 2:
627 emin = DBL_MIN_EXP-
bits;
628 break;
629 default:
630 return 0;
631 }
632
634
635 if (
c==
'+' ||
c==
'-') {
638 }
639
640 for (
i=0;
i<8 && (
c|32)==
"infinity"[
i];
i++)
642 if (
i==3 ||
i==8 || (
i>3 && pok)) {
646 }
648 }
649 if (!
i)
for (
i=0;
i<3 && (
c|32)==
"nan"[
i];
i++)
655 }
658 if (
c-
'0'<10
U ||
c-
'A'<26
U ||
c-
'a'<26
U ||
c==
'_')
659 continue;
660 if (
c==
')')
return NAN;
662 if (!pok) {
663 errno = EINVAL;
665 return 0;
666 }
669 }
671 }
672
675 errno = EINVAL;
677 return 0;
678 }
679
686 }
687
689 }
690
691 static void *
arg_n(va_list ap,
unsigned int n)
692 {
693 void *p;
695 va_list ap2;
697 for (
i=n;
i>1;
i--) va_arg(ap2,
void *);
698 p = va_arg(ap2, void *);
699 va_end(ap2);
700 return p;
701 }
702
704 {
705 if (!dest) return;
709 break;
712 break;
715 break;
718 break;
720 *(
long long *)dest =
i;
721 break;
722 }
723 }
724
726 {
730 const unsigned char *p;
735 int matches=0;
736 unsigned long long x;
737 double y;
739 unsigned char scanset[257];
741
742 for (p=(const unsigned char *)fmt; *p; p++) {
743
750 continue;
751 }
752 if (*p != '%' || p[1] == '%') {
754 if (*p == '%') {
755 p++;
757 } else {
759 }
762 if (
c<0)
goto input_fail;
763 goto match_fail;
764 }
766 continue;
767 }
768
769 p++;
770 if (*p=='*') {
771 dest = 0; p++;
773 dest =
arg_n(ap, *p-
'0'); p+=2;
774 } else {
775 dest = va_arg(ap, void *);
776 }
777
780 }
781
782 if (*p=='m') {
784 p++;
785 }
786
788 switch (*p++) {
789 case 'h':
792 break;
793 case 'l':
796 break;
797 case 'j':
799 break;
800 case 'z':
801 case 't':
803 break;
804 case 'L':
806 break;
807 case 'd': case 'i': case 'o': case 'u': case 'x':
808 case 'a': case 'e': case 'f': case 'g':
809 case 'A': case 'E': case 'F': case 'G': case 'X':
810 case 's': case 'c': case '[':
811 case 'S': case 'C':
812 case 'p': case 'n':
813 p--;
814 break;
815 default:
816 goto fmt_fail;
817 }
818
819 t = *p;
820
821 /* C or S */
822 if ((t&0x2f) == 3) {
823 t |= 32;
825 }
826
827 switch (t) {
828 case 'c':
830 case '[':
831 break;
832 case 'n':
834 /* do not increment match count, etc! */
835 continue;
836 default:
841 }
842
844 if (
shgetc(
f) < 0)
goto input_fail;
846
847 switch (t) {
848 case 's':
849 case 'c':
850 case '[':
851 if (t == 'c' || t == 's') {
852 memset(scanset, -1, sizeof scanset);
853 scanset[0] = 0;
854 if (t == 's') {
855 scanset[1 + '\t'] = 0;
856 scanset[1 + '\n'] = 0;
857 scanset[1 + '\v'] = 0;
858 scanset[1 + '\f'] = 0;
859 scanset[1 + '\r'] = 0;
860 scanset[1 + ' ' ] = 0;
861 }
862 } else {
863 if (*++p ==
'^') p++,
invert = 1;
865 memset(scanset,
invert,
sizeof scanset);
866 scanset[0] = 0;
867 if (*p ==
'-') p++, scanset[1+
'-'] = 1-
invert;
868 else if (*p ==
']') p++, scanset[1+
']'] = 1-
invert;
869 for (; *p != ']'; p++) {
870 if (!*p) goto fmt_fail;
871 if (*p=='-' && p[1] && p[1] != ']')
872 for (
c=p++[-1];
c<*p;
c++)
875 }
876 }
882 } else {
884 }
886 if (!
shcnt(
f))
goto match_fail;
887 if (t ==
'c' &&
shcnt(
f) !=
width)
goto match_fail;
888 if (t != 'c') {
890 }
891 break;
892 case 'p':
893 case 'X':
894 case 'x':
896 goto int_common;
897 case 'o':
899 goto int_common;
900 case 'd':
901 case 'u':
903 goto int_common;
904 case 'i':
906 int_common:
909 goto match_fail;
910 if (t=='p' && dest)
911 *(void **)dest = (void *)(uintptr_t)x;
912 else
914 break;
915 case 'a': case 'A':
916 case 'e': case 'E':
917 case 'f': case 'F':
918 case 'g': case 'G':
921 goto match_fail;
922 if (dest) {
925 *(float *)dest = y;
926 break;
928 *(double *)dest = y;
929 break;
931 *(double *)dest = y;
932 break;
933 }
934 }
935 break;
936 }
937
939 if (dest) matches++;
940 }
941 if (0) {
942 fmt_fail:
943 input_fail:
944 if (!matches) matches--;
945 }
946 match_fail:
947 return matches;
948 }
949
951 {
953 .buf = (
void *)
s, .
cookie = (
void *)
s,
955 };
956
958 }
959
961 {
963 va_list ap;
966 va_end(ap);
968 }