File: base64.c

package info (click to toggle)
tarantool 1.5.2.20.g5f5d924-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 26,568 kB
  • ctags: 18,697
  • sloc: ansic: 109,092; sh: 21,312; cpp: 20,633; xml: 9,666; asm: 2,488; python: 2,195; java: 1,759; perl: 1,002; makefile: 679
file content (296 lines) | stat: -rw-r--r-- 7,645 bytes parent folder | download | duplicates (2)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistributions of source code must retain the above
 *    copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include "third_party/base64.h"
/*
 * This is part of the libb64 project, and has been placed in the
 * public domain. For details, see
 * http://sourceforge.net/projects/libb64
 */


/* {{{ encode */

enum base64_encodestep { step_A, step_B, step_C };

struct base64_encodestate {
	enum base64_encodestep step;
	char result;
	int stepcount;
};

static inline void
base64_encodestate_init(struct base64_encodestate *state)
{
	state->step = step_A;
	state->result = 0;
	state->stepcount = 0;
}

static inline char
base64_encode_value(char value)
{
	static const char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	unsigned codepos = (unsigned) value;
	if (codepos > sizeof(encoding) - 1)
		return '=';
	return encoding[codepos];
}

static int
base64_encode_block(const char *in_bin, int in_len,
		    char *out_base64, int out_len,
		    struct base64_encodestate *state)
{
	const char *const in_end = in_bin + in_len;
	const char *in_pos = in_bin;
	char *out_pos = out_base64;
	char *out_end = out_base64  + out_len;
	char result;
	char fragment;

	result = state->result;

	switch (state->step)
	{
		while (1)
		{
	case step_A:
			if (in_pos == in_end || out_pos >= out_end) {
				state->result = result;
				state->step = step_A;
				return out_pos - out_base64;
			}
			fragment = *in_pos++;
			result = (fragment & 0x0fc) >> 2;
			*out_pos++ = base64_encode_value(result);
			result = (fragment & 0x003) << 4;
	case step_B:
			if (in_pos == in_end || out_pos >= out_end) {
				state->result = result;
				state->step = step_B;
				return out_pos - out_base64;
			}
			fragment = *in_pos++;
			result |= (fragment & 0x0f0) >> 4;
			*out_pos++ = base64_encode_value(result);
			result = (fragment & 0x00f) << 2;
	case step_C:
			if (in_pos == in_end || out_pos + 2 >= out_end) {
				state->result = result;
				state->step = step_C;
				return out_pos - out_base64;
			}
			fragment = *in_pos++;
			result |= (fragment & 0x0c0) >> 6;
			*out_pos++ = base64_encode_value(result);
			result  = (fragment & 0x03f) >> 0;
			*out_pos++ = base64_encode_value(result);

			/*
			 * Each full step (A->B->C) yields
			 * 4 characters.
			 */
			if (++state->stepcount * 4 == BASE64_CHARS_PER_LINE) {
				if (out_pos >= out_end)
					return out_pos - out_base64;
				*out_pos++ = '\n';
				state->stepcount = 0;
			}
		}
	}
	/* control should not reach here */
	return out_pos - out_base64;
}

static int
base64_encode_blockend(char *out_base64, int out_len,
		       struct base64_encodestate *state)
{
	char *out_pos = out_base64;
	char *out_end = out_base64 + out_len;

	switch (state->step) {
	case step_B:
		if (out_pos + 2 >= out_end)
			return out_pos - out_base64;
		*out_pos++ = base64_encode_value(state->result);
		*out_pos++ = '=';
		*out_pos++ = '=';
		break;
	case step_C:
		if (out_pos + 1 >= out_end)
			return out_pos - out_base64;
		*out_pos++ = base64_encode_value(state->result);
		*out_pos++ = '=';
		break;
	case step_A:
		break;
	}
	if (out_pos >= out_end)
		return out_pos - out_base64;
	*out_pos++ = '\n';
	if (out_pos >= out_end)
		return out_pos - out_base64;
	*out_pos = '\0';
	return out_pos - out_base64;
}

int
base64_encode(const char *in_bin, int in_len,
	      char *out_base64, int out_len)
{
	struct base64_encodestate state;
	base64_encodestate_init(&state);
	int res = base64_encode_block(in_bin, in_len, out_base64,
				      out_len, &state);
	return res + base64_encode_blockend(out_base64 + res, out_len - res,
					    &state);
}

/* }}} */

/* {{{ decode */

enum base64_decodestep { step_a, step_b, step_c, step_d };

struct base64_decodestate
{
	enum base64_decodestep step;
	char result;
};

static char
base64_decode_value(char value)
{
	static const char decoding[] = {
		62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58,
		59, 60, 61, -1, -1, -1, -2, -1, -1, -1,  0,  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,
		-1, -1, -1, -1, -1, -1, 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
	};
	static const char decoding_size = sizeof(decoding);
	int codepos = (signed char) value;
	codepos -= 43;
	if (codepos < 0 || codepos > decoding_size)
		return -1;
	return decoding[codepos];
}

static inline void
base64_decodestate_init(struct base64_decodestate *state)
{
	state->step = step_a;
	state->result = 0;
}

static int
base64_decode_block(const char *in_base64, int in_len,
		    char *out_bin, int out_len,
		    struct base64_decodestate *state)
{
	const char *in_pos = in_base64;
	const char *in_end = in_base64 + in_len;
	char *out_pos = out_bin;
	char *out_end = out_bin + out_len;
	char fragment;

	*out_pos = state->result;

	switch (state->step)
	{
		while (1)
		{
	case step_a:
			do {
				if (in_pos == in_end || out_pos >= out_end)
				{
					state->step = step_a;
					state->result = *out_pos;
					return out_pos - out_bin;
				}
				fragment = base64_decode_value(*in_pos++);
			} while (fragment < 0);
			*out_pos    = (fragment & 0x03f) << 2;
	case step_b:
			do {
				if (in_pos == in_end || out_pos + 1 >= out_end)
				{
					state->step = step_b;
					state->result = *out_pos;
					return out_pos - out_bin;
				}
				fragment = base64_decode_value(*in_pos++);
			} while (fragment < 0);
			*out_pos++ |= (fragment & 0x030) >> 4;
			*out_pos    = (fragment & 0x00f) << 4;
	case step_c:
			do {
				if (in_pos == in_end || out_pos + 1 >= out_end)
				{
					state->step = step_c;
					state->result = *out_pos;
					return out_pos - out_bin;
				}
				fragment = base64_decode_value(*in_pos++);
			} while (fragment < 0);
			*out_pos++ |= (fragment & 0x03c) >> 2;
			*out_pos    = (fragment & 0x003) << 6;
	case step_d:
			do {
				if (in_pos == in_end || out_pos >= out_end)
				{
					state->step = step_d;
					state->result = *out_pos;
					return out_pos - out_bin;
				}
				fragment = base64_decode_value(*in_pos++);
			} while (fragment < 0);
			*out_pos++   |= (fragment & 0x03f);
		}
	}
	/* control should not reach here */
	return out_pos - out_bin;
}



int
base64_decode(const char *in_base64, int in_len,
	      char *out_bin, int out_len)
{
	struct base64_decodestate state;
	base64_decodestate_init(&state);
	return base64_decode_block(in_base64, in_len,
				   out_bin, out_len, &state);
}

/* }}} */