aboutsummaryrefslogtreecommitdiff
path: root/src/core/misc.c
blob: 84cfcd803362e71a2efd6d8342aa56582b953f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**************************************************************************
MISC Support Routines
**************************************************************************/

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdlib.h>
#include <ctype.h>
#include <byteswap.h>
#include <ipxe/in.h>
#include <ipxe/timer.h>

unsigned int strtoul_charval ( unsigned int charval ) {

	if ( charval >= 'a' ) {
		charval = ( charval - 'a' + 10 );
	} else if ( charval >= 'A' ) {
		charval = ( charval - 'A' + 10 );
	} else if ( charval <= '9' ) {
		charval = ( charval - '0' );
	}

	return charval;
}

unsigned long strtoul ( const char *p, char **endp, int base ) {
	unsigned long ret = 0;
	int negative = 0;
	unsigned int charval;

	while ( isspace ( *p ) )
		p++;

	if ( *p == '-' ) {
		negative = 1;
		p++;
	}

	base = strtoul_base ( &p, base );

	while ( 1 ) {
		charval = strtoul_charval ( *p );
		if ( charval >= ( unsigned int ) base )
			break;
		ret = ( ( ret * base ) + charval );
		p++;
	}

	if ( negative )
		ret = -ret;

	if ( endp )
		*endp = ( char * ) p;

	return ( ret );
}

/*
 * Local variables:
 *  c-basic-offset: 8
 * End:
 */