xref: /original-bsd/lib/libc/stdlib/strtoq.c (revision 82f2451b)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)strtoq.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 
14 #include <limits.h>
15 #include <errno.h>
16 #include <ctype.h>
17 #include <stdlib.h>
18 
19 /*
20  * Convert a string to a quad integer.
21  *
22  * Ignores `locale' stuff.  Assumes that the upper and lower case
23  * alphabets and digits are each contiguous.
24  */
25 quad_t
strtoq(nptr,endptr,base)26 strtoq(nptr, endptr, base)
27 	const char *nptr;
28 	char **endptr;
29 	register int base;
30 {
31 	register const char *s;
32 	register u_quad_t acc;
33 	register int c;
34 	register u_quad_t qbase, cutoff;
35 	register int neg, any, cutlim;
36 
37 	/*
38 	 * Skip white space and pick up leading +/- sign if any.
39 	 * If base is 0, allow 0x for hex and 0 for octal, else
40 	 * assume decimal; if base is already 16, allow 0x.
41 	 */
42 	s = nptr;
43 	do {
44 		c = *s++;
45 	} while (isspace(c));
46 	if (c == '-') {
47 		neg = 1;
48 		c = *s++;
49 	} else {
50 		neg = 0;
51 		if (c == '+')
52 			c = *s++;
53 	}
54 	if ((base == 0 || base == 16) &&
55 	    c == '0' && (*s == 'x' || *s == 'X')) {
56 		c = s[1];
57 		s += 2;
58 		base = 16;
59 	}
60 	if (base == 0)
61 		base = c == '0' ? 8 : 10;
62 
63 	/*
64 	 * Compute the cutoff value between legal numbers and illegal
65 	 * numbers.  That is the largest legal value, divided by the
66 	 * base.  An input number that is greater than this value, if
67 	 * followed by a legal input character, is too big.  One that
68 	 * is equal to this value may be valid or not; the limit
69 	 * between valid and invalid numbers is then based on the last
70 	 * digit.  For instance, if the range for quads is
71 	 * [-9223372036854775808..9223372036854775807] and the input base
72 	 * is 10, cutoff will be set to 922337203685477580 and cutlim to
73 	 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
74 	 * accumulated a value > 922337203685477580, or equal but the
75 	 * next digit is > 7 (or 8), the number is too big, and we will
76 	 * return a range error.
77 	 *
78 	 * Set any if any `digits' consumed; make it negative to indicate
79 	 * overflow.
80 	 */
81 	qbase = (unsigned)base;
82 	cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX;
83 	cutlim = cutoff % qbase;
84 	cutoff /= qbase;
85 	for (acc = 0, any = 0;; c = *s++) {
86 		if (isdigit(c))
87 			c -= '0';
88 		else if (isalpha(c))
89 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
90 		else
91 			break;
92 		if (c >= base)
93 			break;
94 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
95 			any = -1;
96 		else {
97 			any = 1;
98 			acc *= qbase;
99 			acc += c;
100 		}
101 	}
102 	if (any < 0) {
103 		acc = neg ? QUAD_MIN : QUAD_MAX;
104 		errno = ERANGE;
105 	} else if (neg)
106 		acc = -acc;
107 	if (endptr != 0)
108 		*endptr = (char *)(any ? s - 1 : nptr);
109 	return (acc);
110 }
111