1 /* CC0 (Public domain) - see LICENSE file for details */
9 * hex_decode - Unpack a hex string.
10 * @str: the hexidecimal string
11 * @slen: the length of @str
12 * @buf: the buffer to write the data into
13 * @bufsize: the length of @buf
15 * Returns false if there are any characters which aren't 0-9, a-f or A-F,
16 * of the string wasn't the right length for @bufsize.
19 * unsigned char data[20];
21 * if (!hex_decode(argv[1], strlen(argv[1]), data, 20))
22 * printf("String is malformed!\n");
24 bool hex_decode(const char *str, size_t slen, void *buf, size_t bufsize);
27 * hex_encode - Create a nul-terminated hex string
28 * @buf: the buffer to read the data from
29 * @bufsize: the length of @buf
30 * @dest: the string to fill
31 * @destsize: the max size of the string
33 * Returns true if the string, including terminator, fit in @destsize;
36 * unsigned char buf[] = { 0x1F, 0x2F };
39 * if (!hex_encode(buf, sizeof(buf), str, sizeof(str)))
42 bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize);
45 * hex_str_size - Calculate how big a nul-terminated hex string is
46 * @bytes: bytes of data to represent
49 * unsigned char buf[] = { 0x1F, 0x2F };
50 * char str[hex_str_size(sizeof(buf))];
52 * hex_encode(buf, sizeof(buf), str, sizeof(str));
54 static inline size_t hex_str_size(size_t bytes)
60 * hex_data_size - Calculate how many bytes of data in a hex string
61 * @strlen: the length of the string (with or without NUL)
64 * const char str[] = "1F2F";
65 * unsigned char buf[hex_data_size(sizeof(str))];
67 * hex_decode(str, strlen(str), buf, sizeof(buf));
69 static inline size_t hex_data_size(size_t strlen)
73 #endif /* PETTYCOIN_HEX_H */