xref: /freebsd/lib/libkiconv/quirks.c (revision faf25f48d601ae39f5752602f3020e2e92605625)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Ryuichiro Imura
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 /*
32  * kiconv(3) requires shared linked, and reduce module size
33  * when statically linked.
34  */
35 
36 #ifdef PIC
37 
38 /*
39  * Why do we need quirks?
40  * Since each vendors has their own Unicode mapping rules,
41  * we need some quirks until iconv(3) supports them.
42  * We can define Microsoft mappings here.
43  *
44  * For example, the eucJP and Unocode mapping rule is based on
45  * the JIS standard. Since Microsoft uses cp932 for Unicode mapping
46  * which is not truly based on the JIS standard, reading a file
47  * system created by Microsoft Windows family using eucJP/Unicode
48  * mapping rule will cause a problem. That's why we define eucJP-ms here.
49  * The eucJP-ms has been defined by The Open Group Japan Vendor Council.
50  *
51  * Well, Apple Mac OS also has their own Unicode mappings,
52  * but we won't require these quirks here, because HFS doesn't have
53  * Unicode and HFS+ has decomposed Unicode which can not be
54  * handled by this xlat16 converter.
55  */
56 
57 #include <sys/types.h>
58 #include <sys/iconv.h>
59 
60 #include <stdio.h>
61 #include <string.h>
62 
63 #include "quirks.h"
64 
65 /*
66  * All lists of quirk character set
67  */
68 static struct {
69 	int vendor; /* reserved for non MS mapping */
70 	const char *base_codeset, *quirk_codeset;
71 } quirk_list[] = {
72 	{ KICONV_VENDOR_MICSFT,	"eucJP", "eucJP-ms" },
73 	{ KICONV_VENDOR_MICSFT,	"EUC-JP", "eucJP-ms" },
74 	{ KICONV_VENDOR_MICSFT,	"SJIS", "SJIS-ms" },
75 	{ KICONV_VENDOR_MICSFT,	"Shift_JIS", "SJIS-ms" },
76 	{ KICONV_VENDOR_MICSFT,	"Big5", "Big5-ms" }
77 };
78 
79 /*
80  * The character list to replace for Japanese MS-Windows.
81  */
82 static struct quirk_replace_list quirk_jis_cp932[] = {
83 	{ 0x00a2, 0xffe0 }, /* Cent Sign, Fullwidth Cent Sign */
84 	{ 0x00a3, 0xffe1 }, /* Pound Sign, Fullwidth Pound Sign */
85 	{ 0x00ac, 0xffe2 }, /* Not Sign, Fullwidth Not Sign */
86 	{ 0x2016, 0x2225 }, /* Double Vertical Line, Parallel To */
87 	{ 0x203e, 0x007e }, /* Overline, Tilde */
88 	{ 0x2212, 0xff0d }, /* Minus Sign, Fullwidth Hyphenminus */
89 	{ 0x301c, 0xff5e }  /* Wave Dash, Fullwidth Tilde */
90 };
91 
92 /*
93  * All entries of quirks
94  */
95 #define	NumOf(n)	(sizeof((n)) / sizeof((n)[0]))
96 static struct {
97 	const char *quirk_codeset, *iconv_codeset, *pair_codeset;
98 	struct quirk_replace_list (*replace_list)[];
99 	size_t num_of_replaces;
100 } quirk_table[] = {
101 	{
102 		"eucJP-ms", "eucJP", ENCODING_UNICODE,
103 		(struct quirk_replace_list (*)[])&quirk_jis_cp932,
104 		NumOf(quirk_jis_cp932)
105 	},
106 	{
107 		"SJIS-ms", "CP932", ENCODING_UNICODE,
108 		/* XXX - quirk_replace_list should be NULL */
109 		(struct quirk_replace_list (*)[])&quirk_jis_cp932,
110 		NumOf(quirk_jis_cp932)
111 	},
112 	{
113 		"Big5-ms", "CP950", ENCODING_UNICODE,
114 		NULL, 0
115 	}
116 };
117 
118 
119 const char *
120 kiconv_quirkcs(const char* base, int vendor)
121 {
122 	size_t i;
123 
124 	/*
125 	 * We should compare codeset names ignoring case here,
126 	 * so that quirk could be used for all of the user input
127 	 * patterns.
128 	 */
129 	for (i = 0; i < NumOf(quirk_list); i++)
130 		if (quirk_list[i].vendor == vendor &&
131 		    strcasecmp(quirk_list[i].base_codeset, base) == 0)
132 			return (quirk_list[i].quirk_codeset);
133 
134 	return (base);
135 }
136 
137 /*
138  * Internal Functions
139  */
140 const char *
141 search_quirk(const char *given_codeset,
142 	     const char *pair_codeset,
143 	     struct quirk_replace_list **replace_list,
144 	     size_t *num_of_replaces)
145 {
146 	size_t i;
147 
148 	*replace_list = NULL;
149 	*num_of_replaces = 0;
150 	for (i = 0; i < NumOf(quirk_table); i++)
151 		if (strcmp(quirk_table[i].quirk_codeset, given_codeset) == 0) {
152 			if (strcmp(quirk_table[i].pair_codeset, pair_codeset) == 0) {
153 				*replace_list = *quirk_table[i].replace_list;
154 				*num_of_replaces = quirk_table[i].num_of_replaces;
155 			}
156 			return (quirk_table[i].iconv_codeset);
157 		}
158 
159 	return (given_codeset);
160 }
161 
162 uint16_t
163 quirk_vendor2unix(uint16_t c, struct quirk_replace_list *replace_list, size_t num)
164 {
165 	size_t i;
166 
167 	for (i = 0; i < num; i++)
168 		if (replace_list[i].vendor_code == c)
169 			return (replace_list[i].standard_code);
170 
171 	return (c);
172 }
173 
174 uint16_t
175 quirk_unix2vendor(uint16_t c, struct quirk_replace_list *replace_list, size_t num)
176 {
177 	size_t i;
178 
179 	for (i = 0; i < num; i++)
180 		if (replace_list[i].standard_code == c)
181 			return (replace_list[i].vendor_code);
182 
183 	return (c);
184 }
185 
186 #else /* statically linked */
187 
188 #include <sys/types.h>
189 #include <sys/iconv.h>
190 
191 const char *
192 kiconv_quirkcs(const char* base __unused, int vendor __unused)
193 {
194 
195 	return (base);
196 }
197 
198 #endif /* PIC */
199