6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
36 base16_decode_init(struct base16_decode_ctx
*ctx
)
38 ctx
->word
= ctx
->bits
= 0;
41 enum { HEX_INVALID
= -1, HEX_SPACE
=-2 };
43 static const signed char
44 hex_decode_table
[0x80] =
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
50 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
59 base16_decode_single(struct base16_decode_ctx
*ctx
,
68 digit
= hex_decode_table
[src
];
81 *dst
= (ctx
->word
<< 4) | digit
;
95 base16_decode_update(struct base16_decode_ctx
*ctx
,
104 assert(*dst_length
>= BASE16_DECODE_LENGTH(src_length
));
106 for (i
= 0, done
= 0; i
<src_length
; i
++)
107 switch(base16_decode_single(ctx
, dst
+ done
, src
[i
]))
120 assert(done
<= BASE16_DECODE_LENGTH(src_length
));
127 base16_decode_final(struct base16_decode_ctx
*ctx
)
129 return ctx
->bits
== 0;