remove excess calls to joblist_append()
[lighttpd.git] / src / base64.c
blob35e6a70deb42e42f803829c60abb298ffb32f05e
1 #include "first.h"
3 #include "base64.h"
5 /* reverse mapping:
6 * >= 0: base64 value
7 * -1: invalid character
8 * -2: skip character (whitespace/control)
9 * -3: padding
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 */
44 size_t i;
45 const short* base64_reverse_table;
47 switch (charset) {
48 case BASE64_STANDARD:
49 base64_reverse_table = base64_standard_reverse_table;
50 break;
51 case BASE64_URL:
52 base64_reverse_table = base64_url_reverse_table;
53 break;
54 default:
55 return NULL;
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];
63 short ch;
65 if (c == '\0') break;
66 if (c >= 128) return NULL; /* only 7-bit characters allowed */
68 ch = base64_reverse_table[c];
69 if (-3 == ch) {
70 /* pad character; can only come after 2 base64 digits in a group */
71 if (group < 2) return NULL;
72 break;
73 } else if (-2 == ch) {
74 continue; /* skip character */
75 } else if (ch < 0) {
76 return NULL; /* invalid character, abort */
79 switch(group) {
80 case 0:
81 result[out_pos] = ch << 2;
82 group = 1;
83 break;
84 case 1:
85 result[out_pos++] |= ch >> 4;
86 result[out_pos] = (ch & 0x0f) << 4;
87 group = 2;
88 break;
89 case 2:
90 result[out_pos++] |= ch >>2;
91 result[out_pos] = (ch & 0x03) << 6;
92 group = 3;
93 break;
94 case 3:
95 result[out_pos++] |= ch;
96 group = 0;
97 break;
101 switch(group) {
102 case 0:
103 /* ended on boundary */
104 break;
105 case 1:
106 /* need at least 2 base64 digits per group */
107 return NULL;
108 case 2:
109 /* have 2 base64 digits in last group => one real octect, two zeroes padded */
110 case 3:
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;
116 break;
119 buffer_commit(out, out_pos);
121 return result;
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;
130 size_t i;
131 size_t out_pos = 0;
133 switch (charset) {
134 case BASE64_STANDARD:
135 base64_table = base64_standard_table;
136 break;
137 case BASE64_URL:
138 base64_table = base64_url_table;
139 break;
140 default:
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];
156 out_pos += 4;
158 switch (in_tuple_remainder) {
159 case 0:
160 break;
161 case 1:
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];
167 out_pos += 2;
169 break;
170 case 2:
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];
177 out_pos += 3;
179 break;
181 force_assert(out_pos <= out_length);
182 return out_pos;
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;
189 char padding;
190 size_t out_pos;
192 switch (charset) {
193 case BASE64_STANDARD:
194 padding = base64_standard_table[64];
195 break;
196 case BASE64_URL:
197 padding = base64_url_table[64];
198 break;
199 default:
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;
208 padding_length--;
211 force_assert(out_pos <= out_length);
213 return out_pos;
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);
224 return result;
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);
234 return result;