Initial version imported to CVS
[sfrench/samba-autobuild/.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   }
44 }
45
46 /****************************************************************************
47 initialise the charset arrays
48 ****************************************************************************/
49 void charset_initialise(void)
50 {
51   int i;
52
53   dos_char_map = &xx_dos_char_map[128];
54   upper_char_map = &xx_upper_char_map[128];
55   lower_char_map = &xx_lower_char_map[128];
56
57   for (i= -128;i<=127;i++) {
58     dos_char_map[(char)i] = 0;
59   }
60
61   for (i=0;i<=127;i++) {
62     if (isalnum((char)i) || strchr("._^$~!#%&-{}()@'`",(char)i))
63       add_dos_char(i,0);
64   }
65
66   for (i= -128;i<=127;i++) {
67     char c = (char)i;
68     upper_char_map[i] = lower_char_map[i] = c;
69     if (isupper(c)) lower_char_map[c] = tolower(c);
70     if (islower(c)) upper_char_map[c] = toupper(c);
71   }
72
73   /* valid for all DOS PC */
74   add_dos_char(142,0);     /* A trema      */
75   add_dos_char(143,0);     /* A o          */
76   add_dos_char(144,0);     /* E '          */
77   add_dos_char(146,0);     /* AE           */
78   add_dos_char(153,0);     /* O trema      */
79   add_dos_char(154,0);     /* U trema      */
80   add_dos_char(165,0);     /* N tilda      */
81   add_dos_char(128,0);     /* C cedille    */
82   add_dos_char(156,0);     /* Pound        */
83   add_dos_char(183,0);     /* A `     (WIN)*/
84   add_dos_char(157,0);     /* Phi     (WIN)*/
85   add_dos_char(212,0);     /* E`      (WIN)*/
86 }
87
88
89 /*******************************************************************
90 add characters depending on a string passed by the user
91 ********************************************************************/
92 void add_char_string(char *s)
93 {
94   char *extra_chars = (char *)strdup(s);
95   char *t;
96   if (!extra_chars) return;
97
98   for (t=strtok(extra_chars," \t\r\n"); t; t=strtok(NULL," \t\r\n")) {
99     char c1=0,c2=0;
100     int i1=0,i2=0;
101     if (isdigit(*t) || (*t)=='-') {
102       sscanf(t,"%i:%i",&i1,&i2);
103       add_dos_char(i1,i2);
104     } else {
105       sscanf(t,"%c:%c",&c1,&c2);
106       add_dos_char(c1,c2);
107     }
108   }
109
110   free(extra_chars);
111 }