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