Imported Upstream version 2.4
[platform/upstream/nettle.git] / base16.h
1 /* base16.h
2  *
3  * Hex encoding and decoding, following spki conventions (i.e.
4  * allowing whitespace between digits).
5  */
6
7 /* nettle, low-level cryptographics library
8  *
9  * Copyright (C) 2002 Niels Möller
10  *  
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.
15  * 
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.
20  * 
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., 59 Temple Place - Suite 330, Boston,
24  * MA 02111-1307, USA.
25  */
26  
27 #ifndef NETTLE_BASE16_H_INCLUDED
28 #define NETTLE_BASE16_H_INCLUDED
29
30 #include "nettle-types.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /* Name mangling */
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
43
44 /* Base16 encoding */
45
46 /* Maximum length of output for base16_encode_update. */
47 #define BASE16_ENCODE_LENGTH(length) ((length) * 2)
48
49 /* Encodes a single byte. Always stores two digits in dst[0] and dst[1]. */
50 void
51 base16_encode_single(uint8_t *dst,
52                      uint8_t src);
53
54 /* Always stores BASE16_ENCODE_LENGTH(length) digits in dst. */
55 void
56 base16_encode_update(uint8_t *dst,
57                      unsigned length,
58                      const uint8_t *src);
59
60
61 /* Base16 decoding */
62
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)
66
67 struct base16_decode_ctx
68 {
69   unsigned word;   /* Leftover bits */
70   unsigned bits;   /* Number buffered bits */
71 };
72
73 void
74 base16_decode_init(struct base16_decode_ctx *ctx);
75
76 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
77  * errors. */
78 int
79 base16_decode_single(struct base16_decode_ctx *ctx,
80                      uint8_t *dst,
81                      uint8_t src);
82
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
87  * decoded output. */
88
89 /* Currently results in an assertion failure if *DST_LENGTH is
90  * too small. FIXME: Return some error instead? */
91 int
92 base16_decode_update(struct base16_decode_ctx *ctx,
93                      unsigned *dst_length,
94                      uint8_t *dst,
95                      unsigned src_length,
96                      const uint8_t *src);
97
98 /* Returns 1 on success. */
99 int
100 base16_decode_final(struct base16_decode_ctx *ctx);
101
102 #ifdef __cplusplus
103 }
104 #endif
105
106 #endif /* NETTLE_BASE16_H_INCLUDED */