1  /*
2  * "streamable kanji code filter and converter"
3  * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4  *
5  * LICENSE NOTICES
6  *
7  * This file is part of "streamable kanji code filter and converter",
8  * which is distributed under the terms of GNU Lesser General Public
9  * License (version 2) as published by the Free Software Foundation.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with "streamable kanji code filter and converter";
18  * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19  * Suite 330, Boston, MA  02111-1307  USA
20  *
21  * The author of this file:
22  *
23  */
24 /*
25  * The source code included in this files was separated from mbfilter.c
26  * by Moriyoshi Koizumi <moriyoshi@php.net> on 4 Dec 2002. The file
27  * mbfilter.c is included in this package .
28  *
29  */
30 
31 #include "mbfilter.h"
32 #include "mbfilter_base64.h"
33 
34 const mbfl_encoding mbfl_encoding_base64 = {
35 	mbfl_no_encoding_base64,
36 	"BASE64",
37 	"BASE64",
38 	NULL,
39 	NULL,
40 	MBFL_ENCTYPE_GL_UNSAFE,
41 	NULL,
42 	NULL,
43 	NULL
44 };
45 
46 const struct mbfl_convert_vtbl vtbl_8bit_b64 = {
47 	mbfl_no_encoding_8bit,
48 	mbfl_no_encoding_base64,
49 	mbfl_filt_conv_common_ctor,
50 	NULL,
51 	mbfl_filt_conv_base64enc,
52 	mbfl_filt_conv_base64enc_flush,
53 	NULL,
54 };
55 
56 const struct mbfl_convert_vtbl vtbl_b64_8bit = {
57 	mbfl_no_encoding_base64,
58 	mbfl_no_encoding_8bit,
59 	mbfl_filt_conv_common_ctor,
60 	NULL,
61 	mbfl_filt_conv_base64dec,
62 	mbfl_filt_conv_base64dec_flush,
63 	NULL,
64 };
65 
66 
67 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
68 
69 /*
70  * any => BASE64
71  */
72 static const unsigned char mbfl_base64_table[] = {
73  /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
74    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
75  /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
76    0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
77  /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
78    0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
79  /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
80    0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
81  /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' */
82    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
83 };
84 
mbfl_filt_conv_base64enc(int c,mbfl_convert_filter * filter)85 int mbfl_filt_conv_base64enc(int c, mbfl_convert_filter *filter)
86 {
87 	int n;
88 
89 	n = (filter->status & 0xff);
90 	if (n == 0) {
91 		filter->status++;
92 		filter->cache = (c & 0xff) << 16;
93 	} else if (n == 1) {
94 		filter->status++;
95 		filter->cache |= (c & 0xff) << 8;
96 	} else {
97 		filter->status &= ~0xff;
98 		if ((filter->status & MBFL_BASE64_STS_MIME_HEADER) == 0) {
99 			n = (filter->status & 0xff00) >> 8;
100 			if (n > 72) {
101 				CK((*filter->output_function)(0x0d, filter->data));		/* CR */
102 				CK((*filter->output_function)(0x0a, filter->data));		/* LF */
103 				filter->status &= ~0xff00;
104 			}
105 			filter->status += 0x400;
106 		}
107 		n = filter->cache | (c & 0xff);
108 		CK((*filter->output_function)(mbfl_base64_table[(n >> 18) & 0x3f], filter->data));
109 		CK((*filter->output_function)(mbfl_base64_table[(n >> 12) & 0x3f], filter->data));
110 		CK((*filter->output_function)(mbfl_base64_table[(n >> 6) & 0x3f], filter->data));
111 		CK((*filter->output_function)(mbfl_base64_table[n & 0x3f], filter->data));
112 	}
113 
114 	return 0;
115 }
116 
mbfl_filt_conv_base64enc_flush(mbfl_convert_filter * filter)117 int mbfl_filt_conv_base64enc_flush(mbfl_convert_filter *filter)
118 {
119 	int status, cache, len;
120 
121 	status = filter->status & 0xff;
122 	cache = filter->cache;
123 	len = (filter->status & 0xff00) >> 8;
124 	filter->status &= ~0xffff;
125 	filter->cache = 0;
126 	/* flush fragments */
127 	if (status >= 1) {
128 		if ((filter->status & MBFL_BASE64_STS_MIME_HEADER) == 0) {
129 			if (len > 72){
130 				CK((*filter->output_function)(0x0d, filter->data));		/* CR */
131 				CK((*filter->output_function)(0x0a, filter->data));		/* LF */
132 			}
133 		}
134 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 18) & 0x3f], filter->data));
135 		CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
136 		if (status == 1) {
137 			CK((*filter->output_function)(0x3d, filter->data));		/* '=' */
138 			CK((*filter->output_function)(0x3d, filter->data));		/* '=' */
139 		} else {
140 			CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
141 			CK((*filter->output_function)(0x3d, filter->data));		/* '=' */
142 		}
143 	}
144 	return 0;
145 }
146 
147 /*
148  * BASE64 => any
149  */
mbfl_filt_conv_base64dec(int c,mbfl_convert_filter * filter)150 int mbfl_filt_conv_base64dec(int c, mbfl_convert_filter *filter)
151 {
152 	int n;
153 
154 	if (c == 0x0d || c == 0x0a || c == 0x20 || c == 0x09 || c == 0x3d) {	/* CR or LF or SPACE or HTAB or '=' */
155 		return 0;
156 	}
157 
158 	n = 0;
159 	if (c >= 0x41 && c <= 0x5a) {		/* A - Z */
160 		n = c - 65;
161 	} else if (c >= 0x61 && c <= 0x7a) {	/* a - z */
162 		n = c - 71;
163 	} else if (c >= 0x30 && c <= 0x39) {	/* 0 - 9 */
164 		n = c + 4;
165 	} else if (c == 0x2b) {			/* '+' */
166 		n = 62;
167 	} else if (c == 0x2f) {			/* '/' */
168 		n = 63;
169 	}
170 	n &= 0x3f;
171 
172 	switch (filter->status) {
173 	case 0:
174 		filter->status = 1;
175 		filter->cache = n << 18;
176 		break;
177 	case 1:
178 		filter->status = 2;
179 		filter->cache |= n << 12;
180 		break;
181 	case 2:
182 		filter->status = 3;
183 		filter->cache |= n << 6;
184 		break;
185 	default:
186 		filter->status = 0;
187 		n |= filter->cache;
188 		CK((*filter->output_function)((n >> 16) & 0xff, filter->data));
189 		CK((*filter->output_function)((n >> 8) & 0xff, filter->data));
190 		CK((*filter->output_function)(n & 0xff, filter->data));
191 		break;
192 	}
193 
194 	return 0;
195 }
196 
mbfl_filt_conv_base64dec_flush(mbfl_convert_filter * filter)197 int mbfl_filt_conv_base64dec_flush(mbfl_convert_filter *filter)
198 {
199 	int status, cache;
200 
201 	status = filter->status;
202 	cache = filter->cache;
203 	filter->status = 0;
204 	filter->cache = 0;
205 	/* flush fragments */
206 	if (status >= 2) {
207 		CK((*filter->output_function)((cache >> 16) & 0xff, filter->data));
208 		if (status >= 3) {
209 			CK((*filter->output_function)((cache >> 8) & 0xff, filter->data));
210 		}
211 	}
212 	return 0;
213 }
214