base32hex.c 9.21 KB
Newer Older
1
/*  Copyright (C) 2011-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 16
 */

17
#include "base32hex.h"
18 19 20 21 22 23 24 25

#include <stdlib.h>
#include <stdint.h>

/*! \brief Maximal length of binary input to Base32hex encoding. */
#define MAX_BIN_DATA_LEN	((INT32_MAX / 8) * 5)

/*! \brief Base32hex padding character. */
26 27 28
static const uint8_t base32hex_pad = '=';
/*! \brief Base32hex alphabet.  Beware: original code was upper-case. */
static const uint8_t base32hex_enc[] = "0123456789abcdefghijklmnopqrstuv";
29 30 31 32 33 34 35

/*! \brief Indicates bad Base32hex character. */
#define KO	255
/*! \brief Indicates Base32hex padding character. */
#define PD	 32

/*! \brief Transformation and validation table for decoding Base32hex. */
36
static const uint8_t base32hex_dec[256] = {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	[  0] = KO, [ 43] = KO, ['V'] = 31, [129] = KO, [172] = KO, [215] = KO,
	[  1] = KO, [ 44] = KO, ['W'] = KO, [130] = KO, [173] = KO, [216] = KO,
	[  2] = KO, [ 45] = KO, ['X'] = KO, [131] = KO, [174] = KO, [217] = KO,
	[  3] = KO, [ 46] = KO, ['Y'] = KO, [132] = KO, [175] = KO, [218] = KO,
	[  4] = KO, [ 47] = KO, ['Z'] = KO, [133] = KO, [176] = KO, [219] = KO,
	[  5] = KO, ['0'] =  0, [ 91] = KO, [134] = KO, [177] = KO, [220] = KO,
	[  6] = KO, ['1'] =  1, [ 92] = KO, [135] = KO, [178] = KO, [221] = KO,
	[  7] = KO, ['2'] =  2, [ 93] = KO, [136] = KO, [179] = KO, [222] = KO,
	[  8] = KO, ['3'] =  3, [ 94] = KO, [137] = KO, [180] = KO, [223] = KO,
	[  9] = KO, ['4'] =  4, [ 95] = KO, [138] = KO, [181] = KO, [224] = KO,
	[ 10] = KO, ['5'] =  5, [ 96] = KO, [139] = KO, [182] = KO, [225] = KO,
	[ 11] = KO, ['6'] =  6, ['a'] = 10, [140] = KO, [183] = KO, [226] = KO,
	[ 12] = KO, ['7'] =  7, ['b'] = 11, [141] = KO, [184] = KO, [227] = KO,
	[ 13] = KO, ['8'] =  8, ['c'] = 12, [142] = KO, [185] = KO, [228] = KO,
	[ 14] = KO, ['9'] =  9, ['d'] = 13, [143] = KO, [186] = KO, [229] = KO,
	[ 15] = KO, [ 58] = KO, ['e'] = 14, [144] = KO, [187] = KO, [230] = KO,
	[ 16] = KO, [ 59] = KO, ['f'] = 15, [145] = KO, [188] = KO, [231] = KO,
	[ 17] = KO, [ 60] = KO, ['g'] = 16, [146] = KO, [189] = KO, [232] = KO,
	[ 18] = KO, ['='] = PD, ['h'] = 17, [147] = KO, [190] = KO, [233] = KO,
	[ 19] = KO, [ 62] = KO, ['i'] = 18, [148] = KO, [191] = KO, [234] = KO,
	[ 20] = KO, [ 63] = KO, ['j'] = 19, [149] = KO, [192] = KO, [235] = KO,
	[ 21] = KO, [ 64] = KO, ['k'] = 20, [150] = KO, [193] = KO, [236] = KO,
	[ 22] = KO, ['A'] = 10, ['l'] = 21, [151] = KO, [194] = KO, [237] = KO,
	[ 23] = KO, ['B'] = 11, ['m'] = 22, [152] = KO, [195] = KO, [238] = KO,
	[ 24] = KO, ['C'] = 12, ['n'] = 23, [153] = KO, [196] = KO, [239] = KO,
	[ 25] = KO, ['D'] = 13, ['o'] = 24, [154] = KO, [197] = KO, [240] = KO,
	[ 26] = KO, ['E'] = 14, ['p'] = 25, [155] = KO, [198] = KO, [241] = KO,
	[ 27] = KO, ['F'] = 15, ['q'] = 26, [156] = KO, [199] = KO, [242] = KO,
	[ 28] = KO, ['G'] = 16, ['r'] = 27, [157] = KO, [200] = KO, [243] = KO,
	[ 29] = KO, ['H'] = 17, ['s'] = 28, [158] = KO, [201] = KO, [244] = KO,
	[ 30] = KO, ['I'] = 18, ['t'] = 29, [159] = KO, [202] = KO, [245] = KO,
	[ 31] = KO, ['J'] = 19, ['u'] = 30, [160] = KO, [203] = KO, [246] = KO,
	[ 32] = KO, ['K'] = 20, ['v'] = 31, [161] = KO, [204] = KO, [247] = KO,
	[ 33] = KO, ['L'] = 21, ['w'] = KO, [162] = KO, [205] = KO, [248] = KO,
	[ 34] = KO, ['M'] = 22, ['x'] = KO, [163] = KO, [206] = KO, [249] = KO,
	[ 35] = KO, ['N'] = 23, ['y'] = KO, [164] = KO, [207] = KO, [250] = KO,
	[ 36] = KO, ['O'] = 24, ['z'] = KO, [165] = KO, [208] = KO, [251] = KO,
	[ 37] = KO, ['P'] = 25, [123] = KO, [166] = KO, [209] = KO, [252] = KO,
	[ 38] = KO, ['Q'] = 26, [124] = KO, [167] = KO, [210] = KO, [253] = KO,
	[ 39] = KO, ['R'] = 27, [125] = KO, [168] = KO, [211] = KO, [254] = KO,
	[ 40] = KO, ['S'] = 28, [126] = KO, [169] = KO, [212] = KO, [255] = KO,
	[ 41] = KO, ['T'] = 29, [127] = KO, [170] = KO, [213] = KO,
	[ 42] = KO, ['U'] = 30, [128] = KO, [171] = KO, [214] = KO,
};

int32_t base32hex_decode(const uint8_t  *in,
                         const uint32_t in_len,
                         uint8_t        *out,
                         const uint32_t out_len)
{
	// Checking inputs.
	if (in == NULL || out == NULL) {
		return -1;
	}
	if (in_len > INT32_MAX || out_len < ((in_len + 7) / 8) * 5) {
		return -1;
	}
	if ((in_len % 8) != 0) {
		return -1;
	}

	const uint8_t	*stop = in + in_len;
	uint8_t		*bin = out;
	uint8_t		pad_len = 0;
	uint8_t		c1, c2, c3, c4, c5, c6, c7, c8;

	// Decoding loop takes 8 characters and creates 5 bytes.
	while (in < stop) {
		// Filling and transforming 8 Base32hex chars.
		c1 = base32hex_dec[in[0]];
		c2 = base32hex_dec[in[1]];
		c3 = base32hex_dec[in[2]];
		c4 = base32hex_dec[in[3]];
		c5 = base32hex_dec[in[4]];
		c6 = base32hex_dec[in[5]];
		c7 = base32hex_dec[in[6]];
		c8 = base32hex_dec[in[7]];

		// Check 8. char if is bad or padding.
		if (c8 >= PD) {
			if (c8 == PD && pad_len == 0) {
				pad_len = 1;
			} else {
				return -1;
			}
		}

		// Check 7. char if is bad or padding (if so, 6. must be too).
		if (c7 >= PD) {
			if (c7 == PD && c6 == PD && pad_len == 1) {
				pad_len = 3;
			} else {
				return -1;
			}
		}

		// Check 6. char if is bad or padding.
		if (c6 >= PD) {
			if (!(c6 == PD && pad_len == 3)) {
				return -1;
			}
		}

		// Check 5. char if is bad or padding.
		if (c5 >= PD) {
			if (c5 == PD && pad_len == 3) {
				pad_len = 4;
			} else {
				return -1;
			}
		}

		// Check 4. char if is bad or padding (if so, 3. must be too).
		if (c4 >= PD) {
			if (c4 == PD && c3 == PD && pad_len == 4) {
				pad_len = 6;
			} else {
				return -1;
			}
		}

		// Check 3. char if is bad or padding.
		if (c3 >= PD) {
			if (!(c3 == PD && pad_len == 6)) {
				return -1;
			}
		}

		// 1. and 2. chars must not be padding.
		if (c2 >= PD || c1 >= PD) {
			return -1;
		}

		// Computing of output data based on padding length.
		switch (pad_len) {
		case 0:
			bin[4] = (c7 << 5) + c8;
		case 1:
			bin[3] = (c5 << 7) + (c6 << 2) + (c7 >> 3);
		case 3:
			bin[2] = (c4 << 4) + (c5 >> 1);
		case 4:
			bin[1] = (c2 << 6) + (c3 << 1) + (c4 >> 4);
		case 6:
			bin[0] = (c1 << 3) + (c2 >> 2);
		}

		// Update output end.
		switch (pad_len) {
		case 0:
			bin += 5;
			break;
		case 1:
			bin += 4;
			break;
		case 3:
			bin += 3;
			break;
		case 4:
			bin += 2;
			break;
		case 6:
			bin += 1;
			break;
		}

		in += 8;
	}

	return (bin - out);
}
Vladimír Čunát's avatar
Vladimír Čunát committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

int32_t base32hex_encode(const uint8_t  *in,
                         const uint32_t in_len,
                         uint8_t        *out,
                         const uint32_t out_len)
{
	// Checking inputs.
	if (in == NULL || out == NULL) {
		return -1;
	}
	if (in_len > MAX_BIN_DATA_LEN || out_len < ((in_len + 4) / 5) * 8) {
		return -1;
	}

	uint8_t		rest_len = in_len % 5;
	const uint8_t	*stop = in + in_len - rest_len;
	uint8_t		*text = out;

	// Encoding loop takes 5 bytes and creates 8 characters.
	while (in < stop) {
		text[0] = base32hex_enc[in[0] >> 3];
		text[1] = base32hex_enc[(in[0] & 0x07) << 2 | in[1] >> 6];
		text[2] = base32hex_enc[(in[1] & 0x3E) >> 1];
		text[3] = base32hex_enc[(in[1] & 0x01) << 4 | in[2] >> 4];
		text[4] = base32hex_enc[(in[2] & 0x0F) << 1 | in[3] >> 7];
		text[5] = base32hex_enc[(in[3] & 0x7C) >> 2];
		text[6] = base32hex_enc[(in[3] & 0x03) << 3 | in[4] >> 5];
		text[7] = base32hex_enc[in[4] & 0x1F];
		text += 8;
		in += 5;
	}

	// Processing of padding, if any.
	switch (rest_len) {
	case 4:
		text[0] = base32hex_enc[in[0] >> 3];
		text[1] = base32hex_enc[(in[0] & 0x07) << 2 | in[1] >> 6];
		text[2] = base32hex_enc[(in[1] & 0x3E) >> 1];
		text[3] = base32hex_enc[(in[1] & 0x01) << 4 | in[2] >> 4];
		text[4] = base32hex_enc[(in[2] & 0x0F) << 1 | in[3] >> 7];
		text[5] = base32hex_enc[(in[3] & 0x7C) >> 2];
		text[6] = base32hex_enc[(in[3] & 0x03) << 3];
		text[7] = base32hex_pad;
		text += 8;
		break;
	case 3:
		text[0] = base32hex_enc[in[0] >> 3];
		text[1] = base32hex_enc[(in[0] & 0x07) << 2 | in[1] >> 6];
		text[2] = base32hex_enc[(in[1] & 0x3E) >> 1];
		text[3] = base32hex_enc[(in[1] & 0x01) << 4 | in[2] >> 4];
		text[4] = base32hex_enc[(in[2] & 0x0F) << 1];
		text[5] = base32hex_pad;
		text[6] = base32hex_pad;
		text[7] = base32hex_pad;
		text += 8;
		break;
	case 2:
		text[0] = base32hex_enc[in[0] >> 3];
		text[1] = base32hex_enc[(in[0] & 0x07) << 2 | in[1] >> 6];
		text[2] = base32hex_enc[(in[1] & 0x3E) >> 1];
		text[3] = base32hex_enc[(in[1] & 0x01) << 4];
		text[4] = base32hex_pad;
		text[5] = base32hex_pad;
		text[6] = base32hex_pad;
		text[7] = base32hex_pad;
		text += 8;
		break;
	case 1:
		text[0] = base32hex_enc[in[0] >> 3];
		text[1] = base32hex_enc[(in[0] & 0x07) << 2];
		text[2] = base32hex_pad;
		text[3] = base32hex_pad;
		text[4] = base32hex_pad;
		text[5] = base32hex_pad;
		text[6] = base32hex_pad;
		text[7] = base32hex_pad;
		text += 8;
		break;
	}

	return (text - out);
}