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.
27  *
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "mbfilter.h"
35 #include "mbfilter_iso8859_2.h"
36 #include "unicode_table_iso8859_2.h"
37 
38 static const char *mbfl_encoding_8859_2_aliases[] = {"ISO_8859-2", "latin2", NULL};
39 
40 const mbfl_encoding mbfl_encoding_8859_2 = {
41 	mbfl_no_encoding_8859_2,
42 	"ISO-8859-2",
43 	"ISO-8859-2",
44 	(const char *(*)[])&mbfl_encoding_8859_2_aliases,
45 	NULL,
46 	MBFL_ENCTYPE_SBCS,
47 	&vtbl_8859_2_wchar,
48 	&vtbl_wchar_8859_2
49 };
50 
51 const struct mbfl_identify_vtbl vtbl_identify_8859_2 = {
52 	mbfl_no_encoding_8859_2,
53 	mbfl_filt_ident_common_ctor,
54 	mbfl_filt_ident_common_dtor,
55 	mbfl_filt_ident_true
56 };
57 
58 const struct mbfl_convert_vtbl vtbl_8859_2_wchar = {
59 	mbfl_no_encoding_8859_2,
60 	mbfl_no_encoding_wchar,
61 	mbfl_filt_conv_common_ctor,
62 	mbfl_filt_conv_common_dtor,
63 	mbfl_filt_conv_8859_2_wchar,
64 	mbfl_filt_conv_common_flush
65 };
66 
67 const struct mbfl_convert_vtbl vtbl_wchar_8859_2 = {
68 	mbfl_no_encoding_wchar,
69 	mbfl_no_encoding_8859_2,
70 	mbfl_filt_conv_common_ctor,
71 	mbfl_filt_conv_common_dtor,
72 	mbfl_filt_conv_wchar_8859_2,
73 	mbfl_filt_conv_common_flush
74 };
75 
76 
77 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
78 
79 /*
80  * ISO-8859-2 => wchar
81  */
mbfl_filt_conv_8859_2_wchar(int c,mbfl_convert_filter * filter)82 int mbfl_filt_conv_8859_2_wchar(int c, mbfl_convert_filter *filter)
83 {
84 	int s;
85 
86 	if (c >= 0 && c < 0xa0) {
87 		s = c;
88 	} else if (c >= 0xa0 && c < 0x100) {
89 		s = iso8859_2_ucs_table[c - 0xa0];
90 		if (s <= 0) {
91 			s = c;
92 			s &= MBFL_WCSPLANE_MASK;
93 			s |= MBFL_WCSPLANE_8859_2;
94 		}
95 	} else {
96 		s = c;
97 		s &= MBFL_WCSGROUP_MASK;
98 		s |= MBFL_WCSGROUP_THROUGH;
99 	}
100 
101 	CK((*filter->output_function)(s, filter->data));
102 
103 	return c;
104 }
105 
106 /*
107  * wchar => ISO-8859-2
108  */
mbfl_filt_conv_wchar_8859_2(int c,mbfl_convert_filter * filter)109 int mbfl_filt_conv_wchar_8859_2(int c, mbfl_convert_filter *filter)
110 {
111 	int s, n;
112 
113 	if (c >= 0 && c < 0xa0) {
114 		s = c;
115 	} else {
116 		s = -1;
117 		n = 95;
118 		while (n >= 0) {
119 			if (c == iso8859_2_ucs_table[n]) {
120 				s = 0xa0 + n;
121 				break;
122 			}
123 			n--;
124 		}
125 		if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_8859_2) {
126 			s = c & MBFL_WCSPLANE_MASK;
127 		}
128 	}
129 
130 	if (s >= 0) {
131 		CK((*filter->output_function)(s, filter->data));
132 	} else {
133 		CK(mbfl_filt_conv_illegal_output(c, filter));
134 	}
135 
136 	return c;
137 }
138