7 * -1: invalid character
8 * -2: skip character (whitespace/control)
12 /* BASE64_STANDARD: "A-Z a-z 0-9 + /" maps to 0-63, pad with "=" */
13 static const char base64_standard_table
[66] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
14 static const short base64_standard_reverse_table
[128] = {
15 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
16 -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x00 - 0x0F */
17 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x10 - 0x1F */
18 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 - 0x2F */
19 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1, /* 0x30 - 0x3F */
20 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 - 0x4F */
21 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 - 0x5F */
22 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 - 0x6F */
23 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70 - 0x7F */
26 /* BASE64_URL: "A-Z a-z 0-9 - _" maps to 0-63, pad with "." */
27 static const char base64_url_table
[66] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
28 static const short base64_url_reverse_table
[128] = {
29 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
30 -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x00 - 0x0F */
31 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x10 - 0x1F */
32 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -3, -1, /* 0x20 - 0x2F */
33 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 - 0x3F */
34 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 - 0x4F */
35 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, /* 0x50 - 0x5F */
36 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 - 0x6F */
37 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70 - 0x7F */
40 unsigned char* buffer_append_base64_decode(buffer
*out
, const char* in
, size_t in_length
, base64_charset charset
) {
41 unsigned char *result
;
42 size_t out_pos
= 0; /* current output character (position) that is decoded. can contain partial result */
43 unsigned int group
= 0; /* how many base64 digits in the current group were decoded already. each group has up to 4 digits */
45 const short* base64_reverse_table
;
49 base64_reverse_table
= base64_standard_reverse_table
;
52 base64_reverse_table
= base64_url_reverse_table
;
58 result
= (unsigned char *) buffer_string_prepare_append(out
, 3*(in_length
/ 4) + 3);
60 /* run through the whole string, converting as we go */
61 for (i
= 0; i
< in_length
; i
++) {
62 unsigned char c
= (unsigned char) in
[i
];
66 if (c
>= 128) return NULL
; /* only 7-bit characters allowed */
68 ch
= base64_reverse_table
[c
];
70 /* pad character; can only come after 2 base64 digits in a group */
71 if (group
< 2) return NULL
;
73 } else if (-2 == ch
) {
74 continue; /* skip character */
76 return NULL
; /* invalid character, abort */
81 result
[out_pos
] = ch
<< 2;
85 result
[out_pos
++] |= ch
>> 4;
86 result
[out_pos
] = (ch
& 0x0f) << 4;
90 result
[out_pos
++] |= ch
>>2;
91 result
[out_pos
] = (ch
& 0x03) << 6;
95 result
[out_pos
++] |= ch
;
103 /* ended on boundary */
106 /* need at least 2 base64 digits per group */
109 /* have 2 base64 digits in last group => one real octect, two zeroes padded */
111 /* have 3 base64 digits in last group => two real octects, one zero padded */
113 /* for both cases the current index already is on the first zero padded octet
114 * - check it really is zero (overlapping bits) */
115 if (0 != result
[out_pos
]) return NULL
;
119 buffer_commit(out
, out_pos
);
124 size_t li_to_base64_no_padding(char* out
, size_t out_length
, const unsigned char* in
, size_t in_length
, base64_charset charset
) {
125 const size_t full_tuples
= in_length
/ 3;
126 const size_t in_tuple_remainder
= in_length
% 3;
127 const size_t out_tuple_remainder
= in_tuple_remainder
? 1 + in_tuple_remainder
: 0;
128 const size_t require_space
= 4 * full_tuples
+ out_tuple_remainder
;
129 const char* base64_table
;
134 case BASE64_STANDARD
:
135 base64_table
= base64_standard_table
;
138 base64_table
= base64_url_table
;
141 force_assert(0 && "invalid charset");
144 /* check overflows */
145 force_assert(full_tuples
<= 2*full_tuples
);
146 force_assert(full_tuples
<= 4*full_tuples
);
147 force_assert(4*full_tuples
<= 4*full_tuples
+ out_tuple_remainder
);
148 force_assert(require_space
<= out_length
);
150 for (i
= 2; i
< in_length
; i
+= 3) {
151 unsigned int v
= (in
[i
-2] << 16) | (in
[i
-1] << 8) | in
[i
];
152 out
[out_pos
+3] = base64_table
[v
& 0x3f]; v
>>= 6;
153 out
[out_pos
+2] = base64_table
[v
& 0x3f]; v
>>= 6;
154 out
[out_pos
+1] = base64_table
[v
& 0x3f]; v
>>= 6;
155 out
[out_pos
+0] = base64_table
[v
& 0x3f];
158 switch (in_tuple_remainder
) {
163 /* pretend in[i-1] = in[i] = 0, don't write last two (out_pos+3, out_pos+2) characters */
164 unsigned int v
= (in
[i
-2] << 4);
165 out
[out_pos
+1] = base64_table
[v
& 0x3f]; v
>>= 6;
166 out
[out_pos
+0] = base64_table
[v
& 0x3f];
172 /* pretend in[i] = 0, don't write last (out_pos+3) character */
173 unsigned int v
= (in
[i
-2] << 10) | (in
[i
-1] << 2);
174 out
[out_pos
+2] = base64_table
[v
& 0x3f]; v
>>= 6;
175 out
[out_pos
+1] = base64_table
[v
& 0x3f]; v
>>= 6;
176 out
[out_pos
+0] = base64_table
[v
& 0x3f];
181 force_assert(out_pos
<= out_length
);
185 size_t li_to_base64(char* out
, size_t out_length
, const unsigned char* in
, size_t in_length
, base64_charset charset
) {
186 const size_t in_tuple_remainder
= in_length
% 3;
187 size_t padding_length
= in_tuple_remainder
? 3 - in_tuple_remainder
: 0;
193 case BASE64_STANDARD
:
194 padding
= base64_standard_table
[64];
197 padding
= base64_url_table
[64];
200 force_assert(0 && "invalid charset");
203 force_assert(out_length
>= padding_length
);
204 out_pos
= li_to_base64_no_padding(out
, out_length
- padding_length
, in
, in_length
, charset
);
206 while (padding_length
> 0) {
207 out
[out_pos
++] = padding
;
211 force_assert(out_pos
<= out_length
);
217 char* buffer_append_base64_encode_no_padding(buffer
*out
, const unsigned char* in
, size_t in_length
, base64_charset charset
) {
218 size_t reserve
= 4*(in_length
/3) + 4;
219 char* result
= buffer_string_prepare_append(out
, reserve
);
220 size_t out_pos
= li_to_base64_no_padding(result
, reserve
, in
, in_length
, charset
);
222 buffer_commit(out
, out_pos
);
227 char* buffer_append_base64_encode(buffer
*out
, const unsigned char* in
, size_t in_length
, base64_charset charset
) {
228 size_t reserve
= 4*(in_length
/3) + 4;
229 char* result
= buffer_string_prepare_append(out
, reserve
);
230 size_t out_pos
= li_to_base64(result
, reserve
, in
, in_length
, charset
);
232 buffer_commit(out
, out_pos
);