Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / 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 /**************************************************************************
14 INET_ATON - Convert an ascii x.x.x.x to binary form
15 **************************************************************************/
16 int inet_aton ( const char *cp, struct in_addr *inp ) {
17         const char *p = cp;
18         const char *digits_start;
19         unsigned long ip = 0;
20         unsigned long val;
21         int j;
22         for(j = 0; j <= 3; j++) {
23                 digits_start = p;
24                 val = strtoul(p, ( char ** ) &p, 10);
25                 if ((p == digits_start) || (val > 255)) return 0;
26                 if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
27                 ip = (ip << 8) | val;
28         }
29         if ( *p == '\0' ) {
30                 inp->s_addr = htonl(ip);
31                 return 1;
32         }
33         return 0;
34 }
35
36 unsigned int strtoul_charval ( unsigned int charval ) {
37
38         if ( charval >= 'a' ) {
39                 charval = ( charval - 'a' + 10 );
40         } else if ( charval >= 'A' ) {
41                 charval = ( charval - 'A' + 10 );
42         } else if ( charval <= '9' ) {
43                 charval = ( charval - '0' );
44         }
45
46         return charval;
47 }
48
49 unsigned long strtoul ( const char *p, char **endp, int base ) {
50         unsigned long ret = 0;
51         int negative = 0;
52         unsigned int charval;
53
54         while ( isspace ( *p ) )
55                 p++;
56
57         if ( *p == '-' ) {
58                 negative = 1;
59                 p++;
60         }
61
62         base = strtoul_base ( &p, base );
63
64         while ( 1 ) {
65                 charval = strtoul_charval ( *p );
66                 if ( charval >= ( unsigned int ) base )
67                         break;
68                 ret = ( ( ret * base ) + charval );
69                 p++;
70         }
71
72         if ( negative )
73                 ret = -ret;
74
75         if ( endp )
76                 *endp = ( char * ) p;
77
78         return ( ret );
79 }
80
81 /*
82  * Local variables:
83  *  c-basic-offset: 8
84  * End:
85  */