[ipv4] Rewrite inet_aton()
[ipxe.git] / src / core / strtoull.c
1 /*
2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>
3 * Copyright (C) 2010 Piotr JaroszyƄski <p.jaroszynski@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdlib.h>
23 #include <ctype.h>
24
25 /*
26 * Despite being exactly the same as strtoul() except the long long instead of
27 * long it ends up being much bigger so provide a separate implementation in a
28 * separate object so that it won't be linked in if not used.
29 */
30 unsigned long long strtoull ( const char *p, char **endp, int base ) {
31 unsigned long long ret = 0;
32 int negative = 0;
33 unsigned int charval;
34
35 while ( isspace ( *p ) )
36 p++;
37
38 if ( *p == '-' ) {
39 negative = 1;
40 p++;
41 }
42
43 base = strtoul_base ( &p, base );
44
45 while ( 1 ) {
46 charval = strtoul_charval ( *p );
47 if ( charval >= ( unsigned int ) base )
48 break;
49 ret = ( ( ret * base ) + charval );
50 p++;
51 }
52
53 if ( negative )
54 ret = -ret;
55
56 if ( endp )
57 *endp = ( char * ) p;
58
59 return ( ret );
60 }