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: Den V. Tsopa <tdv@edisoft.ru>
22  * Adaption for CP850: D. Giffeler <dg@artegic.de>
23  *
24  */
25 
26 #include "mbfilter.h"
27 #include "mbfilter_cp850.h"
28 #include "unicode_table_cp850.h"
29 
30 static int mbfl_filt_ident_cp850(int c, mbfl_identify_filter *filter);
31 
32 static const char *mbfl_encoding_cp850_aliases[] = {"CP850", "CP-850", "IBM850", "IBM-850", NULL};
33 
34 const mbfl_encoding mbfl_encoding_cp850 = {
35 	mbfl_no_encoding_cp850,
36 	"CP850",
37 	"CP850",
38 	(const char *(*)[])&mbfl_encoding_cp850_aliases,
39 	NULL,
40 	MBFL_ENCTYPE_SBCS,
41 	&vtbl_cp850_wchar,
42 	&vtbl_wchar_cp850
43 };
44 
45 const struct mbfl_identify_vtbl vtbl_identify_cp850 = {
46 	mbfl_no_encoding_cp850,
47 	mbfl_filt_ident_common_ctor,
48 	mbfl_filt_ident_cp850
49 };
50 
51 const struct mbfl_convert_vtbl vtbl_wchar_cp850 = {
52 	mbfl_no_encoding_wchar,
53 	mbfl_no_encoding_cp850,
54 	mbfl_filt_conv_common_ctor,
55 	NULL,
56 	mbfl_filt_conv_wchar_cp850,
57 	mbfl_filt_conv_common_flush,
58 	NULL,
59 };
60 
61 const struct mbfl_convert_vtbl vtbl_cp850_wchar = {
62 	mbfl_no_encoding_cp850,
63 	mbfl_no_encoding_wchar,
64 	mbfl_filt_conv_common_ctor,
65 	NULL,
66 	mbfl_filt_conv_cp850_wchar,
67 	mbfl_filt_conv_common_flush,
68 	NULL,
69 };
70 
71 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
72 
73 /*
74  * cp850 => wchar
75  */
76 int
mbfl_filt_conv_cp850_wchar(int c,mbfl_convert_filter * filter)77 mbfl_filt_conv_cp850_wchar(int c, mbfl_convert_filter *filter)
78 {
79 	int s;
80 
81 	if (c >= 0 && c < cp850_ucs_table_min) {
82 		s = c;
83 	} else if (c >= cp850_ucs_table_min && c < 0x100) {
84 		s = cp850_ucs_table[c - cp850_ucs_table_min];
85 		if (s <= 0) {
86 			s = c;
87 			s &= MBFL_WCSPLANE_MASK;
88 			s |= MBFL_WCSPLANE_CP850;
89 		}
90 	} else {
91 		s = c;
92 		s &= MBFL_WCSGROUP_MASK;
93 		s |= MBFL_WCSGROUP_THROUGH;
94 	}
95 
96 	CK((*filter->output_function)(s, filter->data));
97 
98 	return c;
99 }
100 
101 /*
102  * wchar => cp850
103  */
104 int
mbfl_filt_conv_wchar_cp850(int c,mbfl_convert_filter * filter)105 mbfl_filt_conv_wchar_cp850(int c, mbfl_convert_filter *filter)
106 {
107 	int s, n;
108 
109 	if (c < 0x80) {
110 		s = c;
111 	} else {
112 		s = -1;
113 		n = cp850_ucs_table_len-1;
114 		while (n >= 0) {
115 			if (c == cp850_ucs_table[n]) {
116 				s = cp850_ucs_table_min + n;
117 				break;
118 			}
119 			n--;
120 		}
121 		if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_CP850) {
122 			s = c & MBFL_WCSPLANE_MASK;
123 		}
124 	}
125 
126 	if (s >= 0) {
127 		CK((*filter->output_function)(s, filter->data));
128 	} else {
129 		CK(mbfl_filt_conv_illegal_output(c, filter));
130 	}
131 
132 	return c;
133 }
134 
mbfl_filt_ident_cp850(int c,mbfl_identify_filter * filter)135 static int mbfl_filt_ident_cp850(int c, mbfl_identify_filter *filter)
136 {
137 	if (c >= 0x80 && c < 0xff)
138 		filter->flag = 0;
139 	else
140 		filter->flag = 1; /* not it */
141 	return c;
142 }
143