aboutsummaryrefslogtreecommitdiff
path: root/lib/libkiconv/quirks.c
blob: e2bb1e66beb145cfaafce73222406e1368430306 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2003 Ryuichiro Imura
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/*
 * kiconv(3) requires shared linked, and reduce module size
 * when statically linked.
 */

#ifdef PIC

/*
 * Why do we need quirks?
 * Since each vendors has their own Unicode mapping rules,
 * we need some quirks until iconv(3) supports them.
 * We can define Microsoft mappings here.
 * 
 * For example, the eucJP and Unocode mapping rule is based on
 * the JIS standard. Since Microsoft uses cp932 for Unicode mapping
 * which is not truly based on the JIS standard, reading a file
 * system created by Microsoft Windows family using eucJP/Unicode
 * mapping rule will cause a problem. That's why we define eucJP-ms here.
 * The eucJP-ms has been defined by The Open Group Japan Vendor Council.
 *
 * Well, Apple Mac OS also has their own Unicode mappings,
 * but we won't require these quirks here, because HFS doesn't have
 * Unicode and HFS+ has decomposed Unicode which can not be
 * handled by this xlat16 converter.
 */

#include <sys/types.h>
#include <sys/iconv.h>

#include <stdio.h>
#include <string.h>

#include "quirks.h"

/*
 * All lists of quirk character set
 */
static struct {
	int vendor; /* reserved for non MS mapping */
	const char *base_codeset, *quirk_codeset;
} quirk_list[] = {
	{ KICONV_VENDOR_MICSFT,	"eucJP", "eucJP-ms" },
	{ KICONV_VENDOR_MICSFT,	"EUC-JP", "eucJP-ms" },
	{ KICONV_VENDOR_MICSFT,	"SJIS", "SJIS-ms" },
	{ KICONV_VENDOR_MICSFT,	"Shift_JIS", "SJIS-ms" },
	{ KICONV_VENDOR_MICSFT,	"Big5", "Big5-ms" }
};

/*
 * The character list to replace for Japanese MS-Windows.
 */
static struct quirk_replace_list quirk_jis_cp932[] = {
	{ 0x00a2, 0xffe0 }, /* Cent Sign, Fullwidth Cent Sign */
	{ 0x00a3, 0xffe1 }, /* Pound Sign, Fullwidth Pound Sign */
	{ 0x00ac, 0xffe2 }, /* Not Sign, Fullwidth Not Sign */
	{ 0x2016, 0x2225 }, /* Double Vertical Line, Parallel To */
	{ 0x203e, 0x007e }, /* Overline, Tilde */
	{ 0x2212, 0xff0d }, /* Minus Sign, Fullwidth Hyphenminus */
	{ 0x301c, 0xff5e }  /* Wave Dash, Fullwidth Tilde */
};

/*
 * All entries of quirks
 */
#define	NumOf(n)	(sizeof((n)) / sizeof((n)[0]))
static struct {
	const char *quirk_codeset, *iconv_codeset, *pair_codeset;
	struct quirk_replace_list (*replace_list)[];
	size_t num_of_replaces;
} quirk_table[] = {
	{
		"eucJP-ms", "eucJP", ENCODING_UNICODE,
		(struct quirk_replace_list (*)[])&quirk_jis_cp932,
		NumOf(quirk_jis_cp932)
	},
	{
		"SJIS-ms", "CP932", ENCODING_UNICODE,
		/* XXX - quirk_replace_list should be NULL */
		(struct quirk_replace_list (*)[])&quirk_jis_cp932,
		NumOf(quirk_jis_cp932)
	},
	{
		"Big5-ms", "CP950", ENCODING_UNICODE,
		NULL, 0
	}
};


const char *
kiconv_quirkcs(const char* base, int vendor)
{
	size_t i;

	/*
	 * We should compare codeset names ignoring case here,
	 * so that quirk could be used for all of the user input
	 * patterns.
	 */
	for (i = 0; i < NumOf(quirk_list); i++)
		if (quirk_list[i].vendor == vendor &&
		    strcasecmp(quirk_list[i].base_codeset, base) == 0)
			return (quirk_list[i].quirk_codeset);

	return (base);
}

/*
 * Internal Functions
 */
const char *
search_quirk(const char *given_codeset,
	     const char *pair_codeset,
	     struct quirk_replace_list **replace_list,
	     size_t *num_of_replaces)
{
	size_t i;

	*replace_list = NULL;
	*num_of_replaces = 0;
	for (i = 0; i < NumOf(quirk_table); i++)
		if (strcmp(quirk_table[i].quirk_codeset, given_codeset) == 0) {
			if (strcmp(quirk_table[i].pair_codeset, pair_codeset) == 0) {
				*replace_list = *quirk_table[i].replace_list;
				*num_of_replaces = quirk_table[i].num_of_replaces;
			}
			return (quirk_table[i].iconv_codeset);
		}

	return (given_codeset);
}

uint16_t
quirk_vendor2unix(uint16_t c, struct quirk_replace_list *replace_list, size_t num)
{
	size_t i;

	for (i = 0; i < num; i++)
		if (replace_list[i].vendor_code == c)
			return (replace_list[i].standard_code);

	return (c);
}

uint16_t
quirk_unix2vendor(uint16_t c, struct quirk_replace_list *replace_list, size_t num)
{
	size_t i;

	for (i = 0; i < num; i++)
		if (replace_list[i].standard_code == c)
			return (replace_list[i].vendor_code);

	return (c);
}

#else /* statically linked */

#include <sys/types.h>
#include <sys/iconv.h>

const char *
kiconv_quirkcs(const char* base __unused, int vendor __unused)
{

	return (base);
}

#endif /* PIC */