Elliott Hughes | f1ada79 | 2014-05-02 17:56:56 -0700 | [diff] [blame] | 1 | /* $OpenBSD: vfwscanf.c,v 1.4 2014/03/19 05:17:01 guenther Exp $ */ |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [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" |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 36 | // An interpretive version of __sccl from vfscanf.c --- a table of all wchar_t values would |
| 37 | // be a little too expensive, and some kind of compressed version isn't worth the trouble. |
| 38 | static inline bool in_ccl(wchar_t wc, const wchar_t* ccl) { |
| 39 | // Is this a negated set? |
| 40 | bool member_result = true; |
| 41 | if (*ccl == '^') { |
| 42 | member_result = false; |
| 43 | ++ccl; |
| 44 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 46 | // The first character may be ']' or '-' without being special. |
| 47 | if (*ccl == '-' || *ccl == ']') { |
| 48 | // A literal match? |
| 49 | if (*ccl == wc) return member_result; |
| 50 | ++ccl; |
| 51 | } |
| 52 | |
| 53 | while (*ccl && *ccl != ']') { |
| 54 | // The last character may be '-' without being special. |
| 55 | if (*ccl == '-' && ccl[1] != '\0' && ccl[1] != ']') { |
| 56 | wchar_t first = *(ccl - 1); |
| 57 | wchar_t last = *(ccl + 1); |
| 58 | if (first <= last) { |
| 59 | // In the range? |
| 60 | if (wc >= first && wc <= last) return member_result; |
| 61 | ccl += 2; |
| 62 | continue; |
| 63 | } |
| 64 | // A '-' is not considered to be part of a range if the character after |
| 65 | // is not greater than the character before, so fall through... |
| 66 | } |
| 67 | // A literal match? |
| 68 | if (*ccl == wc) return member_result; |
| 69 | ++ccl; |
| 70 | } |
| 71 | return !member_result; |
| 72 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 74 | #pragma GCC diagnostic push |
| 75 | #pragma GCC diagnostic ignored "-Wframe-larger-than=" |
| 76 | |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 77 | /* |
| 78 | * vfwscanf |
| 79 | */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 80 | int __vfwscanf(FILE* __restrict fp, const wchar_t* __restrict fmt, __va_list ap) { |
| 81 | wint_t c; /* character from format, or conversion */ |
| 82 | size_t width; /* field width, or 0 */ |
| 83 | wchar_t* p; /* points into all kinds of strings */ |
| 84 | int n; /* handy integer */ |
| 85 | int flags; /* flags as defined above */ |
| 86 | wchar_t* p0; /* saves original value of p when necessary */ |
| 87 | int nassigned; /* number of fields assigned */ |
| 88 | int nconversions; /* number of conversions */ |
| 89 | int nread; /* number of characters consumed from fp */ |
| 90 | int base; /* base argument to strtoimax/strtouimax */ |
| 91 | wchar_t buf[BUF]; /* buffer for numeric conversions */ |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 92 | const wchar_t* ccl; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 93 | wint_t wi; /* handy wint_t */ |
| 94 | char* mbp; /* multibyte string pointer for %c %s %[ */ |
| 95 | size_t nconv; /* number of bytes in mb. conversion */ |
| 96 | char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */ |
| 97 | mbstate_t mbs; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 98 | |
Elliott Hughes | 531199c | 2023-05-09 16:11:49 -0700 | [diff] [blame] | 99 | _SET_ORIENTATION(fp, ORIENT_CHARS); |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 100 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 101 | nassigned = 0; |
| 102 | nconversions = 0; |
| 103 | nread = 0; |
| 104 | base = 0; /* XXX just to keep gcc happy */ |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 105 | for (;;) { |
| 106 | c = *fmt++; |
| 107 | if (c == 0) { |
| 108 | return (nassigned); |
| 109 | } |
| 110 | if (iswspace(c)) { |
| 111 | while ((c = __fgetwc_unlock(fp)) != WEOF && iswspace(c)) |
| 112 | ; |
| 113 | if (c != WEOF) __ungetwc(c, fp); |
| 114 | continue; |
| 115 | } |
| 116 | if (c != '%') goto literal; |
| 117 | width = 0; |
| 118 | flags = 0; |
| 119 | /* |
| 120 | * switch on the format. continue if done; |
| 121 | * break once format type is derived. |
| 122 | */ |
| 123 | again: |
| 124 | c = *fmt++; |
zijunzhao | 7890484 | 2023-05-09 00:54:00 +0000 | [diff] [blame] | 125 | reswitch: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 126 | switch (c) { |
| 127 | case '%': |
| 128 | literal: |
| 129 | if ((wi = __fgetwc_unlock(fp)) == WEOF) goto input_failure; |
| 130 | if (wi != c) { |
| 131 | __ungetwc(wi, fp); |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 132 | goto match_failure; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 133 | } |
| 134 | nread++; |
| 135 | continue; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 136 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 137 | case '*': |
| 138 | flags |= SUPPRESS; |
| 139 | goto again; |
| 140 | case 'j': |
| 141 | flags |= MAXINT; |
| 142 | goto again; |
| 143 | case 'L': |
| 144 | flags |= LONGDBL; |
| 145 | goto again; |
| 146 | case 'h': |
| 147 | if (*fmt == 'h') { |
| 148 | fmt++; |
| 149 | flags |= SHORTSHORT; |
| 150 | } else { |
| 151 | flags |= SHORT; |
| 152 | } |
| 153 | goto again; |
| 154 | case 'l': |
| 155 | if (*fmt == 'l') { |
| 156 | fmt++; |
| 157 | flags |= LLONG; |
| 158 | } else { |
| 159 | flags |= LONG; |
| 160 | } |
| 161 | goto again; |
| 162 | case 'q': |
| 163 | flags |= LLONG; /* deprecated */ |
| 164 | goto again; |
| 165 | case 't': |
| 166 | flags |= PTRINT; |
| 167 | goto again; |
| 168 | case 'z': |
| 169 | flags |= SIZEINT; |
| 170 | goto again; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 171 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 172 | case '0': |
| 173 | case '1': |
| 174 | case '2': |
| 175 | case '3': |
| 176 | case '4': |
| 177 | case '5': |
| 178 | case '6': |
| 179 | case '7': |
| 180 | case '8': |
| 181 | case '9': |
| 182 | width = width * 10 + c - '0'; |
| 183 | goto again; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 184 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 185 | /* |
| 186 | * Conversions. |
| 187 | * Those marked `compat' are for 4.[123]BSD compatibility. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 188 | */ |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 189 | case 'b': |
| 190 | c = CT_INT; |
| 191 | base = 2; |
| 192 | flags |= PFBOK; /* enable 0b prefixing */ |
| 193 | break; |
| 194 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 195 | case 'D': /* compat */ |
| 196 | flags |= LONG; |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 197 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 198 | case 'd': |
| 199 | c = CT_INT; |
| 200 | base = 10; |
| 201 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 202 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 203 | case 'i': |
| 204 | c = CT_INT; |
| 205 | base = 0; |
| 206 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 207 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 208 | case 'O': /* compat */ |
| 209 | flags |= LONG; |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 210 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 211 | case 'o': |
| 212 | c = CT_INT; |
| 213 | flags |= UNSIGNED; |
| 214 | base = 8; |
| 215 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 216 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 217 | case 'u': |
| 218 | c = CT_INT; |
| 219 | flags |= UNSIGNED; |
| 220 | base = 10; |
| 221 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 222 | |
zijunzhao | 7890484 | 2023-05-09 00:54:00 +0000 | [diff] [blame] | 223 | case 'w': { |
| 224 | int size = 0; |
| 225 | bool fast = false; |
| 226 | c = *fmt++; |
zijunzhao | cc475cf | 2023-06-13 00:34:13 +0000 | [diff] [blame] | 227 | if (c == 'f') { |
| 228 | fast = true; |
| 229 | c = *fmt++; |
| 230 | } |
zijunzhao | 7890484 | 2023-05-09 00:54:00 +0000 | [diff] [blame] | 231 | while (is_digit(c)) { |
| 232 | APPEND_DIGIT(size, c); |
| 233 | c = *fmt++; |
| 234 | } |
| 235 | flags |= w_to_flag(size, fast); |
| 236 | goto reswitch; |
| 237 | } |
| 238 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 239 | case 'X': |
| 240 | case 'x': |
| 241 | flags |= PFXOK; /* enable 0x prefixing */ |
| 242 | c = CT_INT; |
| 243 | flags |= UNSIGNED; |
| 244 | base = 16; |
| 245 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 246 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 247 | case 'e': |
| 248 | case 'E': |
| 249 | case 'f': |
| 250 | case 'F': |
| 251 | case 'g': |
| 252 | case 'G': |
| 253 | case 'a': |
| 254 | case 'A': |
| 255 | c = CT_FLOAT; |
| 256 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 257 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 258 | case 's': |
| 259 | c = CT_STRING; |
| 260 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 261 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 262 | case '[': |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 263 | ccl = fmt; |
| 264 | if (*fmt == '^') fmt++; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 265 | if (*fmt == ']') fmt++; |
| 266 | while (*fmt != '\0' && *fmt != ']') fmt++; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 267 | fmt++; |
| 268 | flags |= NOSKIP; |
| 269 | c = CT_CCL; |
| 270 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 271 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 272 | case 'c': |
| 273 | flags |= NOSKIP; |
| 274 | c = CT_CHAR; |
| 275 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 276 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 277 | case 'p': /* pointer format is like hex */ |
| 278 | flags |= POINTER | PFXOK; |
| 279 | c = CT_INT; |
| 280 | flags |= UNSIGNED; |
| 281 | base = 16; |
| 282 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 283 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 284 | case 'n': |
| 285 | nconversions++; |
| 286 | if (flags & SUPPRESS) continue; |
| 287 | if (flags & SHORTSHORT) |
| 288 | *va_arg(ap, signed char*) = nread; |
| 289 | else if (flags & SHORT) |
| 290 | *va_arg(ap, short*) = nread; |
| 291 | else if (flags & LONG) |
| 292 | *va_arg(ap, long*) = nread; |
| 293 | else if (flags & SIZEINT) |
| 294 | *va_arg(ap, ssize_t*) = nread; |
| 295 | else if (flags & PTRINT) |
| 296 | *va_arg(ap, ptrdiff_t*) = nread; |
| 297 | else if (flags & LLONG) |
| 298 | *va_arg(ap, long long*) = nread; |
| 299 | else if (flags & MAXINT) |
| 300 | *va_arg(ap, intmax_t*) = nread; |
| 301 | else |
| 302 | *va_arg(ap, int*) = nread; |
| 303 | continue; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 304 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 305 | /* |
| 306 | * Disgusting backwards compatibility hacks. XXX |
| 307 | */ |
| 308 | case '\0': /* compat */ |
| 309 | return (EOF); |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 310 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 311 | default: /* compat */ |
| 312 | if (iswupper(c)) flags |= LONG; |
| 313 | c = CT_INT; |
| 314 | base = 10; |
| 315 | break; |
| 316 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 317 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 318 | /* |
| 319 | * Consume leading white space, except for formats |
| 320 | * that suppress this. |
| 321 | */ |
| 322 | if ((flags & NOSKIP) == 0) { |
| 323 | while ((wi = __fgetwc_unlock(fp)) != WEOF && iswspace(wi)) nread++; |
| 324 | if (wi == WEOF) goto input_failure; |
| 325 | __ungetwc(wi, fp); |
| 326 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 327 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 328 | /* |
| 329 | * Do the conversion. |
| 330 | */ |
| 331 | switch (c) { |
| 332 | case CT_CHAR: |
| 333 | /* scan arbitrary characters (sets NOSKIP) */ |
| 334 | if (width == 0) width = 1; |
| 335 | if (flags & LONG) { |
| 336 | if (!(flags & SUPPRESS)) p = va_arg(ap, wchar_t*); |
| 337 | n = 0; |
| 338 | while (width-- != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) { |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 339 | if (!(flags & SUPPRESS)) *p++ = static_cast<wchar_t>(wi); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 340 | n++; |
| 341 | } |
| 342 | if (n == 0) goto input_failure; |
| 343 | nread += n; |
| 344 | if (!(flags & SUPPRESS)) nassigned++; |
| 345 | } else { |
| 346 | if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*); |
| 347 | n = 0; |
| 348 | memset(&mbs, 0, sizeof(mbs)); |
| 349 | while (width != 0 && (wi = __fgetwc_unlock(fp)) != WEOF) { |
| 350 | if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) { |
| 351 | nconv = wcrtomb(mbp, wi, &mbs); |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 352 | if (nconv == static_cast<size_t>(-1)) goto input_failure; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 353 | } else { |
| 354 | nconv = wcrtomb(mbbuf, wi, &mbs); |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 355 | if (nconv == static_cast<size_t>(-1)) goto input_failure; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 356 | if (nconv > width) { |
| 357 | __ungetwc(wi, fp); |
| 358 | break; |
| 359 | } |
| 360 | if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv); |
| 361 | } |
| 362 | if (!(flags & SUPPRESS)) mbp += nconv; |
| 363 | width -= nconv; |
| 364 | n++; |
| 365 | } |
| 366 | if (n == 0) goto input_failure; |
| 367 | nread += n; |
| 368 | if (!(flags & SUPPRESS)) nassigned++; |
| 369 | } |
| 370 | nconversions++; |
| 371 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 372 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 373 | case CT_CCL: |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 374 | case CT_STRING: |
| 375 | // CT_CCL: scan a (nonempty) character class (sets NOSKIP). |
| 376 | // CT_STRING: like CCL, but zero-length string OK, & no NOSKIP. |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 377 | if (width == 0) width = SIZE_MAX; // 'infinity'. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 378 | if ((flags & SUPPRESS) && (flags & LONG)) { |
| 379 | n = 0; |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 380 | while ((wi = __fgetwc_unlock(fp)) != WEOF && width-- != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) n++; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 381 | if (wi != WEOF) __ungetwc(wi, fp); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 382 | } else if (flags & LONG) { |
| 383 | p0 = p = va_arg(ap, wchar_t*); |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 384 | while ((wi = __fgetwc_unlock(fp)) != WEOF && width-- != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) { |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 385 | *p++ = static_cast<wchar_t>(wi); |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 386 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 387 | if (wi != WEOF) __ungetwc(wi, fp); |
| 388 | n = p - p0; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 389 | } else { |
| 390 | if (!(flags & SUPPRESS)) mbp = va_arg(ap, char*); |
| 391 | n = 0; |
| 392 | memset(&mbs, 0, sizeof(mbs)); |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 393 | while ((wi = __fgetwc_unlock(fp)) != WEOF && width != 0 && ((c == CT_CCL && in_ccl(wi, ccl)) || (c == CT_STRING && !iswspace(wi)))) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 394 | if (width >= MB_CUR_MAX && !(flags & SUPPRESS)) { |
| 395 | nconv = wcrtomb(mbp, wi, &mbs); |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 396 | if (nconv == static_cast<size_t>(-1)) goto input_failure; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 397 | } else { |
| 398 | nconv = wcrtomb(mbbuf, wi, &mbs); |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 399 | if (nconv == static_cast<size_t>(-1)) goto input_failure; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 400 | if (nconv > width) break; |
| 401 | if (!(flags & SUPPRESS)) memcpy(mbp, mbbuf, nconv); |
| 402 | } |
| 403 | if (!(flags & SUPPRESS)) mbp += nconv; |
| 404 | width -= nconv; |
| 405 | n++; |
| 406 | } |
| 407 | if (wi != WEOF) __ungetwc(wi, fp); |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 408 | } |
| 409 | if (c == CT_CCL && n == 0) goto match_failure; |
| 410 | if (!(flags & SUPPRESS)) { |
| 411 | if (flags & LONG) { |
| 412 | *p = L'\0'; |
| 413 | } else { |
| 414 | *mbp = '\0'; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 415 | } |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 416 | ++nassigned; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 417 | } |
| 418 | nread += n; |
| 419 | nconversions++; |
| 420 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 421 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 422 | case CT_INT: |
| 423 | /* scan an integer as if by strtoimax/strtoumax */ |
| 424 | if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1) |
| 425 | width = sizeof(buf) / sizeof(*buf) - 1; |
| 426 | flags |= SIGNOK | NDIGITS | NZDIGITS; |
| 427 | for (p = buf; width; width--) { |
| 428 | c = __fgetwc_unlock(fp); |
| 429 | /* |
| 430 | * Switch on the character; `goto ok' |
| 431 | * if we accept it as a part of number. |
| 432 | */ |
| 433 | switch (c) { |
| 434 | /* |
| 435 | * The digit 0 is always legal, but is |
| 436 | * special. For %i conversions, if no |
| 437 | * digits (zero or nonzero) have been |
| 438 | * scanned (only signs), we will have |
| 439 | * base==0. In that case, we should set |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 440 | * it to 8 and enable 0b/0x prefixing. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 441 | * Also, if we have not scanned zero digits |
| 442 | * before this, do not turn off prefixing |
| 443 | * (someone else will turn it off if we |
| 444 | * have scanned any nonzero digits). |
| 445 | */ |
| 446 | case '0': |
| 447 | if (base == 0) { |
| 448 | base = 8; |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 449 | flags |= PFBOK | PFXOK; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 450 | } |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 451 | if (flags & NZDIGITS) { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 452 | flags &= ~(SIGNOK | NZDIGITS | NDIGITS); |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 453 | } else { |
| 454 | flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS); |
| 455 | } |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 456 | goto ok; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 457 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 458 | /* 1 through 7 always legal */ |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 459 | case 'B': |
| 460 | case 'b': |
| 461 | // Is this 'b' potentially part of an "0b" prefix? |
| 462 | if ((flags & PFBOK) && p == buf + 1 + !!(flags & HAVESIGN)) { |
| 463 | base = 2; |
| 464 | flags &= ~PFBOK; |
| 465 | goto ok; |
| 466 | } |
| 467 | // No? Fall through and see if it's a hex digit instead then... |
| 468 | __BIONIC_FALLTHROUGH; |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 469 | case '1': |
| 470 | case '2': |
| 471 | case '3': |
| 472 | case '4': |
| 473 | case '5': |
| 474 | case '6': |
| 475 | case '7': |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 476 | case '8': |
| 477 | case '9': |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 478 | case 'A': |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 479 | case 'C': |
| 480 | case 'D': |
| 481 | case 'E': |
| 482 | case 'F': |
| 483 | case 'a': |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 484 | case 'c': |
| 485 | case 'd': |
| 486 | case 'e': |
| 487 | case 'f': |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 488 | if (base == 0) base = 10; |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 489 | if (base != 16 && static_cast<int>(c - '0') >= base) break; /* not legal here */ |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 490 | flags &= ~(SIGNOK | PFBOK | PFXOK | NDIGITS); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 491 | goto ok; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 492 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 493 | /* sign ok only as first character */ |
| 494 | case '+': |
| 495 | case '-': |
| 496 | if (flags & SIGNOK) { |
| 497 | flags &= ~SIGNOK; |
| 498 | flags |= HAVESIGN; |
| 499 | goto ok; |
| 500 | } |
| 501 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 502 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 503 | /* |
| 504 | * x ok iff flag still set and 2nd char (or |
| 505 | * 3rd char if we have a sign). |
| 506 | */ |
| 507 | case 'x': |
| 508 | case 'X': |
| 509 | if ((flags & PFXOK) && p == buf + 1 + !!(flags & HAVESIGN)) { |
| 510 | base = 16; /* if %i */ |
| 511 | flags &= ~PFXOK; |
| 512 | goto ok; |
| 513 | } |
| 514 | break; |
| 515 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 516 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 517 | /* |
| 518 | * If we got here, c is not a legal character |
| 519 | * for a number. Stop accumulating digits. |
| 520 | */ |
| 521 | if (c != WEOF) __ungetwc(c, fp); |
| 522 | break; |
| 523 | ok: |
| 524 | /* |
| 525 | * c is legal: store it and look at the next. |
| 526 | */ |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 527 | *p++ = static_cast<wchar_t>(c); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 528 | } |
| 529 | /* |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 530 | * If we had only a sign, it is no good; push back the sign. |
| 531 | * If the number was `[-+]0[BbXx]`, push back and treat it |
| 532 | * as `[-+]0`. |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 533 | */ |
| 534 | if (flags & NDIGITS) { |
| 535 | if (p > buf) __ungetwc(*--p, fp); |
| 536 | goto match_failure; |
| 537 | } |
| 538 | c = p[-1]; |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 539 | if ((base == 2 && (c == 'b' || c == 'B')) || c == 'x' || c == 'X') { |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 540 | --p; |
| 541 | __ungetwc(c, fp); |
| 542 | } |
| 543 | if ((flags & SUPPRESS) == 0) { |
| 544 | uintmax_t res; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 545 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 546 | *p = '\0'; |
| 547 | if (flags & UNSIGNED) |
| 548 | res = wcstoimax(buf, NULL, base); |
| 549 | else |
| 550 | res = wcstoumax(buf, NULL, base); |
| 551 | if (flags & POINTER) |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 552 | *va_arg(ap, void**) = reinterpret_cast<void*>(res); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 553 | else if (flags & MAXINT) |
| 554 | *va_arg(ap, intmax_t*) = res; |
| 555 | else if (flags & LLONG) |
| 556 | *va_arg(ap, long long*) = res; |
| 557 | else if (flags & SIZEINT) |
| 558 | *va_arg(ap, ssize_t*) = res; |
| 559 | else if (flags & PTRINT) |
| 560 | *va_arg(ap, ptrdiff_t*) = res; |
| 561 | else if (flags & LONG) |
| 562 | *va_arg(ap, long*) = res; |
| 563 | else if (flags & SHORT) |
| 564 | *va_arg(ap, short*) = res; |
| 565 | else if (flags & SHORTSHORT) |
| 566 | *va_arg(ap, signed char*) = res; |
| 567 | else |
| 568 | *va_arg(ap, int*) = res; |
| 569 | nassigned++; |
| 570 | } |
| 571 | nread += p - buf; |
| 572 | nconversions++; |
| 573 | break; |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 574 | |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 575 | case CT_FLOAT: |
| 576 | /* scan a floating point number as if by strtod */ |
| 577 | if (width == 0 || width > sizeof(buf) / sizeof(*buf) - 1) |
| 578 | width = sizeof(buf) / sizeof(*buf) - 1; |
| 579 | if ((width = wparsefloat(fp, buf, buf + width)) == 0) goto match_failure; |
| 580 | if ((flags & SUPPRESS) == 0) { |
| 581 | if (flags & LONGDBL) { |
| 582 | long double res = wcstold(buf, &p); |
| 583 | *va_arg(ap, long double*) = res; |
| 584 | } else if (flags & LONG) { |
| 585 | double res = wcstod(buf, &p); |
| 586 | *va_arg(ap, double*) = res; |
| 587 | } else { |
| 588 | float res = wcstof(buf, &p); |
| 589 | *va_arg(ap, float*) = res; |
| 590 | } |
Elliott Hughes | 69bd8e9 | 2024-02-09 10:04:26 -0800 | [diff] [blame] | 591 | if (static_cast<size_t>(p - buf) != width) abort(); |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 592 | nassigned++; |
| 593 | } |
| 594 | nread += width; |
| 595 | nconversions++; |
| 596 | break; |
| 597 | } |
| 598 | } |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 599 | input_failure: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 600 | return (nconversions != 0 ? nassigned : EOF); |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 601 | match_failure: |
Elliott Hughes | c8f2c52 | 2017-10-31 13:07:51 -0700 | [diff] [blame] | 602 | return (nassigned); |
Elliott Hughes | 01ae00f | 2014-04-29 16:28:56 -0700 | [diff] [blame] | 603 | } |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 604 | #pragma GCC diagnostic pop |