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 /* UHC was introduced by MicroSoft in Windows 95, and is also known as CP949.
31 * It is the same as EUC-KR, but with 8,822 additional characters added to
32 * complete all the characters in the Johab charset. */
33
34 #include "mbfilter.h"
35 #include "mbfilter_uhc.h"
36 #define UNICODE_TABLE_UHC_DEF
37 #include "unicode_table_uhc.h"
38
39 static int mbfl_filt_conv_uhc_wchar_flush(mbfl_convert_filter *filter);
40
41 static const unsigned char mblen_table_uhc[] = { /* 0x81-0xFE */
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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50 1, 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, 2,
54 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
55 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
57 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
58 };
59
60 static const char *mbfl_encoding_uhc_aliases[] = {"CP949", NULL};
61
62 const mbfl_encoding mbfl_encoding_uhc = {
63 mbfl_no_encoding_uhc,
64 "UHC",
65 "UHC",
66 mbfl_encoding_uhc_aliases,
67 mblen_table_uhc,
68 0,
69 &vtbl_uhc_wchar,
70 &vtbl_wchar_uhc,
71 NULL
72 };
73
74 const struct mbfl_convert_vtbl vtbl_uhc_wchar = {
75 mbfl_no_encoding_uhc,
76 mbfl_no_encoding_wchar,
77 mbfl_filt_conv_common_ctor,
78 NULL,
79 mbfl_filt_conv_uhc_wchar,
80 mbfl_filt_conv_uhc_wchar_flush,
81 NULL,
82 };
83
84 const struct mbfl_convert_vtbl vtbl_wchar_uhc = {
85 mbfl_no_encoding_wchar,
86 mbfl_no_encoding_uhc,
87 mbfl_filt_conv_common_ctor,
88 NULL,
89 mbfl_filt_conv_wchar_uhc,
90 mbfl_filt_conv_common_flush,
91 NULL,
92 };
93
94 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
95
mbfl_filt_conv_uhc_wchar(int c,mbfl_convert_filter * filter)96 int mbfl_filt_conv_uhc_wchar(int c, mbfl_convert_filter *filter)
97 {
98 switch (filter->status) {
99 case 0:
100 if (c >= 0 && c < 0x80) { /* latin */
101 CK((*filter->output_function)(c, filter->data));
102 } else if (c > 0x80 && c < 0xfe && c != 0xc9) { /* dbcs lead byte */
103 filter->status = 1;
104 filter->cache = c;
105 } else {
106 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
107 }
108 break;
109
110 case 1: /* dbcs second byte */
111 filter->status = 0;
112 int c1 = filter->cache, w = 0;
113
114 if (c1 >= 0x81 && c1 <= 0xa0 && c >= 0x41 && c <= 0xfe) {
115 w = (c1 - 0x81)*190 + (c - 0x41);
116 if (w >= 0 && w < uhc1_ucs_table_size) {
117 w = uhc1_ucs_table[w];
118 }
119 } else if (c1 >= 0xa1 && c1 <= 0xc6 && c >= 0x41 && c <= 0xfe) {
120 w = (c1 - 0xa1)*190 + (c - 0x41);
121 if (w >= 0 && w < uhc2_ucs_table_size) {
122 w = uhc2_ucs_table[w];
123 }
124 } else if (c1 >= 0xc7 && c1 < 0xfe && c >= 0xa1 && c <= 0xfe) {
125 w = (c1 - 0xc7)*94 + (c - 0xa1);
126 if (w >= 0 && w < uhc3_ucs_table_size) {
127 w = uhc3_ucs_table[w];
128 }
129 }
130
131 if (w == 0) {
132 w = MBFL_BAD_INPUT;
133 }
134 CK((*filter->output_function)(w, filter->data));
135 break;
136
137 default:
138 filter->status = 0;
139 break;
140 }
141
142 return 0;
143 }
144
mbfl_filt_conv_uhc_wchar_flush(mbfl_convert_filter * filter)145 static int mbfl_filt_conv_uhc_wchar_flush(mbfl_convert_filter *filter)
146 {
147 if (filter->status == 1) {
148 /* 2-byte character was truncated */
149 filter->status = 0;
150 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
151 }
152
153 if (filter->flush_function) {
154 (*filter->flush_function)(filter->data);
155 }
156
157 return 0;
158 }
159
mbfl_filt_conv_wchar_uhc(int c,mbfl_convert_filter * filter)160 int mbfl_filt_conv_wchar_uhc(int c, mbfl_convert_filter *filter)
161 {
162 int s = 0;
163
164 if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) {
165 s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min];
166 } else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) {
167 s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min];
168 } else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) {
169 s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min];
170 } else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) {
171 s = ucs_i_uhc_table[c - ucs_i_uhc_table_min];
172 } else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) {
173 s = ucs_s_uhc_table[c - ucs_s_uhc_table_min];
174 } else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) {
175 s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min];
176 } else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) {
177 s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min];
178 }
179
180 if (s == 0 && c != 0) {
181 s = -1;
182 }
183
184 if (s >= 0) {
185 if (s < 0x80) { /* latin */
186 CK((*filter->output_function)(s, filter->data));
187 } else {
188 CK((*filter->output_function)((s >> 8) & 0xff, filter->data));
189 CK((*filter->output_function)(s & 0xff, filter->data));
190 }
191 } else {
192 CK(mbfl_filt_conv_illegal_output(c, filter));
193 }
194
195 return 0;
196 }
197