charcnv.c: Fixed silly bugs detected on IRIX.
[samba.git] / source3 / lib / charcnv.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Character set conversion Extensions
5    Copyright (C) Andrew Tridgell 1992-1997
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. May need more work ? */
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 update_map("\241\244\306\217\312\250\243\235\321\343\323\340\246\227\254\215");
97 update_map("\257\275\261\245\346\206\352\251\263\210\361\344\363\242\266\230");
98 update_map("\274\253\277\276");
99 }
100
101 /*
102  * Convert unix to dos
103  */
104 char *unix2dos_format(char *str,BOOL overwrite)
105 {
106     char *p;
107     char *dp;
108
109     if (!mapsinited) initmaps();
110     if (overwrite) {
111         for (p = str; *p; p++) *p = unix2dos[(unsigned char)*p];
112         return str;
113     } else {
114         for (p = str, dp = cvtbuf; *p; p++,dp++) *dp = unix2dos[(unsigned char)*p];
115         *dp = 0;
116         return cvtbuf;
117     }
118 }
119
120 /*
121  * Convert dos to unix
122  */
123 char *dos2unix_format(char *str, BOOL overwrite)
124 {
125     char *p;
126     char *dp;
127
128     if (!mapsinited) initmaps();
129     if (overwrite) {
130         for (p = str; *p; p++) *p = dos2unix[(unsigned char)*p];
131         return str;
132     } else {
133         for (p = str, dp = cvtbuf; *p; p++,dp++) *dp = dos2unix[(unsigned char)*p];
134         *dp = 0;
135         return cvtbuf;
136     }
137 }
138
139
140 /*
141  * Interpret character set.
142  */
143 int interpret_character_set(char *str, int def)
144 {
145
146     if (strequal (str, "iso8859-1")) {
147         init_iso8859_1();
148     } else if (strequal (str, "iso8859-2")) {
149         init_iso8859_2();
150     } else {
151         DEBUG(0,("unrecognized character set\n"));
152     }
153     return def;
154 }