]> git.saurik.com Git - apple/xnu.git/blob - libkern/stdio/scanf.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / libkern / stdio / scanf.c
1 /*
2 * Copyright (c) 2004-2016 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*-
29 * Copyright (c) 1990, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * This code is derived from software contributed to Berkeley by
33 * Chris Torek.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 #include <sys/cdefs.h>
65
66 #if 0 /* XXX coming soon */
67 #include <ctype.h>
68 #else
69 static inline int
70 isspace(char c)
71 {
72 return c == ' ' || c == '\t' || c == '\n' || c == '\12';
73 }
74 #endif
75 #include <stdarg.h>
76 #include <string.h>
77 #include <sys/param.h>
78 #include <sys/systm.h>
79
80 #define BUF 32 /* Maximum length of numeric string. */
81
82 /*
83 * Flags used during conversion.
84 */
85 #define LONG 0x01 /* l: long or double */
86 #define SHORT 0x04 /* h: short */
87 #define SUPPRESS 0x08 /* *: suppress assignment */
88 #define POINTER 0x10 /* p: void * (as hex) */
89 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
90 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
91 #define SHORTSHORT 0x4000 /* hh: char */
92 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
93
94 /*
95 * The following are used in numeric conversions only:
96 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
97 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
98 */
99 #define SIGNOK 0x40 /* +/- is (still) legal */
100 #define NDIGITS 0x80 /* no digits detected */
101
102 #define DPTOK 0x100 /* (float) decimal point is still legal */
103 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
104
105 #define PFXOK 0x100 /* 0x prefix is (still) legal */
106 #define NZDIGITS 0x200 /* no zero digits detected */
107
108 /*
109 * Conversion types.
110 */
111 #define CT_CHAR 0 /* %c conversion */
112 #define CT_CCL 1 /* %[...] conversion */
113 #define CT_STRING 2 /* %s conversion */
114 #define CT_INT 3 /* %[dioupxX] conversion */
115
116 static const u_char *__sccl(char *, const u_char *);
117
118 int
119 sscanf(const char *ibuf, const char *fmt, ...)
120 {
121 va_list ap;
122 int ret;
123
124 va_start(ap, fmt);
125 ret = vsscanf(ibuf, fmt, ap);
126 va_end(ap);
127 return ret;
128 }
129
130 int
131 vsscanf(const char *inp, char const *fmt0, va_list ap)
132 {
133 int inr;
134 const u_char *fmt = (const u_char *)fmt0;
135 int c; /* character from format, or conversion */
136 size_t width; /* field width, or 0 */
137 char *p; /* points into all kinds of strings */
138 int n; /* handy integer */
139 int flags; /* flags as defined above */
140 char *p0; /* saves original value of p when necessary */
141 int nassigned; /* number of fields assigned */
142 int nconversions; /* number of conversions */
143 int nread; /* number of characters consumed from fp */
144 int base; /* base argument to conversion function */
145 char ccltab[256]; /* character class table for %[...] */
146 char buf[BUF]; /* buffer for numeric conversions */
147
148 /* `basefix' is used to avoid `if' tests in the integer scanner */
149 static short basefix[17] =
150 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
151
152 inr = strlen(inp);
153
154 nassigned = 0;
155 nconversions = 0;
156 nread = 0;
157 base = 0; /* XXX just to keep gcc happy */
158 for (;;) {
159 c = *fmt++;
160 if (c == 0) {
161 return nassigned;
162 }
163 if (isspace(c)) {
164 while (inr > 0 && isspace(*inp)) {
165 nread++;
166 inr--;
167 inp++;
168 }
169 continue;
170 }
171 if (c != '%') {
172 goto literal;
173 }
174 width = 0;
175 flags = 0;
176 /*
177 * switch on the format. continue if done;
178 * break once format type is derived.
179 */
180 again: c = *fmt++;
181 switch (c) {
182 case '%':
183 literal:
184 if (inr <= 0) {
185 goto input_failure;
186 }
187 if (*inp != c) {
188 goto match_failure;
189 }
190 inr--;
191 inp++;
192 nread++;
193 continue;
194
195 case '*':
196 flags |= SUPPRESS;
197 goto again;
198 case 'l':
199 if (flags & LONG) {
200 flags &= ~LONG;
201 flags |= LONGLONG;
202 } else {
203 flags |= LONG;
204 }
205 goto again;
206 case 'q':
207 flags |= LONGLONG; /* not quite */
208 goto again;
209 case 'h':
210 if (flags & SHORT) {
211 flags &= ~SHORT;
212 flags |= SHORTSHORT;
213 } else {
214 flags |= SHORT;
215 }
216 goto again;
217
218 case '0': case '1': case '2': case '3': case '4':
219 case '5': case '6': case '7': case '8': case '9':
220 width = width * 10 + c - '0';
221 goto again;
222
223 /*
224 * Conversions.
225 */
226 case 'd':
227 c = CT_INT;
228 base = 10;
229 break;
230
231 case 'i':
232 c = CT_INT;
233 base = 0;
234 break;
235
236 case 'o':
237 c = CT_INT;
238 flags |= UNSIGNED;
239 base = 8;
240 break;
241
242 case 'u':
243 c = CT_INT;
244 flags |= UNSIGNED;
245 base = 10;
246 break;
247
248 case 'X':
249 case 'x':
250 flags |= PFXOK; /* enable 0x prefixing */
251 c = CT_INT;
252 flags |= UNSIGNED;
253 base = 16;
254 break;
255
256 case 's':
257 c = CT_STRING;
258 break;
259
260 case '[':
261 fmt = __sccl(ccltab, fmt);
262 flags |= NOSKIP;
263 c = CT_CCL;
264 break;
265
266 case 'c':
267 flags |= NOSKIP;
268 c = CT_CHAR;
269 break;
270
271 case 'p': /* pointer format is like hex */
272 flags |= POINTER | PFXOK;
273 c = CT_INT;
274 flags |= UNSIGNED;
275 base = 16;
276 break;
277
278 case 'n':
279 nconversions++;
280 if (flags & SUPPRESS) { /* ??? */
281 continue;
282 }
283 if (flags & SHORTSHORT) {
284 *va_arg(ap, char *) = nread;
285 } else if (flags & SHORT) {
286 *va_arg(ap, short *) = nread;
287 } else if (flags & LONG) {
288 *va_arg(ap, long *) = nread;
289 } else if (flags & LONGLONG) {
290 *va_arg(ap, long long *) = nread;
291 } else {
292 *va_arg(ap, int *) = nread;
293 }
294 continue;
295 }
296
297 /*
298 * We have a conversion that requires input.
299 */
300 if (inr <= 0) {
301 goto input_failure;
302 }
303
304 /*
305 * Consume leading white space, except for formats
306 * that suppress this.
307 */
308 if ((flags & NOSKIP) == 0) {
309 while (isspace(*inp)) {
310 nread++;
311 if (--inr > 0) {
312 inp++;
313 } else {
314 goto input_failure;
315 }
316 }
317 /*
318 * Note that there is at least one character in
319 * the buffer, so conversions that do not set NOSKIP
320 * can no longer result in an input failure.
321 */
322 }
323
324 /*
325 * Do the conversion.
326 */
327 switch (c) {
328 case CT_CHAR:
329 /* scan arbitrary characters (sets NOSKIP) */
330 if (width == 0) {
331 width = 1;
332 }
333 if (flags & SUPPRESS) {
334 size_t sum = 0;
335 for (;;) {
336 if ((n = inr) < (int)width) {
337 sum += n;
338 width -= n;
339 inp += n;
340 if (sum == 0) {
341 goto input_failure;
342 }
343 break;
344 } else {
345 sum += width;
346 inr -= width;
347 inp += width;
348 break;
349 }
350 }
351 nread += sum;
352 } else {
353 bcopy(inp, va_arg(ap, char *), width);
354 inr -= width;
355 inp += width;
356 nread += width;
357 nassigned++;
358 }
359 nconversions++;
360 break;
361
362 case CT_CCL:
363 /* scan a (nonempty) character class (sets NOSKIP) */
364 if (width == 0) {
365 width = (size_t)~0; /* `infinity' */
366 }
367 /* take only those things in the class */
368 if (flags & SUPPRESS) {
369 n = 0;
370 while (ccltab[(unsigned char)*inp]) {
371 n++;
372 inr--;
373 inp++;
374 if (--width == 0) {
375 break;
376 }
377 if (inr <= 0) {
378 if (n == 0) {
379 goto input_failure;
380 }
381 break;
382 }
383 }
384 if (n == 0) {
385 goto match_failure;
386 }
387 } else {
388 p0 = p = va_arg(ap, char *);
389 while (ccltab[(unsigned char)*inp]) {
390 inr--;
391 *p++ = *inp++;
392 if (--width == 0) {
393 break;
394 }
395 if (inr <= 0) {
396 if (p == p0) {
397 goto input_failure;
398 }
399 break;
400 }
401 }
402 n = p - p0;
403 if (n == 0) {
404 goto match_failure;
405 }
406 *p = 0;
407 nassigned++;
408 }
409 nread += n;
410 nconversions++;
411 break;
412
413 case CT_STRING:
414 /* like CCL, but zero-length string OK, & no NOSKIP */
415 if (width == 0) {
416 width = (size_t)~0;
417 }
418 if (flags & SUPPRESS) {
419 n = 0;
420 while (!isspace(*inp)) {
421 n++;
422 inr--;
423 inp++;
424 if (--width == 0) {
425 break;
426 }
427 if (inr <= 0) {
428 break;
429 }
430 }
431 nread += n;
432 } else {
433 p0 = p = va_arg(ap, char *);
434 while (!isspace(*inp)) {
435 inr--;
436 *p++ = *inp++;
437 if (--width == 0) {
438 break;
439 }
440 if (inr <= 0) {
441 break;
442 }
443 }
444 *p = 0;
445 nread += p - p0;
446 nassigned++;
447 }
448 nconversions++;
449 continue;
450
451 case CT_INT:
452 /* scan an integer as if by the conversion function */
453 #ifdef hardway
454 if (width == 0 || width > sizeof(buf) - 1) {
455 width = sizeof(buf) - 1;
456 }
457 #else
458 /* size_t is unsigned, hence this optimisation */
459 if (--width > sizeof(buf) - 2) {
460 width = sizeof(buf) - 2;
461 }
462 width++;
463 #endif
464 flags |= SIGNOK | NDIGITS | NZDIGITS;
465 for (p = buf; width; width--) {
466 c = *inp;
467 /*
468 * Switch on the character; `goto ok'
469 * if we accept it as a part of number.
470 */
471 switch (c) {
472 /*
473 * The digit 0 is always legal, but is
474 * special. For %i conversions, if no
475 * digits (zero or nonzero) have been
476 * scanned (only signs), we will have
477 * base==0. In that case, we should set
478 * it to 8 and enable 0x prefixing.
479 * Also, if we have not scanned zero digits
480 * before this, do not turn off prefixing
481 * (someone else will turn it off if we
482 * have scanned any nonzero digits).
483 */
484 case '0':
485 if (base == 0) {
486 base = 8;
487 flags |= PFXOK;
488 }
489 if (flags & NZDIGITS) {
490 flags &= ~(SIGNOK | NZDIGITS | NDIGITS);
491 } else {
492 flags &= ~(SIGNOK | PFXOK | NDIGITS);
493 }
494 goto ok;
495
496 /* 1 through 7 always legal */
497 case '1': case '2': case '3':
498 case '4': case '5': case '6': case '7':
499 base = basefix[base];
500 flags &= ~(SIGNOK | PFXOK | NDIGITS);
501 goto ok;
502
503 /* digits 8 and 9 ok iff decimal or hex */
504 case '8': case '9':
505 base = basefix[base];
506 if (base <= 8) {
507 break; /* not legal here */
508 }
509 flags &= ~(SIGNOK | PFXOK | NDIGITS);
510 goto ok;
511
512 /* letters ok iff hex */
513 case 'A': case 'B': case 'C':
514 case 'D': case 'E': case 'F':
515 case 'a': case 'b': case 'c':
516 case 'd': case 'e': case 'f':
517 /* no need to fix base here */
518 if (base <= 10) {
519 break; /* not legal here */
520 }
521 flags &= ~(SIGNOK | PFXOK | NDIGITS);
522 goto ok;
523
524 /* sign ok only as first character */
525 case '+': case '-':
526 if (flags & SIGNOK) {
527 flags &= ~SIGNOK;
528 goto ok;
529 }
530 break;
531
532 /* x ok iff flag still set & 2nd char */
533 case 'x': case 'X':
534 if (flags & PFXOK && p == buf + 1) {
535 base = 16; /* if %i */
536 flags &= ~PFXOK;
537 goto ok;
538 }
539 break;
540 }
541
542 /*
543 * If we got here, c is not a legal character
544 * for a number. Stop accumulating digits.
545 */
546 break;
547 ok:
548 /*
549 * c is legal: store it and look at the next.
550 */
551 *p++ = c;
552 if (--inr > 0) {
553 inp++;
554 } else {
555 break; /* end of input */
556 }
557 }
558 /*
559 * If we had only a sign, it is no good; push
560 * back the sign. If the number ends in `x',
561 * it was [sign] '0' 'x', so push back the x
562 * and treat it as [sign] '0'.
563 */
564 if (flags & NDIGITS) {
565 if (p > buf) {
566 inp--;
567 inr++;
568 }
569 goto match_failure;
570 }
571 c = ((u_char *)p)[-1];
572 if (c == 'x' || c == 'X') {
573 --p;
574 inp--;
575 inr++;
576 }
577 if ((flags & SUPPRESS) == 0) {
578 u_quad_t res;
579
580 *p = 0;
581 if ((flags & UNSIGNED) == 0) {
582 res = strtoq(buf, (char **)NULL, base);
583 } else {
584 res = strtouq(buf, (char **)NULL, base);
585 }
586 if (flags & POINTER) {
587 *va_arg(ap, void **) =
588 (void *)(uintptr_t)res;
589 } else if (flags & SHORTSHORT) {
590 *va_arg(ap, char *) = res;
591 } else if (flags & SHORT) {
592 *va_arg(ap, short *) = res;
593 } else if (flags & LONG) {
594 *va_arg(ap, long *) = res;
595 } else if (flags & LONGLONG) {
596 *va_arg(ap, long long *) = res;
597 } else {
598 *va_arg(ap, int *) = res;
599 }
600 nassigned++;
601 }
602 nread += p - buf;
603 nconversions++;
604 break;
605 }
606 }
607 input_failure:
608 return nconversions != 0 ? nassigned : -1;
609 match_failure:
610 return nassigned;
611 }
612
613 /*
614 * Fill in the given table from the scanset at the given format
615 * (just after `['). Return a pointer to the character past the
616 * closing `]'. The table has a 1 wherever characters should be
617 * considered part of the scanset.
618 */
619 static const u_char *
620 __sccl(char *tab, const u_char *fmt)
621 {
622 int c, n, v;
623
624 /* first `clear' the whole table */
625 c = *fmt++; /* first char hat => negated scanset */
626 if (c == '^') {
627 v = 1; /* default => accept */
628 c = *fmt++; /* get new first char */
629 } else {
630 v = 0; /* default => reject */
631 }
632 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
633 (void) memset(tab, v, 256);
634
635 if (c == 0) {
636 return fmt - 1;/* format ended before closing ] */
637 }
638 /*
639 * Now set the entries corresponding to the actual scanset
640 * to the opposite of the above.
641 *
642 * The first character may be ']' (or '-') without being special;
643 * the last character may be '-'.
644 */
645 v = 1 - v;
646 for (;;) {
647 tab[c] = v; /* take character c */
648 doswitch:
649 n = *fmt++; /* and examine the next */
650 switch (n) {
651 case 0: /* format ended too soon */
652 return fmt - 1;
653
654 case '-':
655 /*
656 * A scanset of the form
657 * [01+-]
658 * is defined as `the digit 0, the digit 1,
659 * the character +, the character -', but
660 * the effect of a scanset such as
661 * [a-zA-Z0-9]
662 * is implementation defined. The V7 Unix
663 * scanf treats `a-z' as `the letters a through
664 * z', but treats `a-a' as `the letter a, the
665 * character -, and the letter a'.
666 *
667 * For compatibility, the `-' is not considerd
668 * to define a range if the character following
669 * it is either a close bracket (required by ANSI)
670 * or is not numerically greater than the character
671 * we just stored in the table (c).
672 */
673 n = *fmt;
674 if (n == ']' || n < c) {
675 c = '-';
676 break; /* resume the for(;;) */
677 }
678 fmt++;
679 /* fill in the range */
680 do {
681 tab[++c] = v;
682 } while (c < n);
683 c = n;
684 /*
685 * Alas, the V7 Unix scanf also treats formats
686 * such as [a-c-e] as `the letters a through e'.
687 * This too is permitted by the standard....
688 */
689 goto doswitch;
690
691 case ']': /* end of scanset */
692 return fmt;
693
694 default: /* just another character */
695 c = n;
696 break;
697 }
698 }
699 /* NOTREACHED */
700 }