summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/string/base64.c
blob: e919cba7a6fb8675c5dd0dbe4abc30dc888051f7 (plain)
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
297
298
299
300
301
302
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * mime base64 encode/decode
 *
 * Glenn Fowler
 * David Korn
 * AT&T Research
 */

#include <ast.h>

#define PAD		'='

#define B64_UC		3
#define B64_EC		4
#define B64_CHUNK	15
#define B64_PAD		64
#define B64_SPC		65
#define B64_IGN		66

static unsigned char	map[UCHAR_MAX+1];

static const char	alp[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/*
 * mime base64 encode
 */

ssize_t
base64encode(const void* fb, size_t fz, void** fn, void* tb, size_t tz, void** tn)
{
	register unsigned char*	fp;
	register unsigned char*	tp;
	register unsigned char*	fe;
	register unsigned char*	te;
	register unsigned char*	tc;
	register unsigned char*	m;
	register unsigned long	b;
	size_t			n;
	unsigned char		tmp[B64_EC * B64_CHUNK];

	m = (unsigned char*)alp;
	fp = fe = (unsigned char*)fb;
	if (fz >= 3)
	{
		n = fz % 3;
		fe += fz - n;
		fz = n;
	}
	if (tp = (unsigned char*)tb)
	{
		te = tp + tz - B64_EC + 1;
		n = 0;
	}
	else
	{
		if (fn)
			*fn = fp;
		if (tn)
			*tn = 0;
		tp = tmp;
		te = tp + sizeof(tmp) - B64_EC + 1;
		n = 1;
	}
	for (;;)
	{
		tc = tp + B64_EC * B64_CHUNK;
		do
		{
			if (fp >= fe)
				goto done;
			if (tp >= te)
			{
				if (fn)
					*fn = fp;
				if (tn)
					*tn = tp;
				n = tp - (unsigned char*)tb + 1;
				tp = tmp;
				te = tp + sizeof(tmp) - B64_EC + 1;
			}
			b = *fp++ << 16;
			b |= *fp++ << 8;
			b |= *fp++;
			*tp++ = m[b >> 18];
			*tp++ = m[(b >> 12) & 077];
			*tp++ = m[(b >> 6) & 077];
			*tp++ = m[b & 077];
		} while (tp < tc);
		if (n)
		{
			n += tp - tmp + (fp < fe);
			tp = tmp;
		}
		else
			*tp++ = '\n';
	}
 done:
	if (fz)
	{
		b = *fp++ << 16;
		if (fz == 2)
			b |= *fp++ << 8;
		*tp++ = m[b >> 18];
		*tp++ = m[(b >> 12) & 077];
		*tp++ = (fz == 2) ? m[(b >> 6) & 077] : PAD;
		*tp++ = PAD;
	}
	if (n)
		n += (tp - tmp) - 1;
	else
	{
		if (tp > (unsigned char*)tb && *(tp - 1) == '\n')
			tp--;
		if (tp < te)
			*tp = 0;
		n = tp - (unsigned char*)tb;
		if (tn)
			*tn = tp;
		if (fn)
			*fn = fp;
	}
	return n;
}

/*
 * mime base64 decode
 */

ssize_t
base64decode(const void* fb, size_t fz, void** fn, void* tb, size_t tz, void** tn)
{
	register unsigned char*	fp;
	register unsigned char*	tp;
	register unsigned char*	fe;
	register unsigned char*	te;
	register unsigned char*	tx;
	register unsigned char*	m;
	register int		c;
	register int		state;
	register unsigned long	v;
	unsigned char*		fc;
	ssize_t			n;

	if (!(m = map)[0])
	{
		memset(m, B64_IGN, sizeof(map));
		for (tp = (unsigned char*)alp; c = *tp; tp++)
			m[c] =  tp - (unsigned char*)alp;
		m[PAD] = B64_PAD;
		m[' '] = m['\t'] = m['\n'] = B64_SPC;
	}
	fp = (unsigned char*)fb;
	fe = fp + fz;
	if (tp = (unsigned char*)tb)
	{
		te = tp + tz;
		if (tz > 2)
			tz = 2;
		tx = te - tz;
		n = 0;
	}
	else
	{
		te = tx = tp;
		n = 1;
	}
	for (;;)
	{
		fc = fp;
		state = 0;
		v = 0;
		while (fp < fe)
		{
			if ((c = m[*fp++]) < 64)
			{
				v = (v << 6) | c;
				if (++state == 4)
				{
					if (tp >= tx)
					{
						if (n)
							n += 3;
						else
						{
							n = tp - (unsigned char*)tb + 4;
							if (tp < te)
							{
								*tp++ = (v >> 16);
								if (tp < te)
								{
									*tp++ = (v >> 8);
									if (tp < te)
										*tp++ = (v);
								}
							}
							if (tn)
								*tn = tp;
							if (fn)
								*fn = fc;
						}
					}
					else
					{
						*tp++ = (v >> 16);
						*tp++ = (v >> 8);
						*tp++ = (v);
					}
					fc = fp;
					state = 0;
					v = 0;
				}
			}
			else if (c == B64_PAD)
				break;
		}
		switch (state)
		{
		case 0:
			goto done;
		case 2:
			if (tp < te)
				*tp++ = v >> 4;
			else if (n)
				n++;
			else
			{
				n = tp - (unsigned char*)tb + 2;
				if (tn)
					*tn = tp;
				if (fn)
					*fn = fc;
			}
			break;
		case 3:
			if (tp < te)
			{
				*tp++ = v >> 10;
				if (tp < te)
					*tp++ = v >> 2;
				else
				{
					n = tp - (unsigned char*)tb + 2;
					if (tn)
						*tn = tp;
					if (fn)
						*fn = fc;
				}
			}
			else if (n)
				n += 2;
			else
			{
				n = tp - (unsigned char*)tb + 3;
				if (tn)
					*tn = tp;
				if (fn)
					*fn = fc;
			}
			break;
		}
		while (fp < fe && ((c = m[*fp++]) == B64_PAD || c == B64_SPC));
		if (fp >= fe || c >= 64)
			break;
		fp--;
	}
 done:
	if (n)
		n--;
	else
	{
		if (tp < te)
			*tp = 0;
		n = tp - (unsigned char*)tb;
		if (fn)
			*fn = fp;
		if (tn)
			*tn = tp;
	}
	return n;
}