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': |
| 238 | c = CT_STRING; |
| 239 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 240 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 241 | case '[': |
| 242 | fmt = __sccl(ccltab, fmt); |
| 243 | flags |= NOSKIP; |
| 244 | c = CT_CCL; |
| 245 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 246 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 247 | case 'c': |
| 248 | flags |= NOSKIP; |
| 249 | c = CT_CHAR; |
| 250 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 251 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 252 | case 'p': /* pointer format is like hex */ |
| 253 | flags |= POINTER | PFXOK; |
| 254 | c = CT_INT; |
| 255 | flags |= UNSIGNED; |
| 256 | base = 16; |
| 257 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 258 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 259 | case 'n': |
| 260 | if (flags & SUPPRESS) continue; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 261 | if (flags & SHORTSHORT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 262 | *va_arg(ap, signed char*) = nread; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 263 | } else if (flags & SHORT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 264 | *va_arg(ap, short*) = nread; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 265 | } else if (flags & LONG) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 266 | *va_arg(ap, long*) = nread; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 267 | } else if (flags & SIZEINT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 268 | *va_arg(ap, ssize_t*) = nread; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 269 | } else if (flags & PTRINT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 270 | *va_arg(ap, ptrdiff_t*) = nread; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 271 | } else if (flags & LLONG) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 272 | *va_arg(ap, long long*) = nread; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 273 | } else if (flags & MAXINT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 274 | *va_arg(ap, intmax_t*) = nread; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 275 | } else { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 276 | *va_arg(ap, int*) = nread; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 277 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 278 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 279 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 280 | /* |
| 281 | * Disgusting backwards compatibility hacks. XXX |
| 282 | */ |
| 283 | case '\0': /* compat */ |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 284 | return EOF; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 285 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 286 | default: /* compat */ |
Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 287 | if (IsUpper(c)) flags |= LONG; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 288 | c = CT_INT; |
| 289 | base = 10; |
| 290 | break; |
| 291 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 292 | |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 293 | if ((flags & ALLOCATE) != 0 && c > CT_STRING) { |
| 294 | __fortify_fatal("scanf 'm' only works with %%c/%%s/%%["); |
| 295 | } |
| 296 | if ((flags & (ALLOCATE|SUPPRESS)) == (ALLOCATE|SUPPRESS)) { |
| 297 | __fortify_fatal("scanf 'm' makes no sense with '*'"); |
| 298 | } |
| 299 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 300 | /* |
| 301 | * We have a conversion that requires input. |
| 302 | */ |
| 303 | if (fp->_r <= 0 && __srefill(fp)) goto input_failure; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 304 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 305 | /* |
| 306 | * Consume leading white space, except for formats |
| 307 | * that suppress this. |
| 308 | */ |
| 309 | if ((flags & NOSKIP) == 0) { |
Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 310 | while (IsSpace(*fp->_p)) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 311 | nread++; |
Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 312 | if (--fp->_r > 0) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 313 | fp->_p++; |
Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 314 | } else if (__srefill(fp)) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 315 | goto input_failure; |
Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 316 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 317 | } |
| 318 | /* |
| 319 | * Note that there is at least one character in |
| 320 | * the buffer, so conversions that do not set NOSKIP |
| 321 | * ca no longer result in an input failure. |
| 322 | */ |
| 323 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 324 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 325 | /* |
| 326 | * Do the conversion. |
| 327 | */ |
| 328 | switch (c) { |
| 329 | case CT_CHAR: |
| 330 | /* scan arbitrary characters (sets NOSKIP) */ |
| 331 | if (width == 0) width = 1; |
| 332 | if (flags & LONG) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 333 | if (flags & ALLOCATE) { |
| 334 | allocation = wcp = reinterpret_cast<wchar_t*>(malloc(width * sizeof(wchar_t))); |
| 335 | if (allocation == NULL) goto allocation_failure; |
| 336 | } else if (flags & SUPPRESS) { |
| 337 | wcp = NULL; |
| 338 | } else { |
| 339 | wcp = va_arg(ap, wchar_t*); |
| 340 | } |
| 341 | size_t bytes = 0; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 342 | while (width != 0) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 343 | if (bytes == MB_CUR_MAX) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 344 | fp->_flags |= __SERR; |
| 345 | goto input_failure; |
| 346 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 347 | buf[bytes++] = *fp->_p; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 348 | fp->_p++; |
| 349 | fp->_r--; |
| 350 | memset(&mbs, 0, sizeof(mbs)); |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 351 | nconv = mbrtowc(wcp, buf, bytes, &mbs); |
| 352 | if (nconv == __MB_ERR_ILLEGAL_SEQUENCE) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 353 | fp->_flags |= __SERR; |
| 354 | goto input_failure; |
| 355 | } |
| 356 | if (nconv == 0 && !(flags & SUPPRESS)) *wcp = L'\0'; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 357 | if (nconv != __MB_ERR_INCOMPLETE_SEQUENCE) { |
| 358 | nread += bytes; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 359 | width--; |
| 360 | if (!(flags & SUPPRESS)) wcp++; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 361 | bytes = 0; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 362 | } |
| 363 | if (fp->_r <= 0 && __srefill(fp)) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 364 | if (bytes != 0) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 365 | fp->_flags |= __SERR; |
| 366 | goto input_failure; |
| 367 | } |
| 368 | break; |
| 369 | } |
| 370 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 371 | if (allocation != NULL) { |
| 372 | *va_arg(ap, wchar_t**) = reinterpret_cast<wchar_t*>(allocation); |
| 373 | allocation = NULL; |
| 374 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 375 | if (!(flags & SUPPRESS)) nassigned++; |
| 376 | } else if (flags & SUPPRESS) { |
| 377 | size_t sum = 0; |
| 378 | for (;;) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 379 | if ((n = fp->_r) < width) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 380 | sum += n; |
| 381 | width -= n; |
| 382 | fp->_p += n; |
| 383 | if (__srefill(fp)) { |
| 384 | if (sum == 0) goto input_failure; |
| 385 | break; |
| 386 | } |
| 387 | } else { |
| 388 | sum += width; |
| 389 | fp->_r -= width; |
| 390 | fp->_p += width; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | nread += sum; |
| 395 | } else { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 396 | if (flags & ALLOCATE) { |
| 397 | allocation = p = reinterpret_cast<char*>(malloc(width)); |
| 398 | if (allocation == NULL) goto allocation_failure; |
| 399 | } else { |
| 400 | p = va_arg(ap, char*); |
| 401 | } |
| 402 | size_t r = fread(p, 1, width, fp); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 403 | if (r == 0) goto input_failure; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 404 | if (allocation != NULL) { |
| 405 | *va_arg(ap, char**) = reinterpret_cast<char*>(allocation); |
| 406 | allocation = NULL; |
| 407 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 408 | nread += r; |
| 409 | nassigned++; |
| 410 | } |
| 411 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 412 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 413 | case CT_CCL: |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 414 | case CT_STRING: |
| 415 | // CT_CCL: scan a (nonempty) character class (sets NOSKIP). |
| 416 | // CT_STRING: like CCL, but zero-length string OK, & no NOSKIP. |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 417 | if (width == 0) width = SIZE_MAX; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 418 | if (flags & LONG) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 419 | // TODO: since no-one cares, replace this with a simple fgetwc loop? |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 420 | n = 0; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 421 | if (flags & ALLOCATE) { |
| 422 | capacity = MIN(width, 32); |
| 423 | allocation = wcp = reinterpret_cast<wchar_t*>(malloc(sizeof(wchar_t) * capacity)); |
| 424 | if (allocation == NULL) goto allocation_failure; |
| 425 | } else if (flags & SUPPRESS) { |
| 426 | wcp = NULL; |
| 427 | } else { |
| 428 | wcp = va_arg(ap, wchar_t*); |
| 429 | } |
| 430 | size_t bytes = 0; |
Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 431 | while ((c == CT_CCL || !IsSpace(*fp->_p)) && width != 0) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 432 | if (bytes == MB_CUR_MAX) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 433 | fp->_flags |= __SERR; |
| 434 | goto input_failure; |
| 435 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 436 | buf[bytes++] = *fp->_p; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 437 | fp->_p++; |
| 438 | fp->_r--; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 439 | wchar_t wc = L'\0'; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 440 | memset(&mbs, 0, sizeof(mbs)); |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 441 | nconv = mbrtowc(&wc, buf, bytes, &mbs); |
| 442 | if (nconv == __MB_ERR_ILLEGAL_SEQUENCE) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 443 | fp->_flags |= __SERR; |
| 444 | goto input_failure; |
| 445 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 446 | if (nconv != __MB_ERR_INCOMPLETE_SEQUENCE) { |
| 447 | if ((c == CT_CCL && wctob(wc) != EOF && !ccltab[wctob(wc)]) || (c == CT_STRING && iswspace(wc))) { |
| 448 | while (bytes != 0) { |
| 449 | bytes--; |
| 450 | ungetc(buf[bytes], fp); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 451 | } |
| 452 | break; |
| 453 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 454 | if (wcp) wcp[n] = wc; |
| 455 | n++; |
| 456 | if (allocation != NULL && n == capacity) { |
| 457 | capacity *= 2; |
| 458 | wchar_t* new_allocation = |
| 459 | reinterpret_cast<wchar_t*>(realloc(allocation, sizeof(wchar_t) * capacity)); |
| 460 | if (new_allocation == NULL) goto allocation_failure; |
| 461 | allocation = wcp = new_allocation; |
| 462 | } |
| 463 | nread += bytes; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 464 | width--; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 465 | bytes = 0; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 466 | } |
| 467 | if (fp->_r <= 0 && __srefill(fp)) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 468 | if (bytes != 0) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 469 | fp->_flags |= __SERR; |
| 470 | goto input_failure; |
| 471 | } |
| 472 | break; |
| 473 | } |
| 474 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 475 | if (c == CT_CCL && bytes != 0) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 476 | fp->_flags |= __SERR; |
| 477 | goto input_failure; |
| 478 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 479 | if (allocation != NULL) { |
| 480 | *va_arg(ap, wchar_t**) = reinterpret_cast<wchar_t*>(allocation); |
| 481 | allocation = NULL; |
| 482 | } |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 483 | } else if (flags & SUPPRESS) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 484 | n = 0; |
Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 485 | while ((c == CT_CCL && ccltab[*fp->_p]) || (c == CT_STRING && !IsSpace(*fp->_p))) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 486 | n++, fp->_r--, fp->_p++; |
| 487 | if (--width == 0) break; |
| 488 | if (fp->_r <= 0 && __srefill(fp)) { |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 489 | if (c == CT_CCL && n == 0) goto input_failure; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 493 | nread += n; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 494 | } else { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 495 | if (flags & ALLOCATE) { |
| 496 | capacity = MIN(width, 32); |
| 497 | allocation = p = reinterpret_cast<char*>(malloc(capacity)); |
| 498 | if (allocation == NULL) goto allocation_failure; |
| 499 | } else { |
| 500 | p = va_arg(ap, char*); |
| 501 | } |
| 502 | n = 0; |
Elliott Hughes | 1133fec | 2017-12-19 16:30:55 -0800 | [diff] [blame] | 503 | while ((c == CT_CCL && ccltab[*fp->_p]) || (c == CT_STRING && !IsSpace(*fp->_p))) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 504 | fp->_r--; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 505 | p[n++] = *fp->_p++; |
| 506 | if (allocation != NULL && n == capacity) { |
| 507 | capacity *= 2; |
| 508 | char* new_allocation = reinterpret_cast<char*>(realloc(allocation, capacity)); |
| 509 | if (new_allocation == NULL) goto allocation_failure; |
| 510 | allocation = p = new_allocation; |
| 511 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 512 | if (--width == 0) break; |
| 513 | if (fp->_r <= 0 && __srefill(fp)) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 514 | if (c == CT_CCL && n == 0) goto input_failure; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 515 | break; |
| 516 | } |
| 517 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 518 | nread += n; |
| 519 | if (allocation != NULL) { |
| 520 | *va_arg(ap, char**) = reinterpret_cast<char*>(allocation); |
| 521 | allocation = NULL; |
| 522 | } |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 523 | } |
| 524 | if (c == CT_CCL && n == 0) goto match_failure; |
| 525 | if (!(flags & SUPPRESS)) { |
| 526 | if (flags & LONG) { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 527 | wcp[n] = L'\0'; |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 528 | } else { |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 529 | p[n] = '\0'; |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 530 | } |
| 531 | ++nassigned; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 532 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 533 | break; |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 534 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 535 | case CT_INT: |
| 536 | /* scan an integer as if by strtoimax/strtoumax */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 537 | #ifdef hardway |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 538 | 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] | 539 | #else |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 540 | /* size_t is unsigned, hence this optimisation */ |
| 541 | if (--width > sizeof(buf) - 2) width = sizeof(buf) - 2; |
| 542 | width++; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 543 | #endif |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 544 | flags |= SIGNOK | NDIGITS | NZDIGITS; |
| 545 | for (p = buf; width; width--) { |
| 546 | c = *fp->_p; |
| 547 | /* |
| 548 | * Switch on the character; `goto ok' |
| 549 | * if we accept it as a part of number. |
| 550 | */ |
| 551 | switch (c) { |
| 552 | /* |
| 553 | * The digit 0 is always legal, but is |
| 554 | * special. For %i conversions, if no |
| 555 | * digits (zero or nonzero) have been |
| 556 | * scanned (only signs), we will have |
| 557 | * base==0. In that case, we should set |
| 558 | * it to 8 and enable 0x prefixing. |
| 559 | * Also, if we have not scanned zero digits |
| 560 | * before this, do not turn off prefixing |
| 561 | * (someone else will turn it off if we |
| 562 | * have scanned any nonzero digits). |
| 563 | */ |
| 564 | case '0': |
| 565 | if (base == 0) { |
| 566 | base = 8; |
| 567 | flags |= PFXOK; |
| 568 | } |
| 569 | if (flags & NZDIGITS) |
| 570 | flags &= ~(SIGNOK | NZDIGITS | NDIGITS); |
| 571 | else |
| 572 | flags &= ~(SIGNOK | PFXOK | NDIGITS); |
| 573 | goto ok; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 574 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 575 | /* 1 through 7 always legal */ |
| 576 | case '1': |
| 577 | case '2': |
| 578 | case '3': |
| 579 | case '4': |
| 580 | case '5': |
| 581 | case '6': |
| 582 | case '7': |
| 583 | base = basefix[base]; |
| 584 | flags &= ~(SIGNOK | PFXOK | NDIGITS); |
| 585 | goto ok; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 586 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 587 | /* digits 8 and 9 ok iff decimal or hex */ |
| 588 | case '8': |
| 589 | case '9': |
| 590 | base = basefix[base]; |
| 591 | if (base <= 8) break; /* not legal here */ |
| 592 | flags &= ~(SIGNOK | PFXOK | NDIGITS); |
| 593 | goto ok; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 594 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 595 | /* letters ok iff hex */ |
| 596 | case 'A': |
| 597 | case 'B': |
| 598 | case 'C': |
| 599 | case 'D': |
| 600 | case 'E': |
| 601 | case 'F': |
| 602 | case 'a': |
| 603 | case 'b': |
| 604 | case 'c': |
| 605 | case 'd': |
| 606 | case 'e': |
| 607 | case 'f': |
| 608 | /* no need to fix base here */ |
| 609 | if (base <= 10) break; /* not legal here */ |
| 610 | flags &= ~(SIGNOK | PFXOK | NDIGITS); |
| 611 | goto ok; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 612 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 613 | /* sign ok only as first character */ |
| 614 | case '+': |
| 615 | case '-': |
| 616 | if (flags & SIGNOK) { |
| 617 | flags &= ~SIGNOK; |
| 618 | flags |= HAVESIGN; |
| 619 | goto ok; |
| 620 | } |
| 621 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 622 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 623 | /* |
| 624 | * x ok iff flag still set and 2nd char (or |
| 625 | * 3rd char if we have a sign). |
| 626 | */ |
| 627 | case 'x': |
| 628 | case 'X': |
| 629 | if ((flags & PFXOK) && p == buf + 1 + !!(flags & HAVESIGN)) { |
| 630 | base = 16; /* if %i */ |
| 631 | flags &= ~PFXOK; |
| 632 | goto ok; |
| 633 | } |
| 634 | break; |
| 635 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 636 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 637 | /* |
| 638 | * If we got here, c is not a legal character |
| 639 | * for a number. Stop accumulating digits. |
| 640 | */ |
| 641 | break; |
| 642 | ok: |
| 643 | /* |
| 644 | * c is legal: store it and look at the next. |
| 645 | */ |
| 646 | *p++ = c; |
| 647 | if (--fp->_r > 0) |
| 648 | fp->_p++; |
| 649 | else if (__srefill(fp)) |
| 650 | break; /* EOF */ |
| 651 | } |
| 652 | /* |
| 653 | * If we had only a sign, it is no good; push |
| 654 | * back the sign. If the number ends in `x', |
| 655 | * it was [sign] '0' 'x', so push back the x |
| 656 | * and treat it as [sign] '0'. |
| 657 | */ |
| 658 | if (flags & NDIGITS) { |
| 659 | if (p > buf) (void)ungetc(*(u_char*)--p, fp); |
| 660 | goto match_failure; |
| 661 | } |
| 662 | c = ((u_char*)p)[-1]; |
| 663 | if (c == 'x' || c == 'X') { |
| 664 | --p; |
| 665 | (void)ungetc(c, fp); |
| 666 | } |
| 667 | if ((flags & SUPPRESS) == 0) { |
| 668 | uintmax_t res; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 669 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 670 | *p = '\0'; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 671 | if (flags & UNSIGNED) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 672 | res = strtoumax(buf, NULL, base); |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 673 | } else { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 674 | res = strtoimax(buf, NULL, base); |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 675 | } |
| 676 | if (flags & POINTER) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 677 | *va_arg(ap, void**) = (void*)(uintptr_t)res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 678 | } else if (flags & MAXINT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 679 | *va_arg(ap, intmax_t*) = res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 680 | } else if (flags & LLONG) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 681 | *va_arg(ap, long long*) = res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 682 | } else if (flags & SIZEINT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 683 | *va_arg(ap, ssize_t*) = res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 684 | } else if (flags & PTRINT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 685 | *va_arg(ap, ptrdiff_t*) = res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 686 | } else if (flags & LONG) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 687 | *va_arg(ap, long*) = res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 688 | } else if (flags & SHORT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 689 | *va_arg(ap, short*) = res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 690 | } else if (flags & SHORTSHORT) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 691 | *va_arg(ap, signed char*) = res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 692 | } else { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 693 | *va_arg(ap, int*) = res; |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 694 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 695 | nassigned++; |
| 696 | } |
| 697 | nread += p - buf; |
| 698 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 699 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 700 | case CT_FLOAT: |
| 701 | /* scan a floating point number as if by strtod */ |
| 702 | if (width == 0 || width > sizeof(buf) - 1) width = sizeof(buf) - 1; |
| 703 | if ((width = parsefloat(fp, buf, buf + width)) == 0) goto match_failure; |
| 704 | if ((flags & SUPPRESS) == 0) { |
| 705 | if (flags & LONGDBL) { |
| 706 | long double res = strtold(buf, &p); |
| 707 | *va_arg(ap, long double*) = res; |
| 708 | } else if (flags & LONG) { |
| 709 | double res = strtod(buf, &p); |
| 710 | *va_arg(ap, double*) = res; |
| 711 | } else { |
| 712 | float res = strtof(buf, &p); |
| 713 | *va_arg(ap, float*) = res; |
| 714 | } |
| 715 | if ((size_t)(p - buf) != width) abort(); |
| 716 | nassigned++; |
| 717 | } |
| 718 | nread += width; |
| 719 | break; |
| 720 | } |
| 721 | } |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 722 | allocation_failure: |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 723 | input_failure: |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 724 | free(allocation); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 725 | if (nassigned == 0) nassigned = -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 726 | match_failure: |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 727 | return nassigned; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Fill in the given table from the scanset at the given format |
| 732 | * (just after `['). Return a pointer to the character past the |
| 733 | * closing `]'. The table has a 1 wherever characters should be |
| 734 | * considered part of the scanset. |
| 735 | */ |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 736 | static const unsigned char* __sccl(char* tab, const unsigned char* fmt) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 737 | int c, n, v; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 738 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 739 | /* first `clear' the whole table */ |
| 740 | c = *fmt++; /* first char hat => negated scanset */ |
| 741 | if (c == '^') { |
| 742 | v = 1; /* default => accept */ |
| 743 | c = *fmt++; /* get new first char */ |
| 744 | } else |
| 745 | v = 0; /* default => reject */ |
| 746 | /* should probably use memset here */ |
| 747 | for (n = 0; n < 256; n++) tab[n] = v; |
| 748 | 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] | 749 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 750 | /* |
| 751 | * Now set the entries corresponding to the actual scanset |
| 752 | * to the opposite of the above. |
| 753 | * |
| 754 | * The first character may be ']' (or '-') without being special; |
| 755 | * the last character may be '-'. |
| 756 | */ |
| 757 | v = 1 - v; |
| 758 | for (;;) { |
| 759 | tab[c] = v; /* take character c */ |
| 760 | doswitch: |
| 761 | n = *fmt++; /* and examine the next */ |
| 762 | switch (n) { |
| 763 | case 0: /* format ended too soon */ |
| 764 | return (fmt - 1); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 765 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 766 | case '-': |
| 767 | /* |
| 768 | * A scanset of the form |
| 769 | * [01+-] |
| 770 | * is defined as `the digit 0, the digit 1, |
| 771 | * the character +, the character -', but |
| 772 | * the effect of a scanset such as |
| 773 | * [a-zA-Z0-9] |
| 774 | * is implementation defined. The V7 Unix |
| 775 | * scanf treats `a-z' as `the letters a through |
| 776 | * z', but treats `a-a' as `the letter a, the |
| 777 | * character -, and the letter a'. |
| 778 | * |
| 779 | * For compatibility, the `-' is not considerd |
| 780 | * to define a range if the character following |
| 781 | * it is either a close bracket (required by ANSI) |
| 782 | * or is not numerically greater than the character |
| 783 | * we just stored in the table (c). |
| 784 | */ |
| 785 | n = *fmt; |
| 786 | if (n == ']' || n < c) { |
| 787 | c = '-'; |
| 788 | break; /* resume the for(;;) */ |
| 789 | } |
| 790 | fmt++; |
| 791 | do { /* fill in the range */ |
| 792 | tab[++c] = v; |
| 793 | } while (c < n); |
| 794 | #if 1 /* XXX another disgusting compatibility hack */ |
| 795 | /* |
| 796 | * Alas, the V7 Unix scanf also treats formats |
| 797 | * such as [a-c-e] as `the letters a through e'. |
| 798 | * This too is permitted by the standard.... |
| 799 | */ |
| 800 | goto doswitch; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 801 | #else |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 802 | c = *fmt++; |
| 803 | if (c == 0) return (fmt - 1); |
| 804 | if (c == ']') return (fmt); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 805 | #endif |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 806 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 807 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 808 | case ']': /* end of scanset */ |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame^] | 809 | return fmt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 810 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 811 | default: /* just another character */ |
| 812 | c = n; |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | /* NOTREACHED */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 817 | } |