• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * This file has no copyright assigned and is placed in the Public Domain.
3  * This file is part of the mingw-w64 runtime package.
4  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5  */
6 /*
7     This source code was extracted from the Q8 package created and
8     placed in the PUBLIC DOMAIN by Doug Gwyn <gwyn@arl.mil>
9     last edit:	1999/11/05	gwyn@arl.mil
10 
11 	Implements subclause 7.8.2 of ISO/IEC 9899:1999 (E).
12 
13 	This particular implementation requires the matching <inttypes.h>.
14 	It also assumes that character codes for A..Z and a..z are in
15 	contiguous ascending order; this is true for ASCII but not EBCDIC.
16 */
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <ctype.h>
20 #include <inttypes.h>
21 
22 /* Helper macros */
23 
24 /* convert digit character to number, in any base */
25 #define ToNumber(c)	(isdigit(c) ? (c) - '0' : \
26 			 isupper(c) ? (c) - 'A' + 10 : \
27 			 islower(c) ? (c) - 'a' + 10 : \
28 			 -1		/* "invalid" flag */ \
29 			)
30 /* validate converted digit character for specific base */
31 #define valid(n, b)	((n) >= 0 && (n) < (b))
32 
33 uintmax_t
strtoumax(nptr,endptr,base)34 strtoumax(nptr, endptr, base)
35 	register const char * __restrict__	nptr;
36 	char ** __restrict__			endptr;
37 	register int				base;
38 	{
39 	register uintmax_t	accum;	/* accumulates converted value */
40 	register uintmax_t	next;	/* for computing next value of accum */
41 	register int		n;	/* numeral from digit character */
42 	int			minus;	/* set iff minus sign seen (yes!) */
43 	int			toobig;	/* set iff value overflows */
44 
45 	if ( endptr != NULL )
46 		*endptr = (char *)nptr;	/* in case no conversion's performed */
47 
48 	if ( base < 0 || base == 1 || base > 36 )
49 		{
50 		errno = EDOM;
51 		return 0;		/* unspecified behavior */
52 		}
53 
54 	/* skip initial, possibly empty sequence of white-space characters */
55 
56 	while ( isspace(*nptr) )
57 		++nptr;
58 
59 	/* process subject sequence: */
60 
61 	/* optional sign (yes!) */
62 
63 	if ( (minus = *nptr == '-') || *nptr == '+' )
64 		++nptr;
65 
66 	if ( base == 0 )
67         {
68 		if ( *nptr == '0' )
69             {
70 			if ( nptr[1] == 'X' || nptr[1] == 'x' )
71 				base = 16;
72 			else
73 				base = 8;
74 		    }
75 		else
76 				base = 10;
77 		}
78 
79     /* optional "0x" or "0X" for base 16 */
80 
81 	if ( base == 16 && *nptr == '0' && (nptr[1] == 'X' || nptr[1] == 'x') )
82 		nptr += 2;		/* skip past this prefix */
83 
84 	/* check whether there is at least one valid digit */
85 
86 	n = ToNumber(*nptr);
87 	++nptr;
88 
89 	if ( !valid(n, base) )
90 		return 0;		/* subject seq. not of expected form */
91 
92 	accum = n;
93 
94 	for ( toobig = 0; n = ToNumber(*nptr), valid(n, base); ++nptr )
95 		if ( accum > UINTMAX_MAX / base + 1	/* major wrap-around */
96 		  || (next = base * accum + n) < accum	/* minor wrap-around */
97 		   )
98 			toobig = 1;	/* but keep scanning */
99 		else
100 			accum = next;
101 
102 	if ( endptr != NULL )
103 		*endptr = (char *)nptr;	/* points to first not-valid-digit */
104 
105 	if ( toobig )
106 		{
107 		errno = ERANGE;
108 		return UINTMAX_MAX;
109 		}
110 	else
111 		return minus ? -accum : accum;	/* (yes!) */
112 	}
113 
114 unsigned long long __attribute__ ((alias ("strtoumax")))
115 strtoull (const char* __restrict__ nptr, char ** __restrict__ endptr, int base);
116