2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Ryuichiro Imura
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 * kiconv(3) requires shared linked, and reduce module size
33 * when statically linked.
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.
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.
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.
57 #include <sys/types.h>
58 #include <sys/iconv.h>
66 * All lists of quirk character set
69 int vendor; /* reserved for non MS mapping */
70 const char *base_codeset, *quirk_codeset;
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" }
80 * The character list to replace for Japanese MS-Windows.
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 */
93 * All entries of quirks
95 #define NumOf(n) (sizeof((n)) / sizeof((n)[0]))
97 const char *quirk_codeset, *iconv_codeset, *pair_codeset;
98 struct quirk_replace_list (*replace_list)[];
99 size_t num_of_replaces;
102 "eucJP-ms", "eucJP", ENCODING_UNICODE,
103 (struct quirk_replace_list (*)[])&quirk_jis_cp932,
104 NumOf(quirk_jis_cp932)
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)
113 "Big5-ms", "CP950", ENCODING_UNICODE,
120 kiconv_quirkcs(const char* base, int vendor)
125 * We should compare codeset names ignoring case here,
126 * so that quirk could be used for all of the user input
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);
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)
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;
156 return (quirk_table[i].iconv_codeset);
159 return (given_codeset);
163 quirk_vendor2unix(uint16_t c, struct quirk_replace_list *replace_list, size_t num)
167 for (i = 0; i < num; i++)
168 if (replace_list[i].vendor_code == c)
169 return (replace_list[i].standard_code);
175 quirk_unix2vendor(uint16_t c, struct quirk_replace_list *replace_list, size_t num)
179 for (i = 0; i < num; i++)
180 if (replace_list[i].standard_code == c)
181 return (replace_list[i].vendor_code);
186 #else /* statically linked */
188 #include <sys/types.h>
189 #include <sys/iconv.h>
192 kiconv_quirkcs(const char* base __unused, int vendor __unused)