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. The file
27  * mbfilter.c is included in this package .
28  *
29  */
30 
31 #include "mbfilter.h"
32 #include "mbfilter_byte2.h"
33 
34 const mbfl_encoding mbfl_encoding_byte2be = {
35 	mbfl_no_encoding_byte2be,
36 	"byte2be",
37 	NULL,
38 	NULL,
39 	NULL,
40 	MBFL_ENCTYPE_SBCS,
41 	&vtbl_byte2be_wchar,
42 	&vtbl_wchar_byte2be
43 };
44 
45 const mbfl_encoding mbfl_encoding_byte2le = {
46 	mbfl_no_encoding_byte2le,
47 	"byte2le",
48 	NULL,
49 	NULL,
50 	NULL,
51 	MBFL_ENCTYPE_SBCS,
52 	&vtbl_byte2le_wchar,
53 	&vtbl_wchar_byte2le
54 };
55 
56 const struct mbfl_convert_vtbl vtbl_byte2be_wchar = {
57 	mbfl_no_encoding_byte2be,
58 	mbfl_no_encoding_wchar,
59 	mbfl_filt_conv_common_ctor,
60 	NULL,
61 	mbfl_filt_conv_byte2be_wchar,
62 	mbfl_filt_conv_common_flush,
63 	NULL,
64 };
65 
66 const struct mbfl_convert_vtbl vtbl_wchar_byte2be = {
67 	mbfl_no_encoding_wchar,
68 	mbfl_no_encoding_byte2be,
69 	mbfl_filt_conv_common_ctor,
70 	NULL,
71 	mbfl_filt_conv_wchar_byte2be,
72 	mbfl_filt_conv_common_flush,
73 	NULL,
74 };
75 
76 const struct mbfl_convert_vtbl vtbl_byte2le_wchar = {
77 	mbfl_no_encoding_byte2le,
78 	mbfl_no_encoding_wchar,
79 	mbfl_filt_conv_common_ctor,
80 	NULL,
81 	mbfl_filt_conv_byte2le_wchar,
82 	mbfl_filt_conv_common_flush,
83 	NULL,
84 };
85 
86 const struct mbfl_convert_vtbl vtbl_wchar_byte2le = {
87 	mbfl_no_encoding_wchar,
88 	mbfl_no_encoding_byte2le,
89 	mbfl_filt_conv_common_ctor,
90 	NULL,
91 	mbfl_filt_conv_wchar_byte2le,
92 	mbfl_filt_conv_common_flush,
93 	NULL,
94 };
95 
96 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
97 
mbfl_filt_conv_byte2be_wchar(int c,mbfl_convert_filter * filter)98 int mbfl_filt_conv_byte2be_wchar(int c, mbfl_convert_filter *filter)
99 {
100 	int n;
101 
102 	if (filter->status == 0) {
103 		filter->status = 1;
104 		n = (c & 0xff) << 8;
105 		filter->cache = n;
106 	} else {
107 		filter->status = 0;
108 		n = (c & 0xff) | filter->cache;
109 		CK((*filter->output_function)(n, filter->data));
110 	}
111 	return c;
112 }
113 
mbfl_filt_conv_wchar_byte2be(int c,mbfl_convert_filter * filter)114 int mbfl_filt_conv_wchar_byte2be(int c, mbfl_convert_filter *filter)
115 {
116 	CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
117 	CK((*filter->output_function)(c & 0xff, filter->data));
118 	return c;
119 }
120 
mbfl_filt_conv_byte2le_wchar(int c,mbfl_convert_filter * filter)121 int mbfl_filt_conv_byte2le_wchar(int c, mbfl_convert_filter *filter)
122 {
123 	int n;
124 
125 	if (filter->status == 0) {
126 		filter->status = 1;
127 		n = c & 0xff;
128 		filter->cache = n;
129 	} else {
130 		filter->status = 0;
131 		n = ((c & 0xff) << 8) | filter->cache;
132 		CK((*filter->output_function)(n, filter->data));
133 	}
134 	return c;
135 }
136 
mbfl_filt_conv_wchar_byte2le(int c,mbfl_convert_filter * filter)137 int mbfl_filt_conv_wchar_byte2le(int c, mbfl_convert_filter *filter)
138 {
139 	CK((*filter->output_function)(c & 0xff, filter->data));
140 	CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
141 	return c;
142 }
143