Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / libunls / nls_base.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)nls_base.c       1.5 05/05/01 2000 J. Schilling */
14 /*
15  *      Modifications to make the code portable Copyright (c) 2000 J. Schilling
16  *      Thanks to Georgy Salnikov <sge@nmr.nioch.nsc.ru>
17  *
18  *      Code taken from the Linux kernel.
19  */
20 /*
21  * This program is free software; you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License version 2
23  * as published by the Free Software Foundation.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License along with
31  * this program; see the file COPYING.  If not, write to the Free Software
32  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33  */
34 /*
35  * linux/fs/nls.c
36  *
37  * Native language support--charsets and unicode translations.
38  * By Gordon Chaffee 1996, 1997
39  *
40  */
41
42 #include <mconfig.h>
43 #include <stdio.h>
44 #include <strdefs.h>
45 #include <errno.h>
46 #include "nls.h"
47
48 static struct unls_table *tables = (struct unls_table *) NULL;
49
50 int
51 register_unls(struct unls_table *unls)
52 {
53         struct unls_table ** tmp = &tables;
54
55         if (!unls)
56                 return (-EINVAL);
57         if (unls->unls_next)
58                 return (-EBUSY);
59         while (*tmp) {
60                 if (unls == *tmp) {
61                         return (-EBUSY);
62                 }
63                 tmp = &(*tmp)->unls_next;
64         }
65         unls->unls_next = tables;
66         tables = unls;
67         return (0);
68 }
69
70 int
71 unregister_unls(struct unls_table *unls)
72 {
73         struct unls_table ** tmp = &tables;
74
75         while (*tmp) {
76                 if (unls == *tmp) {
77                         *tmp = unls->unls_next;
78                         return (0);
79                 }
80                 tmp = &(*tmp)->unls_next;
81         }
82         return (-EINVAL);
83 }
84
85 struct unls_table *
86 find_unls(char *charset)
87 {
88         struct unls_table *unls = tables;
89         while (unls) {
90                 if (strcmp(unls->unls_name, charset) == 0)
91                         return (unls);
92                 unls = unls->unls_next;
93         }
94         return (NULL);
95 }
96
97 void
98 list_unls()
99 {
100         struct unls_table *unls = tables;
101         while (unls) {
102                 fprintf(stderr, "%s\n", unls->unls_name);
103                 unls = unls->unls_next;
104         }
105 }
106
107 struct unls_table *
108 load_unls(char *charset)
109 {
110         struct unls_table *unls;
111
112         unls = find_unls(charset);
113         if (unls) {
114                 return (unls);
115         }
116
117         return (NULL);
118 }
119
120 void
121 unload_unls(struct unls_table *nls)
122 {
123 }
124
125 static struct unls_unicode charset2uni[256] = {
126         /* 0x00*/
127         {0x00, 0x00}, {0x01, 0x00}, {0x02, 0x00}, {0x03, 0x00},
128         {0x04, 0x00}, {0x05, 0x00}, {0x06, 0x00}, {0x07, 0x00},
129         {0x08, 0x00}, {0x09, 0x00}, {0x0a, 0x00}, {0x0b, 0x00},
130         {0x0c, 0x00}, {0x0d, 0x00}, {0x0e, 0x00}, {0x0f, 0x00},
131         /* 0x10*/
132         {0x10, 0x00}, {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00},
133         {0x14, 0x00}, {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00},
134         {0x18, 0x00}, {0x19, 0x00}, {0x1a, 0x00}, {0x1b, 0x00},
135         {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00}, {0x1f, 0x00},
136         /* 0x20*/
137         {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00},
138         {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00}, {0x27, 0x00},
139         {0x28, 0x00}, {0x29, 0x00}, {0x2a, 0x00}, {0x2b, 0x00},
140         {0x2c, 0x00}, {0x2d, 0x00}, {0x2e, 0x00}, {0x2f, 0x00},
141         /* 0x30*/
142         {0x30, 0x00}, {0x31, 0x00}, {0x32, 0x00}, {0x33, 0x00},
143         {0x34, 0x00}, {0x35, 0x00}, {0x36, 0x00}, {0x37, 0x00},
144         {0x38, 0x00}, {0x39, 0x00}, {0x3a, 0x00}, {0x3b, 0x00},
145         {0x3c, 0x00}, {0x3d, 0x00}, {0x3e, 0x00}, {0x3f, 0x00},
146         /* 0x40*/
147         {0x40, 0x00}, {0x41, 0x00}, {0x42, 0x00}, {0x43, 0x00},
148         {0x44, 0x00}, {0x45, 0x00}, {0x46, 0x00}, {0x47, 0x00},
149         {0x48, 0x00}, {0x49, 0x00}, {0x4a, 0x00}, {0x4b, 0x00},
150         {0x4c, 0x00}, {0x4d, 0x00}, {0x4e, 0x00}, {0x4f, 0x00},
151         /* 0x50*/
152         {0x50, 0x00}, {0x51, 0x00}, {0x52, 0x00}, {0x53, 0x00},
153         {0x54, 0x00}, {0x55, 0x00}, {0x56, 0x00}, {0x57, 0x00},
154         {0x58, 0x00}, {0x59, 0x00}, {0x5a, 0x00}, {0x5b, 0x00},
155         {0x5c, 0x00}, {0x5d, 0x00}, {0x5e, 0x00}, {0x5f, 0x00},
156         /* 0x60*/
157         {0x60, 0x00}, {0x61, 0x00}, {0x62, 0x00}, {0x63, 0x00},
158         {0x64, 0x00}, {0x65, 0x00}, {0x66, 0x00}, {0x67, 0x00},
159         {0x68, 0x00}, {0x69, 0x00}, {0x6a, 0x00}, {0x6b, 0x00},
160         {0x6c, 0x00}, {0x6d, 0x00}, {0x6e, 0x00}, {0x6f, 0x00},
161         /* 0x70*/
162         {0x70, 0x00}, {0x71, 0x00}, {0x72, 0x00}, {0x73, 0x00},
163         {0x74, 0x00}, {0x75, 0x00}, {0x76, 0x00}, {0x77, 0x00},
164         {0x78, 0x00}, {0x79, 0x00}, {0x7a, 0x00}, {0x7b, 0x00},
165         {0x7c, 0x00}, {0x7d, 0x00}, {0x7e, 0x00}, {0x7f, 0x00},
166         /* 0x80*/
167         {0x80, 0x00}, {0x81, 0x00}, {0x82, 0x00}, {0x83, 0x00},
168         {0x84, 0x00}, {0x85, 0x00}, {0x86, 0x00}, {0x87, 0x00},
169         {0x88, 0x00}, {0x89, 0x00}, {0x8a, 0x00}, {0x8b, 0x00},
170         {0x8c, 0x00}, {0x8d, 0x00}, {0x8e, 0x00}, {0x8f, 0x00},
171         /* 0x90*/
172         {0x90, 0x00}, {0x91, 0x00}, {0x92, 0x00}, {0x93, 0x00},
173         {0x94, 0x00}, {0x95, 0x00}, {0x96, 0x00}, {0x97, 0x00},
174         {0x98, 0x00}, {0x99, 0x00}, {0x9a, 0x00}, {0x9b, 0x00},
175         {0x9c, 0x00}, {0x9d, 0x00}, {0x9e, 0x00}, {0x9f, 0x00},
176         /* 0xa0*/
177         {0xa0, 0x00}, {0xa1, 0x00}, {0xa2, 0x00}, {0xa3, 0x00},
178         {0xa4, 0x00}, {0xa5, 0x00}, {0xa6, 0x00}, {0xa7, 0x00},
179         {0xa8, 0x00}, {0xa9, 0x00}, {0xaa, 0x00}, {0xab, 0x00},
180         {0xac, 0x00}, {0xad, 0x00}, {0xae, 0x00}, {0xaf, 0x00},
181         /* 0xb0*/
182         {0xb0, 0x00}, {0xb1, 0x00}, {0xb2, 0x00}, {0xb3, 0x00},
183         {0xb4, 0x00}, {0xb5, 0x00}, {0xb6, 0x00}, {0xb7, 0x00},
184         {0xb8, 0x00}, {0xb9, 0x00}, {0xba, 0x00}, {0xbb, 0x00},
185         {0xbc, 0x00}, {0xbd, 0x00}, {0xbe, 0x00}, {0xbf, 0x00},
186         /* 0xc0*/
187         {0xc0, 0x00}, {0xc1, 0x00}, {0xc2, 0x00}, {0xc3, 0x00},
188         {0xc4, 0x00}, {0xc5, 0x00}, {0xc6, 0x00}, {0xc7, 0x00},
189         {0xc8, 0x00}, {0xc9, 0x00}, {0xca, 0x00}, {0xcb, 0x00},
190         {0xcc, 0x00}, {0xcd, 0x00}, {0xce, 0x00}, {0xcf, 0x00},
191         /* 0xd0*/
192         {0xd0, 0x00}, {0xd1, 0x00}, {0xd2, 0x00}, {0xd3, 0x00},
193         {0xd4, 0x00}, {0xd5, 0x00}, {0xd6, 0x00}, {0xd7, 0x00},
194         {0xd8, 0x00}, {0xd9, 0x00}, {0xda, 0x00}, {0xdb, 0x00},
195         {0xdc, 0x00}, {0xdd, 0x00}, {0xde, 0x00}, {0xdf, 0x00},
196         /* 0xe0*/
197         {0xe0, 0x00}, {0xe1, 0x00}, {0xe2, 0x00}, {0xe3, 0x00},
198         {0xe4, 0x00}, {0xe5, 0x00}, {0xe6, 0x00}, {0xe7, 0x00},
199         {0xe8, 0x00}, {0xe9, 0x00}, {0xea, 0x00}, {0xeb, 0x00},
200         {0xec, 0x00}, {0xed, 0x00}, {0xee, 0x00}, {0xef, 0x00},
201         /* 0xf0*/
202         {0xf0, 0x00}, {0xf1, 0x00}, {0xf2, 0x00}, {0xf3, 0x00},
203         {0xf4, 0x00}, {0xf5, 0x00}, {0xf6, 0x00}, {0xf7, 0x00},
204         {0xf8, 0x00}, {0xf9, 0x00}, {0xfa, 0x00}, {0xfb, 0x00},
205         {0xfc, 0x00}, {0xfd, 0x00}, {0xfe, 0x00}, {0xff, 0x00},
206 };
207
208 static unsigned char page00[256] = {
209         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 0x00-0x07 */
210         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* 0x08-0x0f */
211         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, /* 0x10-0x17 */
212         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, /* 0x18-0x1f */
213         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* 0x20-0x27 */
214         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* 0x28-0x2f */
215         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* 0x30-0x37 */
216         0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 0x38-0x3f */
217         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* 0x40-0x47 */
218         0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* 0x48-0x4f */
219         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, /* 0x50-0x57 */
220         0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* 0x58-0x5f */
221         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 0x60-0x67 */
222         0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, /* 0x68-0x6f */
223         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, /* 0x70-0x77 */
224         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, /* 0x78-0x7f */
225
226         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 0x80-0x87 */
227         0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 0x88-0x8f */
228         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, /* 0x90-0x97 */
229         0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 0x98-0x9f */
230         0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, /* 0xa0-0xa7 */
231         0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* 0xa8-0xaf */
232         0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, /* 0xb0-0xb7 */
233         0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, /* 0xb8-0xbf */
234         0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 0xc0-0xc7 */
235         0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, /* 0xc8-0xcf */
236         0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, /* 0xd0-0xd7 */
237         0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, /* 0xd8-0xdf */
238         0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* 0xe0-0xe7 */
239         0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, /* 0xe8-0xef */
240         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 0xf0-0xf7 */
241         0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, /* 0xf8-0xff */
242 };
243
244 static unsigned char *page_uni2charset[256] = {
245         page00
246 };
247
248
249 static struct unls_table default_table = {
250         "default",
251         page_uni2charset,
252         charset2uni,
253         NULL
254 };
255
256
257
258 /* Returns a simple default translation table */
259 struct unls_table *
260 load_unls_default()
261 {
262         return (&default_table);
263 }