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