2
* convert from text form of unsigned long to binary
3
* Copyright (C) 2000 Henry Spencer.
5
* This library is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU Library General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or (at your
8
* option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
10
* This library is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13
* License for more details.
15
* RCSID $Id: ttoul.c,v 1.4 2004/04/09 18:00:38 mcr Exp $
21
- ttoul - convert text substring to unsigned long number
23
const char * /* NULL for success, else string literal */
24
ttoul(src, srclen, base, resultp)
26
size_t srclen; /* 0 means strlen(src) */
27
int base; /* 0 means figure it out */
28
unsigned long *resultp;
31
static char hex[] = "0123456789abcdef";
32
static char uchex[] = "0123456789ABCDEF";
43
return "empty string";
46
if (srclen > 2 && *src == '0' &&
47
(*(src+1) == 'x' || *(src+1) == 'X'))
48
return ttoul(src+2, srclen-2, 16, resultp);
49
if (srclen > 1 && *src == '0')
50
return ttoul(src+1, srclen-1, 8, resultp);
51
return ttoul(src, srclen, 10, resultp);
53
if (base != 8 && base != 10 && base != 16)
54
return "unsupported number base";
67
return "non-hex digit in hex number";
72
/* defer length check to catch invalid digits first */
73
if (srclen > sizeof(unsigned long) * 2)
74
return "hex number too long";
76
rlimit = ULONG_MAX / base;
77
dlimit = (int)(ULONG_MAX - rlimit*base);
81
if (d < 0 || d >= base)
82
return "non-digit in number";
83
if (r > rlimit || (r == rlimit && d > dlimit))
84
return "unsigned-long overflow";