]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1990, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 4. Neither the name of the University nor the names of its contributors | |
14 | * may be used to endorse or promote products derived from this software | |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | #include <sys/cdefs.h> | |
31 | __FBSDID("$FreeBSD: src/lib/libc/locale/wcstoul.c,v 1.2 2007/01/09 00:28:01 imp Exp $"); | |
32 | ||
33 | #include <ctype.h> | |
34 | #include <errno.h> | |
35 | #include <limits.h> | |
36 | #include <wchar.h> | |
37 | #include <wctype.h> | |
38 | ||
39 | /* | |
40 | * Convert a wide character string to an unsigned long integer. | |
41 | */ | |
42 | unsigned long | |
43 | wcstoul(const wchar_t * __restrict nptr, wchar_t ** __restrict endptr, int base) | |
44 | { | |
45 | const wchar_t *s; | |
46 | unsigned long acc; | |
47 | wchar_t c; | |
48 | unsigned long cutoff; | |
49 | int neg, any, cutlim; | |
50 | ||
51 | /* | |
52 | * See strtol for comments as to the logic used. | |
53 | */ | |
54 | s = nptr; | |
55 | do { | |
56 | c = *s++; | |
57 | } while (iswspace(c)); | |
58 | if (c == L'-') { | |
59 | neg = 1; | |
60 | c = *s++; | |
61 | } else { | |
62 | neg = 0; | |
63 | if (c == L'+') | |
64 | c = *s++; | |
65 | } | |
66 | if ((base == 0 || base == 16) && | |
67 | c == L'0' && (*s == L'x' || *s == L'X')) { | |
68 | c = s[1]; | |
69 | s += 2; | |
70 | base = 16; | |
71 | } | |
72 | if (base == 0) | |
73 | base = c == L'0' ? 8 : 10; | |
74 | acc = any = 0; | |
75 | if (base < 2 || base > 36) | |
76 | goto noconv; | |
77 | ||
78 | cutoff = ULONG_MAX / base; | |
79 | cutlim = ULONG_MAX % base; | |
80 | for ( ; ; c = *s++) { | |
81 | #ifdef notyet | |
82 | if (iswdigit(c)) | |
83 | c = digittoint(c); | |
84 | else | |
85 | #endif | |
86 | if (c >= L'0' && c <= L'9') | |
87 | c -= L'0'; | |
88 | else if (c >= L'A' && c <= L'Z') | |
89 | c -= L'A' - 10; | |
90 | else if (c >= L'a' && c <= L'z') | |
91 | c -= L'a' - 10; | |
92 | else | |
93 | break; | |
94 | if (c >= base) | |
95 | break; | |
96 | if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) | |
97 | any = -1; | |
98 | else { | |
99 | any = 1; | |
100 | acc *= base; | |
101 | acc += c; | |
102 | } | |
103 | } | |
104 | if (any < 0) { | |
105 | acc = ULONG_MAX; | |
106 | errno = ERANGE; | |
107 | } else if (!any) { | |
108 | noconv: | |
109 | errno = EINVAL; | |
110 | } else if (neg) | |
111 | acc = -acc; | |
112 | if (endptr != NULL) | |
113 | *endptr = (wchar_t *)(any ? s - 1 : nptr); | |
114 | return (acc); | |
115 | } |