12 #include <stddef.h> |
12 #include <stddef.h> |
13 #include <string.h> |
13 #include <string.h> |
14 |
14 |
15 #include "util.h" |
15 #include "util.h" |
16 |
16 |
|
17 static int8_t hextable[256] = { |
|
18 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
19 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
20 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */ |
|
22 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */ |
|
23 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
24 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */ |
|
25 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
26 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
|
34 }; |
|
35 |
17 static inline int hexdigit(const char *p, Py_ssize_t off) |
36 static inline int hexdigit(const char *p, Py_ssize_t off) |
18 { |
37 { |
19 char c = p[off]; |
38 int8_t val = hextable[(unsigned char)p[off]]; |
20 |
39 |
21 if (c >= '0' && c <= '9') |
40 if (val >= 0) { |
22 return c - '0'; |
41 return val; |
23 if (c >= 'a' && c <= 'f') |
42 } |
24 return c - 'a' + 10; |
|
25 if (c >= 'A' && c <= 'F') |
|
26 return c - 'A' + 10; |
|
27 |
43 |
28 PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); |
44 PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); |
29 return 0; |
45 return 0; |
30 } |
46 } |
31 |
47 |