5 Copyright (C) 2002 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
44 base16_decode_init(struct base16_decode_ctx *ctx)
46 ctx->word = ctx->bits = 0;
49 enum { HEX_INVALID = -1, HEX_SPACE=-2 };
51 static const signed char
52 hex_decode_table[0x80] =
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
57 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
58 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
67 base16_decode_single(struct base16_decode_ctx *ctx,
76 digit = hex_decode_table[src];
89 *dst = (ctx->word << 4) | digit;
103 base16_decode_update(struct base16_decode_ctx *ctx,
112 for (i = done = 0; i<src_length; i++)
113 switch(base16_decode_single(ctx, dst + done, src[i]))
126 assert(done <= BASE16_DECODE_LENGTH(src_length));
133 base16_decode_final(struct base16_decode_ctx *ctx)
135 return ctx->bits == 0;