1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* util/support/hex.c - hex encoding/decoding implementation */ 3 /* 4 * Copyright (C) 2018 by the Massachusetts Institute of Technology. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 30 * OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <k5-platform.h> 34 #include <k5-hex.h> 35 #include <ctype.h> 36 37 static inline char 38 hex_digit(uint8_t bval, int uppercase) 39 { 40 assert(bval <= 0xF); 41 if (bval < 10) 42 return '0' + bval; 43 else if (uppercase) 44 return 'A' + (bval - 10); 45 else 46 return 'a' + (bval - 10); 47 } 48 49 int 50 k5_hex_encode(const void *bytes, size_t len, int uppercase, char **hex_out) 51 { 52 size_t i; 53 const uint8_t *p = bytes; 54 char *hex; 55 56 *hex_out = NULL; 57 58 hex = malloc(len * 2 + 1); 59 if (hex == NULL) 60 return ENOMEM; 61 62 for (i = 0; i < len; i++) { 63 hex[i * 2] = hex_digit(p[i] >> 4, uppercase); 64 hex[i * 2 + 1] = hex_digit(p[i] & 0xF, uppercase); 65 } 66 hex[len * 2] = '\0'; 67 68 *hex_out = hex; 69 return 0; 70 } 71 72 /* Decode a hex digit. Return 0-15 on success, -1 on invalid input. */ 73 static inline int 74 decode_hexchar(unsigned char c) 75 { 76 if (isdigit(c)) 77 return c - '0'; 78 if (c >= 'A' && c <= 'F') 79 return c - 'A' + 10; 80 if (c >= 'a' && c <= 'f') 81 return c - 'a' + 10; 82 return -1; 83 } 84 85 int 86 k5_hex_decode(const char *hex, uint8_t **bytes_out, size_t *len_out) 87 { 88 size_t hexlen, i; 89 int h1, h2; 90 uint8_t *bytes; 91 92 *bytes_out = NULL; 93 *len_out = 0; 94 95 hexlen = strlen(hex); 96 if (hexlen % 2 != 0) 97 return EINVAL; 98 bytes = malloc(hexlen / 2 + 1); 99 if (bytes == NULL) 100 return ENOMEM; 101 102 for (i = 0; i < hexlen / 2; i++) { 103 h1 = decode_hexchar(hex[i * 2]); 104 h2 = decode_hexchar(hex[i * 2 + 1]); 105 if (h1 == -1 || h2 == -1) { 106 free(bytes); 107 return EINVAL; 108 } 109 bytes[i] = h1 * 16 + h2; 110 } 111 bytes[i] = 0; 112 113 *bytes_out = bytes; 114 *len_out = hexlen / 2; 115 return 0; 116 } 117