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_ascii.h"
33 
34 static int mbfl_filt_ident_ascii(int c, mbfl_identify_filter *filter);
35 
36 static const char *mbfl_encoding_ascii_aliases[] = {"ANSI_X3.4-1968", "iso-ir-6", "ANSI_X3.4-1986", "ISO_646.irv:1991", "US-ASCII", "ISO646-US", "us", "IBM367", "IBM-367", "cp367", "csASCII", NULL};
37 
38 const mbfl_encoding mbfl_encoding_ascii = {
39 	mbfl_no_encoding_ascii,
40 	"ASCII",
41 	"US-ASCII", /* preferred MIME name */
42 	(const char *(*)[])&mbfl_encoding_ascii_aliases,
43 	NULL,
44 	MBFL_ENCTYPE_SBCS,
45 	&vtbl_ascii_wchar,
46 	&vtbl_wchar_ascii
47 };
48 
49 const struct mbfl_identify_vtbl vtbl_identify_ascii = {
50 	mbfl_no_encoding_ascii,
51 	mbfl_filt_ident_common_ctor,
52 	mbfl_filt_ident_ascii
53 };
54 
55 const struct mbfl_convert_vtbl vtbl_ascii_wchar = {
56 	mbfl_no_encoding_ascii,
57 	mbfl_no_encoding_wchar,
58 	mbfl_filt_conv_common_ctor,
59 	NULL,
60 	mbfl_filt_conv_ascii_wchar,
61 	mbfl_filt_conv_common_flush,
62 	NULL,
63 };
64 
65 const struct mbfl_convert_vtbl vtbl_wchar_ascii = {
66 	mbfl_no_encoding_wchar,
67 	mbfl_no_encoding_ascii,
68 	mbfl_filt_conv_common_ctor,
69 	NULL,
70 	mbfl_filt_conv_wchar_ascii,
71 	mbfl_filt_conv_common_flush,
72 	NULL,
73 };
74 
75 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
76 
77 /*
78  * ASCII => wchar
79  */
mbfl_filt_conv_ascii_wchar(int c,mbfl_convert_filter * filter)80 int mbfl_filt_conv_ascii_wchar(int c, mbfl_convert_filter *filter)
81 {
82 	return (*filter->output_function)(c, filter->data);
83 }
84 
85 
86 /*
87  * wchar => ASCII
88  */
mbfl_filt_conv_wchar_ascii(int c,mbfl_convert_filter * filter)89 int mbfl_filt_conv_wchar_ascii(int c, mbfl_convert_filter *filter)
90 {
91 	if (c >= 0 && c < 0x80) {
92 		CK((*filter->output_function)(c, filter->data));
93 	} else {
94 		CK(mbfl_filt_conv_illegal_output(c, filter));
95 	}
96 
97 	return c;
98 }
99 
mbfl_filt_ident_ascii(int c,mbfl_identify_filter * filter)100 static int mbfl_filt_ident_ascii(int c, mbfl_identify_filter *filter)
101 {
102 	if (c >= 0x20 && c < 0x80) {
103 		;
104 	} else if (c == 0x0d || c == 0x0a || c == 0x09 || c == 0) {	/* CR or LF or HTAB or null */
105 		;
106 	} else {
107 		filter->flag = 1;
108 	}
109 
110 	return c;
111 }
112