]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/vfwscanf.c
2 valist worker function for the wide-character fscanf.
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 * Copyright (c) 1990, 1993
14 * The Regents of the University of California. All rights reserved.
16 * This code is derived from software contributed to Berkeley by
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 * must display the following acknowledgement:
29 * This product includes software developed by the University of
30 * California, Berkeley and its contributors.
31 * 4. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp
48 NetBSD: vfwscanf.c,v 1.2 2005/06/12 05:48:41 lukem Exp
50 #include <LibConfig.h>
51 #include <sys/EfiCdefs.h>
53 #include "namespace.h"
65 #include "reentrant.h"
68 #ifndef NO_FLOATING_POINT
72 #if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */
73 #pragma warning ( disable : 4244 ) // Allow wint_t to wchar_t conversions
74 #pragma warning ( disable : 4305 ) // Allow truncation from UINT64 to void*
75 #pragma warning ( disable : 4701 ) // Disable false warning for local variable p near line 375
79 #define BUF 513 /* Maximum length of numeric string. */
82 * Flags used during conversion.
84 #define LONG 0x01 /* l: long or double */
85 #define LONGDBL 0x02 /* L: long double */
86 #define SHORT 0x04 /* h: short */
87 #define SUPPRESS 0x08 /* *: suppress assignment */
88 #define POINTER 0x10 /* p: void * (as hex) */
89 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
90 #define LONGLONG 0x400 /* ll: quad_t (+ deprecated q: quad) */
91 #define INTMAXT 0x800 /* j: intmax_t */
92 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
93 #define SIZET 0x2000 /* z: size_t */
94 #define SHORTSHORT 0x4000 /* hh: char */
95 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
98 * The following are used in integral conversions only:
99 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
101 #define SIGNOK 0x40 /* +/- is (still) legal */
102 #define NDIGITS 0x80 /* no digits detected */
103 #define PFXOK 0x100 /* 0x prefix is (still) legal */
104 #define NZDIGITS 0x200 /* no zero digits detected */
105 #define HAVESIGN 0x10000 /* sign detected */
110 #define CT_CHAR 0 /* %c conversion */
111 #define CT_CCL 1 /* %[...] conversion */
112 #define CT_STRING 2 /* %s conversion */
113 #define CT_INT 3 /* %[dioupxX] conversion */
114 #define CT_FLOAT 4 /* %[efgEFG] conversion */
116 static int parsefloat(FILE *, wchar_t *, wchar_t *);
119 (cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
120 (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
126 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
131 _SET_ORIENTATION(fp
, 1);
132 ret
= __vfwscanf_unlocked(fp
, fmt
, ap
);
138 * Non-MT-safe version.
141 __vfwscanf_unlocked(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
143 wint_t c
; /* character from format, or conversion */
144 size_t width
; /* field width, or 0 */
145 wchar_t *p
= NULL
; /* points into all kinds of strings */
146 int n
; /* handy integer */
147 int flags
; /* flags as defined above */
148 wchar_t *p0
; /* saves original value of p when necessary */
149 int nassigned
; /* number of fields assigned */
150 int nconversions
; /* number of conversions */
151 int nread
; /* number of characters consumed from fp */
152 int base
; /* base argument to conversion function */
153 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
154 const wchar_t *ccls
; /* character class start */
155 const wchar_t *ccle
; /* character class end */
156 int cclcompl
; /* ccl is complemented? */
157 wint_t wi
; /* handy wint_t */
158 char *mbp
; /* multibyte string pointer for %c %s %[ */
159 size_t nconv
; /* number of bytes in mb. conversion */
160 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
163 static const mbstate_t initial
= { 0 };
164 /* `basefix' is used to avoid `if' tests in the integer scanner */
165 static short basefix
[17] =
166 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
182 while ((c
= __fgetwc_unlock(fp
)) != WEOF
&&
194 * switch on the format. continue if done;
195 * break once format type is derived.
201 if ((wi
= __fgetwc_unlock(fp
)) == WEOF
)
224 flags
|= LONGLONG
; /* not quite */
243 case '0': case '1': case '2': case '3': case '4':
244 case '5': case '6': case '7': case '8': case '9':
245 width
= width
* 10 + c
- '0';
275 flags
|= PFXOK
; /* enable 0x prefixing */
281 #ifndef NO_FLOATING_POINT
283 case 'E': case 'F': case 'G':
285 case 'e': case 'f': case 'g':
306 while (*fmt
!= '\0' && *fmt
!= ']')
322 case 'p': /* pointer format is like hex */
323 flags
|= POINTER
| PFXOK
;
324 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
325 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
331 if (flags
& SUPPRESS
) /* ??? */
333 if (flags
& SHORTSHORT
)
334 *va_arg(ap
, char *) = (char)nread
;
335 else if (flags
& SHORT
)
336 *va_arg(ap
, short *) = (short)nread
;
337 else if (flags
& LONG
)
338 *va_arg(ap
, long *) = (long)nread
;
339 else if (flags
& LONGLONG
)
340 *va_arg(ap
, INT64
*) = (INT64
)nread
; // was quad_t
341 else if (flags
& INTMAXT
)
342 *va_arg(ap
, intmax_t *) = (intmax_t)nread
;
343 else if (flags
& SIZET
)
344 *va_arg(ap
, size_t *) = (size_t)nread
;
345 else if (flags
& PTRDIFFT
)
346 *va_arg(ap
, ptrdiff_t *) = (ptrdiff_t)nread
;
348 *va_arg(ap
, int *) = nread
;
355 * Disgusting backwards compatibility hack. XXX
357 case '\0': /* compat */
362 * Consume leading white space, except for formats
363 * that suppress this.
365 if ((flags
& NOSKIP
) == 0) {
366 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&& iswspace(wi
))
379 /* scan arbitrary characters (sets NOSKIP) */
383 if (!(flags
& SUPPRESS
))
384 p
= va_arg(ap
, wchar_t *);
386 while (width
-- != 0 &&
387 (wi
= __fgetwc_unlock(fp
)) != WEOF
) {
388 if (!(flags
& SUPPRESS
))
395 if (!(flags
& SUPPRESS
))
398 if (!(flags
& SUPPRESS
))
399 mbp
= va_arg(ap
, char *);
403 (wi
= __fgetwc_unlock(fp
)) != WEOF
) {
404 if (width
>= MB_CUR_MAX
&&
405 !(flags
& SUPPRESS
)) {
406 nconv
= wcrtomb(mbp
, (wchar_t)wi
, &mbs
);
407 if (nconv
== (size_t)-1)
410 nconv
= wcrtomb(mbbuf
, (wchar_t)wi
,
412 if (nconv
== (size_t)-1)
418 if (!(flags
& SUPPRESS
))
422 if (!(flags
& SUPPRESS
))
430 if (!(flags
& SUPPRESS
))
437 /* scan a (nonempty) character class (sets NOSKIP) */
439 width
= (size_t)~0; /* `infinity' */
440 /* take only those things in the class */
441 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
443 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
444 width
-- != 0 && INCCL(wi
))
450 } else if (flags
& LONG
) {
451 p0
= p
= va_arg(ap
, wchar_t *);
452 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
453 width
-- != 0 && INCCL(wi
))
463 if (!(flags
& SUPPRESS
))
464 mbp
= va_arg(ap
, char *);
467 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
468 width
!= 0 && INCCL(wi
)) {
469 if (width
>= MB_CUR_MAX
&&
470 !(flags
& SUPPRESS
)) {
471 nconv
= wcrtomb(mbp
, wi
, &mbs
);
472 if (nconv
== (size_t)-1)
475 nconv
= wcrtomb(mbbuf
, wi
,
477 if (nconv
== (size_t)-1)
481 if (!(flags
& SUPPRESS
))
485 if (!(flags
& SUPPRESS
))
492 if (!(flags
& SUPPRESS
)) {
502 /* like CCL, but zero-length string OK, & no NOSKIP */
505 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
506 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
512 } else if (flags
& LONG
) {
513 p0
= p
= va_arg(ap
, wchar_t *);
514 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
525 if (!(flags
& SUPPRESS
))
526 mbp
= va_arg(ap
, char *);
528 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
531 if (width
>= MB_CUR_MAX
&&
532 !(flags
& SUPPRESS
)) {
533 nconv
= wcrtomb(mbp
, wi
, &mbs
);
534 if (nconv
== (size_t)-1)
537 nconv
= wcrtomb(mbbuf
, wi
,
539 if (nconv
== (size_t)-1)
543 if (!(flags
& SUPPRESS
))
547 if (!(flags
& SUPPRESS
))
554 if (!(flags
& SUPPRESS
)) {
563 /* scan an integer as if by the conversion function */
564 if (width
== 0 || width
> sizeof(buf
) /
566 width
= sizeof(buf
) / sizeof(*buf
) - 1;
567 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
568 for (p
= buf
; width
; width
--) {
569 c
= __fgetwc_unlock(fp
);
571 * Switch on the character; `goto ok'
572 * if we accept it as a part of number.
577 * The digit 0 is always legal, but is
578 * special. For %i conversions, if no
579 * digits (zero or nonzero) have been
580 * scanned (only signs), we will have
581 * base==0. In that case, we should set
582 * it to 8 and enable 0x prefixing.
583 * Also, if we have not scanned zero digits
584 * before this, do not turn off prefixing
585 * (someone else will turn it off if we
586 * have scanned any nonzero digits).
593 if (flags
& NZDIGITS
)
594 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
596 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
599 /* 1 through 7 always legal */
600 case '1': case '2': case '3':
601 case '4': case '5': case '6': case '7':
602 base
= basefix
[base
];
603 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
606 /* digits 8 and 9 ok iff decimal or hex */
608 base
= basefix
[base
];
610 break; /* not legal here */
611 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
614 /* letters ok iff hex */
615 case 'A': case 'B': case 'C':
616 case 'D': case 'E': case 'F':
617 case 'a': case 'b': case 'c':
618 case 'd': case 'e': case 'f':
619 /* no need to fix base here */
621 break; /* not legal here */
622 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
625 /* sign ok only as first character */
627 if (flags
& SIGNOK
) {
635 * x ok iff flag still set & 2nd char (or
636 * 3rd char if we have a sign).
639 if (flags
& PFXOK
&& p
==
640 buf
+ 1 + !!(flags
& HAVESIGN
)) {
641 base
= 16; /* if %i */
649 * If we got here, c is not a legal character
650 * for a number. Stop accumulating digits.
657 * c is legal: store it and look at the next.
662 * If we had only a sign, it is no good; push
663 * back the sign. If the number ends in `x',
664 * it was [sign] '0' 'x', so push back the x
665 * and treat it as [sign] '0'.
667 if (flags
& NDIGITS
) {
673 if (c
== 'x' || c
== 'X') {
677 if ((flags
& SUPPRESS
) == 0) {
681 if ((flags
& UNSIGNED
) == 0)
682 res
= wcstoimax(buf
, NULL
, base
);
684 res
= wcstoumax(buf
, NULL
, base
);
686 if (flags
& POINTER
) {
687 *va_arg(ap
, void **) = (void *)res
;
689 else if (flags
& SHORTSHORT
) {
690 *va_arg(ap
, char *) = (char)res
;
692 else if (flags
& SHORT
) {
693 *va_arg(ap
, short *) = (short)res
;
695 else if (flags
& LONG
) {
696 *va_arg(ap
, long *) = (long)res
;
698 else if (flags
& LONGLONG
) {
699 *va_arg(ap
, INT64
*) = res
; // was quad_t
701 else if (flags
& INTMAXT
) {
702 *va_arg(ap
, intmax_t *) = res
;
704 else if (flags
& PTRDIFFT
) {
705 *va_arg(ap
, ptrdiff_t *) = (ptrdiff_t)res
;
707 else if (flags
& SIZET
) {
708 *va_arg(ap
, size_t *) = (size_t)res
;
711 *va_arg(ap
, int *) = (int)res
;
719 #ifndef NO_FLOATING_POINT
721 /* scan a floating point number as if by strtod */
722 if (width
== 0 || width
> sizeof(buf
) /
724 width
= sizeof(buf
) / sizeof(*buf
) - 1;
725 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
727 if ((flags
& SUPPRESS
) == 0) {
728 #ifdef REAL_LONG_DOUBLE_SUPPORT
729 if (flags
& LONGDBL
) {
730 long double res
= wcstold(buf
, &p
);
731 *va_arg(ap
, long double *) = res
;
734 if (flags
& (LONG
| LONGDBL
)) {
735 double res
= wcstod(buf
, &p
);
736 *va_arg(ap
, double *) = res
;
739 float res
= wcstof(buf
, &p
);
740 *va_arg(ap
, float *) = res
;
743 if (p
- buf
!= width
)
751 #endif /* !NO_FLOATING_POINT */
755 return (nconversions
!= 0 ? nassigned
: EOF
);
760 #ifndef NO_FLOATING_POINT
762 parsefloat(FILE *fp
, wchar_t *buf
, wchar_t *end
)
767 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
768 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
771 wchar_t decpt
= (wchar_t)(unsigned char)*localeconv()->decimal_point
;
772 int gotmantdig
= 0, ishex
= 0;
775 * We set commit = p whenever the string we have read so far
776 * constitutes a valid representation of a floating point
777 * number by itself. At some point, the parse will complete
778 * or fail, and we will ungetc() back to the last commit point.
779 * To ensure that the file offset gets updated properly, it is
780 * always necessary to read at least one character that doesn't
781 * match; thus, we can't short-circuit "infinity" or "nan(...)".
785 for (p
= buf
; p
< end
; ) {
786 if ((wint_t)(c
= __fgetwc_unlock(fp
)) == WEOF
)
792 if (c
== '-' || c
== '+')
817 (c
!= "nfinity"[infnanpos
] &&
818 c
!= "NFINITY"[infnanpos
]))
820 if (infnanpos
== 1 || infnanpos
== 6)
821 commit
= p
; /* inf or infinity */
826 case -1: /* XXX kludge to deal with nan(...) */
829 if (c
!= 'A' && c
!= 'a')
833 if (c
!= 'N' && c
!= 'n')
846 } else if (!iswalnum(c
) && c
!= '_')
854 if (c
== 'X' || c
== 'x') {
857 } else { /* we saw a '0', but no 'x' */
862 if ((ishex
&& iswxdigit(c
)) || iswdigit(c
))
873 if (((c
== 'E' || c
== 'e') && !ishex
) ||
874 ((c
== 'P' || c
== 'p') && ishex
)) {
879 } else if ((ishex
&& iswxdigit(c
)) || iswdigit(c
)) {
887 if (c
== '-' || c
== '+')
905 if ((wint_t)c
!= WEOF
)
910 return (commit
- buf
);