loader: remove shouting from ORB's variable name
[hvf.git] / cp / nucleus / util.c
blob79236ac7ec7574c8c692697f830b978ce6fd9141
1 /*
2 * (C) Copyright 2007-2011 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * This file is released under the GPLv2. See the COPYING file for more
5 * details.
6 */
8 #include <slab.h>
9 #include <util.h>
11 char *strdup(char *s, int flags)
13 char *d;
14 int len;
16 for(d=s; *d; d++)
19 len = d - s;
21 d = malloc(sizeof(char)*len, flags);
22 if (!d)
23 return NULL;
25 memcpy(d, s, len*sizeof(char));
27 return d;
30 int hex(char *a, char *b, u64 *out)
32 u64 val;
33 char c;
35 if (a>=b)
36 return 1;
38 val = 0;
40 while(a<b) {
41 c = *a;
43 if ((c >= '0') && (c <= '9'))
44 val = (val << 4) | (*a - '0');
45 else if ((c >= 'A') && (c <= 'F'))
46 val = (val << 4) | (*a - 'A' + 10);
47 else if ((c >= 'a') && (c <= 'f'))
48 val = (val << 4) | (*a - 'a' + 10);
49 else
50 return 1;
52 a++;
55 *out = val;
57 return 0;
60 int bcd2dec(u64 val, u64 *out)
62 u64 v;
63 u64 scale;
65 for(v=0,scale=1; val; scale*=10, val>>=4) {
66 u64 x = val & 0xf;
68 if (x>=10)
69 return 1;
71 v += (x*scale);
74 *out = v;
76 return 0;