3 * Hex encoding and decoding, following spki conventions (i.e.
4 * allowing whitespace between digits).
7 /* nettle, low-level cryptographics library
9 * Copyright (C) 2002 Niels Möller
11 * The nettle library is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or (at your
14 * option) any later version.
16 * The nettle library is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 * License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with the nettle library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 #ifndef NETTLE_BASE16_H_INCLUDED
28 #define NETTLE_BASE16_H_INCLUDED
30 #include "nettle-types.h"
37 #define base16_encode_single nettle_base16_encode_single
38 #define base16_encode_update nettle_base16_encode_update
39 #define base16_decode_init nettle_base16_decode_init
40 #define base16_decode_single nettle_base16_decode_single
41 #define base16_decode_update nettle_base16_decode_update
42 #define base16_decode_final nettle_base16_decode_final
46 /* Maximum length of output for base16_encode_update. */
47 #define BASE16_ENCODE_LENGTH(length) ((length) * 2)
49 /* Encodes a single byte. Always stores two digits in dst[0] and dst[1]. */
51 base16_encode_single(uint8_t *dst
,
54 /* Always stores BASE16_ENCODE_LENGTH(length) digits in dst. */
56 base16_encode_update(uint8_t *dst
,
63 /* Maximum length of output for base16_decode_update. */
64 /* We have at most 4 buffered bits, and a total of (length + 1) * 4 bits. */
65 #define BASE16_DECODE_LENGTH(length) (((length) + 1) / 2)
67 struct base16_decode_ctx
69 unsigned word
; /* Leftover bits */
70 unsigned bits
; /* Number buffered bits */
74 base16_decode_init(struct base16_decode_ctx
*ctx
);
76 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
79 base16_decode_single(struct base16_decode_ctx
*ctx
,
83 /* Returns 1 on success, 0 on error. DST should point to an area of
84 * size at least BASE16_DECODE_LENGTH(length), and for sanity
85 * checking, *DST_LENGTH should be initialized to the size of that
86 * area before the call. *DST_LENGTH is updated to the amount of
89 /* Currently results in an assertion failure if *DST_LENGTH is
90 * too small. FIXME: Return some error instead? */
92 base16_decode_update(struct base16_decode_ctx
*ctx
,
98 /* Returns 1 on success. */
100 base16_decode_final(struct base16_decode_ctx
*ctx
);
106 #endif /* NETTLE_BASE16_H_INCLUDED */