[ipv4] Rewrite inet_aton()
[ipxe.git] / src / core / misc.c
1 /**************************************************************************
2 MISC Support Routines
3 **************************************************************************/
4
5 FILE_LICENCE ( GPL2_OR_LATER );
6
7 #include <stdlib.h>
8 #include <ctype.h>
9 #include <byteswap.h>
10 #include <ipxe/in.h>
11 #include <ipxe/timer.h>
12
13 unsigned int strtoul_charval ( unsigned int charval ) {
14
15 if ( charval >= 'a' ) {
16 charval = ( charval - 'a' + 10 );
17 } else if ( charval >= 'A' ) {
18 charval = ( charval - 'A' + 10 );
19 } else if ( charval <= '9' ) {
20 charval = ( charval - '0' );
21 }
22
23 return charval;
24 }
25
26 unsigned long strtoul ( const char *p, char **endp, int base ) {
27 unsigned long ret = 0;
28 int negative = 0;
29 unsigned int charval;
30
31 while ( isspace ( *p ) )
32 p++;
33
34 if ( *p == '-' ) {
35 negative = 1;
36 p++;
37 }
38
39 base = strtoul_base ( &p, base );
40
41 while ( 1 ) {
42 charval = strtoul_charval ( *p );
43 if ( charval >= ( unsigned int ) base )
44 break;
45 ret = ( ( ret * base ) + charval );
46 p++;
47 }
48
49 if ( negative )
50 ret = -ret;
51
52 if ( endp )
53 *endp = ( char * ) p;
54
55 return ( ret );
56 }
57
58 /*
59 * Local variables:
60 * c-basic-offset: 8
61 * End:
62 */