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 part: Marcus Boerger <helly@php.net>
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.
27  *
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 
38 #ifdef HAVE_STRINGS_H
39 #include <strings.h>
40 #endif
41 
42 #include "mbfilter.h"
43 #include "mbfilter_htmlent.h"
44 #include "html_entities.h"
45 
46 static const int htmlentitifieds[256] = {
47   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49   0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0,
51   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
58   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
59   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
61   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
62   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
63 };
64 
65 static const char *mbfl_encoding_html_ent_aliases[] = {"HTML", "html", NULL};
66 
67 const mbfl_encoding mbfl_encoding_html_ent = {
68 	mbfl_no_encoding_html_ent,
69 	"HTML-ENTITIES",
70 	"HTML-ENTITIES",
71 	(const char *(*)[])&mbfl_encoding_html_ent_aliases,
72 	NULL,
73 	MBFL_ENCTYPE_ENC_STRM | MBFL_ENCTYPE_GL_UNSAFE
74 };
75 
76 const struct mbfl_convert_vtbl vtbl_wchar_html = {
77 	mbfl_no_encoding_wchar,
78 	mbfl_no_encoding_html_ent,
79 	mbfl_filt_conv_common_ctor,
80 	mbfl_filt_conv_common_dtor,
81 	mbfl_filt_conv_html_enc,
82 	mbfl_filt_conv_html_enc_flush
83 };
84 
85 const struct mbfl_convert_vtbl vtbl_html_wchar = {
86 	mbfl_no_encoding_html_ent,
87 	mbfl_no_encoding_wchar,
88 	mbfl_filt_conv_html_dec_ctor,
89 	mbfl_filt_conv_html_dec_dtor,
90 	mbfl_filt_conv_html_dec,
91 	mbfl_filt_conv_html_dec_flush,
92 	mbfl_filt_conv_html_dec_copy };
93 
94 
95 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
96 
97 /*
98  * any => HTML
99  */
mbfl_filt_conv_html_enc(int c,mbfl_convert_filter * filter)100 int mbfl_filt_conv_html_enc(int c, mbfl_convert_filter *filter)
101 {
102 	int tmp[64];
103 	int i;
104 	unsigned int uc;
105 	const mbfl_html_entity_entry *e;
106 
107 	if (c < sizeof(htmlentitifieds) / sizeof(htmlentitifieds[0]) &&
108 				htmlentitifieds[c] != 1) {
109 		CK((*filter->output_function)(c, filter->data));
110 	} else {
111  		CK((*filter->output_function)('&', filter->data));
112 		for (i = 0; (e = &mbfl_html_entity_list[i])->name != NULL; i++) {
113 			if (c == e->code) {
114 				char *p;
115 
116 				for (p = e->name; *p != '\0'; p++) {
117 					CK((*filter->output_function)((int)*p, filter->data));
118 				}
119 				goto last;
120 			}
121 		}
122 
123 		{
124 			int *p = tmp + sizeof(tmp) / sizeof(tmp[0]);
125 
126 			CK((*filter->output_function)('#', filter->data));
127 
128 			uc = (unsigned int)c;
129 
130 			*(--p) = '\0';
131 			do {
132 				*(--p) = "0123456789"[uc % 10];
133 				uc /= 10;
134 			} while (uc);
135 
136 			for (; *p != '\0'; p++) {
137 				CK((*filter->output_function)(*p, filter->data));
138 			}
139 		}
140 	last:
141 		CK((*filter->output_function)(';', filter->data));
142 	}
143 	return c;
144 }
145 
mbfl_filt_conv_html_enc_flush(mbfl_convert_filter * filter)146 int mbfl_filt_conv_html_enc_flush(mbfl_convert_filter *filter)
147 {
148 	filter->status = 0;
149 	filter->opaque = NULL;
150 
151 	if (filter->flush_function != NULL) {
152 		(*filter->flush_function)(filter->data);
153 	}
154 
155 	return 0;
156 }
157 
158 /*
159  * HTML => any
160  */
161 #define html_enc_buffer_size	16
162 static const char html_entity_chars[] = "#0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
163 
mbfl_filt_conv_html_dec_ctor(mbfl_convert_filter * filter)164 void mbfl_filt_conv_html_dec_ctor(mbfl_convert_filter *filter)
165 {
166 	filter->status = 0;
167 	filter->opaque = mbfl_malloc(html_enc_buffer_size+1);
168 }
169 
mbfl_filt_conv_html_dec_dtor(mbfl_convert_filter * filter)170 void mbfl_filt_conv_html_dec_dtor(mbfl_convert_filter *filter)
171 {
172 	filter->status = 0;
173 	if (filter->opaque)
174 	{
175 		mbfl_free((void*)filter->opaque);
176 	}
177 	filter->opaque = NULL;
178 }
179 
mbfl_filt_conv_html_dec(int c,mbfl_convert_filter * filter)180 int mbfl_filt_conv_html_dec(int c, mbfl_convert_filter *filter)
181 {
182 	int  pos, ent = 0;
183 	mbfl_html_entity_entry *entity;
184 	char *buffer = (char*)filter->opaque;
185 
186 	if (!filter->status) {
187 		if (c == '&' ) {
188 			filter->status = 1;
189 			buffer[0] = '&';
190 		} else {
191 			CK((*filter->output_function)(c, filter->data));
192 		}
193 	} else {
194 		if (c == ';') {
195 			if (buffer[1]=='#') {
196 				if (filter->status > 2 && (buffer[2] == 'x' || buffer[2] == 'X')) {
197 					if (filter->status > 3) {
198 						/* numeric entity */
199 						for (pos=3; pos<filter->status; pos++) {
200 							int v =  buffer[pos];
201 							if (v >= '0' && v <= '9') {
202 								v = v - '0';
203 							} else if (v >= 'A' && v <= 'F') {
204 								v = v - 'A' + 10;
205 							} else if (v >= 'a' && v <= 'f') {
206 								v = v - 'a' + 10;
207 							} else {
208 								ent = -1;
209 								break;
210 							}
211 							ent = ent * 16 + v;
212 						}
213 					} else {
214 						ent = -1;
215 					}
216 				} else {
217 					/* numeric entity */
218 					if (filter->status > 2) {
219 						for (pos=2; pos<filter->status; pos++) {
220 							int v = buffer[pos];
221 							if (v >= '0' && v <= '9') {
222 								v = v - '0';
223 							} else {
224 								ent = -1;
225 								break;
226 							}
227 							ent = ent*10 + v;
228 						}
229 					} else {
230 						ent = -1;
231 					}
232 				}
233 				if (ent >= 0 && ent < 0x110000) {
234 					CK((*filter->output_function)(ent, filter->data));
235 				} else {
236 					for (pos = 0; pos < filter->status; pos++) {
237 						CK((*filter->output_function)(buffer[pos], filter->data));
238 					}
239 					CK((*filter->output_function)(c, filter->data));
240 				}
241 				filter->status = 0;
242 				/*php_error_docref("ref.mbstring", E_NOTICE, "mbstring decoded '%s'=%d", buffer, ent);*/
243 			} else {
244 				/* named entity */
245 				buffer[filter->status] = 0;
246 				entity = (mbfl_html_entity_entry *)mbfl_html_entity_list;
247 				while (entity->name) {
248 					if (!strcmp(buffer+1, entity->name))	{
249 						ent = entity->code;
250 						break;
251 					}
252 					entity++;
253 				}
254 				if (ent) {
255 					/* decoded */
256 					CK((*filter->output_function)(ent, filter->data));
257 					filter->status = 0;
258 					/*php_error_docref("ref.mbstring", E_NOTICE,"mbstring decoded '%s'=%d", buffer, ent);*/
259 				} else {
260 					/* failure */
261 					buffer[filter->status++] = ';';
262 					buffer[filter->status] = 0;
263 					/* php_error_docref("ref.mbstring", E_WARNING, "mbstring cannot decode '%s'", buffer); */
264 					mbfl_filt_conv_html_dec_flush(filter);
265 				}
266 			}
267 		} else {
268 			/* add character */
269 			buffer[filter->status++] = c;
270 			/* add character and check */
271 			if (!strchr(html_entity_chars, c) || filter->status+1==html_enc_buffer_size || (c=='#' && filter->status>2))
272 			{
273 				/* illegal character or end of buffer */
274 				if (c=='&')
275 					filter->status--;
276 				buffer[filter->status] = 0;
277 				/* php_error_docref("ref.mbstring", E_WARNING, "mbstring cannot decode '%s'", buffer)l */
278 				mbfl_filt_conv_html_dec_flush(filter);
279 				if (c=='&')
280 				{
281 					buffer[filter->status++] = '&';
282 				}
283 			}
284 		}
285 	}
286 	return c;
287 }
288 
mbfl_filt_conv_html_dec_flush(mbfl_convert_filter * filter)289 int mbfl_filt_conv_html_dec_flush(mbfl_convert_filter *filter)
290 {
291 	int status, pos = 0;
292 	unsigned char *buffer;
293 	int err = 0;
294 
295 	buffer = (unsigned char*)filter->opaque;
296 	status = filter->status;
297 	filter->status = 0;
298 
299 	/* flush fragments */
300 	while (status--) {
301 		int e = (*filter->output_function)(buffer[pos++], filter->data);
302 		if (e != 0)
303 			err = e;
304 	}
305 
306 	if (filter->flush_function != NULL) {
307 		(*filter->flush_function)(filter->data);
308 	}
309 
310 	return err;
311 }
312 
mbfl_filt_conv_html_dec_copy(mbfl_convert_filter * src,mbfl_convert_filter * dest)313 void mbfl_filt_conv_html_dec_copy(mbfl_convert_filter *src, mbfl_convert_filter *dest)
314 {
315 	*dest = *src;
316 	dest->opaque = mbfl_malloc(html_enc_buffer_size+1);
317 	memcpy(dest->opaque, src->opaque, html_enc_buffer_size+1);
318 }
319