Warnings purge of src/core
[gpxe.git] / src / core / misc.c
blob58399136e672c693d6d5260d3c4de3013d86ecae
1 /**************************************************************************
2 MISC Support Routines
3 **************************************************************************/
5 #include <etherboot.h>
6 #include <console.h>
7 #include <stdlib.h>
8 #include <stdio.h>
10 /**************************************************************************
11 IPCHKSUM - Checksum IP Header
12 **************************************************************************/
13 uint16_t ipchksum(const void *data, unsigned long length)
15 unsigned long sum;
16 unsigned long i;
17 const uint8_t *ptr;
19 /* In the most straight forward way possible,
20 * compute an ip style checksum.
22 sum = 0;
23 ptr = data;
24 for(i = 0; i < length; i++) {
25 unsigned long value;
26 value = ptr[i];
27 if (i & 1) {
28 value <<= 8;
30 /* Add the new value */
31 sum += value;
32 /* Wrap around the carry */
33 if (sum > 0xFFFF) {
34 sum = (sum + (sum >> 16)) & 0xFFFF;
37 return (~cpu_to_le16(sum)) & 0xFFFF;
40 uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new)
42 unsigned long checksum;
43 sum = ~sum & 0xFFFF;
44 new = ~new & 0xFFFF;
45 if (offset & 1) {
46 /* byte swap the sum if it came from an odd offset
47 * since the computation is endian independant this
48 * works.
50 new = bswap_16(new);
52 checksum = sum + new;
53 if (checksum > 0xFFFF) {
54 checksum -= 0xFFFF;
56 return (~checksum) & 0xFFFF;
59 /**************************************************************************
60 SLEEP
61 **************************************************************************/
62 unsigned int sleep(unsigned int secs)
64 unsigned long tmo;
66 for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
68 return 0;
71 /**************************************************************************
72 INTERRUPTIBLE SLEEP
73 **************************************************************************/
74 void interruptible_sleep(int secs)
76 printf("<sleep>\n");
77 sleep(secs);
80 /**************************************************************************
81 STRCASECMP (not entirely correct, but this will do for our purposes)
82 **************************************************************************/
83 int strcasecmp(const char *a, const char *b)
85 while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
86 return((*a & ~0x20) - (*b & ~0x20));
89 /**************************************************************************
90 INET_ATON - Convert an ascii x.x.x.x to binary form
91 **************************************************************************/
92 int inet_aton ( const char *cp, struct in_addr *inp ) {
93 const char *p = cp;
94 const char *digits_start;
95 unsigned long ip = 0;
96 unsigned long val;
97 int j;
98 for(j = 0; j <= 3; j++) {
99 digits_start = p;
100 val = strtoul(p, ( char ** ) &p, 10);
101 if ((p == digits_start) || (val > 255)) return 0;
102 if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
103 ip = (ip << 8) | val;
105 if ( *p == '\0' ) {
106 inp->s_addr = htonl(ip);
107 return 1;
109 return 0;
112 int isspace ( int c ) {
113 switch ( c ) {
114 case ' ':
115 case '\f':
116 case '\n':
117 case '\r':
118 case '\t':
119 case '\v':
120 return 1;
121 default:
122 return 0;
126 unsigned long strtoul ( const char *p, char **endp, int base ) {
127 unsigned long ret = 0;
128 unsigned int charval;
130 while ( isspace ( *p ) )
131 p++;
133 if ( base == 0 ) {
134 base = 10;
135 if ( *p == '0' ) {
136 p++;
137 base = 8;
138 if ( ( *p | 0x20 ) == 'x' ) {
139 p++;
140 base = 16;
145 while ( 1 ) {
146 charval = *p;
147 if ( charval >= 'a' ) {
148 charval = ( charval - 'a' + 10 );
149 } else if ( charval >= 'A' ) {
150 charval = ( charval - 'A' + 10 );
151 } else {
152 charval = ( charval - '0' );
154 if ( charval >= ( unsigned int ) base )
155 break;
156 ret = ( ( ret * base ) + charval );
157 p++;
160 if ( endp )
161 *endp = ( char * ) p;
163 return ( ret );
167 * Local variables:
168 * c-basic-offset: 8
169 * End: