blob: c48dd36894fd8660e44941b3559c810001cbb057 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $OpenBSD: vfscanf.c,v 1.21 2006/01/13 21:33:28 millert Exp $ */
2/*-
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <ctype.h>
35#include <inttypes.h>
36#include <stdarg.h>
37#include <stddef.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include "local.h"
41
42#ifdef FLOATING_POINT
43#include "floatio.h"
44#endif
45
46#define BUF 513 /* Maximum length of numeric string. */
47
48/*
49 * Flags used during conversion.
50 */
51#define LONG 0x00001 /* l: long or double */
52#define LONGDBL 0x00002 /* L: long double; unimplemented */
53#define SHORT 0x00004 /* h: short */
54#define SHORTSHORT 0x00008 /* hh: 8 bit integer */
55#define LLONG 0x00010 /* ll: long long (+ deprecated q: quad) */
56#define POINTER 0x00020 /* p: void * (as hex) */
57#define SIZEINT 0x00040 /* z: (signed) size_t */
58#define MAXINT 0x00080 /* j: intmax_t */
59#define PTRINT 0x00100 /* t: ptrdiff_t */
60#define NOSKIP 0x00200 /* [ or c: do not skip blanks */
61#define SUPPRESS 0x00400 /* *: suppress assignment */
62#define UNSIGNED 0x00800 /* %[oupxX] conversions */
63
64/*
65 * The following are used in numeric conversions only:
66 * SIGNOK, HAVESIGN, NDIGITS, DPTOK, and EXPOK are for floating point;
67 * SIGNOK, HAVESIGN, NDIGITS, PFXOK, and NZDIGITS are for integral.
68 */
69#define SIGNOK 0x01000 /* +/- is (still) legal */
70#define HAVESIGN 0x02000 /* sign detected */
71#define NDIGITS 0x04000 /* no digits detected */
72
73#define DPTOK 0x08000 /* (float) decimal point is still legal */
74#define EXPOK 0x10000 /* (float) exponent (e+3, etc) still legal */
75
76#define PFXOK 0x08000 /* 0x prefix is (still) legal */
77#define NZDIGITS 0x10000 /* no zero digits detected */
78
79/*
80 * Conversion types.
81 */
82#define CT_CHAR 0 /* %c conversion */
83#define CT_CCL 1 /* %[...] conversion */
84#define CT_STRING 2 /* %s conversion */
85#define CT_INT 3 /* integer, i.e., strtoimax or strtoumax */
86#define CT_FLOAT 4 /* floating, i.e., strtod */
87
88#define u_char unsigned char
89#define u_long unsigned long
90
91static u_char *__sccl(char *, u_char *);
92
93#if !defined(VFSCANF)
94#define VFSCANF vfscanf
95#endif
96
97/*
98 * vfscanf
99 */
100int
101VFSCANF(FILE *fp, const char *fmt0, __va_list ap)
102{
103 u_char *fmt = (u_char *)fmt0;
104 int c; /* character from format, or conversion */
105 size_t width; /* field width, or 0 */
106 char *p; /* points into all kinds of strings */
107 int n; /* handy integer */
108 int flags; /* flags as defined above */
109 char *p0; /* saves original value of p when necessary */
110 int nassigned; /* number of fields assigned */
111 int nread; /* number of characters consumed from fp */
112 int base; /* base argument to strtoimax/strtouimax */
113 char ccltab[256]; /* character class table for %[...] */
114 char buf[BUF]; /* buffer for numeric conversions */
115
116 /* `basefix' is used to avoid `if' tests in the integer scanner */
117 static short basefix[17] =
118 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
119
120 _SET_ORIENTATION(fp, -1);
121
122 nassigned = 0;
123 nread = 0;
124 base = 0; /* XXX just to keep gcc happy */
125 for (;;) {
126 c = *fmt++;
127 if (c == 0)
128 return (nassigned);
129 if (isspace(c)) {
130 while ((fp->_r > 0 || __srefill(fp) == 0) &&
131 isspace(*fp->_p))
132 nread++, fp->_r--, fp->_p++;
133 continue;
134 }
135 if (c != '%')
136 goto literal;
137 width = 0;
138 flags = 0;
139 /*
140 * switch on the format. continue if done;
141 * break once format type is derived.
142 */
143again: c = *fmt++;
144 switch (c) {
145 case '%':
146literal:
147 if (fp->_r <= 0 && __srefill(fp))
148 goto input_failure;
149 if (*fp->_p != c)
150 goto match_failure;
151 fp->_r--, fp->_p++;
152 nread++;
153 continue;
154
155 case '*':
156 flags |= SUPPRESS;
157 goto again;
158 case 'j':
159 flags |= MAXINT;
160 goto again;
161 case 'L':
Chris Fries712e4f82011-05-04 09:54:06 -0500162 flags |=
163 (*fmt == 'd') ? LLONG :
164 (*fmt == 'i') ? LLONG :
165 (*fmt == 'o') ? LLONG :
166 (*fmt == 'u') ? LLONG :
167 (*fmt == 'x') ? LLONG :
168 LONGDBL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169 goto again;
170 case 'h':
171 if (*fmt == 'h') {
172 fmt++;
173 flags |= SHORTSHORT;
174 } else {
175 flags |= SHORT;
176 }
177 goto again;
178 case 'l':
179 if (*fmt == 'l') {
180 fmt++;
181 flags |= LLONG;
182 } else {
183 flags |= LONG;
184 }
185 goto again;
186 case 'q':
187 flags |= LLONG; /* deprecated */
188 goto again;
189 case 't':
190 flags |= PTRINT;
191 goto again;
192 case 'z':
193 flags |= SIZEINT;
194 goto again;
195
196 case '0': case '1': case '2': case '3': case '4':
197 case '5': case '6': case '7': case '8': case '9':
198 width = width * 10 + c - '0';
199 goto again;
200
201 /*
202 * Conversions.
203 * Those marked `compat' are for 4.[123]BSD compatibility.
204 *
205 * (According to ANSI, E and X formats are supposed
206 * to the same as e and x. Sorry about that.)
207 */
208 case 'D': /* compat */
209 flags |= LONG;
210 /* FALLTHROUGH */
211 case 'd':
212 c = CT_INT;
213 base = 10;
214 break;
215
216 case 'i':
217 c = CT_INT;
218 base = 0;
219 break;
220
221 case 'O': /* compat */
222 flags |= LONG;
223 /* FALLTHROUGH */
224 case 'o':
225 c = CT_INT;
226 flags |= UNSIGNED;
227 base = 8;
228 break;
229
230 case 'u':
231 c = CT_INT;
232 flags |= UNSIGNED;
233 base = 10;
234 break;
235
236 case 'X':
237 case 'x':
238 flags |= PFXOK; /* enable 0x prefixing */
239 c = CT_INT;
240 flags |= UNSIGNED;
241 base = 16;
242 break;
243
244#ifdef FLOATING_POINT
245 case 'E':
246 case 'G':
247 case 'e':
248 case 'f':
249 case 'g':
250 c = CT_FLOAT;
251 break;
252#endif
253
254 case 's':
255 c = CT_STRING;
256 break;
257
258 case '[':
259 fmt = __sccl(ccltab, fmt);
260 flags |= NOSKIP;
261 c = CT_CCL;
262 break;
263
264 case 'c':
265 flags |= NOSKIP;
266 c = CT_CHAR;
267 break;
268
269 case 'p': /* pointer format is like hex */
270 flags |= POINTER | PFXOK;
271 c = CT_INT;
272 flags |= UNSIGNED;
273 base = 16;
274 break;
275
276 case 'n':
277 if (flags & SUPPRESS)
278 continue;
279 if (flags & SHORTSHORT)
280 *va_arg(ap, __signed char *) = nread;
281 else if (flags & SHORT)
282 *va_arg(ap, short *) = nread;
283 else if (flags & LONG)
284 *va_arg(ap, long *) = nread;
285 else if (flags & SIZEINT)
286 *va_arg(ap, ssize_t *) = nread;
287 else if (flags & PTRINT)
288 *va_arg(ap, ptrdiff_t *) = nread;
289 else if (flags & LLONG)
290 *va_arg(ap, long long *) = nread;
291 else if (flags & MAXINT)
292 *va_arg(ap, intmax_t *) = nread;
293 else
294 *va_arg(ap, int *) = nread;
295 continue;
296
297 /*
298 * Disgusting backwards compatibility hacks. XXX
299 */
300 case '\0': /* compat */
301 return (EOF);
302
303 default: /* compat */
304 if (isupper(c))
305 flags |= LONG;
306 c = CT_INT;
307 base = 10;
308 break;
309 }
310
311 /*
312 * We have a conversion that requires input.
313 */
314 if (fp->_r <= 0 && __srefill(fp))
315 goto input_failure;
316
317 /*
318 * Consume leading white space, except for formats
319 * that suppress this.
320 */
321 if ((flags & NOSKIP) == 0) {
322 while (isspace(*fp->_p)) {
323 nread++;
324 if (--fp->_r > 0)
325 fp->_p++;
326 else if (__srefill(fp))
327 goto input_failure;
328 }
329 /*
330 * Note that there is at least one character in
331 * the buffer, so conversions that do not set NOSKIP
332 * ca no longer result in an input failure.
333 */
334 }
335
336 /*
337 * Do the conversion.
338 */
339 switch (c) {
340
341 case CT_CHAR:
342 /* scan arbitrary characters (sets NOSKIP) */
343 if (width == 0)
344 width = 1;
345 if (flags & SUPPRESS) {
346 size_t sum = 0;
347 for (;;) {
348 if ((n = fp->_r) < (int)width) {
349 sum += n;
350 width -= n;
351 fp->_p += n;
352 if (__srefill(fp)) {
353 if (sum == 0)
354 goto input_failure;
355 break;
356 }
357 } else {
358 sum += width;
359 fp->_r -= width;
360 fp->_p += width;
361 break;
362 }
363 }
364 nread += sum;
365 } else {
366 size_t r = fread((void *)va_arg(ap, char *), 1,
367 width, fp);
368
369 if (r == 0)
370 goto input_failure;
371 nread += r;
372 nassigned++;
373 }
374 break;
375
376 case CT_CCL:
377 /* scan a (nonempty) character class (sets NOSKIP) */
378 if (width == 0)
379 width = (size_t)~0; /* `infinity' */
380 /* take only those things in the class */
381 if (flags & SUPPRESS) {
382 n = 0;
383 while (ccltab[*fp->_p]) {
384 n++, fp->_r--, fp->_p++;
385 if (--width == 0)
386 break;
387 if (fp->_r <= 0 && __srefill(fp)) {
388 if (n == 0)
389 goto input_failure;
390 break;
391 }
392 }
393 if (n == 0)
394 goto match_failure;
395 } else {
396 p0 = p = va_arg(ap, char *);
397 while (ccltab[*fp->_p]) {
398 fp->_r--;
399 *p++ = *fp->_p++;
400 if (--width == 0)
401 break;
402 if (fp->_r <= 0 && __srefill(fp)) {
403 if (p == p0)
404 goto input_failure;
405 break;
406 }
407 }
408 n = p - p0;
409 if (n == 0)
410 goto match_failure;
411 *p = '\0';
412 nassigned++;
413 }
414 nread += n;
415 break;
416
417 case CT_STRING:
418 /* like CCL, but zero-length string OK, & no NOSKIP */
419 if (width == 0)
420 width = (size_t)~0;
421 if (flags & SUPPRESS) {
422 n = 0;
423 while (!isspace(*fp->_p)) {
424 n++, fp->_r--, fp->_p++;
425 if (--width == 0)
426 break;
427 if (fp->_r <= 0 && __srefill(fp))
428 break;
429 }
430 nread += n;
431 } else {
432 p0 = p = va_arg(ap, char *);
433 while (!isspace(*fp->_p)) {
434 fp->_r--;
435 *p++ = *fp->_p++;
436 if (--width == 0)
437 break;
438 if (fp->_r <= 0 && __srefill(fp))
439 break;
440 }
441 *p = '\0';
442 nread += p - p0;
443 nassigned++;
444 }
445 continue;
446
447 case CT_INT:
448 /* scan an integer as if by strtoimax/strtoumax */
449#ifdef hardway
450 if (width == 0 || width > sizeof(buf) - 1)
451 width = sizeof(buf) - 1;
452#else
453 /* size_t is unsigned, hence this optimisation */
454 if (--width > sizeof(buf) - 2)
455 width = sizeof(buf) - 2;
456 width++;
457#endif
458 flags |= SIGNOK | NDIGITS | NZDIGITS;
459 for (p = buf; width; width--) {
460 c = *fp->_p;
461 /*
462 * Switch on the character; `goto ok'
463 * if we accept it as a part of number.
464 */
465 switch (c) {
466
467 /*
468 * The digit 0 is always legal, but is
469 * special. For %i conversions, if no
470 * digits (zero or nonzero) have been
471 * scanned (only signs), we will have
472 * base==0. In that case, we should set
473 * it to 8 and enable 0x prefixing.
474 * Also, if we have not scanned zero digits
475 * before this, do not turn off prefixing
476 * (someone else will turn it off if we
477 * have scanned any nonzero digits).
478 */
479 case '0':
480 if (base == 0) {
481 base = 8;
482 flags |= PFXOK;
483 }
484 if (flags & NZDIGITS)
485 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
486 else
487 flags &= ~(SIGNOK|PFXOK|NDIGITS);
488 goto ok;
489
490 /* 1 through 7 always legal */
491 case '1': case '2': case '3':
492 case '4': case '5': case '6': case '7':
493 base = basefix[base];
494 flags &= ~(SIGNOK | PFXOK | NDIGITS);
495 goto ok;
496
497 /* digits 8 and 9 ok iff decimal or hex */
498 case '8': case '9':
499 base = basefix[base];
500 if (base <= 8)
501 break; /* not legal here */
502 flags &= ~(SIGNOK | PFXOK | NDIGITS);
503 goto ok;
504
505 /* letters ok iff hex */
506 case 'A': case 'B': case 'C':
507 case 'D': case 'E': case 'F':
508 case 'a': case 'b': case 'c':
509 case 'd': case 'e': case 'f':
510 /* no need to fix base here */
511 if (base <= 10)
512 break; /* not legal here */
513 flags &= ~(SIGNOK | PFXOK | NDIGITS);
514 goto ok;
515
516 /* sign ok only as first character */
517 case '+': case '-':
518 if (flags & SIGNOK) {
519 flags &= ~SIGNOK;
520 flags |= HAVESIGN;
521 goto ok;
522 }
523 break;
524
525 /*
526 * x ok iff flag still set and 2nd char (or
527 * 3rd char if we have a sign).
528 */
529 case 'x': case 'X':
530 if ((flags & PFXOK) && p ==
531 buf + 1 + !!(flags & HAVESIGN)) {
532 base = 16; /* if %i */
533 flags &= ~PFXOK;
534 goto ok;
535 }
536 break;
537 }
538
539 /*
540 * If we got here, c is not a legal character
541 * for a number. Stop accumulating digits.
542 */
543 break;
544 ok:
545 /*
546 * c is legal: store it and look at the next.
547 */
548 *p++ = c;
549 if (--fp->_r > 0)
550 fp->_p++;
551 else if (__srefill(fp))
552 break; /* EOF */
553 }
554 /*
555 * If we had only a sign, it is no good; push
556 * back the sign. If the number ends in `x',
557 * it was [sign] '0' 'x', so push back the x
558 * and treat it as [sign] '0'.
559 */
560 if (flags & NDIGITS) {
561 if (p > buf)
562 (void) ungetc(*(u_char *)--p, fp);
563 goto match_failure;
564 }
565 c = ((u_char *)p)[-1];
566 if (c == 'x' || c == 'X') {
567 --p;
568 (void) ungetc(c, fp);
569 }
570 if ((flags & SUPPRESS) == 0) {
571 uintmax_t res;
572
573 *p = '\0';
574 if (flags & UNSIGNED)
575 res = strtoumax(buf, NULL, base);
576 else
577 res = strtoimax(buf, NULL, base);
578 if (flags & POINTER)
579 *va_arg(ap, void **) =
580 (void *)(uintptr_t)res;
581 else if (flags & MAXINT)
582 *va_arg(ap, intmax_t *) = res;
583 else if (flags & LLONG)
584 *va_arg(ap, long long *) = res;
585 else if (flags & SIZEINT)
586 *va_arg(ap, ssize_t *) = res;
587 else if (flags & PTRINT)
588 *va_arg(ap, ptrdiff_t *) = res;
589 else if (flags & LONG)
590 *va_arg(ap, long *) = res;
591 else if (flags & SHORT)
592 *va_arg(ap, short *) = res;
593 else if (flags & SHORTSHORT)
594 *va_arg(ap, __signed char *) = res;
595 else
596 *va_arg(ap, int *) = res;
597 nassigned++;
598 }
599 nread += p - buf;
600 break;
601
602#ifdef FLOATING_POINT
603 case CT_FLOAT:
604 /* scan a floating point number as if by strtod */
605#ifdef hardway
606 if (width == 0 || width > sizeof(buf) - 1)
607 width = sizeof(buf) - 1;
608#else
609 /* size_t is unsigned, hence this optimisation */
610 if (--width > sizeof(buf) - 2)
611 width = sizeof(buf) - 2;
612 width++;
613#endif
614 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
615 for (p = buf; width; width--) {
616 c = *fp->_p;
617 /*
618 * This code mimicks the integer conversion
619 * code, but is much simpler.
620 */
621 switch (c) {
622
623 case '0': case '1': case '2': case '3':
624 case '4': case '5': case '6': case '7':
625 case '8': case '9':
626 flags &= ~(SIGNOK | NDIGITS);
627 goto fok;
628
629 case '+': case '-':
630 if (flags & SIGNOK) {
631 flags &= ~SIGNOK;
632 goto fok;
633 }
634 break;
635 case '.':
636 if (flags & DPTOK) {
637 flags &= ~(SIGNOK | DPTOK);
638 goto fok;
639 }
640 break;
641 case 'e': case 'E':
642 /* no exponent without some digits */
643 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
644 flags =
645 (flags & ~(EXPOK|DPTOK)) |
646 SIGNOK | NDIGITS;
647 goto fok;
648 }
649 break;
650 }
651 break;
652 fok:
653 *p++ = c;
654 if (--fp->_r > 0)
655 fp->_p++;
656 else if (__srefill(fp))
657 break; /* EOF */
658 }
659 /*
660 * If no digits, might be missing exponent digits
661 * (just give back the exponent) or might be missing
662 * regular digits, but had sign and/or decimal point.
663 */
664 if (flags & NDIGITS) {
665 if (flags & EXPOK) {
666 /* no digits at all */
667 while (p > buf)
668 ungetc(*(u_char *)--p, fp);
669 goto match_failure;
670 }
671 /* just a bad exponent (e and maybe sign) */
672 c = *(u_char *)--p;
673 if (c != 'e' && c != 'E') {
674 (void) ungetc(c, fp);/* sign */
675 c = *(u_char *)--p;
676 }
677 (void) ungetc(c, fp);
678 }
679 if ((flags & SUPPRESS) == 0) {
680 double res;
681
682 *p = '\0';
683 res = strtod(buf, (char **) NULL);
684 if (flags & LONGDBL)
685 *va_arg(ap, long double *) = res;
686 else if (flags & LONG)
687 *va_arg(ap, double *) = res;
688 else
689 *va_arg(ap, float *) = res;
690 nassigned++;
691 }
692 nread += p - buf;
693 break;
694#endif /* FLOATING_POINT */
695 }
696 }
697input_failure:
698 return (nassigned ? nassigned : -1);
699match_failure:
700 return (nassigned);
701}
702
703/*
704 * Fill in the given table from the scanset at the given format
705 * (just after `['). Return a pointer to the character past the
706 * closing `]'. The table has a 1 wherever characters should be
707 * considered part of the scanset.
708 */
709static u_char *
710__sccl(char *tab, u_char *fmt)
711{
712 int c, n, v;
713
714 /* first `clear' the whole table */
715 c = *fmt++; /* first char hat => negated scanset */
716 if (c == '^') {
717 v = 1; /* default => accept */
718 c = *fmt++; /* get new first char */
719 } else
720 v = 0; /* default => reject */
721 /* should probably use memset here */
722 for (n = 0; n < 256; n++)
723 tab[n] = v;
724 if (c == 0)
725 return (fmt - 1);/* format ended before closing ] */
726
727 /*
728 * Now set the entries corresponding to the actual scanset
729 * to the opposite of the above.
730 *
731 * The first character may be ']' (or '-') without being special;
732 * the last character may be '-'.
733 */
734 v = 1 - v;
735 for (;;) {
736 tab[c] = v; /* take character c */
737doswitch:
738 n = *fmt++; /* and examine the next */
739 switch (n) {
740
741 case 0: /* format ended too soon */
742 return (fmt - 1);
743
744 case '-':
745 /*
746 * A scanset of the form
747 * [01+-]
748 * is defined as `the digit 0, the digit 1,
749 * the character +, the character -', but
750 * the effect of a scanset such as
751 * [a-zA-Z0-9]
752 * is implementation defined. The V7 Unix
753 * scanf treats `a-z' as `the letters a through
754 * z', but treats `a-a' as `the letter a, the
755 * character -, and the letter a'.
756 *
757 * For compatibility, the `-' is not considerd
758 * to define a range if the character following
759 * it is either a close bracket (required by ANSI)
760 * or is not numerically greater than the character
761 * we just stored in the table (c).
762 */
763 n = *fmt;
764 if (n == ']' || n < c) {
765 c = '-';
766 break; /* resume the for(;;) */
767 }
768 fmt++;
769 do { /* fill in the range */
770 tab[++c] = v;
771 } while (c < n);
772#if 1 /* XXX another disgusting compatibility hack */
773 /*
774 * Alas, the V7 Unix scanf also treats formats
775 * such as [a-c-e] as `the letters a through e'.
776 * This too is permitted by the standard....
777 */
778 goto doswitch;
779#else
780 c = *fmt++;
781 if (c == 0)
782 return (fmt - 1);
783 if (c == ']')
784 return (fmt);
785#endif
786 break;
787
788 case ']': /* end of scanset */
789 return (fmt);
790
791 default: /* just another character */
792 c = n;
793 break;
794 }
795 }
796 /* NOTREACHED */
797}