strtoull.c revision 1.1 1 1.1 christos /* $NetBSD: strtoull.c,v 1.1 2017/01/28 20:46:53 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1992, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * Copyright (c) 2011 The FreeBSD Foundation
8 1.1 christos * All rights reserved.
9 1.1 christos * Portions of this software were developed by David Chisnall
10 1.1 christos * under sponsorship from the FreeBSD Foundation.
11 1.1 christos *
12 1.1 christos * Redistribution and use in source and binary forms, with or without
13 1.1 christos * modification, are permitted provided that the following conditions
14 1.1 christos * are met:
15 1.1 christos * 1. Redistributions of source code must retain the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer.
17 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 christos * notice, this list of conditions and the following disclaimer in the
19 1.1 christos * documentation and/or other materials provided with the distribution.
20 1.1 christos * 4. Neither the name of the University nor the names of its contributors
21 1.1 christos * may be used to endorse or promote products derived from this software
22 1.1 christos * without specific prior written permission.
23 1.1 christos *
24 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 christos * SUCH DAMAGE.
35 1.1 christos */
36 1.1 christos
37 1.1 christos #include <config.h>
38 1.1 christos
39 1.1 christos #include <string.h>
40 1.1 christos
41 1.1 christos #include <krb5/roken.h>
42 1.1 christos
43 1.1 christos /* #include <sys/cdefs.h> */
44 1.1 christos
45 1.1 christos #include <limits.h>
46 1.1 christos #include <errno.h>
47 1.1 christos #include <ctype.h>
48 1.1 christos #include <stdlib.h>
49 1.1 christos #include <stdint.h>
50 1.1 christos
51 1.1 christos /*
52 1.1 christos * Convert a string to an unsigned long long integer.
53 1.1 christos *
54 1.1 christos * Assumes that the upper and lower case
55 1.1 christos * alphabets and digits are each contiguous.
56 1.1 christos */
57 1.1 christos ROKEN_LIB_FUNCTION unsigned long long ROKEN_LIB_CALL
58 1.1 christos strtoull(const char * nptr, char ** endptr, int base)
59 1.1 christos {
60 1.1 christos const char *s;
61 1.1 christos unsigned long long acc;
62 1.1 christos char c;
63 1.1 christos unsigned long long cutoff;
64 1.1 christos int neg, any, cutlim;
65 1.1 christos
66 1.1 christos /*
67 1.1 christos * See strtoq for comments as to the logic used.
68 1.1 christos */
69 1.1 christos s = nptr;
70 1.1 christos do {
71 1.1 christos c = *s++;
72 1.1 christos } while (isspace((unsigned char)c));
73 1.1 christos if (c == '-') {
74 1.1 christos neg = 1;
75 1.1 christos c = *s++;
76 1.1 christos } else {
77 1.1 christos neg = 0;
78 1.1 christos if (c == '+')
79 1.1 christos c = *s++;
80 1.1 christos }
81 1.1 christos if ((base == 0 || base == 16) &&
82 1.1 christos c == '0' && (*s == 'x' || *s == 'X') &&
83 1.1 christos ((s[1] >= '0' && s[1] <= '9') ||
84 1.1 christos (s[1] >= 'A' && s[1] <= 'F') ||
85 1.1 christos (s[1] >= 'a' && s[1] <= 'f'))) {
86 1.1 christos c = s[1];
87 1.1 christos s += 2;
88 1.1 christos base = 16;
89 1.1 christos }
90 1.1 christos if (base == 0)
91 1.1 christos base = c == '0' ? 8 : 10;
92 1.1 christos acc = any = 0;
93 1.1 christos if (base < 2 || base > 36)
94 1.1 christos goto noconv;
95 1.1 christos
96 1.1 christos cutoff = ULLONG_MAX / base;
97 1.1 christos cutlim = ULLONG_MAX % base;
98 1.1 christos for ( ; ; c = *s++) {
99 1.1 christos if (c >= '0' && c <= '9')
100 1.1 christos c -= '0';
101 1.1 christos else if (c >= 'A' && c <= 'Z')
102 1.1 christos c -= 'A' - 10;
103 1.1 christos else if (c >= 'a' && c <= 'z')
104 1.1 christos c -= 'a' - 10;
105 1.1 christos else
106 1.1 christos break;
107 1.1 christos if (c >= base)
108 1.1 christos break;
109 1.1 christos if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
110 1.1 christos any = -1;
111 1.1 christos else {
112 1.1 christos any = 1;
113 1.1 christos acc *= base;
114 1.1 christos acc += c;
115 1.1 christos }
116 1.1 christos }
117 1.1 christos if (any < 0) {
118 1.1 christos acc = ULLONG_MAX;
119 1.1 christos errno = ERANGE;
120 1.1 christos } else if (!any) {
121 1.1 christos noconv:
122 1.1 christos errno = EINVAL;
123 1.1 christos } else if (neg)
124 1.1 christos acc = -acc;
125 1.1 christos if (endptr != NULL)
126 1.1 christos *endptr = (char *)(any ? s - 1 : nptr);
127 1.1 christos return (acc);
128 1.1 christos }
129 1.1 christos
130