2 * Copyright (c) 2003 Ryuichiro Imura
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/lib/libkiconv/quirks.c,v 1.1.30.1 2009/04/15 03:14:26 kensmith Exp $
30 * Why do we need quirks?
31 * Since each vendors has their own Unicode mapping rules,
32 * we need some quirks until iconv(3) supports them.
33 * We can define Microsoft mappings here.
35 * For example, the eucJP and Unocode mapping rule is based on
36 * the JIS standard. Since Microsoft uses cp932 for Unicode mapping
37 * witch is not truly based on the JIS standard, reading a file
38 * system created by Microsoft Windows family using eucJP/Unicode
39 * mapping rule will cause a problem. That's why we define eucJP-ms here.
40 * The eucJP-ms has been defined by The Open Group Japan Vendor Coucil.
42 * Well, Apple Mac OS also has their own Unicode mappings,
43 * but we won't require these quirks here, because HFS doesn't have
44 * Unicode and HFS+ has decomposed Unicode which can not be
45 * handled by this xlat16 converter.
48 #include <sys/types.h>
49 #include <sys/iconv.h>
57 * All lists of quirk character set
60 int vendor
; /* reserved for non MS mapping */
61 const char *base_codeset
, *quirk_codeset
;
63 { KICONV_VENDOR_MICSFT
, "eucJP", "eucJP-ms" },
64 { KICONV_VENDOR_MICSFT
, "EUC-JP", "eucJP-ms" },
65 { KICONV_VENDOR_MICSFT
, "SJIS", "SJIS-ms" },
66 { KICONV_VENDOR_MICSFT
, "Shift_JIS", "SJIS-ms" },
67 { KICONV_VENDOR_MICSFT
, "Big5", "Big5-ms" }
71 * The character list to replace for Japanese MS-Windows.
73 static struct quirk_replace_list quirk_jis_cp932
[] = {
74 { 0x00a2, 0xffe0 }, /* Cent Sign, Fullwidth Cent Sign */
75 { 0x00a3, 0xffe1 }, /* Pound Sign, Fullwidth Pound Sign */
76 { 0x00ac, 0xffe2 }, /* Not Sign, Fullwidth Not Sign */
77 { 0x2016, 0x2225 }, /* Double Vertical Line, Parallel To */
78 { 0x203e, 0x007e }, /* Overline, Tilde */
79 { 0x2212, 0xff0d }, /* Minus Sign, Fullwidth Hyphenminus */
80 { 0x301c, 0xff5e } /* Wave Dash, Fullwidth Tilde */
84 * All entries of quirks
86 #define NumOf(n) (sizeof((n)) / sizeof((n)[0]))
88 const char *quirk_codeset
, *iconv_codeset
, *pair_codeset
;
89 struct quirk_replace_list (*replace_list
)[];
90 size_t num_of_replaces
;
93 "eucJP-ms", "eucJP", ENCODING_UNICODE
,
94 (struct quirk_replace_list (*)[])&quirk_jis_cp932
,
95 NumOf(quirk_jis_cp932
)
98 "SJIS-ms", "CP932", ENCODING_UNICODE
,
99 /* XXX - quirk_replace_list should be NULL */
100 (struct quirk_replace_list (*)[])&quirk_jis_cp932
,
101 NumOf(quirk_jis_cp932
)
104 "Big5-ms", "CP950", ENCODING_UNICODE
,
111 kiconv_quirkcs(const char* base
, int vendor
)
116 * We should compare codeset names ignoring case here,
117 * so that quirk could be used for all of the user input
120 for (i
= 0; i
< NumOf(quirk_list
); i
++)
121 if (quirk_list
[i
].vendor
== vendor
&&
122 strcasecmp(quirk_list
[i
].base_codeset
, base
) == 0)
123 return (quirk_list
[i
].quirk_codeset
);
132 search_quirk(const char *given_codeset
,
133 const char *pair_codeset
,
134 struct quirk_replace_list
**replace_list
,
135 size_t *num_of_replaces
)
139 *replace_list
= NULL
;
140 *num_of_replaces
= 0;
141 for (i
= 0; i
< NumOf(quirk_table
); i
++)
142 if (strcmp(quirk_table
[i
].quirk_codeset
, given_codeset
) == 0) {
143 if (strcmp(quirk_table
[i
].pair_codeset
, pair_codeset
) == 0) {
144 *replace_list
= *quirk_table
[i
].replace_list
;
145 *num_of_replaces
= quirk_table
[i
].num_of_replaces
;
147 return (quirk_table
[i
].iconv_codeset
);
150 return (given_codeset
);
154 quirk_vendor2unix(uint16_t c
, struct quirk_replace_list
*replace_list
, size_t num
)
158 for (i
= 0; i
< num
; i
++)
159 if (replace_list
[i
].vendor_code
== c
)
160 return (replace_list
[i
].standard_code
);
166 quirk_unix2vendor(uint16_t c
, struct quirk_replace_list
*replace_list
, size_t num
)
170 for (i
= 0; i
< num
; i
++)
171 if (replace_list
[i
].standard_code
== c
)
172 return (replace_list
[i
].vendor_code
);