| Calvin Wan | d88e810 | 2023-09-29 21:20:48 | [diff] [blame] | 1 | #ifndef HEX_LL_H |
| 2 | #define HEX_LL_H |
| 3 | |
| 4 | extern const signed char hexval_table[256]; |
| 5 | static inline unsigned int hexval(unsigned char c) |
| 6 | { |
| 7 | return hexval_table[c]; |
| 8 | } |
| 9 | |
| 10 | /* |
| 11 | * Convert two consecutive hexadecimal digits into a char. Return a |
| 12 | * negative value on error. Don't run over the end of short strings. |
| 13 | */ |
| 14 | static inline int hex2chr(const char *s) |
| 15 | { |
| 16 | unsigned int val = hexval(s[0]); |
| 17 | return (val & ~0xf) ? val : (val << 4) | hexval(s[1]); |
| 18 | } |
| 19 | |
| 20 | /* |
| 21 | * Read `len` pairs of hexadecimal digits from `hex` and write the |
| 22 | * values to `binary` as `len` bytes. Return 0 on success, or -1 if |
| 23 | * the input does not consist of hex digits). |
| 24 | */ |
| 25 | int hex_to_bytes(unsigned char *binary, const char *hex, size_t len); |
| 26 | |
| 27 | #endif |