]>
Commit | Line | Data |
---|---|---|
2aa62f2b | 1 | /** @file\r |
2 | Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r | |
3 | This program and the accompanying materials\r | |
4 | are licensed and made available under the terms and conditions of the BSD License\r | |
5 | which accompanies this distribution. The full text of the license may be found at\r | |
6 | http://opensource.org/licenses/bsd-license.php\r | |
7 | \r | |
8 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
9 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
10 | \r | |
11 | Copyright (c) 1990, 1993\r | |
12 | The Regents of the University of California. All rights reserved.\r | |
13 | \r | |
14 | Redistribution and use in source and binary forms, with or without\r | |
15 | modification, are permitted provided that the following conditions\r | |
16 | are met:\r | |
17 | 1. Redistributions of source code must retain the above copyright\r | |
18 | notice, this list of conditions and the following disclaimer.\r | |
19 | 2. Redistributions in binary form must reproduce the above copyright\r | |
20 | notice, this list of conditions and the following disclaimer in the\r | |
21 | documentation and/or other materials provided with the distribution.\r | |
22 | 3. Neither the name of the University nor the names of its contributors\r | |
23 | may be used to endorse or promote products derived from this software\r | |
24 | without specific prior written permission.\r | |
25 | \r | |
26 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r | |
27 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
28 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
29 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r | |
30 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
31 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
32 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
33 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
34 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
35 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
36 | SUCH DAMAGE.\r | |
37 | \r | |
38 | Original version ID:\r | |
39 | @(#)strtoul.c 8.1 (Berkeley) 6/4/93\r | |
40 | Citrus: xpg4dl/FreeBSD/lib/libc/locale/wcstoul.c,v 1.2 2001/09/21 16:11:41 yamt Exp\r | |
41 | NetBSD: wcstoul.c,v 1.1 2001/09/27 16:30:37 yamt Exp\r | |
42 | NetBSD: _wcstoul.h,v 1.3 2005/11/29 03:11:59 christos Exp\r | |
43 | */\r | |
44 | \r | |
d7ce7006 | 45 | #include <Library/BaseLib.h>\r |
46 | \r | |
2aa62f2b | 47 | /*\r |
48 | * function template for wcstoul, wcstoull and wcstoumax.\r | |
49 | *\r | |
50 | * parameters:\r | |
51 | * _FUNCNAME : function name\r | |
52 | * __wUINT : return type\r | |
53 | * __wINT : signed version of __wUINT\r | |
54 | * __wUINT_MAX : upper limit of the return type\r | |
55 | */\r | |
56 | \r | |
57 | __wUINT\r | |
58 | _FUNCNAME(\r | |
59 | const wchar_t *nptr,\r | |
60 | wchar_t **endptr,\r | |
61 | int base\r | |
62 | )\r | |
63 | {\r | |
64 | const wchar_t *s;\r | |
65 | __wUINT acc, cutoff;\r | |
66 | wint_t wc;\r | |
67 | int i;\r | |
68 | int neg, any, cutlim;\r | |
69 | \r | |
70 | _DIAGASSERT(nptr != NULL);\r | |
71 | /* endptr may be NULL */\r | |
72 | \r | |
73 | if (base && (base < 2 || base > 36)) {\r | |
74 | errno = EINVAL;\r | |
75 | return 0;\r | |
76 | }\r | |
77 | \r | |
78 | /*\r | |
79 | * Skip white space and pick up leading +/- sign if any.\r | |
80 | * If base is 0, allow 0x for hex and 0 for octal, else\r | |
81 | * assume decimal; if base is already 16, allow 0x.\r | |
82 | */\r | |
83 | s = nptr;\r | |
84 | do {\r | |
85 | wc = (wchar_t) *s++;\r | |
86 | } while (iswspace(wc));\r | |
87 | if (wc == L'-') {\r | |
88 | neg = 1;\r | |
89 | wc = *s++;\r | |
90 | } else {\r | |
91 | neg = 0;\r | |
92 | if (wc == L'+')\r | |
93 | wc = *s++;\r | |
94 | }\r | |
95 | if ((base == 0 || base == 16) &&\r | |
96 | wc == L'0' && (*s == L'x' || *s == L'X')) {\r | |
97 | wc = s[1];\r | |
98 | s += 2;\r | |
99 | base = 16;\r | |
100 | }\r | |
101 | if (base == 0)\r | |
102 | base = wc == L'0' ? 8 : 10;\r | |
103 | \r | |
104 | /*\r | |
105 | * See strtoul for comments as to the logic used.\r | |
106 | */\r | |
d7ce7006 | 107 | cutoff = (__wUINT)DivU64x32 ((UINT64) __wUINT_MAX, (UINT32) base);\r |
108 | cutlim = (int) ModU64x32 ((UINT64) __wUINT_MAX, (UINT32) base);\r | |
2aa62f2b | 109 | for (acc = 0, any = 0;; wc = (wint_t) *s++) {\r |
110 | i = __wctoint((wchar_t)wc);\r | |
111 | if (i == -1) {\r | |
112 | break;\r | |
113 | }\r | |
114 | if (i >= base)\r | |
115 | break;\r | |
116 | if (any < 0)\r | |
117 | continue;\r | |
118 | if (acc > cutoff || (acc == cutoff && i > cutlim)) {\r | |
119 | any = -1;\r | |
120 | acc = __wUINT_MAX;\r | |
121 | errno = ERANGE;\r | |
122 | } else {\r | |
123 | any = 1;\r | |
124 | acc *= (__wUINT)base;\r | |
125 | acc += i;\r | |
126 | }\r | |
127 | }\r | |
128 | if (neg && any > 0)\r | |
129 | acc = (__wUINT)(-((__wINT)acc));\r | |
130 | if (endptr != 0)\r | |
131 | *endptr = __UNCONST(any ? s - 1 : nptr);\r | |
132 | return (acc);\r | |
133 | }\r |