PageRenderTime 143ms CodeModel.GetById 22ms app.highlight 94ms RepoModel.GetById 19ms app.codeStats 0ms

/fs/nls/nls_euc-jp.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 581 lines | 491 code | 54 blank | 36 comment | 115 complexity | 05d7d4d0b6d9689ca980a7b1cee69c5c MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1/*
  2 * linux/fs/nls/nls_euc-jp.c
  3 *
  4 * Added `OSF/JVC Recommended Code Set Conversion Specification
  5 * between Japanese EUC and Shift-JIS' support: <hirofumi@mail.parknet.co.jp>
  6 * (http://www.opengroup.or.jp/jvc/cde/sjis-euc-e.html)
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/string.h>
 12#include <linux/nls.h>
 13#include <linux/errno.h>
 14
 15static struct nls_table *p_nls;
 16
 17#define IS_SJIS_LOW_BYTE(l)	((0x40 <= (l)) && ((l) <= 0xFC) && ((l) != 0x7F))
 18/* JIS X 0208 (include NEC spesial characters) */
 19#define IS_SJIS_JISX0208(h, l)	((((0x81 <= (h)) && ((h) <= 0x9F))	\
 20				 || ((0xE0 <= (h)) && ((h) <= 0xEA)))	\
 21				 && IS_SJIS_LOW_BYTE(l))
 22#define IS_SJIS_JISX0201KANA(c)	((0xA1 <= (c)) && ((c) <= 0xDF))
 23#define IS_SJIS_UDC_LOW(h, l)	(((0xF0 <= (h)) && ((h) <= 0xF4))	\
 24				 && IS_SJIS_LOW_BYTE(l))
 25#define IS_SJIS_UDC_HI(h, l)	(((0xF5 <= (h)) && ((h) <= 0xF9))	\
 26				 && IS_SJIS_LOW_BYTE(l))
 27#define IS_SJIS_IBM(h, l)	(((0xFA <= (h)) && ((h) <= 0xFC))	\
 28				 && IS_SJIS_LOW_BYTE(l))
 29#define IS_SJIS_NECIBM(h, l)	(((0xED <= (h)) && ((h) <= 0xEE))	\
 30				 && IS_SJIS_LOW_BYTE(l))
 31#define MAP_SJIS2EUC(sjis_hi, sjis_lo, sjis_p, euc_hi, euc_lo, euc_p) {		\
 32	if ((sjis_lo) >= 0x9F) {						\
 33		(euc_hi) = (sjis_hi) * 2 - (((sjis_p) * 2 - (euc_p)) - 1);	\
 34		(euc_lo) = (sjis_lo) + 2;					\
 35	} else {								\
 36		(euc_hi) = (sjis_hi) * 2 - ((sjis_p) * 2 - (euc_p));		\
 37		(euc_lo) = (sjis_lo) + ((sjis_lo) >= 0x7F ? 0x60 : 0x61);	\
 38	}									\
 39} while(0)
 40
 41#define SS2		(0x8E)		/* Single Shift 2 */
 42#define SS3		(0x8F)		/* Single Shift 3 */
 43#define IS_EUC_BYTE(c)		((0xA1 <= (c)) && ((c) <= 0xFE))
 44#define IS_EUC_JISX0208(h, l)	(IS_EUC_BYTE(h) && IS_EUC_BYTE(l))
 45#define IS_EUC_JISX0201KANA(h, l)	(((h) == SS2) && (0xA1 <= (l) && (l) <= 0xDF))
 46#define IS_EUC_UDC_LOW(h, l)	(((0xF5 <= (h)) && ((h) <= 0xFE))	\
 47				 && IS_EUC_BYTE(l))
 48#define IS_EUC_UDC_HI(h, l)	IS_EUC_UDC_LOW(h, l) /* G3 block */
 49#define MAP_EUC2SJIS(euc_hi, euc_lo, euc_p, sjis_hi, sjis_lo, sjis_p) {		\
 50	if ((euc_hi) & 1) {							\
 51		(sjis_hi) = (euc_hi) / 2 + ((sjis_p) - (euc_p) / 2);		\
 52		(sjis_lo) = (euc_lo) - ((euc_lo) >= 0xE0 ? 0x60 : 0x61);	\
 53	} else {								\
 54		(sjis_hi) = (euc_hi) / 2 + (((sjis_p) - (euc_p) / 2) - 1);	\
 55		(sjis_lo) = (euc_lo) - 2;					\
 56	}									\
 57} while(0)
 58
 59/* SJIS IBM extended characters to EUC map */
 60static const unsigned char sjisibm2euc_map[][2] = {
 61	{0xF3, 0xF3}, {0xF3, 0xF4}, {0xF3, 0xF5}, {0xF3, 0xF6}, {0xF3, 0xF7},
 62	{0xF3, 0xF8}, {0xF3, 0xF9}, {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{0xCC, 0xD9},
108	{0xF4, 0xD0}, {0xCD, 0xBB}, {0xF4, 0xD1}, {0xCE, 0xBB}, {0xF4, 0xD2},
109	{0xCE, 0xBA}, {0xCE, 0xC3}, {0xF4, 0xD3}, {0xCE, 0xF2}, {0xB3, 0xDD},
110	{0xCF, 0xD5}, {0xCF, 0xE2}, {0xCF, 0xE9}, {0xCF, 0xED}, {0xF4, 0xD4},
111	{0xF4, 0xD5}, {0xF4, 0xD6}, {0x00, 0x00}, {0xF4, 0xD7}, {0xD0, 0xE5},
112	{0xF4, 0xD8}, {0xD0, 0xE9}, {0xD1, 0xE8}, {0xF4, 0xD9}, {0xF4, 0xDA},
113	{0xD1, 0xEC}, {0xD2, 0xBB}, {0xF4, 0xDB}, {0xD3, 0xE1}, {0xD3, 0xE8},
114	{0xD4, 0xA7}, {0xF4, 0xDC}, {0xF4, 0xDD}, {0xD4, 0xD4}, {0xD4, 0xF2},
115	{0xD5, 0xAE}, {0xF4, 0xDE}, {0xD7, 0xDE}, {0xF4, 0xDF}, {0xD8, 0xA2},
116	{0xD8, 0xB7}, {0xD8, 0xC1}, {0xD8, 0xD1}, {0xD8, 0xF4}, {0xD9, 0xC6},
117	{0xD9, 0xC8}, {0xD9, 0xD1}, {0xF4, 0xE0}, {0xF4, 0xE1}, {0xF4, 0xE2},
118	{0xF4, 0xE3}, {0xF4, 0xE4}, {0xDC, 0xD3}, {0xDD, 0xC8}, {0xDD, 0xD4},
119	{0xDD, 0xEA}, {0xDD, 0xFA}, {0xDE, 0xA4}, {0xDE, 0xB0}, {0xF4, 0xE5},
120	{0xDE, 0xB5}, {0xDE, 0xCB}, {0xF4, 0xE6}, {0xDF, 0xB9}, {0xF4, 0xE7},
121	{0xDF, 0xC3}, {0xF4, 0xE8}, {0xF4, 0xE9}, {0xE0, 0xD9}, {0xF4, 0xEA},
122	{0xF4, 0xEB}, {0xE1, 0xE2}, {0xF4, 0xEC}, {0xF4, 0xED}, {0xF4, 0xEE},
123	{0xE2, 0xC7}, {0xE3, 0xA8}, {0xE3, 0xA6}, {0xE3, 0xA9}, {0xE3, 0xAF},
124	{0xE3, 0xB0}, {0xE3, 0xAA}, {0xE3, 0xAB}, {0xE3, 0xBC}, {0xE3, 0xC1},
125	{0xE3, 0xBF}, {0xE3, 0xD5}, {0xE3, 0xD8}, {0xE3, 0xD6}, {0xE3, 0xDF},
126	{0xE3, 0xE3}, {0xE3, 0xE1}, {0xE3, 0xD4}, {0xE3, 0xE9}, {0xE4, 0xA6},
127	{0xE3, 0xF1}, {0xE3, 0xF2}, {0xE4, 0xCB}, {0xE4, 0xC1}, {0xE4, 0xC3},
128	{0xE4, 0xBE}, {0xF4, 0xEF}, {0xE4, 0xC0}, {0xE4, 0xC7}, {0xE4, 0xBF},
129	{0xE4, 0xE0}, {0xE4, 0xDE}, {0xE4, 0xD1}, {0xF4, 0xF0}, {0xE4, 0xDC},
130	{0xE4, 0xD2}, {0xE4, 0xDB}, {0xE4, 0xD4}, {0xE4, 0xFA}, {0xE4, 0xEF},
131	{0xE5, 0xB3}, {0xE5, 0xBF}, {0xE5, 0xC9}, {0xE5, 0xD0}, {0xE5, 0xE2},
132	{0xE5, 0xEA}, {0xE5, 0xEB}, {0xF4, 0xF1}, {0xF4, 0xF2}, {0xF4, 0xF3},
133	{0xE6, 0xE8}, {0xE6, 0xEF}, {0xE7, 0xAC}, {0xF4, 0xF4}, {0xE7, 0xAE},
134	{0xF4, 0xF5}, {0xE7, 0xB1}, {0xF4, 0xF6}, {0xE7, 0xB2}, {0xE8, 0xB1},
135	{0xE8, 0xB6}, {0xF4, 0xF7}, {0xF4, 0xF8}, {0xE8, 0xDD}, {0xF4, 0xF9},
136	{0xF4, 0xFA}, {0xE9, 0xD1}, {0xF4, 0xFB}, {0xE9, 0xED}, {0xEA, 0xCD},
137	{0xF4, 0xFC}, {0xEA, 0xDB}, {0xEA, 0xE6}, {0xEA, 0xEA}, {0xEB, 0xA5},
138	{0xEB, 0xFB}, {0xEB, 0xFA}, {0xF4, 0xFD}, {0xEC, 0xD6}, {0xF4, 0xFE},
139};
140
141#define IS_EUC_IBM2JISX0208(h, l) \
142		(((h) == 0xA2 && (l) == 0xCC) || ((h) == 0xA2 && (l) == 0xE8))
143
144/* EUC to SJIS IBM extended characters map (G3 JIS X 0212 block) */
145static struct {
146	unsigned short euc;
147	unsigned char sjis[2];
148} euc2sjisibm_jisx0212_map[] = {
149	{0xA2C3, {0xFA, 0x55}}, {0xB0A9, {0xFA, 0x68}}, {0xB0C8, {0xFA, 0x69}},
150	{0xB0D2, {0xFA, 0x6B}}, {0xB0D4, {0xFA, 0x6C}}, {0xB0E3, {0xFA, 0x6D}},
151	{0xB0EE, {0xFA, 0x6E}}, {0xB1A3, {0xFA, 0x70}}, {0xB1A7, {0xFA, 0x6F}},
152	{0xB1A9, {0xFA, 0x72}}, {0xB1AC, {0xFA, 0x71}}, {0xB1BB, {0xFA, 0x61}},
153	{0xB1BE, {0xFA, 0x73}}, {0xB1C8, {0xFA, 0x76}}, {0xB1D7, {0xFA, 0x77}},
154	{0xB1D8, {0xFA, 0x75}}, {0xB1DF, {0xFA, 0x74}}, {0xB1E1, {0xFA, 0x7A}},
155	{0xB1E3, {0xFA, 0x78}}, {0xB1F4, {0xFA, 0x79}}, {0xB2A3, {0xFA, 0x7B}},
156	{0xB2BB, {0xFA, 0x7D}}, {0xB2E6, {0xFA, 0x7E}}, {0xB2ED, {0xFA, 0x80}},
157	{0xB2F5, {0xFA, 0x81}}, {0xB2FC, {0xFA, 0x82}}, {0xB3B5, {0xFA, 0x84}},
158	{0xB3D8, {0xFA, 0x85}}, {0xB3DB, {0xFA, 0x86}}, {0xB3DD, {0xFB, 0x77}},
159	{0xB3E5, {0xFA, 0x87}}, {0xB3EE, {0xFA, 0x88}}, {0xB3FB, {0xFA, 0x89}},
160	{0xB4C0, {0xFA, 0x8C}}, {0xB4C7, {0xFA, 0x8D}}, {0xB4D0, {0xFA, 0x8E}},
161	{0xB4DE, {0xFA, 0x8F}}, {0xB5AA, {0xFA, 0x91}}, {0xB5AF, {0xFA, 0x93}},
162	{0xB5C4, {0xFA, 0x94}}, {0xB5E8, {0xFA, 0x95}}, {0xB7C2, {0xFA, 0x97}},
163	{0xB7E4, {0xFA, 0x98}}, {0xB7E7, {0xFA, 0x9A}}, {0xB7E8, {0xFA, 0x99}},
164	{0xB8CE, {0xFA, 0x9E}}, {0xB8E1, {0xFA, 0x9F}}, {0xB8F5, {0xFA, 0xA0}},
165	{0xB8F7, {0xFA, 0xA1}}, {0xB8F8, {0xFA, 0xA2}}, {0xB8FC, {0xFA, 0xA3}},
166	{0xB9AF, {0xFA, 0xA4}}, {0xB9B7, {0xFA, 0xA5}}, {0xBABE, {0xFA, 0xA6}},
167	{0xBADB, {0xFA, 0xA7}}, {0xBAE1, {0xFA, 0xA9}}, {0xBAEB, {0xFA, 0xAB}},
168	{0xBBB3, {0xFA, 0xAC}}, {0xBBB8, {0xFA, 0xAD}}, {0xBBCA, {0xFA, 0xAF}},
169	{0xBBD0, {0xFA, 0xB2}}, {0xBBDE, {0xFA, 0xB3}}, {0xBBF4, {0xFA, 0xB4}},
170	{0xBBF5, {0xFA, 0xB5}}, {0xBBF9, {0xFA, 0xB6}}, {0xBCE4, {0xFA, 0xB7}},
171	{0xBCED, {0xFA, 0xB8}}, {0xBCF4, {0xFA, 0x67}}, {0xBCFE, {0xFA, 0xB9}},
172	{0xBDC2, {0xFA, 0xBB}}, {0xBDE7, {0xFA, 0xBC}}, {0xBDF0, {0xFA, 0xBE}},
173	{0xBEAC, {0xFA, 0xC0}}, {0xBEB0, {0xFA, 0xBF}}, {0xBEB3, {0xFA, 0xC2}},
174	{0xBEBD, {0xFA, 0xC3}}, {0xBEC9, {0xFA, 0xC5}}, {0xBECD, {0xFA, 0xC4}},
175	{0xBEE4, {0xFA, 0xC6}}, {0xBFA8, {0xFA, 0xC7}}, {0xBFC9, {0xFA, 0xC8}},
176	{0xC0C4, {0xFA, 0xC9}}, {0xC0E4, {0xFA, 0xCA}}, {0xC0F4, {0xFA, 0xCB}},
177	{0xC1A6, {0xFA, 0xCC}}, {0xC1F5, {0xFA, 0xCE}}, {0xC1F8, {0xFA, 0xD1}},
178	{0xC1FC, {0xFA, 0xCF}}, {0xC2A1, {0xFA, 0xD3}}, {0xC2A5, {0xFA, 0xD4}},
179	{0xC2AB, {0xFA, 0xD2}}, {0xC2AD, {0xFA, 0x63}}, {0xC2B8, {0xFA, 0xD6}},
180	{0xC2BA, {0xFA, 0xD7}}, {0xC2BF, {0xFA, 0x66}}, {0xC2C4, {0xFA, 0xD9}},
181	{0xC2D2, {0xFA, 0xDA}}, {0xC2D7, {0xFA, 0xDB}}, {0xC2DB, {0xFA, 0xDC}},
182	{0xC2DE, {0xFA, 0xDD}}, {0xC2ED, {0xFA, 0xDE}}, {0xC2F0, {0xFA, 0xDF}},
183	{0xC3A1, {0xFA, 0xE1}}, {0xC3B5, {0xFA, 0xE2}}, {0xC3B9, {0xFA, 0xE4}},
184	{0xC3C9, {0xFA, 0xE3}}, {0xC3D8, {0xFA, 0xE6}}, {0xC3FC, {0xFA, 0x64}},
185	{0xC3FE, {0xFA, 0xE7}}, {0xC4CC, {0xFA, 0xE9}}, {0xC4D9, {0xFA, 0xEB}},
186	{0xC4EA, {0xFA, 0xEC}}, {0xC4FD, {0xFA, 0xED}}, {0xC5A7, {0xFA, 0xEF}},
187	{0xC5B5, {0xFA, 0xF0}}, {0xC5B6, {0xFA, 0xF1}}, {0xC5D5, {0xFA, 0xF3}},
188	{0xC6B8, {0xFA, 0xF4}}, {0xC6D7, {0xFA, 0xF5}}, {0xC6E0, {0xFA, 0xF6}},
189	{0xC6E3, {0xFA, 0xF8}}, {0xC6EA, {0xFA, 0xF7}}, {0xC7A1, {0xFA, 0xF9}},
190	{0xC7AB, {0xFA, 0xFA}}, {0xC7C3, {0xFA, 0xFC}}, {0xC7C7, {0xFA, 0xFB}},
191	{0xC7CB, {0xFB, 0x40}}, {0xC7CF, {0xFB, 0x41}}, {0xC7D9, {0xFB, 0x42}},
192	{0xC7E6, {0xFB, 0x45}}, {0xC7EB, {0xFB, 0x48}}, {0xC7EE, {0xFB, 0x46}},
193	{0xC7F0, {0xFB, 0x49}}, {0xC7FC, {0xFB, 0x47}}, {0xC8B1, {0xFB, 0x4A}},
194	{0xC8E5, {0xFB, 0x4B}}, {0xC8F8, {0xFB, 0x4C}}, {0xC9A6, {0xFB, 0x4D}},
195	{0xC9AB, {0xFB, 0x4E}}, {0xC9AD, {0xFB, 0x4F}}, {0xC9CA, {0xFB, 0x51}},
196	{0xC9D3, {0xFB, 0x52}}, {0xC9E3, {0xFB, 0x54}}, {0xC9E9, {0xFB, 0x53}},
197	{0xC9F4, {0xFB, 0x56}}, {0xC9F5, {0xFB, 0x57}}, {0xC9FC, {0xFB, 0x55}},
198	{0xCAB3, {0xFB, 0x59}}, {0xCABD, {0xFB, 0x5A}}, {0xCAEF, {0xFB, 0x5B}},
199	{0xCAF1, {0xFB, 0x5C}}, {0xCBAE, {0xFB, 0x5D}}, {0xCBCA, {0xFB, 0x5F}},
200	{0xCBE6, {0xFB, 0x60}}, {0xCBEA, {0xFB, 0x61}}, {0xCBEE, {0xFB, 0x64}},
201	{0xCBF0, {0xFB, 0x62}}, {0xCBF4, {0xFB, 0x63}}, {0xCBF9, {0xFB, 0x66}},
202	{0xCCA5, {0xFB, 0x65}}, {0xCCAB, {0xFB, 0x67}}, {0xCCAD, {0xFB, 0x69}},
203	{0xCCAE, {0xFB, 0x68}}, {0xCCB2, {0xFB, 0x6A}}, {0xCCC2, {0xFB, 0x6B}},
204	{0xCCD0, {0xFB, 0x6C}}, {0xCCD9, {0xFB, 0x6D}}, {0xCDAA, {0xFA, 0xA8}},
205	{0xCDBB, {0xFB, 0x6F}}, {0xCEBA, {0xFB, 0x73}}, {0xCEBB, {0xFB, 0x71}},
206	{0xCEC3, {0xFB, 0x74}}, {0xCEF2, {0xFB, 0x76}}, {0xCFD5, {0xFB, 0x78}},
207	{0xCFE2, {0xFB, 0x79}}, {0xCFE9, {0xFB, 0x7A}}, {0xCFED, {0xFB, 0x7B}},
208	{0xD0E5, {0xFB, 0x81}}, {0xD0E9, {0xFB, 0x83}}, {0xD1E8, {0xFB, 0x84}},
209	{0xD1EC, {0xFB, 0x87}}, {0xD2BB, {0xFB, 0x88}}, {0xD3E1, {0xFB, 0x8A}},
210	{0xD3E8, {0xFB, 0x8B}}, {0xD4A7, {0xFB, 0x8C}}, {0xD4D4, {0xFB, 0x8F}},
211	{0xD4E3, {0xFA, 0x5C}}, {0xD4F2, {0xFB, 0x90}}, {0xD5AE, {0xFB, 0x91}},
212	{0xD7DE, {0xFB, 0x93}}, {0xD8A2, {0xFB, 0x95}}, {0xD8B7, {0xFB, 0x96}},
213	{0xD8C1, {0xFB, 0x97}}, {0xD8D1, {0xFB, 0x98}}, {0xD8F4, {0xFB, 0x99}},
214	{0xD9A1, {0xFA, 0x60}}, {0xD9C6, {0xFB, 0x9A}}, {0xD9C8, {0xFB, 0x9B}},
215	{0xD9D1, {0xFB, 0x9C}}, {0xDCD3, {0xFB, 0xA2}}, {0xDCDF, {0xFA, 0x5D}},
216	{0xDDC8, {0xFB, 0xA3}}, {0xDDD4, {0xFB, 0xA4}}, {0xDDEA, {0xFB, 0xA5}},
217	{0xDDFA, {0xFB, 0xA6}}, {0xDEA4, {0xFB, 0xA7}}, {0xDEB0, {0xFB, 0xA8}},
218	{0xDEB5, {0xFB, 0xAA}}, {0xDECB, {0xFB, 0xAB}}, {0xDFB9, {0xFB, 0xAD}},
219	{0xDFC3, {0xFB, 0xAF}}, {0xE0D9, {0xFB, 0xB2}}, {0xE1E2, {0xFB, 0xB5}},
220	{0xE2C7, {0xFB, 0xB9}}, {0xE3A6, {0xFB, 0xBB}}, {0xE3A8, {0xFB, 0xBA}},
221	{0xE3A9, {0xFB, 0xBC}}, {0xE3AA, {0xFB, 0xBF}}, {0xE3AB, {0xFB, 0xC0}},
222	{0xE3AF, {0xFB, 0xBD}}, {0xE3B0, {0xFB, 0xBE}}, {0xE3BC, {0xFB, 0xC1}},
223	{0xE3BF, {0xFB, 0xC3}}, {0xE3C1, {0xFB, 0xC2}}, {0xE3D4, {0xFB, 0xCA}},
224	{0xE3D5, {0xFB, 0xC4}}, {0xE3D6, {0xFB, 0xC6}}, {0xE3D8, {0xFB, 0xC5}},
225	{0xE3DF, {0xFB, 0xC7}}, {0xE3E1, {0xFB, 0xC9}}, {0xE3E3, {0xFB, 0xC8}},
226	{0xE3E9, {0xFB, 0xCB}}, {0xE3F1, {0xFB, 0xCD}}, {0xE3F2, {0xFB, 0xCE}},
227	{0xE3F8, {0xFA, 0x5F}}, {0xE4A6, {0xFB, 0xCC}}, {0xE4BE, {0xFB, 0xD2}},
228	{0xE4BF, {0xFB, 0xD6}}, {0xE4C0, {0xFB, 0xD4}}, {0xE4C1, {0xFB, 0xD0}},
229	{0xE4C3, {0xFB, 0xD1}}, {0xE4C7, {0xFB, 0xD5}}, {0xE4CB, {0xFB, 0xCF}},
230	{0xE4D0, {0xFA, 0x65}}, {0xE4D1, {0xFB, 0xD9}}, {0xE4D2, {0xFB, 0xDC}},
231	{0xE4D4, {0xFB, 0xDE}}, {0xE4DB, {0xFB, 0xDD}}, {0xE4DC, {0xFB, 0xDB}},
232	{0xE4DE, {0xFB, 0xD8}}, {0xE4E0, {0xFB, 0xD7}}, {0xE4E9, {0xFA, 0x5E}},
233	{0xE4EF, {0xFB, 0xE0}}, {0xE4FA, {0xFB, 0xDF}}, {0xE5B3, {0xFB, 0xE1}},
234	{0xE5BF, {0xFB, 0xE2}}, {0xE5C9, {0xFB, 0xE3}}, {0xE5D0, {0xFB, 0xE4}},
235	{0xE5E2, {0xFB, 0xE5}}, {0xE5EA, {0xFB, 0xE6}}, {0xE5EB, {0xFB, 0xE7}},
236	{0xE6E8, {0xFB, 0xEB}}, {0xE6EF, {0xFB, 0xEC}}, {0xE7AC, {0xFB, 0xED}},
237	{0xE7AE, {0xFB, 0xEF}}, {0xE7B1, {0xFB, 0xF1}}, {0xE7B2, {0xFB, 0xF3}},
238	{0xE8B1, {0xFB, 0xF4}}, {0xE8B6, {0xFB, 0xF5}}, {0xE8DD, {0xFB, 0xF8}},
239	{0xE9D1, {0xFB, 0xFB}}, {0xE9ED, {0xFC, 0x40}}, {0xEACD, {0xFC, 0x41}},
240	{0xEADB, {0xFC, 0x43}}, {0xEAE6, {0xFC, 0x44}}, {0xEAEA, {0xFC, 0x45}},
241	{0xEBA5, {0xFC, 0x46}}, {0xEBFA, {0xFC, 0x48}}, {0xEBFB, {0xFC, 0x47}},
242	{0xECD6, {0xFC, 0x4A}},
243};
244
245/* EUC to SJIS IBM extended characters map (G3 Upper block) */
246static const unsigned char euc2sjisibm_g3upper_map[][2] = {
247	{0xFA, 0x40}, {0xFA, 0x41}, {0xFA, 0x42}, {0xFA, 0x43}, {0xFA, 0x44},
248	{0xFA, 0x45}, {0xFA, 0x46}, {0xFA, 0x47}, {0xFA, 0x48}, {0xFA, 0x49},
249	{0xFA, 0x4A}, {0xFA, 0x4B}, {0xFA, 0x4C}, {0xFA, 0x4D}, {0xFA, 0x4E},
250	{0xFA, 0x4F}, {0xFA, 0x50}, {0xFA, 0x51}, {0xFA, 0x52}, {0xFA, 0x53},
251	{0xFA, 0x56}, {0xFA, 0x57}, {0xFA, 0x58}, {0xFA, 0x59}, {0xFA, 0x5A},
252	{0xFA, 0x62}, {0xFA, 0x6A}, {0xFA, 0x7C}, {0xFA, 0x83}, {0xFA, 0x8A},
253	{0xFA, 0x8B}, {0xFA, 0x90}, {0xFA, 0x92}, {0xFA, 0x96}, {0xFA, 0x9B},
254	{0xFA, 0x9C}, {0xFA, 0x9D}, {0xFA, 0xAA}, {0xFA, 0xAE}, {0xFA, 0xB0},
255	{0xFA, 0xB1}, {0xFA, 0xBA}, {0xFA, 0xBD}, {0xFA, 0xC1}, {0xFA, 0xCD},
256	{0xFA, 0xD0}, {0xFA, 0xD5}, {0xFA, 0xD8}, {0xFA, 0xE0}, {0xFA, 0xE5},
257	{0xFA, 0xE8}, {0xFA, 0xEA}, {0xFA, 0xEE}, {0xFA, 0xF2}, {0xFB, 0x43},
258	{0xFB, 0x44}, {0xFB, 0x50}, {0xFB, 0x58}, {0xFB, 0x5E}, {0xFB, 0x6E},
259	{0xFB, 0x70}, {0xFB, 0x72}, {0xFB, 0x75}, {0xFB, 0x7C}, {0xFB, 0x7D},
260	{0xFB, 0x7E}, {0xFB, 0x80}, {0xFB, 0x82}, {0xFB, 0x85}, {0xFB, 0x86},
261	{0xFB, 0x89}, {0xFB, 0x8D}, {0xFB, 0x8E}, {0xFB, 0x92}, {0xFB, 0x94},
262	{0xFB, 0x9D}, {0xFB, 0x9E}, {0xFB, 0x9F}, {0xFB, 0xA0}, {0xFB, 0xA1},
263	{0xFB, 0xA9}, {0xFB, 0xAC}, {0xFB, 0xAE}, {0xFB, 0xB0}, {0xFB, 0xB1},
264	{0xFB, 0xB3}, {0xFB, 0xB4}, {0xFB, 0xB6}, {0xFB, 0xB7}, {0xFB, 0xB8},
265	{0xFB, 0xD3}, {0xFB, 0xDA}, {0xFB, 0xE8}, {0xFB, 0xE9}, {0xFB, 0xEA},
266	{0xFB, 0xEE}, {0xFB, 0xF0}, {0xFB, 0xF2}, {0xFB, 0xF6}, {0xFB, 0xF7},
267	{0xFB, 0xF9}, {0xFB, 0xFA}, {0xFB, 0xFC}, {0xFC, 0x42}, {0xFC, 0x49},
268	{0xFC, 0x4B},
269};
270
271static inline int sjisibm2euc(unsigned char *euc, const unsigned char sjis_hi,
272			      const unsigned char sjis_lo);
273static inline int euc2sjisibm_jisx0212(unsigned char *sjis, const unsigned char euc_hi,
274				       const unsigned char euc_lo);
275static inline int euc2sjisibm_g3upper(unsigned char *sjis, const unsigned char euc_hi,
276				      const unsigned char euc_lo);
277static inline int euc2sjisibm(unsigned char *sjis, const unsigned char euc_hi,
278			      const unsigned char euc_lo);
279static inline int sjisnec2sjisibm(unsigned char *sjisibm,
280				  const unsigned char sjisnec_hi,
281				  const unsigned char sjisnec_lo);
282
283/* SJIS IBM extended characters to EUC */
284static inline int sjisibm2euc(unsigned char *euc, const unsigned char sjis_hi,
285			      const unsigned char sjis_lo)
286{
287	int index;
288
289	index = ((sjis_hi - 0xFA) * (0xFD - 0x40)) + (sjis_lo - 0x40);
290	if (IS_EUC_IBM2JISX0208(sjisibm2euc_map[index][0],
291				sjisibm2euc_map[index][1])) {
292		euc[0] = sjisibm2euc_map[index][0];
293		euc[1] = sjisibm2euc_map[index][1];
294		return 2;
295	} else {
296		euc[0] = SS3;
297		euc[1] = sjisibm2euc_map[index][0];
298		euc[2] = sjisibm2euc_map[index][1];
299		return 3;
300	}
301}
302
303/* EUC to SJIS IBM extended characters (G3 JIS X 0212 block) */
304static inline int euc2sjisibm_jisx0212(unsigned char *sjis, const unsigned char euc_hi,
305				       const unsigned char euc_lo)
306{
307	int index, min_index, max_index;
308	unsigned short euc;
309
310	min_index = 0;
311	max_index = ARRAY_SIZE(euc2sjisibm_jisx0212_map) - 1;
312	euc = (euc_hi << 8) | euc_lo;
313
314	while (min_index <= max_index) {
315		index = (min_index + max_index) / 2;
316		if (euc < euc2sjisibm_jisx0212_map[index].euc)
317			max_index = index - 1;
318		else
319			min_index = index + 1;
320		if (euc == euc2sjisibm_jisx0212_map[index].euc) {
321			sjis[0] = euc2sjisibm_jisx0212_map[index].sjis[0];
322			sjis[1] = euc2sjisibm_jisx0212_map[index].sjis[1];
323			return 3;
324		}
325	}
326	return 0;
327}
328
329/* EUC to SJIS IBM extended characters (G3 Upper block) */
330static inline int euc2sjisibm_g3upper(unsigned char *sjis, const unsigned char euc_hi,
331				      const unsigned char euc_lo)
332{
333	int index;
334
335	if (euc_hi == 0xF3)
336		index = ((euc_hi << 8) | euc_lo) - 0xF3F3;
337	else
338		index = ((euc_hi << 8) | euc_lo) - 0xF4A1 + 12;
339
340	if ((index < 0) || (index >= ARRAY_SIZE(euc2sjisibm_g3upper_map)))
341		return 0;
342
343	sjis[0] = euc2sjisibm_g3upper_map[index][0];
344	sjis[1] = euc2sjisibm_g3upper_map[index][1];
345
346	return 3;
347}
348
349/* EUC to SJIS IBM extended characters (G3 block) */
350static inline int euc2sjisibm(unsigned char *sjis, const unsigned char euc_hi,
351			      const unsigned char euc_lo)
352{
353	int n;
354
355#if 0
356	if ((euc_hi == 0xA2) && (euc_lo == 0xCC)) {
357		sjis[0] = 0xFA;
358		sjis[1] = 0x54;
359		return 2;
360	} else if ((euc_hi == 0xA2) && (euc_lo == 0xE8)) {
361		sjis[0] = 0xFA;
362		sjis[1] = 0x5B;
363		return 2;
364	}
365#endif
366	if ((n = euc2sjisibm_g3upper(sjis, euc_hi, euc_lo))) {
367		return n;
368	} else if ((n = euc2sjisibm_jisx0212(sjis, euc_hi, euc_lo))) {
369		return n;
370	}
371
372	return 0;
373}
374
375/* NEC/IBM extended characters to IBM extended characters */
376static inline int sjisnec2sjisibm(unsigned char *sjisibm,
377				  const unsigned char sjisnec_hi,
378				  const unsigned char sjisnec_lo)
379{
380	int count;
381
382	if (! IS_SJIS_NECIBM(sjisnec_hi, sjisnec_lo))
383		return 0;
384
385	if ((sjisnec_hi == 0xEE) && (sjisnec_lo == 0xF9)) {
386		sjisibm[0] = 0x81;
387		sjisibm[1] = 0xCA;
388		return 2;
389	}
390
391	if ((sjisnec_hi == 0xEE) && (sjisnec_lo >= 0xEF)) {
392		count = (sjisnec_hi << 8 | sjisnec_lo)
393			- (sjisnec_lo <= 0xF9 ? 0xEEEF : (0xEEEF - 10));
394	} else {
395		count = (sjisnec_hi - 0xED) * (0xFC - 0x40)
396			+ (sjisnec_lo - 0x40) + (0x5C - 0x40);
397		if (sjisnec_lo >= 0x7F)
398			count--;
399	}
400
401	sjisibm[0] = 0xFA + (count / (0xFC - 0x40));
402	sjisibm[1] = 0x40 + (count % (0xFC - 0x40));
403	if (sjisibm[1] >= 0x7F)
404		sjisibm[1]++;
405
406	return 2;
407}
408
409static int uni2char(const wchar_t uni,
410		    unsigned char *out, int boundlen)
411{
412	int n;
413
414	if (!p_nls)
415		return -EINVAL;
416	if ((n = p_nls->uni2char(uni, out, boundlen)) < 0)
417		return n;
418
419	/* translate SJIS into EUC-JP */
420	if (n == 1) {
421		if (IS_SJIS_JISX0201KANA(out[0])) {
422			/* JIS X 0201 KANA */
423			if (boundlen < 2)
424				return -ENAMETOOLONG;
425
426			out[1] = out[0];
427			out[0] = SS2;
428			return 2;
429		}
430	} else if (n == 2) {
431		/* NEC/IBM extended characters to IBM extended characters */
432		sjisnec2sjisibm(out, out[0], out[1]);
433
434		if (IS_SJIS_UDC_LOW(out[0], out[1])) {
435			/* User defined characters half low */
436			MAP_SJIS2EUC(out[0], out[1], 0xF0, out[0], out[1], 0xF5);
437		} else if (IS_SJIS_UDC_HI(out[0], out[1])) {
438			/* User defined characters half high */
439			unsigned char ch, cl;
440
441			if (boundlen < 3)
442				return -ENAMETOOLONG;
443
444			n = 3; ch = out[0]; cl = out[1];
445			out[0] = SS3;
446			MAP_SJIS2EUC(ch, cl, 0xF5, out[1], out[2], 0xF5);
447		} else if (IS_SJIS_IBM(out[0], out[1])) {
448			/* IBM extended characters */
449			unsigned char euc[3], i;
450
451			n = sjisibm2euc(euc, out[0], out[1]);
452			if (boundlen < n)
453				return -ENAMETOOLONG;
454			for (i = 0; i < n; i++)
455				out[i] = euc[i];
456		} else if (IS_SJIS_JISX0208(out[0], out[1])) {
457			/* JIS X 0208 (include NEC special characters) */
458			out[0] = (out[0]^0xA0)*2 + 0x5F;
459			if (out[1] > 0x9E)
460				out[0]++;
461
462			if (out[1] < 0x7F)
463				out[1] = out[1] + 0x61;
464			else if (out[1] < 0x9F)
465				out[1] = out[1] + 0x60;
466			else
467				out[1] = out[1] + 0x02;
468		} else {
469			/* Invalid characters */
470			return -EINVAL;
471		}
472	}
473	else
474		return -EINVAL;
475
476	return n;
477}
478
479static int char2uni(const unsigned char *rawstring, int boundlen,
480		    wchar_t *uni)
481{
482	unsigned char sjis_temp[2];
483	int euc_offset, n;
484
485	if ( !p_nls )
486		return -EINVAL;
487	if (boundlen <= 0)
488		return -ENAMETOOLONG;
489
490	/* translate EUC-JP into SJIS */
491	if (rawstring[0] > 0x7F) {
492		if (rawstring[0] == SS3) {
493			if (boundlen < 3)
494				return -EINVAL;
495			euc_offset = 3;
496
497			if (IS_EUC_UDC_HI(rawstring[1], rawstring[2])) {
498				/* User defined characters half high */
499				MAP_EUC2SJIS(rawstring[1], rawstring[2], 0xF5,
500					     sjis_temp[0], sjis_temp[1], 0xF5);
501			} else if (euc2sjisibm(sjis_temp,rawstring[1],rawstring[2])) {
502				/* IBM extended characters */
503			} else {
504				/* JIS X 0212 and Invalid characters*/
505				return -EINVAL;
506
507				/* 'GETA' with SJIS coding */
508				/* sjis_temp[0] = 0x81; */
509				/* sjis_temp[1] = 0xAC; */
510			}
511		} else {
512			if (boundlen < 2)
513				return -EINVAL;
514			euc_offset = 2;
515
516			if (IS_EUC_JISX0201KANA(rawstring[0], rawstring[1])) {
517				/* JIS X 0201 KANA */
518				sjis_temp[0] = rawstring[1];
519				sjis_temp[1] = 0x00;
520			} else if (IS_EUC_UDC_LOW(rawstring[0], rawstring[1])) {
521				/* User defined characters half low */
522				MAP_EUC2SJIS(rawstring[0], rawstring[1], 0xF5,
523					     sjis_temp[0], sjis_temp[1], 0xF0);
524			} else if (IS_EUC_JISX0208(rawstring[0], rawstring[1])) {
525				/* JIS X 0208 (include NEC spesial characters) */
526				sjis_temp[0] = ((rawstring[0]-0x5f)/2) ^ 0xA0;
527				if (!(rawstring[0] & 1))
528					sjis_temp[1] = rawstring[1] - 0x02;
529				else if (rawstring[1] < 0xE0)
530					sjis_temp[1] = rawstring[1] - 0x61;
531				else
532					sjis_temp[1] = rawstring[1] - 0x60;
533			} else {
534				/* Invalid characters */
535				return -EINVAL;
536			}
537		}
538	} else {
539		euc_offset = 1;
540
541		/* JIS X 0201 ROMAJI */
542		sjis_temp[0] = rawstring[0];
543		sjis_temp[1] = 0x00;
544	}
545
546	if ( (n = p_nls->char2uni(sjis_temp, sizeof(sjis_temp), uni)) < 0)
547		return n;
548
549	return euc_offset;
550}
551
552static struct nls_table table = {
553	.charset	= "euc-jp",
554	.uni2char	= uni2char,
555	.char2uni	= char2uni,
556	.owner		= THIS_MODULE,
557};
558
559static int __init init_nls_euc_jp(void)
560{
561	p_nls = load_nls("cp932");
562
563	if (p_nls) {
564		table.charset2upper = p_nls->charset2upper;
565		table.charset2lower = p_nls->charset2lower;
566		return register_nls(&table);
567	}
568
569	return -EINVAL;
570}
571
572static void __exit exit_nls_euc_jp(void)
573{
574	unregister_nls(&table);
575	unload_nls(p_nls);
576}
577
578module_init(init_nls_euc_jp)
579module_exit(exit_nls_euc_jp)
580
581MODULE_LICENSE("Dual BSD/GPL");