- changed the umask handling. We now set the umask to 0 and explicitly
[kai/samba.git] / source3 / lib / charset.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Character set handling
5    Copyright (C) Andrew Tridgell 1992-1995
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 #define CHARSET_C
23 #include "includes.h"
24
25 extern int DEBUGLEVEL;
26
27 char xx_dos_char_map[256];
28 char xx_upper_char_map[256];
29 char xx_lower_char_map[256];
30
31 char *dos_char_map = NULL;
32 char *upper_char_map = NULL;
33 char *lower_char_map = NULL;
34
35 static void add_dos_char(int lower, int upper)
36 {
37   DEBUG(6,("Adding chars 0%o 0%o\n",lower,upper));
38   if (lower) dos_char_map[(char)lower] = 1;
39   if (upper) dos_char_map[(char)upper] = 1;
40   if (lower && upper) {
41     lower_char_map[(char)upper] = (char)lower;
42     upper_char_map[(char)lower] = (char)upper;
43     lower_char_map[(char)lower] = (char)lower;
44     upper_char_map[(char)upper] = (char)upper;
45   }
46 }
47
48 /****************************************************************************
49 initialise the charset arrays
50 ****************************************************************************/
51 void charset_initialise(void)
52 {
53   int i;
54
55   dos_char_map = &xx_dos_char_map[128];
56   upper_char_map = &xx_upper_char_map[128];
57   lower_char_map = &xx_lower_char_map[128];
58
59   for (i= -128;i<=127;i++) {
60     dos_char_map[(char)i] = 0;
61   }
62
63   for (i=0;i<=127;i++) {
64     if (isalnum((char)i) || strchr("._^$~!#%&-{}()@'`",(char)i))
65       add_dos_char(i,0);
66   }
67
68   for (i= -128;i<=127;i++) {
69     char c = (char)i;
70     upper_char_map[i] = lower_char_map[i] = c;
71     if (isupper(c)) lower_char_map[c] = tolower(c);
72     if (islower(c)) upper_char_map[c] = toupper(c);
73   }
74
75   /* valid for all DOS PC */
76   add_dos_char(142,0);     /* A trema      */
77   add_dos_char(143,0);     /* A o          */
78   add_dos_char(144,0);     /* E '          */
79   add_dos_char(146,0);     /* AE           */
80   add_dos_char(153,0);     /* O trema      */
81   add_dos_char(154,0);     /* U trema      */
82   add_dos_char(165,0);     /* N tilda      */
83   add_dos_char(128,0);     /* C cedille    */
84   add_dos_char(156,0);     /* Pound        */
85   add_dos_char(183,0);     /* A `     (WIN)*/
86   add_dos_char(157,0);     /* Phi     (WIN)*/
87   add_dos_char(212,0);     /* E`      (WIN)*/
88 }
89
90
91 /*******************************************************************
92 add characters depending on a string passed by the user
93 ********************************************************************/
94 void add_char_string(char *s)
95 {
96   char *extra_chars = (char *)strdup(s);
97   char *t;
98   if (!extra_chars) return;
99
100   for (t=strtok(extra_chars," \t\r\n"); t; t=strtok(NULL," \t\r\n")) {
101     char c1=0,c2=0;
102     int i1=0,i2=0;
103     if (isdigit(*t) || (*t)=='-') {
104       sscanf(t,"%i:%i",&i1,&i2);
105       add_dos_char(i1,i2);
106     } else {
107       sscanf(t,"%c:%c",&c1,&c2);
108       add_dos_char(c1,c2);
109     }
110   }
111
112   free(extra_chars);
113 }