Dibbler - a portable DHCPv6  1.0.2RC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nettle/base64.h
Go to the documentation of this file.
1 /* base64.h
2  *
3  * "ASCII armor" codecs.
4  */
5 
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2002 Niels Möller, Dan Egnor
9  *
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.
14  *
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.
19  *
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., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25 
26 #ifndef NETTLE_BASE64_H_INCLUDED
27 #define NETTLE_BASE64_H_INCLUDED
28 
29 #include "nettle-types.h"
30 
31 /* Name mangling */
32 #define base64_encode_init nettle_base64_encode_init
33 #define base64_encode_single nettle_base64_encode_single
34 #define base64_encode_update nettle_base64_encode_update
35 #define base64_encode_final nettle_base64_encode_final
36 #define base64_encode_raw nettle_base64_encode_raw
37 #define base64_encode_group nettle_base64_encode_group
38 #define base64_decode_init nettle_base64_decode_init
39 #define base64_decode_single nettle_base64_decode_single
40 #define base64_decode_update nettle_base64_decode_update
41 #define base64_decode_final nettle_base64_decode_final
42 
43 #define BASE64_BINARY_BLOCK_SIZE 3
44 #define BASE64_TEXT_BLOCK_SIZE 4
45 
46 /* Base64 encoding */
47 
48 /* Maximum length of output for base64_encode_update. NOTE: Doesn't
49  * include any padding that base64_encode_final may add. */
50 /* We have at most 4 buffered bits, and a total of (4 + length * 8) bits. */
51 #define BASE64_ENCODE_LENGTH(length) (((length) * 8 + 4)/6)
52 
53 /* Maximum lengbth of output generated by base64_encode_final. */
54 #define BASE64_ENCODE_FINAL_LENGTH 3
55 
56 /* Exact length of output generated by base64_encode_raw, including
57  * padding. */
58 #define BASE64_ENCODE_RAW_LENGTH(length) ((((length) + 2)/3)*4)
59 
61 {
62  unsigned word; /* Leftover bits */
63  unsigned bits; /* Number of bits, always 0, 2, or 4. */
64 };
65 
66 void
68 
69 /* Encodes a single byte. Returns amount of output (always 1 or 2). */
70 unsigned
72  uint8_t *dst,
73  uint8_t src);
74 
75 /* Returns the number of output characters. DST should point to an
76  * area of size at least BASE64_ENCODE_LENGTH(length). */
77 unsigned
79  uint8_t *dst,
80  unsigned length,
81  const uint8_t *src);
82 
83 /* DST should point to an area of size at least
84  * BASE64_ENCODE_FINAL_LENGTH */
85 unsigned
87  uint8_t *dst);
88 
89 /* Lower level functions */
90 
91 /* Encodes a string in one go, including any padding at the end.
92  * Generates exactly BASE64_ENCODE_RAW_LENGTH(length) bytes of output.
93  * Supports overlapped operation, if src <= dst. */
94 void
95 base64_encode_raw(uint8_t *dst, unsigned length, const uint8_t *src);
96 
97 void
98 base64_encode_group(uint8_t *dst, uint32_t group);
99 
100 
101 /* Base64 decoding */
102 
103 /* Maximum length of output for base64_decode_update. */
104 /* We have at most 6 buffered bits, and a total of (length + 1) * 6 bits. */
105 #define BASE64_DECODE_LENGTH(length) ((((length) + 1) * 6) / 8)
106 
108 {
109  unsigned word; /* Leftover bits */
110  unsigned bits; /* Number buffered bits */
111 
112  /* Number of padding characters encountered */
113  unsigned padding;
114 };
115 
116 void
118 
119 /* Decodes a single byte. Returns amount of output (0 or 1), or -1 on
120  * errors. */
121 int
123  uint8_t *dst,
124  uint8_t src);
125 
126 /* Returns 1 on success, 0 on error. DST should point to an area of
127  * size at least BASE64_DECODE_LENGTH(length), and for sanity
128  * checking, *DST_LENGTH should be initialized to the size of that
129  * area before the call. *DST_LENGTH is updated to the amount of
130  * decoded output. */
131 
132 /* FIXME: Currently results in an assertion failure if *DST_LENGTH is
133  * too small. Return some error instead? */
134 int
136  unsigned *dst_length,
137  uint8_t *dst,
138  unsigned src_length,
139  const uint8_t *src);
140 
141 /* Returns 1 on success. */
142 int
144 
145 #endif /* NETTLE_BASE64_H_INCLUDED */
146