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