hex.c (1282B)
1 /* CC0 license (public domain) - see LICENSE file for details */ 2 #include "hex.h" 3 #include <assert.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 static bool char_to_hex(unsigned char *val, char c) 8 { 9 if (c >= '0' && c <= '9') { 10 *val = c - '0'; 11 return true; 12 } 13 if (c >= 'a' && c <= 'f') { 14 *val = c - 'a' + 10; 15 return true; 16 } 17 if (c >= 'A' && c <= 'F') { 18 *val = c - 'A' + 10; 19 return true; 20 } 21 return false; 22 } 23 24 bool hex_decode(const char *str, size_t slen, void *buf, size_t bufsize) 25 { 26 unsigned char v1, v2; 27 unsigned char *p = buf; 28 29 while (slen > 1) { 30 if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1])) 31 return false; 32 if (!bufsize) 33 return false; 34 *(p++) = (v1 << 4) | v2; 35 str += 2; 36 slen -= 2; 37 bufsize--; 38 } 39 return slen == 0 && bufsize == 0; 40 } 41 42 bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize) 43 { 44 size_t i; 45 46 if (destsize < hex_str_size(bufsize)) 47 return false; 48 49 for (i = 0; i < bufsize; i++) { 50 unsigned int c = ((const unsigned char *)buf)[i]; 51 *(dest++) = hexchar(c >> 4); 52 *(dest++) = hexchar(c & 0xF); 53 } 54 *dest = '\0'; 55 56 return true; 57 }