3930d265acbd41379ee452c517836efac2e8c036
[gpxe.git] / src / core / misc.c
1 /**************************************************************************
2 MISC Support Routines
3 **************************************************************************/
4
5 #include "etherboot.h"
6 #include "console.h"
7
8 /**************************************************************************
9 IPCHKSUM - Checksum IP Header
10 **************************************************************************/
11 uint16_t ipchksum(const void *data, unsigned long length)
12 {
13         unsigned long sum;
14         unsigned long i;
15         const uint8_t *ptr;
16
17         /* In the most straight forward way possible,
18          * compute an ip style checksum.
19          */
20         sum = 0;
21         ptr = data;
22         for(i = 0; i < length; i++) {
23                 unsigned long value;
24                 value = ptr[i];
25                 if (i & 1) {
26                         value <<= 8;
27                 }
28                 /* Add the new value */
29                 sum += value;
30                 /* Wrap around the carry */
31                 if (sum > 0xFFFF) {
32                         sum = (sum + (sum >> 16)) & 0xFFFF;
33                 }
34         }
35         return (~cpu_to_le16(sum)) & 0xFFFF;
36 }
37
38 uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new)
39 {
40         unsigned long checksum;
41         sum = ~sum & 0xFFFF;
42         new = ~new & 0xFFFF;
43         if (offset & 1) {
44                 /* byte swap the sum if it came from an odd offset 
45                  * since the computation is endian independant this
46                  * works.
47                  */
48                 new = bswap_16(new);
49         }
50         checksum = sum + new;
51         if (checksum > 0xFFFF) {
52                 checksum -= 0xFFFF;
53         }
54         return (~checksum) & 0xFFFF;
55 }
56
57
58
59 /**************************************************************************
60 RANDOM - compute a random number between 0 and 2147483647L or 2147483562?
61 **************************************************************************/
62 int32_t random(void)
63 {
64         static int32_t seed = 0;
65         int32_t q;
66         if (!seed) /* Initialize linear congruential generator */
67                 seed = currticks();
68         /* simplified version of the LCG given in Bruce Schneier's
69            "Applied Cryptography" */
70         q = seed/53668;
71         if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L;
72         return seed;
73 }
74
75 /**************************************************************************
76 SLEEP
77 **************************************************************************/
78 unsigned int sleep(unsigned int secs)
79 {
80         unsigned long tmo;
81
82         for (tmo = currticks()+secs*TICKS_PER_SEC; currticks() < tmo; ) {
83         }
84         return 0;
85 }
86
87 /**************************************************************************
88 INTERRUPTIBLE SLEEP
89 **************************************************************************/
90 void interruptible_sleep(int secs)
91 {
92         printf("<sleep>\n");
93         sleep(secs);
94 }
95
96 /**************************************************************************
97 TWIDDLE
98 **************************************************************************/
99 void twiddle(void)
100 {
101 #ifdef BAR_PROGRESS
102         static int count=0;
103         static const char tiddles[]="-\\|/";
104         static unsigned long lastticks = 0;
105         unsigned long ticks;
106 #endif
107         if ( ! as_main_program ) return;
108 #ifdef  BAR_PROGRESS
109         /* Limit the maximum rate at which characters are printed */
110         ticks = currticks();
111         if ((lastticks + (TICKS_PER_SEC/18)) > ticks)
112                 return;
113         lastticks = ticks;
114
115         putchar(tiddles[(count++)&3]);
116         putchar('\b');
117 #else
118         putchar('.');
119 #endif  /* BAR_PROGRESS */
120 }
121
122 /**************************************************************************
123 STRCASECMP (not entirely correct, but this will do for our purposes)
124 **************************************************************************/
125 int strcasecmp(const char *a, const char *b)
126 {
127         while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
128         return((*a & ~0x20) - (*b & ~0x20));
129 }
130
131 /**************************************************************************
132 INET_ATON - Convert an ascii x.x.x.x to binary form
133 **************************************************************************/
134 int inet_aton ( const char *cp, struct in_addr *inp ) {
135         const char *p = cp;
136         const char *digits_start;
137         unsigned long ip = 0;
138         unsigned long val;
139         int j;
140         for(j = 0; j <= 3; j++) {
141                 digits_start = p;
142                 val = strtoul(p, ( char ** ) &p, 10);
143                 if ((p == digits_start) || (val > 255)) return 0;
144                 if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
145                 ip = (ip << 8) | val;
146         }
147         if ( *p == '\0' ) {
148                 inp->s_addr = htonl(ip);
149                 return 1;
150         }
151         return 0;
152 }
153
154 unsigned long strtoul ( const char *p, char **endp, int base ) {
155         unsigned long ret = 0;
156         unsigned int charval;
157
158         if ( base == 0 ) {
159                 base = 10;
160                 if ( *p == '0' ) {
161                         p++;
162                         base = 8;
163                         if ( ( *p | 0x20 ) == 'x' ) {
164                                 p++;
165                                 base = 16;
166                         }
167                 }
168         }
169
170         while ( 1 ) {
171                 charval = *p;
172                 if ( charval >= 'a' ) {
173                         charval = ( charval - 'a' + 10 );
174                 } else if ( charval >= 'A' ) {
175                         charval = ( charval - 'A' + 10 );
176                 } else {
177                         charval = ( charval - '0' );
178                 }
179                 if ( charval >= ( unsigned int ) base )
180                         break;
181                 ret = ( ( ret * base ) + charval );
182                 p++;
183         }
184
185         if ( endp )
186                 *endp = ( char * ) p;
187
188         return ( ret );
189 }
190
191 /*
192  * Local variables:
193  *  c-basic-offset: 8
194  * End:
195  */