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_kr.c
26 * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27 *
28 */
29
30 #include "mbfilter.h"
31 #include "mbfilter_uhc.h"
32 #define UNICODE_TABLE_UHC_DEF
33 #include "unicode_table_uhc.h"
34
35 static int mbfl_filt_ident_uhc(int c, mbfl_identify_filter *filter);
36
37 static const unsigned char mblen_table_uhc[] = { /* 0x81-0xFE */
38 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
47 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
54 };
55
56 static const char *mbfl_encoding_uhc_aliases[] = {"CP949", NULL};
57
58 const mbfl_encoding mbfl_encoding_uhc = {
59 mbfl_no_encoding_uhc,
60 "UHC",
61 "UHC",
62 (const char *(*)[])&mbfl_encoding_uhc_aliases,
63 mblen_table_uhc,
64 MBFL_ENCTYPE_MBCS,
65 &vtbl_uhc_wchar,
66 &vtbl_wchar_uhc
67 };
68
69 const struct mbfl_identify_vtbl vtbl_identify_uhc = {
70 mbfl_no_encoding_uhc,
71 mbfl_filt_ident_common_ctor,
72 mbfl_filt_ident_uhc
73 };
74
75 const struct mbfl_convert_vtbl vtbl_uhc_wchar = {
76 mbfl_no_encoding_uhc,
77 mbfl_no_encoding_wchar,
78 mbfl_filt_conv_common_ctor,
79 NULL,
80 mbfl_filt_conv_uhc_wchar,
81 mbfl_filt_conv_common_flush,
82 NULL,
83 };
84
85 const struct mbfl_convert_vtbl vtbl_wchar_uhc = {
86 mbfl_no_encoding_wchar,
87 mbfl_no_encoding_uhc,
88 mbfl_filt_conv_common_ctor,
89 NULL,
90 mbfl_filt_conv_wchar_uhc,
91 mbfl_filt_conv_common_flush,
92 NULL,
93 };
94
95 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
96
97 /*
98 * UHC => wchar
99 */
100 int
mbfl_filt_conv_uhc_wchar(int c,mbfl_convert_filter * filter)101 mbfl_filt_conv_uhc_wchar(int c, mbfl_convert_filter *filter)
102 {
103 int c1, w = 0, flag = 0;
104
105 switch (filter->status) {
106 case 0:
107 if (c >= 0 && c < 0x80) { /* latin */
108 CK((*filter->output_function)(c, filter->data));
109 } else if (c > 0x80 && c < 0xff && c != 0xc9) { /* dbcs lead byte */
110 filter->status = 1;
111 filter->cache = c;
112 } else {
113 w = c & MBFL_WCSGROUP_MASK;
114 w |= MBFL_WCSGROUP_THROUGH;
115 CK((*filter->output_function)(w, filter->data));
116 }
117 break;
118
119 case 1: /* dbcs second byte */
120 filter->status = 0;
121 c1 = filter->cache;
122
123 if ( c1 >= 0x81 && c1 <= 0xa0){
124 w = (c1 - 0x81)*190 + (c - 0x41);
125 if (w >= 0 && w < uhc1_ucs_table_size) {
126 flag = 1;
127 w = uhc1_ucs_table[w];
128 } else {
129 w = 0;
130 }
131 } else if ( c1 >= 0xa1 && c1 <= 0xc6){
132 w = (c1 - 0xa1)*190 + (c - 0x41);
133 if (w >= 0 && w < uhc2_ucs_table_size) {
134 flag = 2;
135 w = uhc2_ucs_table[w];
136 } else {
137 w = 0;
138 }
139 } else if ( c1 >= 0xc7 && c1 <= 0xfe){
140 w = (c1 - 0xc7)*94 + (c - 0xa1);
141 if (w >= 0 && w < uhc3_ucs_table_size) {
142 flag = 3;
143 w = uhc3_ucs_table[w];
144 } else {
145 w = 0;
146 }
147 }
148 if (flag > 0){
149 if (w <= 0) {
150 w = (c1 << 8) | c;
151 w &= MBFL_WCSPLANE_MASK;
152 w |= MBFL_WCSPLANE_UHC;
153 }
154 CK((*filter->output_function)(w, filter->data));
155 } else {
156 if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
157 CK((*filter->output_function)(c, filter->data));
158 } else {
159 w = (c1 << 8) | c;
160 w &= MBFL_WCSGROUP_MASK;
161 w |= MBFL_WCSGROUP_THROUGH;
162 CK((*filter->output_function)(w, filter->data));
163 }
164 }
165 break;
166
167 default:
168 filter->status = 0;
169 break;
170 }
171
172 return c;
173 }
174
175 /*
176 * wchar => UHC
177 */
178 int
mbfl_filt_conv_wchar_uhc(int c,mbfl_convert_filter * filter)179 mbfl_filt_conv_wchar_uhc(int c, mbfl_convert_filter *filter)
180 {
181 int c1, s;
182
183 s = 0;
184 if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) {
185 s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min];
186 } else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) {
187 s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min];
188 } else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) {
189 s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min];
190 } else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) {
191 s = ucs_i_uhc_table[c - ucs_i_uhc_table_min];
192 } else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) {
193 s = ucs_s_uhc_table[c - ucs_s_uhc_table_min];
194 } else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) {
195 s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min];
196 } else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) {
197 s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min];
198 }
199 if (s <= 0) {
200 c1 = c & ~MBFL_WCSPLANE_MASK;
201 if (c1 == MBFL_WCSPLANE_UHC) {
202 s = c & MBFL_WCSPLANE_MASK;
203 }
204 if (c == 0) {
205 s = 0;
206 } else if (s <= 0) {
207 s = -1;
208 }
209 }
210 if (s >= 0) {
211 if (s < 0x80) { /* latin */
212 CK((*filter->output_function)(s, filter->data));
213 } else {
214 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
215 CK((*filter->output_function)(s & 0xff, filter->data));
216 }
217 } else {
218 CK(mbfl_filt_conv_illegal_output(c, filter));
219 }
220
221 return c;
222 }
223
mbfl_filt_ident_uhc(int c,mbfl_identify_filter * filter)224 static int mbfl_filt_ident_uhc(int c, mbfl_identify_filter *filter)
225 {
226 switch (filter->status) {
227 case 0: /* latin */
228 if (c >= 0 && c < 0x80) { /* ok */
229 ;
230 } else if (c >= 0x81 && c <= 0xa0) { /* dbcs first char */
231 filter->status= 1;
232 } else if (c >= 0xa1 && c <= 0xc6) { /* dbcs first char */
233 filter->status= 2;
234 } else if (c >= 0xc7 && c <= 0xfe) { /* dbcs first char */
235 filter->status= 3;
236 } else { /* bad */
237 filter->flag = 1;
238 }
239
240 case 1:
241 case 2:
242 if (c < 0x41 || (c > 0x5a && c < 0x61)
243 || (c > 0x7a && c < 0x81) || c > 0xfe) { /* bad */
244 filter->flag = 1;
245 }
246 filter->status = 0;
247 break;
248
249 case 3:
250 if (c < 0xa1 || c > 0xfe) { /* bad */
251 filter->flag = 1;
252 }
253 filter->status = 0;
254 break;
255
256 default:
257 filter->status = 0;
258 break;
259 }
260
261 return c;
262 }
263