Updated Eastern European translation table from dan@feld.cvut.cz.
[samba.git] / source / lib / charcnv.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Character set conversion Extensions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 */
22 #include "includes.h"
23 #define CTRLZ 26
24 extern int DEBUGLEVEL;
25
26 static char cvtbuf[1024];
27
28 static BOOL mapsinited = 0;
29
30 static char unix2dos[256];
31 static char dos2unix[256];
32
33 static void initmaps() {
34     int k;
35
36     for (k = 0; k < 256; k++) unix2dos[k] = k;
37     for (k = 0; k < 256; k++) dos2unix[k] = k;
38
39     mapsinited = True;
40 }
41
42 static void update_map(char * str) {
43     char *p;
44
45     for (p = str; *p; p++) {
46         if (p[1]) {
47             unix2dos[(unsigned char)*p] = p[1];
48             dos2unix[(unsigned char)p[1]] = *p;
49             p++;
50         }
51     }
52 }
53
54 static void init_iso8859_1() {
55
56     int i;
57     if (!mapsinited) initmaps();
58
59     /* Do not map undefined characters to some accidental code */
60     for (i = 128; i < 256; i++) 
61     {
62        unix2dos[i] = CTRLZ;
63        dos2unix[i] = CTRLZ;
64     }
65
66 /* MSDOS Code Page 850 -> ISO-8859 */
67 update_map("\240\377\241\255\242\275\243\234\244\317\245\276\246\335\247\365");
68 update_map("\250\371\251\270\252\246\253\256\254\252\255\360\256\251\257\356");
69 update_map("\260\370\261\361\262\375\263\374\264\357\265\346\266\364\267\372");
70 update_map("\270\367\271\373\272\247\273\257\274\254\275\253\276\363\277\250");
71 update_map("\300\267\301\265\302\266\303\307\304\216\305\217\306\222\307\200");
72 update_map("\310\324\311\220\312\322\313\323\314\336\315\326\316\327\317\330");
73 update_map("\320\321\321\245\322\343\323\340\324\342\325\345\326\231\327\236");
74 update_map("\330\235\331\353\332\351\333\352\334\232\335\355\336\350\337\341");
75 update_map("\340\205\341\240\342\203\343\306\344\204\345\206\346\221\347\207");
76 update_map("\350\212\351\202\352\210\353\211\354\215\355\241\356\214\357\213");
77 update_map("\360\320\361\244\362\225\363\242\364\223\365\344\366\224\367\366");
78 update_map("\370\233\371\227\372\243\373\226\374\201\375\354\376\347\377\230");
79
80 }
81
82 /* Init for eastern european languages. */
83
84 static void init_iso8859_2() {
85
86     int i;
87     if (!mapsinited) initmaps();
88
89     /* Do not map undefined characters to some accidental code */
90     for (i = 128; i < 256; i++) 
91     {
92        unix2dos[i] = CTRLZ;
93        dos2unix[i] = CTRLZ;
94     }
95
96 /*
97  * Tranlation table created by dan@feld.cvut.cz
98  * Requires client code page = 852
99  * and character set = ISO8859-2 in smb.conf
100  */
101 update_map("\173\173\174\174\175\175\176\176\241\244\242\364\243\235\244\317");
102 update_map("\245\225\246\227\247\365\250\371\251\346\252\270\253\233\254\215");
103 update_map("\256\246\257\275\261\245\262\362\263\210\264\357\265\226\2263\210\264\357\265\226\266\230");
104 update_map("\267\363\270\367\271\347\272\255\273\234\274\253\275\361\276\247");
105 update_map("\277\276\300\350\301\265\302\266\303\306\304\216\305\221\306\217");
106 update_map("\307\200\310\254\311\220\312\250\313\323\314\267\315\326\316\327");
107 update_map("\317\322\320\321\321\343\322\325\323\340\324\342\325\212\326\231");
108 update_map("\327\236\330\374\331\336\332\351\333\353\334\232\335\355\336\335");
109 update_map("\337\341\340\352\341\240\342\203\343\307\344\204\345\222\346\206");
110 update_map("\347\207\350\237\351\202\352\251\353\211\354\330\355\241\356\214");
111 update_map("\357\324\360\320\361\344\362\345\363\242\364\223\365\213\366\224");
112 update_map("\367\366\370\375\371\205\372\243\373\373\374\201\375\354\376\356");
113 update_map("\377\372");
114
115 }
116
117 /* Init for russian language (iso8859-5) */
118
119 /* Added by Max Khon <max@iclub.nsu.ru> */
120
121 static void init_iso8859_5()
122 {
123   int i;
124   if (!mapsinited) initmaps();
125
126   /* Do not map undefined characters to some accidental code */
127   for (i = 128; i < 256; i++) 
128   {
129      unix2dos[i] = CTRLZ;
130      dos2unix[i] = CTRLZ;
131   }
132
133 /* MSDOS Code Page 866 -> ISO8859-5 */
134 update_map("\200\260\201\261\202\262\203\263\204\264\205\265\206\266\207\267");
135 update_map("\210\270\211\271\212\272\213\273\214\274\215\275\216\276\217\277");
136 update_map("\220\300\221\301\222\302\223\303\224\304\225\305\226\306\227\307");
137 update_map("\230\310\231\311\232\312\233\313\234\314\235\315\236\316\237\317");
138 update_map("\240\320\241\321\242\322\243\323\244\324\245\325\246\326\247\327");
139 update_map("\250\330\251\331\252\332\253\333\254\334\255\335\256\336\257\337");
140 update_map("\340\340\341\341\342\342\343\343\344\344\345\345\346\346\347\347");
141 update_map("\350\350\351\351\352\352\353\353\354\354\355\355\356\356\357\357");
142 update_map("\360\241\361\361\362\244\363\364\364\247\365\367\366\256\367\376");
143 update_map("\374\360\377\240");
144 }
145
146 /* Init for russian language (koi8) */
147
148 static void init_koi8_r()
149 {
150   if (!mapsinited) initmaps();
151
152   /* There aren't undefined characters between 128 and 255 */
153
154 /* MSDOS Code Page 866 -> KOI8-R */
155 update_map("\200\304\201\263\202\332\203\277\204\300\205\331\206\303\207\264");
156 update_map("\210\302\211\301\212\305\213\337\214\334\215\333\216\335\217\336");
157 update_map("\220\260\221\261\222\262\223\364\224\376\225\371\226\373\227\367");
158 update_map("\230\363\231\362\232\377\233\365\234\370\235\375\236\372\237\366");
159 update_map("\240\315\241\272\242\325\243\361\244\326\245\311\246\270\247\267");
160 update_map("\250\273\251\324\252\323\253\310\254\276\255\275\256\274\257\306");
161 update_map("\260\307\261\314\262\265\263\360\264\266\265\271\266\321\267\322");
162 update_map("\270\313\271\317\272\320\273\312\274\330\275\327\276\316\277\374");
163 update_map("\300\356\301\240\302\241\303\346\304\244\305\245\306\344\307\243");
164 update_map("\310\345\311\250\312\251\313\252\314\253\315\254\316\255\317\256");
165 update_map("\320\257\321\357\322\340\323\341\324\342\325\343\326\246\327\242");
166 update_map("\330\354\331\353\332\247\333\350\334\355\335\351\336\347\337\352");
167 update_map("\340\236\341\200\342\201\343\226\344\204\345\205\346\224\347\203");
168 update_map("\350\225\351\210\352\211\353\212\354\213\355\214\356\215\357\216");
169 update_map("\360\217\361\237\362\220\363\221\364\222\365\223\366\206\367\202");
170 update_map("\370\234\371\233\372\207\373\230\374\235\375\231\376\227\377\232");
171 }
172
173 /*
174  * Convert unix to dos
175  */
176 char *unix2dos_format(char *str,BOOL overwrite)
177 {
178     char *p;
179     char *dp;
180
181     if (!mapsinited) initmaps();
182
183       if (overwrite) {
184           for (p = str; *p; p++) *p = unix2dos[(unsigned char)*p];
185           return str;
186       } else {
187           for (p = str, dp = cvtbuf; *p; p++,dp++) *dp = unix2dos[(unsigned char)*p];
188           *dp = 0;
189           return cvtbuf;
190       }
191 }
192
193 /*
194  * Convert dos to unix
195  */
196 char *dos2unix_format(char *str, BOOL overwrite)
197 {
198     char *p;
199     char *dp;
200
201     if (!mapsinited) initmaps();
202
203       if (overwrite) {
204           for (p = str; *p; p++) *p = dos2unix[(unsigned char)*p];
205           return str;
206       } else {
207           for (p = str, dp = cvtbuf; *p; p++,dp++) *dp = dos2unix[(unsigned char)*p];
208           *dp = 0;
209           return cvtbuf;
210       }
211 }
212
213
214 /*
215  * Interpret character set.
216  */
217 void interpret_character_set(char *str)
218 {
219     if (strequal (str, "iso8859-1")) {
220         init_iso8859_1();
221     } else if (strequal (str, "iso8859-2")) {
222         init_iso8859_2();
223     } else if (strequal (str, "iso8859-5")) {
224         init_iso8859_5();
225     } else if (strequal (str, "koi8-r")) {
226         init_koi8_r();
227     } else {
228         DEBUG(0,("unrecognized character set\n"));
229     }
230 }