2 * convert from ASCII form of unsigned long to binary
3 * Copyright (C) 1998, 1999 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: atoul.c,v 1.6 1999/12/07 05:00:52 henry Exp $
21 - atoul - convert ASCII substring to unsigned long number
23 const char * /* NULL for success, else string literal */
24 atoul(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";
45 if (base == 0 || base == 13) {
46 if (srclen > 2 && *src == '0' && CIEQ(*(src+1), 'x'))
47 return atoul(src+2, srclen-2, 16, resultp);
48 if (srclen > 1 && *src == '0' && base != 13)
49 return atoul(src+1, srclen-1, 8, resultp);
50 return atoul(src, srclen, 10, resultp);
52 if (base != 8 && base != 10 && base != 16)
53 return "unsupported number base";
66 return "non-hex-digit in hex number";
71 /* defer length check to catch invalid digits first */
72 if (srclen > sizeof(unsigned long) * 2)
73 return "hex number too long";
75 rlimit = ULONG_MAX / base;
76 dlimit = (int)(ULONG_MAX - rlimit*base);
80 if (d < 0 || d >= base)
81 return "non-digit in number";
82 if (r > rlimit || (r == rlimit && d > dlimit))
83 return "unsigned-long overflow";