Delete nettle-stdint.h
[gd/nettle] / base16.h
1 /* base16.h
2    
3    Hex encoding and decoding, following spki conventions (i.e.
4    allowing whitespace between digits).
5
6    Copyright (C) 2002 Niels Möller
7
8    This file is part of GNU Nettle.
9
10    GNU Nettle is free software: you can redistribute it and/or
11    modify it under the terms of either:
12
13      * the GNU Lesser General Public License as published by the Free
14        Software Foundation; either version 3 of the License, or (at your
15        option) any later version.
16
17    or
18
19      * the GNU General Public License as published by the Free
20        Software Foundation; either version 2 of the License, or (at your
21        option) any later version.
22
23    or both in parallel, as here.
24
25    GNU Nettle is distributed in the hope that it will be useful,
26    but WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28    General Public License for more details.
29
30    You should have received copies of the GNU General Public License and
31    the GNU Lesser General Public License along with this program.  If
32    not, see http://www.gnu.org/licenses/.
33 */
34  
35 #ifndef NETTLE_BASE16_H_INCLUDED
36 #define NETTLE_BASE16_H_INCLUDED
37
38 #include "nettle-types.h"
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 /* Name mangling */
45 #define base16_encode_single nettle_base16_encode_single
46 #define base16_encode_update nettle_base16_encode_update
47 #define base16_decode_init nettle_base16_decode_init
48 #define base16_decode_single nettle_base16_decode_single
49 #define base16_decode_update nettle_base16_decode_update
50 #define base16_decode_final nettle_base16_decode_final
51
52 /* Base16 encoding */
53
54 /* Maximum length of output for base16_encode_update. */
55 #define BASE16_ENCODE_LENGTH(length) ((length) * 2)
56
57 /* Encodes a single byte. Always stores two digits in dst[0] and dst[1]. */
58 void
59 base16_encode_single(char *dst,
60                      uint8_t src);
61
62 /* Always stores BASE16_ENCODE_LENGTH(length) digits in dst. */
63 void
64 base16_encode_update(char *dst,
65                      size_t length,
66                      const uint8_t *src);
67
68
69 /* Base16 decoding */
70
71 /* Maximum length of output for base16_decode_update. */
72 /* We have at most 4 buffered bits, and a total of (length + 1) * 4 bits. */
73 #define BASE16_DECODE_LENGTH(length) (((length) + 1) / 2)
74
75 struct base16_decode_ctx
76 {
77   unsigned char word; /* Leftover bits */
78   unsigned char bits; /* Number buffered bits */
79 };
80
81 void
82 base16_decode_init(struct base16_decode_ctx *ctx);
83
84 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
85  * errors. */
86 int
87 base16_decode_single(struct base16_decode_ctx *ctx,
88                      uint8_t *dst,
89                      char src);
90
91 /* Returns 1 on success, 0 on error. DST should point to an area of
92  * size at least BASE16_DECODE_LENGTH(length). The amount of data
93  * generated is returned in *DST_LENGTH. */
94
95 int
96 base16_decode_update(struct base16_decode_ctx *ctx,
97                      size_t *dst_length,
98                      uint8_t *dst,
99                      size_t src_length,
100                      const char *src);
101
102 /* Returns 1 on success. */
103 int
104 base16_decode_final(struct base16_decode_ctx *ctx);
105
106 #ifdef __cplusplus
107 }
108 #endif
109
110 #endif /* NETTLE_BASE16_H_INCLUDED */