1*6022c2c2Smillert /* $OpenBSD: strtoimax.c,v 1.4 2017/07/06 16:23:11 millert Exp $ */
2b252b5f6Sschwarze /*
3aa522acbSmillert * Copyright (c) 1992 The Regents of the University of California.
4aa522acbSmillert * All rights reserved.
5aa522acbSmillert *
6aa522acbSmillert * Redistribution and use in source and binary forms, with or without
7aa522acbSmillert * modification, are permitted provided that the following conditions
8aa522acbSmillert * are met:
9aa522acbSmillert * 1. Redistributions of source code must retain the above copyright
10aa522acbSmillert * notice, this list of conditions and the following disclaimer.
11aa522acbSmillert * 2. Redistributions in binary form must reproduce the above copyright
12aa522acbSmillert * notice, this list of conditions and the following disclaimer in the
13aa522acbSmillert * documentation and/or other materials provided with the distribution.
14aa522acbSmillert * 3. Neither the name of the University nor the names of its contributors
15aa522acbSmillert * may be used to endorse or promote products derived from this software
16aa522acbSmillert * without specific prior written permission.
17aa522acbSmillert *
18aa522acbSmillert * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19aa522acbSmillert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20aa522acbSmillert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21aa522acbSmillert * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22aa522acbSmillert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23aa522acbSmillert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24aa522acbSmillert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25aa522acbSmillert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26aa522acbSmillert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27aa522acbSmillert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28aa522acbSmillert * SUCH DAMAGE.
29aa522acbSmillert */
30aa522acbSmillert
31aa522acbSmillert #include <ctype.h>
32aa522acbSmillert #include <errno.h>
33aa522acbSmillert #include <inttypes.h>
34aa522acbSmillert
35aa522acbSmillert /*
36aa522acbSmillert * Convert a string to an intmax_t
37aa522acbSmillert *
38aa522acbSmillert * Ignores `locale' stuff. Assumes that the upper and lower case
39aa522acbSmillert * alphabets and digits are each contiguous.
40aa522acbSmillert */
41aa522acbSmillert intmax_t
strtoimax(const char * nptr,char ** endptr,int base)42aa522acbSmillert strtoimax(const char *nptr, char **endptr, int base)
43aa522acbSmillert {
44aa522acbSmillert const char *s;
45aa522acbSmillert intmax_t acc, cutoff;
46aa522acbSmillert int c;
47aa522acbSmillert int neg, any, cutlim;
48aa522acbSmillert
49aa522acbSmillert /*
50b252b5f6Sschwarze * Ensure that base is between 2 and 36 inclusive, or the special
51b252b5f6Sschwarze * value of 0.
52b252b5f6Sschwarze */
53b252b5f6Sschwarze if (base < 0 || base == 1 || base > 36) {
54b252b5f6Sschwarze if (endptr != 0)
55b252b5f6Sschwarze *endptr = (char *)nptr;
56b252b5f6Sschwarze errno = EINVAL;
57b252b5f6Sschwarze return 0;
58b252b5f6Sschwarze }
59b252b5f6Sschwarze
60b252b5f6Sschwarze /*
61aa522acbSmillert * Skip white space and pick up leading +/- sign if any.
62aa522acbSmillert * If base is 0, allow 0x for hex and 0 for octal, else
63aa522acbSmillert * assume decimal; if base is already 16, allow 0x.
64aa522acbSmillert */
65aa522acbSmillert s = nptr;
66aa522acbSmillert do {
67aa522acbSmillert c = (unsigned char) *s++;
68aa522acbSmillert } while (isspace(c));
69aa522acbSmillert if (c == '-') {
70aa522acbSmillert neg = 1;
71aa522acbSmillert c = *s++;
72aa522acbSmillert } else {
73aa522acbSmillert neg = 0;
74aa522acbSmillert if (c == '+')
75aa522acbSmillert c = *s++;
76aa522acbSmillert }
77*6022c2c2Smillert if ((base == 0 || base == 16) && c == '0' &&
78*6022c2c2Smillert (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
79aa522acbSmillert c = s[1];
80aa522acbSmillert s += 2;
81aa522acbSmillert base = 16;
82aa522acbSmillert }
83aa522acbSmillert if (base == 0)
84aa522acbSmillert base = c == '0' ? 8 : 10;
85aa522acbSmillert
86aa522acbSmillert /*
87aa522acbSmillert * Compute the cutoff value between legal numbers and illegal
88aa522acbSmillert * numbers. That is the largest legal value, divided by the
89aa522acbSmillert * base. An input number that is greater than this value, if
90aa522acbSmillert * followed by a legal input character, is too big. One that
91aa522acbSmillert * is equal to this value may be valid or not; the limit
92aa522acbSmillert * between valid and invalid numbers is then based on the last
93aa522acbSmillert * digit. For instance, if the range for intmax_t is
94aa522acbSmillert * [-9223372036854775808..9223372036854775807] and the input base
95aa522acbSmillert * is 10, cutoff will be set to 922337203685477580 and cutlim to
96aa522acbSmillert * either 7 (neg==0) or 8 (neg==1), meaning that if we have
97aa522acbSmillert * accumulated a value > 922337203685477580, or equal but the
98aa522acbSmillert * next digit is > 7 (or 8), the number is too big, and we will
99aa522acbSmillert * return a range error.
100aa522acbSmillert *
101aa522acbSmillert * Set any if any `digits' consumed; make it negative to indicate
102aa522acbSmillert * overflow.
103aa522acbSmillert */
104aa522acbSmillert cutoff = neg ? INTMAX_MIN : INTMAX_MAX;
105aa522acbSmillert cutlim = cutoff % base;
106aa522acbSmillert cutoff /= base;
107aa522acbSmillert if (neg) {
108aa522acbSmillert if (cutlim > 0) {
109aa522acbSmillert cutlim -= base;
110aa522acbSmillert cutoff += 1;
111aa522acbSmillert }
112aa522acbSmillert cutlim = -cutlim;
113aa522acbSmillert }
114aa522acbSmillert for (acc = 0, any = 0;; c = (unsigned char) *s++) {
115aa522acbSmillert if (isdigit(c))
116aa522acbSmillert c -= '0';
117aa522acbSmillert else if (isalpha(c))
118aa522acbSmillert c -= isupper(c) ? 'A' - 10 : 'a' - 10;
119aa522acbSmillert else
120aa522acbSmillert break;
121aa522acbSmillert if (c >= base)
122aa522acbSmillert break;
123aa522acbSmillert if (any < 0)
124aa522acbSmillert continue;
125aa522acbSmillert if (neg) {
126aa522acbSmillert if (acc < cutoff || (acc == cutoff && c > cutlim)) {
127aa522acbSmillert any = -1;
128aa522acbSmillert acc = INTMAX_MIN;
129aa522acbSmillert errno = ERANGE;
130aa522acbSmillert } else {
131aa522acbSmillert any = 1;
132aa522acbSmillert acc *= base;
133aa522acbSmillert acc -= c;
134aa522acbSmillert }
135aa522acbSmillert } else {
136aa522acbSmillert if (acc > cutoff || (acc == cutoff && c > cutlim)) {
137aa522acbSmillert any = -1;
138aa522acbSmillert acc = INTMAX_MAX;
139aa522acbSmillert errno = ERANGE;
140aa522acbSmillert } else {
141aa522acbSmillert any = 1;
142aa522acbSmillert acc *= base;
143aa522acbSmillert acc += c;
144aa522acbSmillert }
145aa522acbSmillert }
146aa522acbSmillert }
147aa522acbSmillert if (endptr != 0)
148aa522acbSmillert *endptr = (char *) (any ? s - 1 : nptr);
149aa522acbSmillert return (acc);
150aa522acbSmillert }
15138a75b98Sguenther DEF_STRONG(strtoimax);
152