2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
5 * The Regents of the University of California. All rights reserved.
7 * By using this file, you agree to the terms and conditions set
8 * forth in the LICENSE file which can be found at the top level of
9 * the sendmail distribution.
13 SM_IDSTR(id
, "@(#)$Id: strto.c,v 1.18 2001/12/30 04:59:37 gshapiro Exp $")
15 #include <sys/param.h>
16 #include <sys/types.h>
20 #include <sm/limits.h>
22 #include <sm/string.h>
25 ** SM_STRTOLL -- Convert a string to a (signed) long long integer.
27 ** Ignores `locale' stuff. Assumes that the upper and lower case
28 ** alphabets and digits are each contiguous.
31 ** nptr -- string containing number
32 ** endptr -- location of first invalid character
33 ** base -- numeric base that 'nptr' number is based in
36 ** Failure: on underflow LLONG_MIN is returned; on overflow
37 ** LLONG_MAX is returned and errno is set.
38 ** When 'endptr' == '\0' then the entire string 'nptr'
40 ** Success: returns the converted number
44 sm_strtoll(nptr
, endptr
, base
)
50 register const char *s
;
51 register LONGLONG_T acc
, cutoff
;
53 register int any
, cutlim
;
56 ** Skip white space and pick up leading +/- sign if any.
57 ** If base is 0, allow 0x for hex and 0 for octal, else
58 ** assume decimal; if base is already 16, allow 0x.
64 c
= (unsigned char) *s
++;
65 } while (isascii(c
) && isspace(c
));
77 if ((base
== 0 || base
== 16) &&
78 c
== '0' && (*s
== 'x' || *s
== 'X'))
85 base
= c
== '0' ? 8 : 10;
88 ** Compute the cutoff value between legal numbers and illegal
89 ** numbers. That is the largest legal value, divided by the
90 ** base. An input number that is greater than this value, if
91 ** followed by a legal input character, is too big. One that
92 ** is equal to this value may be valid or not; the limit
93 ** between valid and invalid numbers is then based on the last
94 ** digit. For instance, if the range for long-long's is
95 ** [-9223372036854775808..9223372036854775807] and the input base
96 ** is 10, cutoff will be set to 922337203685477580 and cutlim to
97 ** either 7 (!neg) or 8 (neg), meaning that if we have
98 ** accumulated a value > 922337203685477580, or equal but the
99 ** next digit is > 7 (or 8), the number is too big, and we will
100 ** return a range error.
102 ** Set any if any `digits' consumed; make it negative to indicate
106 cutoff
= neg
? LLONG_MIN
: LLONG_MAX
;
107 cutlim
= cutoff
% base
;
118 for (acc
= 0, any
= 0;; c
= (unsigned char) *s
++)
120 if (isascii(c
) && isdigit(c
))
122 else if (isascii(c
) && isalpha(c
))
123 c
-= isupper(c
) ? 'A' - 10 : 'a' - 10;
132 if (acc
< cutoff
|| (acc
== cutoff
&& c
> cutlim
))
147 if (acc
> cutoff
|| (acc
== cutoff
&& c
> cutlim
))
162 *endptr
= (char *) (any
? s
- 1 : nptr
);
167 ** SM_STRTOULL -- Convert a string to an unsigned long long integer.
169 ** Ignores `locale' stuff. Assumes that the upper and lower case
170 ** alphabets and digits are each contiguous.
173 ** nptr -- string containing (unsigned) number
174 ** endptr -- location of first invalid character
175 ** base -- numeric base that 'nptr' number is based in
178 ** Failure: on overflow ULLONG_MAX is returned and errno is set.
179 ** When 'endptr' == '\0' then the entire string 'nptr'
181 ** Success: returns the converted number
185 sm_strtoull(nptr
, endptr
, base
)
190 register const char *s
;
191 register ULONGLONG_T acc
, cutoff
;
194 register int any
, cutlim
;
196 /* See sm_strtoll for comments as to the logic used. */
200 c
= (unsigned char) *s
++;
201 } while (isascii(c
) && isspace(c
));
212 if ((base
== 0 || base
== 16) &&
213 c
== '0' && (*s
== 'x' || *s
== 'X'))
220 base
= c
== '0' ? 8 : 10;
222 cutoff
= ULLONG_MAX
/ (ULONGLONG_T
)base
;
223 cutlim
= ULLONG_MAX
% (ULONGLONG_T
)base
;
224 for (acc
= 0, any
= 0;; c
= (unsigned char) *s
++)
226 if (isascii(c
) && isdigit(c
))
228 else if (isascii(c
) && isalpha(c
))
229 c
-= isupper(c
) ? 'A' - 10 : 'a' - 10;
236 if (acc
> cutoff
|| (acc
== cutoff
&& c
> cutlim
))
245 acc
*= (ULONGLONG_T
)base
;
250 acc
= -((LONGLONG_T
) acc
);
252 *endptr
= (char *) (any
? s
- 1 : nptr
);