Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / fs / nls / nls_euc-jp.c
blob196a076984635cc6e9a40f081cd0f106345b3ed0
1 /*
2 * linux/fs/nls_euc-jp.c
3 */
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/nls.h>
9 #include <linux/errno.h>
11 static struct nls_table *p_nls;
14 #define SS2 (0x8E) /* Single Shift 2 */
15 #define SS3 (0x8F) /* Single Shift 3 */
17 static int uni2char(const wchar_t uni,
18 unsigned char *out, int boundlen)
20 int n;
22 if ( !p_nls )
23 return -EINVAL;
24 if ( (n = p_nls->uni2char(uni, out, boundlen)) < 0)
25 return n;
27 /* translate SJIS into EUC-JP */
28 if (n == 1) {
29 /* JIS X 201 KANA */
30 if (0xA1 <= out[0] && out[0] <= 0xDF) {
31 if (boundlen <= 1)
32 return -ENAMETOOLONG;
33 out[1] = out[0];
34 out[0] = SS2;
35 n = 2;
37 } else if (n == 2) {
38 /* JIS X 208 */
40 /* SJIS codes 0xF0xx to 0xFFxx are machine-dependent codes and user-defining characters */
41 if (out[0] >= 0xF0) {
42 out[0] = 0x81; /* 'GETA' with SJIS coding */
43 out[1] = 0xAC;
46 out[0] = (out[0]^0xA0)*2 + 0x5F;
47 if (out[1] > 0x9E)
48 out[0]++;
50 if (out[1] < 0x7F)
51 out[1] = out[1] + 0x61;
52 else if (out[1] < 0x9F)
53 out[1] = out[1] + 0x60;
54 else
55 out[1] = out[1] + 0x02;
57 else
58 return -EINVAL;
60 return n;
63 static int char2uni(const unsigned char *rawstring, int boundlen,
64 wchar_t *uni)
66 unsigned char sjis_temp[2];
67 int euc_offset, n;
69 if ( !p_nls )
70 return -EINVAL;
71 if (boundlen <= 0)
72 return -ENAMETOOLONG;
74 if (boundlen == 1) {
75 *uni = rawstring[0];
76 return 1;
79 /* translate EUC-JP into SJIS */
80 if (rawstring[0] > 0x7F) {
81 if (rawstring[0] == SS2) {
82 /* JIS X 201 KANA */
83 sjis_temp[0] = rawstring[1];
84 sjis_temp[1] = 0x00;
85 euc_offset = 2;
86 } else if (rawstring[0] == SS3) {
87 /* JIS X 212 */
88 sjis_temp[0] = 0x81; /* 'GETA' with SJIS coding */
89 sjis_temp[1] = 0xAC;
90 euc_offset = 3;
91 } else {
92 /* JIS X 208 */
93 sjis_temp[0] = ((rawstring[0]-0x5f)/2) ^ 0xA0;
94 if (!(rawstring[0]&1))
95 sjis_temp[1] = rawstring[1] - 0x02;
96 else if (rawstring[1] < 0xE0)
97 sjis_temp[1] = rawstring[1] - 0x61;
98 else
99 sjis_temp[1] = rawstring[1] - 0x60;
100 euc_offset = 2;
102 } else {
103 /* JIS X 201 ROMAJI */
104 sjis_temp[0] = rawstring[0];
105 sjis_temp[1] = rawstring[1];
106 euc_offset = 1;
109 if ( (n = p_nls->char2uni(sjis_temp, boundlen, uni)) < 0)
110 return n;
112 return euc_offset;
115 static struct nls_table table = {
116 "euc-jp",
117 uni2char,
118 char2uni,
119 NULL,
120 NULL,
121 THIS_MODULE,
124 static int __init init_nls_euc_jp(void)
126 p_nls = load_nls("cp932");
128 if (p_nls) {
129 table.charset2upper = p_nls->charset2upper;
130 table.charset2lower = p_nls->charset2lower;
131 return register_nls(&table);
134 return -EINVAL;
137 static void __exit exit_nls_euc_jp(void)
139 unregister_nls(&table);
140 unload_nls(p_nls);
143 module_init(init_nls_euc_jp)
144 module_exit(exit_nls_euc_jp)
147 * Overrides for Emacs so that we follow Linus's tabbing style.
148 * Emacs will notice this stuff at the end of the file and automatically
149 * adjust the settings for this buffer only. This must remain at the end
150 * of the file.
152 ---------------------------------------------------------------------------
153 * Local variables:
154 * c-indent-level: 8
155 * c-brace-imaginary-offset: 0
156 * c-brace-offset: -8
157 * c-argdecl-indent: 8
158 * c-label-offset: -8
159 * c-continued-statement-offset: 8
160 * c-continued-brace-offset: 0
161 * End: