forked from jb55/nostril
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
base64.h
253 lines (224 loc) · 8.54 KB
/
base64.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* Licensed under BSD-MIT - see LICENSE file for details */
#ifndef CCAN_BASE64_H
#define CCAN_BASE64_H
#include <stddef.h>
#include <stdbool.h>
#include <sys/types.h>
#if defined(_MSC_VER)
#ifndef _SSIZE_T_DEFINED
#ifdef _WIN64
typedef unsigned __int64 ssize_t;
#else
typedef _W64 unsigned int ssize_t;
#endif
#define _SSIZE_T_DEFINED
#endif
#endif
/**
* base64_maps_t - structure to hold maps for encode/decode
*/
typedef struct {
char encode_map[64];
signed char decode_map[256];
} base64_maps_t;
/**
* base64_encoded_length - Calculate encode buffer length
* @param srclen the size of the data to be encoded
* @note add 1 to this to get null-termination
* @return Buffer length required for encode
*/
size_t base64_encoded_length(size_t srclen);
/**
* base64_decoded_length - Calculate decode buffer length
* @param srclen Length of the data to be decoded
* @note This does not return the size of the decoded data! see base64_decode
* @return Minimum buffer length for safe decode
*/
size_t base64_decoded_length(size_t srclen);
/**
* base64_init_maps - populate a base64_maps_t based on a supplied alphabet
* @param dest A base64 maps object
* @param src Alphabet to populate the maps from (e.g. base64_alphabet_rfc4648)
*/
void base64_init_maps(base64_maps_t *dest, const char src[64]);
/**
* base64_encode_triplet_using_maps - encode 3 bytes into base64 using a specific alphabet
* @param maps Maps to use for encoding (see base64_init_maps)
* @param dest Buffer containing 3 bytes
* @param src Buffer containing 4 characters
*/
void base64_encode_triplet_using_maps(const base64_maps_t *maps,
char dest[4], const char src[3]);
/**
* base64_encode_tail_using_maps - encode the final bytes of a source using a specific alphabet
* @param maps Maps to use for encoding (see base64_init_maps)
* @param dest Buffer containing 4 bytes
* @param src Buffer containing srclen bytes
* @param srclen Number of bytes (<= 3) to encode in src
*/
void base64_encode_tail_using_maps(const base64_maps_t *maps, char dest[4],
const char *src, size_t srclen);
/**
* base64_encode_using_maps - encode a buffer into base64 using a specific alphabet
* @param maps Maps to use for encoding (see base64_init_maps)
* @param dest Buffer to encode into
* @param destlen Length of dest
* @param src Buffer to encode
* @param srclen Length of the data to encode
* @return Number of encoded bytes set in dest. -1 on error (and errno set)
* @note dest will be nul-padded to destlen (past any required padding)
* @note sets errno = EOVERFLOW if destlen is too small
*/
ssize_t base64_encode_using_maps(const base64_maps_t *maps,
char *dest, size_t destlen,
const char *src, size_t srclen);
/*
* base64_char_in_alphabet - returns true if character can be part of an encoded string
* @param maps A base64 maps object (see base64_init_maps)
* @param b64char Character to check
*/
bool base64_char_in_alphabet(const base64_maps_t *maps, char b64char);
/**
* base64_decode_using_maps - decode a base64-encoded string using a specific alphabet
* @param maps A base64 maps object (see base64_init_maps)
* @param dest Buffer to decode into
* @param destlen length of dest
* @param src the buffer to decode
* @param srclen the length of the data to decode
* @return Number of decoded bytes set in dest. -1 on error (and errno set)
* @note dest will be nul-padded to destlen
* @note sets errno = EOVERFLOW if destlen is too small
* @note sets errno = EDOM if src contains invalid characters
*/
ssize_t base64_decode_using_maps(const base64_maps_t *maps,
char *dest, size_t destlen,
const char *src, size_t srclen);
/**
* base64_decode_quartet_using_maps - decode 4 bytes from base64 using a specific alphabet
* @param maps A base64 maps object (see base64_init_maps)
* @param dest Buffer containing 3 bytes
* @param src Buffer containing 4 bytes
* @return Number of decoded bytes set in dest. -1 on error (and errno set)
* @note sets errno = EDOM if src contains invalid characters
*/
ssize_t base64_decode_quartet_using_maps(const base64_maps_t *maps,
char dest[3], const char src[4]);
/**
* base64_decode_tail_using_maps - decode the final bytes of a base64 string using a specific alphabet
* @param maps A base64 maps object (see base64_init_maps)
* @param dest Buffer containing 3 bytes
* @param src Buffer containing 4 bytes - padded with '=' as required
* @param srclen Number of bytes to decode in src
* @return Number of decoded bytes set in dest. -1 on error (and errno set)
* @note sets errno = EDOM if src contains invalid characters
* @note sets errno = EINVAL if src is an invalid base64 tail
*/
ssize_t base64_decode_tail_using_maps(const base64_maps_t *maps, char *dest,
const char *src, size_t srclen);
/* the rfc4648 functions: */
extern const base64_maps_t base64_maps_rfc4648;
/**
* base64_encode - Encode a buffer into base64 according to rfc4648
* @param dest Buffer to encode into
* @param destlen Length of the destination buffer
* @param src Buffer to encode
* @param srclen Length of the data to encode
* @return Number of encoded bytes set in dest. -1 on error (and errno set)
* @note dest will be nul-padded to destlen (past any required padding)
* @note sets errno = EOVERFLOW if destlen is too small
*
* This function encodes src according to http://tools.ietf.org/html/rfc4648
*
* Example:
* size_t encoded_length;
* char dest[100];
* const char *src = "This string gets encoded";
* encoded_length = base64_encode(dest, sizeof(dest), src, strlen(src));
* printf("Returned data of length %zd @%p\n", encoded_length, &dest);
*/
static inline
ssize_t base64_encode(char *dest, size_t destlen,
const char *src, size_t srclen)
{
return base64_encode_using_maps(&base64_maps_rfc4648,
dest, destlen, src, srclen);
}
/**
* base64_encode_triplet - encode 3 bytes into base64 according to rfc4648
* @param dest Buffer containing 4 bytes
* @param src Buffer containing 3 bytes
*/
static inline
void base64_encode_triplet(char dest[4], const char src[3])
{
base64_encode_triplet_using_maps(&base64_maps_rfc4648, dest, src);
}
/**
* base64_encode_tail - encode the final bytes of a source according to rfc4648
* @param dest Buffer containing 4 bytes
* @param src Buffer containing srclen bytes
* @param srclen Number of bytes (<= 3) to encode in src
*/
static inline
void base64_encode_tail(char dest[4], const char *src, size_t srclen)
{
base64_encode_tail_using_maps(&base64_maps_rfc4648, dest, src, srclen);
}
/**
* base64_decode - decode An rfc4648 base64-encoded string
* @param dest Buffer to decode into
* @param destlen Length of the destination buffer
* @param src Buffer to decode
* @param srclen Length of the data to decode
* @return Number of decoded bytes set in dest. -1 on error (and errno set)
* @note dest will be nul-padded to destlen
* @note sets errno = EOVERFLOW if destlen is too small
* @note sets errno = EDOM if src contains invalid characters
*
* This function decodes the buffer according to
* http://tools.ietf.org/html/rfc4648
*
* Example:
* size_t decoded_length;
* char ret[100];
* const char *src = "Zm9vYmFyYmF6";
* decoded_length = base64_decode(ret, sizeof(ret), src, strlen(src));
* printf("Returned data of length %zd @%p\n", decoded_length, &ret);
*/
static inline
ssize_t base64_decode(char *dest, size_t destlen,
const char *src, size_t srclen)
{
return base64_decode_using_maps(&base64_maps_rfc4648,
dest, destlen, src, srclen);
}
/**
* base64_decode_quartet - decode the first 4 characters in src into dest
* @param dest Buffer containing 3 bytes
* @param src Buffer containing 4 characters
* @return Number of decoded bytes set in dest. -1 on error (and errno set)
* @note sets errno = EDOM if src contains invalid characters
*/
static inline
ssize_t base64_decode_quartet(char dest[3], const char src[4])
{
return base64_decode_quartet_using_maps(&base64_maps_rfc4648,
dest, src);
}
/**
* @brief decode the final bytes of a base64 string from src into dest
* @param dest Buffer containing 3 bytes
* @param src Buffer containing 4 bytes - padded with '=' as required
* @param srclen Number of bytes to decode in src
* @return Number of decoded bytes set in dest. -1 on error (and errno set)
* @note sets errno = EDOM if src contains invalid characters
* @note sets errno = EINVAL if src is an invalid base64 tail
*/
static inline
ssize_t base64_decode_tail(char dest[3], const char *src, size_t srclen)
{
return base64_decode_tail_using_maps(&base64_maps_rfc4648,
dest, src, srclen);
}
/* end rfc4648 functions */
#endif /* CCAN_BASE64_H */