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: Wez Furlong <wez@thebrainroom.com>
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_cp1252.h"
36 #include "unicode_table_cp1252.h"
37 
38 static int mbfl_filt_ident_cp1252(int c, mbfl_identify_filter *filter);
39 
40 static const char *mbfl_encoding_cp1252_aliases[] = {"cp1252", NULL};
41 
42 const mbfl_encoding mbfl_encoding_cp1252 = {
43 	mbfl_no_encoding_cp1252,
44 	"Windows-1252",
45 	"Windows-1252",
46 	(const char *(*)[])&mbfl_encoding_cp1252_aliases,
47 	NULL,
48 	MBFL_ENCTYPE_SBCS
49 };
50 
51 const struct mbfl_identify_vtbl vtbl_identify_cp1252 = {
52 	mbfl_no_encoding_cp1252,
53 	mbfl_filt_ident_common_ctor,
54 	mbfl_filt_ident_common_dtor,
55 	mbfl_filt_ident_cp1252
56 };
57 
58 const struct mbfl_convert_vtbl vtbl_cp1252_wchar = {
59 	mbfl_no_encoding_cp1252,
60 	mbfl_no_encoding_wchar,
61 	mbfl_filt_conv_common_ctor,
62 	mbfl_filt_conv_common_dtor,
63 	mbfl_filt_conv_cp1252_wchar,
64 	mbfl_filt_conv_common_flush
65 };
66 
67 const struct mbfl_convert_vtbl vtbl_wchar_cp1252 = {
68 	mbfl_no_encoding_wchar,
69 	mbfl_no_encoding_cp1252,
70 	mbfl_filt_conv_common_ctor,
71 	mbfl_filt_conv_common_dtor,
72 	mbfl_filt_conv_wchar_cp1252,
73 	mbfl_filt_conv_common_flush
74 };
75 
76 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
77 
78 /*
79  * wchar => cp1252
80  */
mbfl_filt_conv_wchar_cp1252(int c,mbfl_convert_filter * filter)81 int mbfl_filt_conv_wchar_cp1252(int c, mbfl_convert_filter *filter)
82 {
83 	int s=-1, n;
84 
85 	if (c >= 0x100)	{
86 		/* look it up from the cp1252 table */
87 		s = -1;
88 		n = 31;
89 		while (n >= 0) {
90 			if (c == cp1252_ucs_table[n] && c != 0xfffe) {
91 				s = 0x80 + n;
92 				break;
93 			}
94 			n--;
95 		}
96 		if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_8859_1)
97 		{
98 			s = c & MBFL_WCSPLANE_MASK;
99 		}
100 	}
101 	else if (c >= 0 && c < 0x100) {
102 		s = c;
103 	}
104 	if (s >= 0) {
105 		CK((*filter->output_function)(s, filter->data));
106 	} else {
107 		if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
108 			CK(mbfl_filt_conv_illegal_output(c, filter));
109 		}
110 	}
111 	return c;
112 }
113 
114 /*
115  * cp1252 => wchar
116  */
mbfl_filt_conv_cp1252_wchar(int c,mbfl_convert_filter * filter)117 int mbfl_filt_conv_cp1252_wchar(int c, mbfl_convert_filter *filter)
118 {
119 	int s;
120 
121 	if (c >= 0x80 && c < 0xa0) {
122 		s = cp1252_ucs_table[c - 0x80];
123 	} else {
124 		s = c;
125 	}
126 
127 	CK((*filter->output_function)(s, filter->data));
128 
129 	return c;
130 }
131 
132 /* We only distinguish the MS extensions to ISO-8859-1.
133  * Actually, this is pretty much a NO-OP, since the identification
134  * system doesn't allow us to discriminate between a positive match,
135  * a possible match and a definite non-match.
136  * The problem here is that cp1252 looks like SJIS for certain chars.
137  * */
mbfl_filt_ident_cp1252(int c,mbfl_identify_filter * filter)138 static int mbfl_filt_ident_cp1252(int c, mbfl_identify_filter *filter)
139 {
140 	if (c >= 0x80 && c < 0xa0)
141 		filter->flag = 0;
142 	else
143 		filter->flag = 1; /* not it */
144 	return c;
145 }
146