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