1/***********************************************************************
2*                                                                      *
3*               This software is part of the ast package               *
4*          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5*                      and is licensed under the                       *
6*                 Eclipse Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*          http://www.eclipse.org/org/documents/epl-v10.html           *
11*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12*                                                                      *
13*              Information and Software Systems Research               *
14*                            AT&T Research                             *
15*                           Florham Park NJ                            *
16*                                                                      *
17*                 Glenn Fowler <gsf@research.att.com>                  *
18*                  David Korn <dgk@research.att.com>                   *
19*                   Phong Vo <kpv@research.att.com>                    *
20*                                                                      *
21***********************************************************************/
22/*
23 * AT&T Research
24 * Glenn Fowler
25 * Phong Vo
26 *
27 * common header and implementation for
28 *
29 *	strtol		strtoul		strton
30 *	strtoll		strtoull	strtonll
31 *	strntol		strntoul	strnton
32 *	strntoll	strntoull	strntonll
33 *
34 * define these macros to instantiate an implementation:
35 *
36 *	S2I_function	the function name
37 *	S2I_number	the signed number type
38 *	S2I_unumber	the unsigned number type
39 *	S2I_unsigned	1 for unsigned, 0 for signed
40 *	S2I_qualifier	1 for optional qualifier suffix, 0 otherwise
41 *	S2I_multiplier	1 for optional multiplier suffix, 0 otherwise
42 *	S2I_size	the second argument is the input string size
43 *
44 * convert string to number
45 * errno=ERANGE on overflow (LONG_MAX) or underflow (LONG_MIN)
46 * if non-null e will point to first unrecognized char in s
47 * if basep!=0 it points to the default base on input and
48 * will point to the explicit base on return
49 * a default base of 0 will determine the base from the input
50 * a default base of 1 will determine the base from the input using bb#*
51 * a base prefix in the string overrides *b
52 * *b will not be set if the string has no base prefix
53 * if m>1 and no multipler was specified then the result is multiplied by m
54 * if m<0 then multipliers are not consumed
55 * if a base arg or prefix is specified then multiplier is not consumed
56 *
57 * integer numbers are of the form:
58 *
59 *	[sign][base][number[qualifier]][multiplier]
60 *
61 *	base:		nnn#		base nnn
62 *			0[xX]		hex
63 *			0		octal
64 *			[1-9]		decimal
65 *
66 *	number:		[0-9a-zA-Z]*
67 *
68 *	qualifier:	[lL]
69 *			[uU]
70 *			[uU][lL]
71 *			[lL][uU]
72 *			[lL][lL][uU]
73 *			[uU][lL][lL]
74 *
75 *	multiplier:	.		pseudo-float if m>1
76 *			[bB]		block (512)
77 *			[cC]		char (1)
78 *			[gG]		giga (1000*1000*1000)
79 *			[gG]i		gibi (1024*1024*1024)
80 *			[kK]		kilo (1000)
81 *			[kK]i		kibi (1024)
82 *			[mM]		mega (1000*1000)
83 *			[mM]i		mibi (1024*1024)
84 */
85
86#include <ast.h>
87#include <ctype.h>
88
89#include "sfhdr.h"
90
91#if !__STD_C && !defined(const)
92#define const
93#endif
94
95#ifndef ERANGE
96#define ERANGE		EINVAL
97#endif
98
99#define QL		01
100#define QU		02
101
102#define S2I_umax	(~((S2I_unumber)0))
103
104#if S2I_unsigned
105#define S2I_type	S2I_unumber
106#define S2I_min		0
107#define S2I_max		S2I_umax
108#else
109#define S2I_type	S2I_number
110#define S2I_min		(-S2I_max-1)
111#define S2I_max		(S2I_umax>>1)
112#endif
113
114#if S2I_size
115#define S2I_valid(s)	((s)<(z))
116#else
117#define S2I_valid(s)	1
118#endif
119
120#define ADDOVER(n,c,s)	((S2I_umax-(n))<((S2I_unumber)((c)+(s))))
121#define MPYOVER(n,c)	(((S2I_unumber)(n))>(S2I_umax/(c)))
122
123static const S2I_unumber	mm[] =
124{
125	0,
126	S2I_umax /  1,
127	S2I_umax /  2,
128	S2I_umax /  3,
129	S2I_umax /  4,
130	S2I_umax /  5,
131	S2I_umax /  6,
132	S2I_umax /  7,
133	S2I_umax /  8,
134	S2I_umax /  9,
135	S2I_umax / 10,
136	S2I_umax / 11,
137	S2I_umax / 12,
138	S2I_umax / 13,
139	S2I_umax / 14,
140	S2I_umax / 15,
141	S2I_umax / 16,
142	S2I_umax / 17,
143	S2I_umax / 18,
144	S2I_umax / 19,
145	S2I_umax / 20,
146	S2I_umax / 21,
147	S2I_umax / 22,
148	S2I_umax / 23,
149	S2I_umax / 24,
150	S2I_umax / 25,
151	S2I_umax / 26,
152	S2I_umax / 27,
153	S2I_umax / 28,
154	S2I_umax / 29,
155	S2I_umax / 30,
156	S2I_umax / 31,
157	S2I_umax / 32,
158	S2I_umax / 33,
159	S2I_umax / 34,
160	S2I_umax / 35,
161	S2I_umax / 36,
162	S2I_umax / 37,
163	S2I_umax / 38,
164	S2I_umax / 39,
165	S2I_umax / 40,
166	S2I_umax / 41,
167	S2I_umax / 42,
168	S2I_umax / 43,
169	S2I_umax / 44,
170	S2I_umax / 45,
171	S2I_umax / 46,
172	S2I_umax / 47,
173	S2I_umax / 48,
174	S2I_umax / 49,
175	S2I_umax / 50,
176	S2I_umax / 51,
177	S2I_umax / 52,
178	S2I_umax / 53,
179	S2I_umax / 54,
180	S2I_umax / 55,
181	S2I_umax / 56,
182	S2I_umax / 57,
183	S2I_umax / 58,
184	S2I_umax / 59,
185	S2I_umax / 60,
186	S2I_umax / 61,
187	S2I_umax / 62,
188	S2I_umax / 63,
189	S2I_umax / 64,
190};
191
192#if defined(__EXPORT__)
193#define extern		__EXPORT__
194#endif
195extern S2I_type
196#undef	extern
197#if S2I_size
198#if S2I_multiplier
199#if __STD_C
200S2I_function(const char* a, size_t size, char** e, char* basep, int m)
201#else
202S2I_function(a, size, e, basep, m) const char* a; size_t size; char** e; char* basep; int m;
203#endif
204#else
205#if __STD_C
206S2I_function(const char* a, size_t size, char** e, int base)
207#else
208S2I_function(a, size, e, base) const char* a; size_t size; char** e; int base;
209#endif
210#endif
211#else
212#if S2I_multiplier
213#if __STD_C
214S2I_function(const char* a, char** e, char* basep, int m)
215#else
216S2I_function(a, e, basep, m) const char* a; char** e; char* basep; int m;
217#endif
218#else
219#if __STD_C
220S2I_function(const char* a, char** e, int base)
221#else
222S2I_function(a, e, base) const char* a; char** e; int base;
223#endif
224#endif
225#endif
226{
227	register unsigned char*	s = (unsigned char*)a;
228#if S2I_size
229	register unsigned char*	z = s + size;
230#endif
231	register S2I_unumber	n;
232	register S2I_unumber	x;
233	register int		c;
234	register int		shift;
235	register unsigned char*	p;
236	register unsigned char*	cv;
237	unsigned char*		b;
238	unsigned char*		k;
239	S2I_unumber		v;
240#if S2I_multiplier
241	register int		base;
242#endif
243	int			negative;
244	int			overflow = 0;
245	int			decimal = 0;
246	int			thousand = 0;
247#if !S2I_unsigned
248	int			qualifier = 0;
249#endif
250
251#if S2I_multiplier
252	base = basep ? *((unsigned char*)basep) : 0;
253#else
254	if (base > 36 && base <= SF_RADIX)
255	{
256		static int	conformance = -1;
257
258		if (conformance < 0)
259			conformance = !strcmp(astconf("CONFORMANCE", NiL, NiL), "standard");
260		if (conformance)
261			base = 1;
262	}
263#endif
264	if (base && (base < 2 || base > SF_RADIX))
265	{
266		errno = EINVAL;
267		return 0;
268	}
269	while (S2I_valid(s) && isspace(*s))
270		s++;
271	if ((negative = S2I_valid(s) && (*s == '-')) || S2I_valid(s) && *s == '+')
272		k = ++s;
273	else
274		k = 0;
275	p = s;
276	if (!base)
277	{
278		if (S2I_valid(p) && (c = *p++) >= '0' && c <= '9')
279		{
280			n = c - '0';
281			if (S2I_valid(p) && (c = *p) >= '0' && c <= '9')
282			{
283				n = (n << 3) + (n << 1) + c - '0';
284				p++;
285			}
286			if (S2I_valid(p) && *p == '#')
287			{
288				if (n >= 2 && n <= 64)
289				{
290					k = s = p + 1;
291					base = n;
292				}
293			}
294			else if (base)
295				base = 0;
296			else if (S2I_valid(s) && *s == '0' && S2I_valid(s + 1))
297			{
298				if ((c = *(s + 1)) == 'x' || c == 'X')
299				{
300					k = s += 2;
301					base = 16;
302				}
303				else if (c >= '0' && c <= '7')
304				{
305					s++;
306					base = 8;
307				}
308			}
309		}
310		if (!base)
311			base = 10;
312		else if (base < 2 || base > SF_RADIX)
313		{
314			errno = EINVAL;
315			return 0;
316		}
317#if S2I_multiplier
318		else
319		{
320			if (basep)
321				*basep = base;
322			m = -1;
323		}
324#endif
325	}
326#if S2I_multiplier
327	else
328		m = -1;
329#endif
330
331	/*
332	 * this part transcribed from sfvscanf()
333	 */
334
335	SFSETLOCALE(&decimal, &thousand);
336	x = mm[base];
337	n = 0;
338	if (base == 10)
339	{
340		b = s;
341		p = 0;
342		for (;;)
343		{
344			if (S2I_valid(s) && (c = *s++) >= '0' && c <= '9')
345			{
346				if (n > x)
347					overflow = 1;
348				else
349				{
350					n = (n << 3) + (n << 1);
351					c -= '0';
352					if (ADDOVER(n, c, negative))
353						overflow = 1;
354					n += c;
355				}
356			}
357			else if (p && (s - p) != (3 + S2I_valid(s)))
358			{
359				s = p;
360				n = v;
361				c = 0;
362				break;
363			}
364			else if (!S2I_valid(s) || c != thousand)
365				break;
366			else if (!p && (s - b) > 4)
367			{
368				if (e)
369					*e = (char*)s - 1;
370				if (overflow)
371				{
372					errno = ERANGE;
373#if S2I_unsigned
374					n = S2I_max;
375#else
376					n = negative ? S2I_min : S2I_max;
377#endif
378				}
379				return n;
380			}
381			else
382			{
383				p = s;
384				v = n;
385			}
386		}
387	}
388	else
389	{
390		SFCVINIT();
391		cv = base <= 36 ? _Sfcv36 : _Sfcv64;
392		if ((base & ~(base - 1)) == base)
393		{
394#if !S2I_unsigned
395			qualifier |= QU;
396#endif
397			if (base < 8)
398				shift = base <  4 ? 1 : 2;
399			else if (base < 32)
400				shift = base < 16 ? 3 : 4;
401			else
402				shift = base < 64 ? 5 : 6;
403			while (S2I_valid(s) && (c = cv[*s++]) < base)
404			{
405				if (n > x)
406					overflow = 1;
407				else
408				{
409					n <<= shift;
410					if (ADDOVER(n, c, negative))
411						overflow = 1;
412					n += c;
413				}
414			}
415		}
416		else
417			while (S2I_valid(s) && (c = cv[*s++]) < base)
418			{
419				if (n > x)
420					overflow = 1;
421				else
422				{
423					n *= base;
424					if (ADDOVER(n, c, negative))
425						overflow = 1;
426					n += c;
427				}
428			}
429		c = *(s - 1);
430	}
431
432#if S2I_qualifier
433
434	/*
435	 * optional qualifier suffix
436	 */
437
438	if (S2I_valid(s) && s > (unsigned char*)(a + 1))
439	{
440		base = 0;
441		for (;;)
442		{
443			if (!(base & QL) && (c == 'l' || c == 'L'))
444			{
445				base |= QL;
446				if (!S2I_valid(s))
447					break;
448				c = *s++;
449				if (c == 'l' || c == 'L')
450				{
451					if (!S2I_valid(s))
452						break;
453					c = *s++;
454				}
455			}
456			else if (!(base & QU) && (c == 'u' || c == 'U'))
457			{
458				base |= QU;
459#if !S2I_unsigned
460				qualifier |= QU;
461#endif
462				if (!S2I_valid(s))
463					break;
464				c = *s++;
465			}
466			else
467				break;
468		}
469	}
470#endif
471	if (S2I_valid(s))
472	{
473#if S2I_multiplier
474		/*
475		 * optional multiplier suffix
476		 */
477
478		if (m < 0 || s == (unsigned char*)(a + 1))
479			s--;
480		else
481		{
482			x = m != 1;
483			switch (c)
484			{
485			case 'b':
486			case 'B':
487				shift = 9;
488				x = 0;
489				break;
490			case 'k':
491			case 'K':
492				shift = 10;
493				break;
494			case 'm':
495			case 'M':
496				shift = 20;
497				break;
498			case 'g':
499			case 'G':
500				shift = 30;
501				break;
502			case 't':
503			case 'T':
504				shift = 40;
505				break;
506			case 'p':
507			case 'P':
508				shift = 50;
509				break;
510			case 'e':
511			case 'E':
512				shift = 60;
513				break;
514			default:
515				if (m <= 1)
516					v = 0;
517				else if (c == decimal && S2I_valid(s))
518				{
519					if (MPYOVER(n, m))
520						overflow = 1;
521					n *= m;
522					v = 0;
523					while (S2I_valid(s) && (c = *s++) >= '0' && c <= '9')
524						v += (m /= 10) * (c - '0');
525					if (ADDOVER(n, v, negative))
526						overflow = 1;
527					n += v;
528					v = 0;
529				}
530				else
531					v = m;
532				s--;
533				shift = 0;
534				break;
535			}
536			if (shift)
537			{
538				if (S2I_valid(s))
539					switch (*s)
540					{
541					case 'i':
542					case 'I':
543						s++;
544						x = 0;
545						break;
546					}
547				if (S2I_valid(s))
548					switch (*s)
549					{
550					case 'b':
551					case 'B':
552						s++;
553						break;
554					}
555				if (x)
556				{
557					v = 1;
558					for (shift /= 10; shift; shift--)
559					{
560						if (v >= (S2I_max/1000))
561						{
562							v = 0;
563							overflow = 1;
564						}
565						v *= 1000;
566					}
567				}
568				else
569#if S2I_unsigned
570				if (shift >= (sizeof(S2I_type) * CHAR_BIT))
571#else
572				if (shift >= (sizeof(S2I_type) * CHAR_BIT - 1))
573#endif
574				{
575					v = 0;
576					overflow = 1;
577				}
578				else
579					v = ((S2I_unumber)1) << shift;
580			}
581			if (v)
582			{
583				if (MPYOVER(n, v))
584					overflow = 1;
585				n *= v;
586			}
587		}
588#else
589		s--;
590#endif
591	}
592	if (s == k)
593	{
594		s--;
595#if S2I_multiplier
596		if (basep)
597			*basep = 10;
598#endif
599	}
600#if !S2I_unsigned
601	else if (!(qualifier & QU))
602	{
603		if (negative)
604		{
605			if (!n)
606			{
607				b = k;
608				do
609				{
610					if (b >= s)
611					{
612						negative = 0;
613						break;
614					}
615				} while (*b++ == '0');
616			}
617			if (negative && (n - 1) > S2I_max)
618				overflow = 1;
619		}
620		else if (n > S2I_max)
621			overflow = 1;
622	}
623#endif
624	if (e)
625		*e = (char*)s;
626	if (overflow)
627	{
628#if !S2I_unsigned
629		if (negative)
630		{
631			if (x << 1)
632				errno = ERANGE;
633			return (S2I_type)S2I_min;
634		}
635#endif
636		errno = ERANGE;
637		return (S2I_type)S2I_max;
638	}
639	return negative ? -n : n;
640}
641