| Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 1 | /*	$OpenBSD: vfscanf.c,v 1.31 2014/03/19 05:17:01 guenther Exp $ */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | #include <inttypes.h> | 
|  | 35 | #include <stdarg.h> | 
|  | 36 | #include <stddef.h> | 
|  | 37 | #include <stdio.h> | 
|  | 38 | #include <stdlib.h> | 
| Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 39 | #include <string.h> | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 40 | #include <sys/param.h> | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 41 | #include <wctype.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | #include "local.h" | 
|  | 43 |  | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 44 | #include <private/bionic_ctype.h> | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 45 | #include <private/bionic_fortify.h> | 
|  | 46 | #include <private/bionic_mbstate.h> | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 47 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 48 | #define BUF 513 /* Maximum length of numeric string. */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 |  | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 50 | // Flags used during conversion. | 
|  | 51 | // Size/type: | 
|  | 52 | #define LONG       0x00001 // l: long or double | 
|  | 53 | #define LONGDBL    0x00002 // L: long double | 
|  | 54 | #define SHORT      0x00004 // h: short | 
|  | 55 | #define SHORTSHORT 0x00008 // hh: 8 bit integer | 
|  | 56 | #define LLONG      0x00010 // ll: long long (+ deprecated q: quad) | 
|  | 57 | #define POINTER    0x00020 // p: void* (as hex) | 
|  | 58 | #define SIZEINT    0x00040 // z: (signed) size_t | 
|  | 59 | #define MAXINT     0x00080 // j: intmax_t | 
|  | 60 | #define PTRINT     0x00100 // t: ptrdiff_t | 
|  | 61 | #define NOSKIP     0x00200 // [ or c: do not skip blanks | 
|  | 62 | // Modifiers: | 
|  | 63 | #define SUPPRESS   0x00400 // *: suppress assignment | 
|  | 64 | #define UNSIGNED   0x00800 // %[oupxX] conversions | 
|  | 65 | #define ALLOCATE   0x01000 // m: allocate a char* | 
|  | 66 | // Internal use during integer parsing: | 
|  | 67 | #define SIGNOK     0x02000 // +/- is (still) legal | 
|  | 68 | #define HAVESIGN   0x04000 // Sign detected | 
|  | 69 | #define NDIGITS    0x08000 // No digits detected | 
|  | 70 | #define PFXOK      0x10000 // "0x" prefix is (still) legal | 
|  | 71 | #define NZDIGITS   0x20000 // No zero digits detected | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 |  | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 73 | // Conversion types. | 
|  | 74 | #define CT_CHAR 0   // %c conversion | 
|  | 75 | #define CT_CCL 1    // %[...] conversion | 
|  | 76 | #define CT_STRING 2 // %s conversion | 
|  | 77 | #define CT_INT 3    // Integer: strtoimax/strtoumax | 
|  | 78 | #define CT_FLOAT 4  // Float: strtod | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 79 |  | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 80 | static const unsigned char* __sccl(char*, const unsigned char*); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 81 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 82 | /* | 
| Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 83 | * Internal, unlocked version of vfscanf | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | */ | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 85 | int __svfscanf(FILE* fp, const char* fmt0, va_list ap) { | 
|  | 86 | const unsigned char* fmt = reinterpret_cast<const unsigned char*>(fmt0); | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 87 | int c;            /* character from format, or conversion */ | 
|  | 88 | size_t width;     /* field width, or 0 */ | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 89 | char* p; | 
|  | 90 | wchar_t* wcp; | 
|  | 91 | size_t n; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 92 | int flags;        /* flags as defined above */ | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 93 | int nassigned;    /* number of fields assigned */ | 
|  | 94 | int nread;        /* number of characters consumed from fp */ | 
|  | 95 | int base;         /* base argument to strtoimax/strtouimax */ | 
|  | 96 | char ccltab[256]; /* character class table for %[...] */ | 
|  | 97 | char buf[BUF];    /* buffer for numeric conversions */ | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 98 | size_t nconv;     /* length of multibyte sequence converted */ | 
|  | 99 | mbstate_t mbs; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 100 | void* allocation = NULL; // Allocated but unassigned result for %mc/%ms/%m[. | 
|  | 101 | size_t capacity = 0; // Number of char/wchar_t units allocated in `allocation`. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 102 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 103 | /* `basefix' is used to avoid `if' tests in the integer scanner */ | 
|  | 104 | static short basefix[17] = { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 105 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 106 | _SET_ORIENTATION(fp, -1); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 107 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 108 | nassigned = 0; | 
|  | 109 | nread = 0; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 110 | for (;;) { | 
|  | 111 | c = *fmt++; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 112 | if (c == 0) return nassigned; | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 113 | if (IsSpace(c)) { | 
|  | 114 | while ((fp->_r > 0 || __srefill(fp) == 0) && IsSpace(*fp->_p)) nread++, fp->_r--, fp->_p++; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 115 | continue; | 
|  | 116 | } | 
|  | 117 | if (c != '%') goto literal; | 
|  | 118 | width = 0; | 
|  | 119 | flags = 0; | 
|  | 120 | /* | 
|  | 121 | * switch on the format.  continue if done; | 
|  | 122 | * break once format type is derived. | 
|  | 123 | */ | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 124 | again: | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 125 | c = *fmt++; | 
|  | 126 | switch (c) { | 
|  | 127 | case '%': | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 128 | literal: | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 129 | if (fp->_r <= 0 && __srefill(fp)) goto input_failure; | 
|  | 130 | if (*fp->_p != c) goto match_failure; | 
|  | 131 | fp->_r--, fp->_p++; | 
|  | 132 | nread++; | 
|  | 133 | continue; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 134 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 135 | case '*': | 
|  | 136 | flags |= SUPPRESS; | 
|  | 137 | goto again; | 
|  | 138 | case 'j': | 
|  | 139 | flags |= MAXINT; | 
|  | 140 | goto again; | 
|  | 141 | case 'L': | 
|  | 142 | flags |= LONGDBL; | 
|  | 143 | goto again; | 
|  | 144 | case 'h': | 
|  | 145 | if (*fmt == 'h') { | 
|  | 146 | fmt++; | 
|  | 147 | flags |= SHORTSHORT; | 
|  | 148 | } else { | 
|  | 149 | flags |= SHORT; | 
|  | 150 | } | 
|  | 151 | goto again; | 
|  | 152 | case 'l': | 
|  | 153 | if (*fmt == 'l') { | 
|  | 154 | fmt++; | 
|  | 155 | flags |= LLONG; | 
|  | 156 | } else { | 
|  | 157 | flags |= LONG; | 
|  | 158 | } | 
|  | 159 | goto again; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 160 | case 'm': | 
|  | 161 | flags |= ALLOCATE; | 
|  | 162 | goto again; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 163 | case 'q': | 
|  | 164 | flags |= LLONG; /* deprecated */ | 
|  | 165 | goto again; | 
|  | 166 | case 't': | 
|  | 167 | flags |= PTRINT; | 
|  | 168 | goto again; | 
|  | 169 | case 'z': | 
|  | 170 | flags |= SIZEINT; | 
|  | 171 | goto again; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 172 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 173 | case '0': | 
|  | 174 | case '1': | 
|  | 175 | case '2': | 
|  | 176 | case '3': | 
|  | 177 | case '4': | 
|  | 178 | case '5': | 
|  | 179 | case '6': | 
|  | 180 | case '7': | 
|  | 181 | case '8': | 
|  | 182 | case '9': | 
|  | 183 | width = width * 10 + c - '0'; | 
|  | 184 | goto again; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 185 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 186 | /* | 
|  | 187 | * Conversions. | 
|  | 188 | * Those marked `compat' are for 4.[123]BSD compatibility. | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 189 | */ | 
|  | 190 | case 'D': /* compat */ | 
|  | 191 | flags |= LONG; | 
|  | 192 | /* FALLTHROUGH */ | 
|  | 193 | case 'd': | 
|  | 194 | c = CT_INT; | 
|  | 195 | base = 10; | 
|  | 196 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 197 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 198 | case 'i': | 
|  | 199 | c = CT_INT; | 
|  | 200 | base = 0; | 
|  | 201 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 202 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 203 | case 'O': /* compat */ | 
|  | 204 | flags |= LONG; | 
|  | 205 | /* FALLTHROUGH */ | 
|  | 206 | case 'o': | 
|  | 207 | c = CT_INT; | 
|  | 208 | flags |= UNSIGNED; | 
|  | 209 | base = 8; | 
|  | 210 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 211 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 212 | case 'u': | 
|  | 213 | c = CT_INT; | 
|  | 214 | flags |= UNSIGNED; | 
|  | 215 | base = 10; | 
|  | 216 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 217 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 218 | case 'X': | 
|  | 219 | case 'x': | 
|  | 220 | flags |= PFXOK; /* enable 0x prefixing */ | 
|  | 221 | c = CT_INT; | 
|  | 222 | flags |= UNSIGNED; | 
|  | 223 | base = 16; | 
|  | 224 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 225 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 226 | case 'e': | 
|  | 227 | case 'E': | 
|  | 228 | case 'f': | 
|  | 229 | case 'F': | 
|  | 230 | case 'g': | 
|  | 231 | case 'G': | 
|  | 232 | case 'a': | 
|  | 233 | case 'A': | 
|  | 234 | c = CT_FLOAT; | 
|  | 235 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 236 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 237 | case 's': | 
| Elliott Hughes | 3048a36 | 2018-01-19 17:58:07 -0800 | [diff] [blame] | 238 | memset(ccltab, 1, 256); | 
|  | 239 | ccltab['\t'] = ccltab['\n'] = ccltab['\v'] = ccltab['\f'] = ccltab['\r'] = ccltab[' '] = 0; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 240 | c = CT_STRING; | 
|  | 241 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 242 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 243 | case '[': | 
|  | 244 | fmt = __sccl(ccltab, fmt); | 
|  | 245 | flags |= NOSKIP; | 
|  | 246 | c = CT_CCL; | 
|  | 247 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 248 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 249 | case 'c': | 
|  | 250 | flags |= NOSKIP; | 
|  | 251 | c = CT_CHAR; | 
|  | 252 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 253 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 254 | case 'p': /* pointer format is like hex */ | 
|  | 255 | flags |= POINTER | PFXOK; | 
|  | 256 | c = CT_INT; | 
|  | 257 | flags |= UNSIGNED; | 
|  | 258 | base = 16; | 
|  | 259 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 260 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 261 | case 'n': | 
|  | 262 | if (flags & SUPPRESS) continue; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 263 | if (flags & SHORTSHORT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 264 | *va_arg(ap, signed char*) = nread; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 265 | } else if (flags & SHORT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 266 | *va_arg(ap, short*) = nread; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 267 | } else if (flags & LONG) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 268 | *va_arg(ap, long*) = nread; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 269 | } else if (flags & SIZEINT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 270 | *va_arg(ap, ssize_t*) = nread; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 271 | } else if (flags & PTRINT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 272 | *va_arg(ap, ptrdiff_t*) = nread; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 273 | } else if (flags & LLONG) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 274 | *va_arg(ap, long long*) = nread; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 275 | } else if (flags & MAXINT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 276 | *va_arg(ap, intmax_t*) = nread; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 277 | } else { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 278 | *va_arg(ap, int*) = nread; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 279 | } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 280 | continue; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 281 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 282 | /* | 
|  | 283 | * Disgusting backwards compatibility hacks.	XXX | 
|  | 284 | */ | 
|  | 285 | case '\0': /* compat */ | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 286 | return EOF; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 287 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 288 | default: /* compat */ | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 289 | if (IsUpper(c)) flags |= LONG; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 290 | c = CT_INT; | 
|  | 291 | base = 10; | 
|  | 292 | break; | 
|  | 293 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 294 |  | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 295 | if ((flags & ALLOCATE) != 0 && c > CT_STRING) { | 
|  | 296 | __fortify_fatal("scanf 'm' only works with %%c/%%s/%%["); | 
|  | 297 | } | 
|  | 298 | if ((flags & (ALLOCATE|SUPPRESS)) == (ALLOCATE|SUPPRESS)) { | 
|  | 299 | __fortify_fatal("scanf 'm' makes no sense with '*'"); | 
|  | 300 | } | 
|  | 301 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 302 | /* | 
|  | 303 | * We have a conversion that requires input. | 
|  | 304 | */ | 
|  | 305 | if (fp->_r <= 0 && __srefill(fp)) goto input_failure; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 306 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 307 | /* | 
|  | 308 | * Consume leading white space, except for formats | 
|  | 309 | * that suppress this. | 
|  | 310 | */ | 
|  | 311 | if ((flags & NOSKIP) == 0) { | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 312 | while (IsSpace(*fp->_p)) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 313 | nread++; | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 314 | if (--fp->_r > 0) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 315 | fp->_p++; | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 316 | } else if (__srefill(fp)) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 317 | goto input_failure; | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 318 | } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 319 | } | 
|  | 320 | /* | 
|  | 321 | * Note that there is at least one character in | 
|  | 322 | * the buffer, so conversions that do not set NOSKIP | 
|  | 323 | * ca no longer result in an input failure. | 
|  | 324 | */ | 
|  | 325 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 326 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 327 | /* | 
|  | 328 | * Do the conversion. | 
|  | 329 | */ | 
|  | 330 | switch (c) { | 
|  | 331 | case CT_CHAR: | 
|  | 332 | /* scan arbitrary characters (sets NOSKIP) */ | 
|  | 333 | if (width == 0) width = 1; | 
|  | 334 | if (flags & LONG) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 335 | if (flags & ALLOCATE) { | 
|  | 336 | allocation = wcp = reinterpret_cast<wchar_t*>(malloc(width * sizeof(wchar_t))); | 
|  | 337 | if (allocation == NULL) goto allocation_failure; | 
|  | 338 | } else if (flags & SUPPRESS) { | 
|  | 339 | wcp = NULL; | 
|  | 340 | } else { | 
|  | 341 | wcp = va_arg(ap, wchar_t*); | 
|  | 342 | } | 
|  | 343 | size_t bytes = 0; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 344 | while (width != 0) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 345 | if (bytes == MB_CUR_MAX) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 346 | fp->_flags |= __SERR; | 
|  | 347 | goto input_failure; | 
|  | 348 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 349 | buf[bytes++] = *fp->_p; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 350 | fp->_p++; | 
|  | 351 | fp->_r--; | 
|  | 352 | memset(&mbs, 0, sizeof(mbs)); | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 353 | nconv = mbrtowc(wcp, buf, bytes, &mbs); | 
|  | 354 | if (nconv == __MB_ERR_ILLEGAL_SEQUENCE) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 355 | fp->_flags |= __SERR; | 
|  | 356 | goto input_failure; | 
|  | 357 | } | 
|  | 358 | if (nconv == 0 && !(flags & SUPPRESS)) *wcp = L'\0'; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 359 | if (nconv != __MB_ERR_INCOMPLETE_SEQUENCE) { | 
|  | 360 | nread += bytes; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 361 | width--; | 
|  | 362 | if (!(flags & SUPPRESS)) wcp++; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 363 | bytes = 0; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 364 | } | 
|  | 365 | if (fp->_r <= 0 && __srefill(fp)) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 366 | if (bytes != 0) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 367 | fp->_flags |= __SERR; | 
|  | 368 | goto input_failure; | 
|  | 369 | } | 
|  | 370 | break; | 
|  | 371 | } | 
|  | 372 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 373 | if (allocation != NULL) { | 
|  | 374 | *va_arg(ap, wchar_t**) = reinterpret_cast<wchar_t*>(allocation); | 
|  | 375 | allocation = NULL; | 
|  | 376 | } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 377 | if (!(flags & SUPPRESS)) nassigned++; | 
|  | 378 | } else if (flags & SUPPRESS) { | 
|  | 379 | size_t sum = 0; | 
|  | 380 | for (;;) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 381 | if ((n = fp->_r) < width) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 382 | sum += n; | 
|  | 383 | width -= n; | 
|  | 384 | fp->_p += n; | 
|  | 385 | if (__srefill(fp)) { | 
|  | 386 | if (sum == 0) goto input_failure; | 
|  | 387 | break; | 
|  | 388 | } | 
|  | 389 | } else { | 
|  | 390 | sum += width; | 
|  | 391 | fp->_r -= width; | 
|  | 392 | fp->_p += width; | 
|  | 393 | break; | 
|  | 394 | } | 
|  | 395 | } | 
|  | 396 | nread += sum; | 
|  | 397 | } else { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 398 | if (flags & ALLOCATE) { | 
|  | 399 | allocation = p = reinterpret_cast<char*>(malloc(width)); | 
|  | 400 | if (allocation == NULL) goto allocation_failure; | 
|  | 401 | } else { | 
|  | 402 | p = va_arg(ap, char*); | 
|  | 403 | } | 
|  | 404 | size_t r = fread(p, 1, width, fp); | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 405 | if (r == 0) goto input_failure; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 406 | if (allocation != NULL) { | 
|  | 407 | *va_arg(ap, char**) = reinterpret_cast<char*>(allocation); | 
|  | 408 | allocation = NULL; | 
|  | 409 | } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 410 | nread += r; | 
|  | 411 | nassigned++; | 
|  | 412 | } | 
|  | 413 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 414 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 415 | case CT_CCL: | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 416 | case CT_STRING: | 
|  | 417 | // CT_CCL: scan a (nonempty) character class (sets NOSKIP). | 
|  | 418 | // CT_STRING: like CCL, but zero-length string OK, & no NOSKIP. | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 419 | if (width == 0) width = SIZE_MAX; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 420 | if (flags & LONG) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 421 | // TODO: since no-one cares, replace this with a simple fgetwc loop? | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 422 | n = 0; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 423 | if (flags & ALLOCATE) { | 
|  | 424 | capacity = MIN(width, 32); | 
|  | 425 | allocation = wcp = reinterpret_cast<wchar_t*>(malloc(sizeof(wchar_t) * capacity)); | 
|  | 426 | if (allocation == NULL) goto allocation_failure; | 
|  | 427 | } else if (flags & SUPPRESS) { | 
|  | 428 | wcp = NULL; | 
|  | 429 | } else { | 
|  | 430 | wcp = va_arg(ap, wchar_t*); | 
|  | 431 | } | 
|  | 432 | size_t bytes = 0; | 
| Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 433 | while ((c == CT_CCL || !IsSpace(*fp->_p)) && width != 0) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 434 | if (bytes == MB_CUR_MAX) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 435 | fp->_flags |= __SERR; | 
|  | 436 | goto input_failure; | 
|  | 437 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 438 | buf[bytes++] = *fp->_p; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 439 | fp->_p++; | 
|  | 440 | fp->_r--; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 441 | wchar_t wc = L'\0'; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 442 | memset(&mbs, 0, sizeof(mbs)); | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 443 | nconv = mbrtowc(&wc, buf, bytes, &mbs); | 
|  | 444 | if (nconv == __MB_ERR_ILLEGAL_SEQUENCE) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 445 | fp->_flags |= __SERR; | 
|  | 446 | goto input_failure; | 
|  | 447 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 448 | if (nconv != __MB_ERR_INCOMPLETE_SEQUENCE) { | 
|  | 449 | if ((c == CT_CCL && wctob(wc) != EOF && !ccltab[wctob(wc)]) || (c == CT_STRING && iswspace(wc))) { | 
|  | 450 | while (bytes != 0) { | 
|  | 451 | bytes--; | 
|  | 452 | ungetc(buf[bytes], fp); | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 453 | } | 
|  | 454 | break; | 
|  | 455 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 456 | if (wcp) wcp[n] = wc; | 
|  | 457 | n++; | 
|  | 458 | if (allocation != NULL && n == capacity) { | 
|  | 459 | capacity *= 2; | 
|  | 460 | wchar_t* new_allocation = | 
|  | 461 | reinterpret_cast<wchar_t*>(realloc(allocation, sizeof(wchar_t) * capacity)); | 
|  | 462 | if (new_allocation == NULL) goto allocation_failure; | 
|  | 463 | allocation = wcp = new_allocation; | 
|  | 464 | } | 
|  | 465 | nread += bytes; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 466 | width--; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 467 | bytes = 0; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 468 | } | 
|  | 469 | if (fp->_r <= 0 && __srefill(fp)) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 470 | if (bytes != 0) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 471 | fp->_flags |= __SERR; | 
|  | 472 | goto input_failure; | 
|  | 473 | } | 
|  | 474 | break; | 
|  | 475 | } | 
|  | 476 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 477 | if (c == CT_CCL && bytes != 0) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 478 | fp->_flags |= __SERR; | 
|  | 479 | goto input_failure; | 
|  | 480 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 481 | if (allocation != NULL) { | 
|  | 482 | *va_arg(ap, wchar_t**) = reinterpret_cast<wchar_t*>(allocation); | 
|  | 483 | allocation = NULL; | 
|  | 484 | } | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 485 | } else if (flags & SUPPRESS) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 486 | n = 0; | 
| Elliott Hughes | 3048a36 | 2018-01-19 17:58:07 -0800 | [diff] [blame] | 487 | while (ccltab[*fp->_p]) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 488 | n++, fp->_r--, fp->_p++; | 
|  | 489 | if (--width == 0) break; | 
|  | 490 | if (fp->_r <= 0 && __srefill(fp)) { | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 491 | if (c == CT_CCL && n == 0) goto input_failure; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 492 | break; | 
|  | 493 | } | 
|  | 494 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 495 | nread += n; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 496 | } else { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 497 | if (flags & ALLOCATE) { | 
|  | 498 | capacity = MIN(width, 32); | 
|  | 499 | allocation = p = reinterpret_cast<char*>(malloc(capacity)); | 
|  | 500 | if (allocation == NULL) goto allocation_failure; | 
|  | 501 | } else { | 
|  | 502 | p = va_arg(ap, char*); | 
|  | 503 | } | 
|  | 504 | n = 0; | 
| Elliott Hughes | 3048a36 | 2018-01-19 17:58:07 -0800 | [diff] [blame] | 505 | while (ccltab[*fp->_p]) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 506 | fp->_r--; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 507 | p[n++] = *fp->_p++; | 
|  | 508 | if (allocation != NULL && n == capacity) { | 
|  | 509 | capacity *= 2; | 
|  | 510 | char* new_allocation = reinterpret_cast<char*>(realloc(allocation, capacity)); | 
|  | 511 | if (new_allocation == NULL) goto allocation_failure; | 
|  | 512 | allocation = p = new_allocation; | 
|  | 513 | } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 514 | if (--width == 0) break; | 
|  | 515 | if (fp->_r <= 0 && __srefill(fp)) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 516 | if (c == CT_CCL && n == 0) goto input_failure; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 517 | break; | 
|  | 518 | } | 
|  | 519 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 520 | nread += n; | 
|  | 521 | if (allocation != NULL) { | 
|  | 522 | *va_arg(ap, char**) = reinterpret_cast<char*>(allocation); | 
|  | 523 | allocation = NULL; | 
|  | 524 | } | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 525 | } | 
|  | 526 | if (c == CT_CCL && n == 0) goto match_failure; | 
|  | 527 | if (!(flags & SUPPRESS)) { | 
|  | 528 | if (flags & LONG) { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 529 | wcp[n] = L'\0'; | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 530 | } else { | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 531 | p[n] = '\0'; | 
| Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 532 | } | 
|  | 533 | ++nassigned; | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 534 | } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 535 | break; | 
| Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 536 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 537 | case CT_INT: | 
|  | 538 | /* scan an integer as if by strtoimax/strtoumax */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 539 | #ifdef hardway | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 540 | if (width == 0 || width > sizeof(buf) - 1) width = sizeof(buf) - 1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 541 | #else | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 542 | /* size_t is unsigned, hence this optimisation */ | 
|  | 543 | if (--width > sizeof(buf) - 2) width = sizeof(buf) - 2; | 
|  | 544 | width++; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 545 | #endif | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 546 | flags |= SIGNOK | NDIGITS | NZDIGITS; | 
|  | 547 | for (p = buf; width; width--) { | 
|  | 548 | c = *fp->_p; | 
|  | 549 | /* | 
|  | 550 | * Switch on the character; `goto ok' | 
|  | 551 | * if we accept it as a part of number. | 
|  | 552 | */ | 
|  | 553 | switch (c) { | 
|  | 554 | /* | 
|  | 555 | * The digit 0 is always legal, but is | 
|  | 556 | * special.  For %i conversions, if no | 
|  | 557 | * digits (zero or nonzero) have been | 
|  | 558 | * scanned (only signs), we will have | 
|  | 559 | * base==0.  In that case, we should set | 
|  | 560 | * it to 8 and enable 0x prefixing. | 
|  | 561 | * Also, if we have not scanned zero digits | 
|  | 562 | * before this, do not turn off prefixing | 
|  | 563 | * (someone else will turn it off if we | 
|  | 564 | * have scanned any nonzero digits). | 
|  | 565 | */ | 
|  | 566 | case '0': | 
|  | 567 | if (base == 0) { | 
|  | 568 | base = 8; | 
|  | 569 | flags |= PFXOK; | 
|  | 570 | } | 
|  | 571 | if (flags & NZDIGITS) | 
|  | 572 | flags &= ~(SIGNOK | NZDIGITS | NDIGITS); | 
|  | 573 | else | 
|  | 574 | flags &= ~(SIGNOK | PFXOK | NDIGITS); | 
|  | 575 | goto ok; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 576 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 577 | /* 1 through 7 always legal */ | 
|  | 578 | case '1': | 
|  | 579 | case '2': | 
|  | 580 | case '3': | 
|  | 581 | case '4': | 
|  | 582 | case '5': | 
|  | 583 | case '6': | 
|  | 584 | case '7': | 
|  | 585 | base = basefix[base]; | 
|  | 586 | flags &= ~(SIGNOK | PFXOK | NDIGITS); | 
|  | 587 | goto ok; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 588 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 589 | /* digits 8 and 9 ok iff decimal or hex */ | 
|  | 590 | case '8': | 
|  | 591 | case '9': | 
|  | 592 | base = basefix[base]; | 
|  | 593 | if (base <= 8) break; /* not legal here */ | 
|  | 594 | flags &= ~(SIGNOK | PFXOK | NDIGITS); | 
|  | 595 | goto ok; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 596 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 597 | /* letters ok iff hex */ | 
|  | 598 | case 'A': | 
|  | 599 | case 'B': | 
|  | 600 | case 'C': | 
|  | 601 | case 'D': | 
|  | 602 | case 'E': | 
|  | 603 | case 'F': | 
|  | 604 | case 'a': | 
|  | 605 | case 'b': | 
|  | 606 | case 'c': | 
|  | 607 | case 'd': | 
|  | 608 | case 'e': | 
|  | 609 | case 'f': | 
|  | 610 | /* no need to fix base here */ | 
|  | 611 | if (base <= 10) break; /* not legal here */ | 
|  | 612 | flags &= ~(SIGNOK | PFXOK | NDIGITS); | 
|  | 613 | goto ok; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 614 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 615 | /* sign ok only as first character */ | 
|  | 616 | case '+': | 
|  | 617 | case '-': | 
|  | 618 | if (flags & SIGNOK) { | 
|  | 619 | flags &= ~SIGNOK; | 
|  | 620 | flags |= HAVESIGN; | 
|  | 621 | goto ok; | 
|  | 622 | } | 
|  | 623 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 624 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 625 | /* | 
|  | 626 | * x ok iff flag still set and 2nd char (or | 
|  | 627 | * 3rd char if we have a sign). | 
|  | 628 | */ | 
|  | 629 | case 'x': | 
|  | 630 | case 'X': | 
|  | 631 | if ((flags & PFXOK) && p == buf + 1 + !!(flags & HAVESIGN)) { | 
|  | 632 | base = 16; /* if %i */ | 
|  | 633 | flags &= ~PFXOK; | 
|  | 634 | goto ok; | 
|  | 635 | } | 
|  | 636 | break; | 
|  | 637 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 638 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 639 | /* | 
|  | 640 | * If we got here, c is not a legal character | 
|  | 641 | * for a number.  Stop accumulating digits. | 
|  | 642 | */ | 
|  | 643 | break; | 
|  | 644 | ok: | 
|  | 645 | /* | 
|  | 646 | * c is legal: store it and look at the next. | 
|  | 647 | */ | 
|  | 648 | *p++ = c; | 
|  | 649 | if (--fp->_r > 0) | 
|  | 650 | fp->_p++; | 
|  | 651 | else if (__srefill(fp)) | 
|  | 652 | break; /* EOF */ | 
|  | 653 | } | 
|  | 654 | /* | 
|  | 655 | * If we had only a sign, it is no good; push | 
|  | 656 | * back the sign.  If the number ends in `x', | 
|  | 657 | * it was [sign] '0' 'x', so push back the x | 
|  | 658 | * and treat it as [sign] '0'. | 
|  | 659 | */ | 
|  | 660 | if (flags & NDIGITS) { | 
|  | 661 | if (p > buf) (void)ungetc(*(u_char*)--p, fp); | 
|  | 662 | goto match_failure; | 
|  | 663 | } | 
|  | 664 | c = ((u_char*)p)[-1]; | 
|  | 665 | if (c == 'x' || c == 'X') { | 
|  | 666 | --p; | 
|  | 667 | (void)ungetc(c, fp); | 
|  | 668 | } | 
|  | 669 | if ((flags & SUPPRESS) == 0) { | 
|  | 670 | uintmax_t res; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 671 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 672 | *p = '\0'; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 673 | if (flags & UNSIGNED) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 674 | res = strtoumax(buf, NULL, base); | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 675 | } else { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 676 | res = strtoimax(buf, NULL, base); | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 677 | } | 
|  | 678 | if (flags & POINTER) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 679 | *va_arg(ap, void**) = (void*)(uintptr_t)res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 680 | } else if (flags & MAXINT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 681 | *va_arg(ap, intmax_t*) = res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 682 | } else if (flags & LLONG) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 683 | *va_arg(ap, long long*) = res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 684 | } else if (flags & SIZEINT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 685 | *va_arg(ap, ssize_t*) = res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 686 | } else if (flags & PTRINT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 687 | *va_arg(ap, ptrdiff_t*) = res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 688 | } else if (flags & LONG) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 689 | *va_arg(ap, long*) = res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 690 | } else if (flags & SHORT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 691 | *va_arg(ap, short*) = res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 692 | } else if (flags & SHORTSHORT) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 693 | *va_arg(ap, signed char*) = res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 694 | } else { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 695 | *va_arg(ap, int*) = res; | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 696 | } | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 697 | nassigned++; | 
|  | 698 | } | 
|  | 699 | nread += p - buf; | 
|  | 700 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 701 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 702 | case CT_FLOAT: | 
|  | 703 | /* scan a floating point number as if by strtod */ | 
|  | 704 | if (width == 0 || width > sizeof(buf) - 1) width = sizeof(buf) - 1; | 
|  | 705 | if ((width = parsefloat(fp, buf, buf + width)) == 0) goto match_failure; | 
|  | 706 | if ((flags & SUPPRESS) == 0) { | 
|  | 707 | if (flags & LONGDBL) { | 
|  | 708 | long double res = strtold(buf, &p); | 
|  | 709 | *va_arg(ap, long double*) = res; | 
|  | 710 | } else if (flags & LONG) { | 
|  | 711 | double res = strtod(buf, &p); | 
|  | 712 | *va_arg(ap, double*) = res; | 
|  | 713 | } else { | 
|  | 714 | float res = strtof(buf, &p); | 
|  | 715 | *va_arg(ap, float*) = res; | 
|  | 716 | } | 
|  | 717 | if ((size_t)(p - buf) != width) abort(); | 
|  | 718 | nassigned++; | 
|  | 719 | } | 
|  | 720 | nread += width; | 
|  | 721 | break; | 
|  | 722 | } | 
|  | 723 | } | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 724 | allocation_failure: | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 725 | input_failure: | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 726 | free(allocation); | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 727 | if (nassigned == 0) nassigned = -1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 728 | match_failure: | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 729 | return nassigned; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 730 | } | 
|  | 731 |  | 
|  | 732 | /* | 
|  | 733 | * Fill in the given table from the scanset at the given format | 
|  | 734 | * (just after `[').  Return a pointer to the character past the | 
|  | 735 | * closing `]'.  The table has a 1 wherever characters should be | 
|  | 736 | * considered part of the scanset. | 
|  | 737 | */ | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 738 | static const unsigned char* __sccl(char* tab, const unsigned char* fmt) { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 739 | int c, n, v; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 740 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 741 | /* first `clear' the whole table */ | 
|  | 742 | c = *fmt++; /* first char hat => negated scanset */ | 
|  | 743 | if (c == '^') { | 
|  | 744 | v = 1;      /* default => accept */ | 
|  | 745 | c = *fmt++; /* get new first char */ | 
| Elliott Hughes | 3048a36 | 2018-01-19 17:58:07 -0800 | [diff] [blame] | 746 | } else { | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 747 | v = 0; /* default => reject */ | 
| Elliott Hughes | 3048a36 | 2018-01-19 17:58:07 -0800 | [diff] [blame] | 748 | } | 
|  | 749 | memset(tab, v, 256); | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 750 | if (c == 0) return (fmt - 1); /* format ended before closing ] */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 751 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 752 | /* | 
|  | 753 | * Now set the entries corresponding to the actual scanset | 
|  | 754 | * to the opposite of the above. | 
|  | 755 | * | 
|  | 756 | * The first character may be ']' (or '-') without being special; | 
|  | 757 | * the last character may be '-'. | 
|  | 758 | */ | 
|  | 759 | v = 1 - v; | 
|  | 760 | for (;;) { | 
|  | 761 | tab[c] = v; /* take character c */ | 
|  | 762 | doswitch: | 
|  | 763 | n = *fmt++; /* and examine the next */ | 
|  | 764 | switch (n) { | 
|  | 765 | case 0: /* format ended too soon */ | 
|  | 766 | return (fmt - 1); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 767 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 768 | case '-': | 
|  | 769 | /* | 
|  | 770 | * A scanset of the form | 
|  | 771 | *	[01+-] | 
|  | 772 | * is defined as `the digit 0, the digit 1, | 
|  | 773 | * the character +, the character -', but | 
|  | 774 | * the effect of a scanset such as | 
|  | 775 | *	[a-zA-Z0-9] | 
|  | 776 | * is implementation defined.  The V7 Unix | 
|  | 777 | * scanf treats `a-z' as `the letters a through | 
|  | 778 | * z', but treats `a-a' as `the letter a, the | 
|  | 779 | * character -, and the letter a'. | 
|  | 780 | * | 
|  | 781 | * For compatibility, the `-' is not considerd | 
|  | 782 | * to define a range if the character following | 
|  | 783 | * it is either a close bracket (required by ANSI) | 
|  | 784 | * or is not numerically greater than the character | 
|  | 785 | * we just stored in the table (c). | 
|  | 786 | */ | 
|  | 787 | n = *fmt; | 
|  | 788 | if (n == ']' || n < c) { | 
|  | 789 | c = '-'; | 
|  | 790 | break; /* resume the for(;;) */ | 
|  | 791 | } | 
|  | 792 | fmt++; | 
|  | 793 | do { /* fill in the range */ | 
|  | 794 | tab[++c] = v; | 
|  | 795 | } while (c < n); | 
|  | 796 | #if 1 /* XXX another disgusting compatibility hack */ | 
|  | 797 | /* | 
|  | 798 | * Alas, the V7 Unix scanf also treats formats | 
|  | 799 | * such as [a-c-e] as `the letters a through e'. | 
|  | 800 | * This too is permitted by the standard.... | 
|  | 801 | */ | 
|  | 802 | goto doswitch; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 803 | #else | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 804 | c = *fmt++; | 
|  | 805 | if (c == 0) return (fmt - 1); | 
|  | 806 | if (c == ']') return (fmt); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 807 | #endif | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 808 | break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 809 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 810 | case ']': /* end of scanset */ | 
| Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 811 | return fmt; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 812 |  | 
| Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 813 | default: /* just another character */ | 
|  | 814 | c = n; | 
|  | 815 | break; | 
|  | 816 | } | 
|  | 817 | } | 
|  | 818 | /* NOTREACHED */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 819 | } |