1 /*- 2 * Copyright (c) 2003 Ryuichiro Imura 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/lib/libkiconv/quirks.c,v 1.1.30.1 2009/04/15 03:14:26 kensmith Exp $ 27 */ 28 29 /* 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. 34 * 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. 41 * 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. 46 */ 47 48 #include <sys/types.h> 49 #include <sys/iconv.h> 50 51 #include <stdio.h> 52 #include <string.h> 53 54 #include "quirks.h" 55 56 /* 57 * All lists of quirk character set 58 */ 59 static struct { 60 int vendor; /* reserved for non MS mapping */ 61 const char *base_codeset, *quirk_codeset; 62 } quirk_list[] = { 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" } 68 }; 69 70 /* 71 * The character list to replace for Japanese MS-Windows. 72 */ 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 */ 81 }; 82 83 /* 84 * All entries of quirks 85 */ 86 #define NumOf(n) (sizeof((n)) / sizeof((n)[0])) 87 static struct { 88 const char *quirk_codeset, *iconv_codeset, *pair_codeset; 89 struct quirk_replace_list (*replace_list)[]; 90 size_t num_of_replaces; 91 } quirk_table[] = { 92 { 93 "eucJP-ms", "eucJP", ENCODING_UNICODE, 94 (struct quirk_replace_list (*)[])&quirk_jis_cp932, 95 NumOf(quirk_jis_cp932) 96 }, 97 { 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) 102 }, 103 { 104 "Big5-ms", "CP950", ENCODING_UNICODE, 105 NULL, 0 106 } 107 }; 108 109 110 const char * 111 kiconv_quirkcs(const char* base, int vendor) 112 { 113 size_t i; 114 115 /* 116 * We should compare codeset names ignoring case here, 117 * so that quirk could be used for all of the user input 118 * patterns. 119 */ 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); 124 125 return (base); 126 } 127 128 /* 129 * Internal Functions 130 */ 131 const char * 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) 136 { 137 size_t i; 138 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; 146 } 147 return (quirk_table[i].iconv_codeset); 148 } 149 150 return (given_codeset); 151 } 152 153 uint16_t 154 quirk_vendor2unix(uint16_t c, struct quirk_replace_list *replace_list, size_t num) 155 { 156 size_t i; 157 158 for (i = 0; i < num; i++) 159 if (replace_list[i].vendor_code == c) 160 return (replace_list[i].standard_code); 161 162 return (c); 163 } 164 165 uint16_t 166 quirk_unix2vendor(uint16_t c, struct quirk_replace_list *replace_list, size_t num) 167 { 168 size_t i; 169 170 for (i = 0; i < num; i++) 171 if (replace_list[i].standard_code == c) 172 return (replace_list[i].vendor_code); 173 174 return (c); 175 } 176