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
, uint8_t src
);
53 /* Always stores BASE16_ENCODE_LENGTH(length) digits in dst. */
55 base16_encode_update(uint8_t * dst
,
56 unsigned length
, const uint8_t * src
);
61 /* Maximum length of output for base16_decode_update. */
62 /* We have at most 4 buffered bits, and a total of (length + 1) * 4 bits. */
63 #define BASE16_DECODE_LENGTH(length) (((length) + 1) / 2)
65 struct base16_decode_ctx
{
66 unsigned word
; /* Leftover bits */
67 unsigned bits
; /* Number buffered bits */
71 base16_decode_init(struct base16_decode_ctx
*ctx
);
73 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
76 base16_decode_single(struct base16_decode_ctx
*ctx
,
77 uint8_t * dst
, uint8_t src
);
79 /* Returns 1 on success, 0 on error. DST should point to an area of
80 * size at least BASE16_DECODE_LENGTH(length), and for sanity
81 * checking, *DST_LENGTH should be initialized to the size of that
82 * area before the call. *DST_LENGTH is updated to the amount of
85 /* Currently results in an assertion failure if *DST_LENGTH is
86 * too small. FIXME: Return some error instead? */
88 base16_decode_update(struct base16_decode_ctx
*ctx
,
91 unsigned src_length
, const uint8_t * src
);
93 /* Returns 1 on success. */
95 base16_decode_final(struct base16_decode_ctx
*ctx
);
100 #endif /* NETTLE_BASE16_H_INCLUDED */