]>
Commit | Line | Data |
---|---|---|
d88e8106 CW |
1 | #include "git-compat-util.h" |
2 | #include "hex-ll.h" | |
3 | ||
4 | const signed char hexval_table[256] = { | |
5 | -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */ | |
6 | -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */ | |
7 | -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */ | |
8 | -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */ | |
9 | -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */ | |
10 | -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */ | |
11 | 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */ | |
12 | 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */ | |
13 | -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */ | |
14 | -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */ | |
15 | -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */ | |
16 | -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */ | |
17 | -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */ | |
18 | -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */ | |
19 | -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */ | |
20 | -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */ | |
21 | -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */ | |
22 | -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */ | |
23 | -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */ | |
24 | -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */ | |
25 | -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */ | |
26 | -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */ | |
27 | -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */ | |
28 | -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */ | |
29 | -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */ | |
30 | -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */ | |
31 | -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */ | |
32 | -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */ | |
33 | -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */ | |
34 | -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */ | |
35 | -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */ | |
36 | -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */ | |
37 | }; | |
38 | ||
39 | int hex_to_bytes(unsigned char *binary, const char *hex, size_t len) | |
40 | { | |
41 | for (; len; len--, hex += 2) { | |
42 | unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); | |
43 | ||
44 | if (val & ~0xff) | |
45 | return -1; | |
46 | *binary++ = val; | |
47 | } | |
48 | return 0; | |
49 | } |