2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 const char *unix_error_string (int error_num
);
26 extern int DEBUGLEVEL
;
29 * Codepage definitions.
33 /* lower->upper mapping for IBM Code Page 850 - MS-DOS Latin 1 */
34 unsigned char const cp_850
[][4] = {
35 /* dec col/row oct hex description */
36 /* 133 08/05 205 85 a grave */
37 /* 183 11/07 267 B7 A grave */ {0x85,0xB7,1,1},
38 /* 160 10/00 240 A0 a acute */
39 /* 181 11/05 265 B5 A acute */ {0xA0,0xB5,1,1},
40 /* 131 08/03 203 83 a circumflex */
41 /* 182 11/06 266 B6 A circumflex */ {0x83,0xB6,1,1},
42 /* 198 12/06 306 C6 a tilde */
43 /* 199 12/07 307 C7 A tilde */ {0xC6,0xC7,1,1},
44 /* 132 08/04 204 84 a diaeresis */
45 /* 142 08/14 216 8E A diaeresis */ {0x84,0x8E,1,1},
46 /* 134 08/06 206 86 a ring */
47 /* 143 08/15 217 8F A ring */ {0x86,0x8F,1,1},
48 /* 145 09/01 221 91 ae diphthong */
49 /* 146 09/02 222 92 AE diphthong */ {0x91,0x92,1,1},
50 /* 135 08/07 207 87 c cedilla */
51 /* 128 08/00 200 80 C cedilla */ {0x87,0x80,1,1},
52 /* 138 08/10 212 8A e grave */
53 /* 212 13/04 324 D4 E grave */ {0x8A,0xD4,1,1},
54 /* 130 08/02 202 82 e acute */
55 /* 144 09/00 220 90 E acute */ {0x82,0x90,1,1},
56 /* 136 08/08 210 88 e circumflex */
57 /* 210 13/02 322 D2 E circumflex */ {0x88,0xD2,1,1},
58 /* 137 08/09 211 89 e diaeresis */
59 /* 211 13/03 323 D3 E diaeresis */ {0x89,0xD3,1,1},
60 /* 141 08/13 215 8D i grave */
61 /* 222 13/14 336 DE I grave */ {0x8D,0xDE,1,1},
62 /* 161 10/01 241 A1 i acute */
63 /* 214 13/06 326 D6 I acute */ {0xA1,0xD6,1,1},
64 /* 140 08/12 214 8C i circumflex */
65 /* 215 13/07 327 D7 I circumflex */ {0x8C,0xD7,1,1},
66 /* 139 08/11 213 8B i diaeresis */
67 /* 216 13/08 330 D8 I diaeresis */ {0x8B,0xD8,1,1},
68 /* 208 13/00 320 D0 Icelandic eth */
69 /* 209 13/01 321 D1 Icelandic Eth */ {0xD0,0xD1,1,1},
70 /* 164 10/04 244 A4 n tilde */
71 /* 165 10/05 245 A5 N tilde */ {0xA4,0xA5,1,1},
72 /* 149 09/05 225 95 o grave */
73 /* 227 14/03 343 E3 O grave */ {0x95,0xE3,1,1},
74 /* 162 10/02 242 A2 o acute */
75 /* 224 14/00 340 E0 O acute */ {0xA2,0xE0,1,1},
76 /* 147 09/03 223 93 o circumflex */
77 /* 226 14/02 342 E2 O circumflex */ {0x93,0xE2,1,1},
78 /* 228 14/04 344 E4 o tilde */
79 /* 229 14/05 345 E5 O tilde */ {0xE4,0xE5,1,1},
80 /* 148 09/04 224 94 o diaeresis */
81 /* 153 09/09 231 99 O diaeresis */ {0x94,0x99,1,1},
82 /* 155 09/11 233 9B o slash */
83 /* 157 09/13 235 9D O slash */ {0x9B,0x9D,1,1},
84 /* 151 09/07 227 97 u grave */
85 /* 235 14/11 353 EB U grave */ {0x97,0xEB,1,1},
86 /* 163 10/03 243 A3 u acute */
87 /* 233 14/09 351 E9 U acute */ {0xA3,0xE9,1,1},
88 /* 150 09/06 226 96 u circumflex */
89 /* 234 14/10 352 EA U circumflex */ {0x96,0xEA,1,1},
90 /* 129 08/01 201 81 u diaeresis */
91 /* 154 09/10 232 9A U diaeresis */ {0x81,0x9A,1,1},
92 /* 236 14/12 354 EC y acute */
93 /* 237 14/13 355 ED Y acute */ {0xEC,0xED,1,1},
94 /* 231 14/07 347 E7 Icelandic thorn */
95 /* 232 14/08 350 E8 Icelandic Thorn */ {0xE7,0xE8,1,1},
97 {0x9C,0,0,0}, /* Pound */
101 /* lower->upper mapping for IBM Code Page 932 - MS-DOS Japanese SJIS */
102 unsigned char const cp_932
[][4] = {
107 char xx_dos_char_map
[256];
108 char xx_upper_char_map
[256];
109 char xx_lower_char_map
[256];
111 char *dos_char_map
= xx_dos_char_map
;
112 char *upper_char_map
= xx_upper_char_map
;
113 char *lower_char_map
= xx_lower_char_map
;
116 * This code has been extended to deal with ascynchronous mappings
117 * like MS-DOS Latin US (Code page 437) where things like :
118 * a acute are capitalized to 'A', but the reverse mapping
119 * must not hold true. This allows the filename case insensitive
120 * matching in do_match() to work, as the DOS/Win95/NT client
121 * uses 'A' as a mask to match against characters like a acute.
122 * This is the meaning behind the parameters that allow a
123 * mapping from lower to upper, but not upper to lower.
126 static void add_dos_char(int lower
, BOOL map_lower_to_upper
,
127 int upper
, BOOL map_upper_to_lower
)
131 DEBUGADD( 6, ( "Adding chars 0x%x 0x%x (l->u = %s) (u->l = %s)\n",
133 map_lower_to_upper
? "True" : "False",
134 map_upper_to_lower
? "True" : "False" ) );
135 if (lower
) dos_char_map
[lower
] = 1;
136 if (upper
) dos_char_map
[upper
] = 1;
137 lower_char_map
[lower
] = (char)lower
; /* Define tolower(lower) */
138 upper_char_map
[upper
] = (char)upper
; /* Define toupper(upper) */
139 if (lower
&& upper
) {
140 if(map_upper_to_lower
)
141 lower_char_map
[upper
] = (char)lower
;
142 if(map_lower_to_upper
)
143 upper_char_map
[lower
] = (char)upper
;
147 /****************************************************************************
148 initialise the charset arrays
149 ****************************************************************************/
150 void charset_initialise(void)
155 /* include <locale.h> in includes.h if available for OS */
156 /* we take only standard 7-bit ASCII definitions from ctype */
157 setlocale(LC_ALL
,"C");
160 for (i
= 0;i
<=255;i
++) {
164 for (i
=0;i
<=127;i
++) {
165 if (isalnum(i
) || strchr("._^$~!#%&-{}()@'`",(char)i
))
166 add_dos_char(i
,False
,0,False
);
169 for (i
=0; i
<=255; i
++) {
171 upper_char_map
[i
] = lower_char_map
[i
] = c
;
173 /* Some systems have buggy isupper/islower for characters
174 above 127. Best not to rely on them. */
176 if (isupper((int)c
)) lower_char_map
[i
] = tolower(c
);
177 if (islower((int)c
)) upper_char_map
[i
] = toupper(c
);
182 /****************************************************************************
183 load the client codepage.
184 ****************************************************************************/
186 typedef const unsigned char (*codepage_p
)[4];
188 static codepage_p
load_client_codepage( int client_codepage
)
190 pstring codepage_file_name
;
191 unsigned char buf
[8];
194 codepage_p cp_p
= NULL
;
197 DEBUG(5, ("load_client_codepage: loading codepage %d.\n", client_codepage
));
199 if(strlen(CODEPAGEDIR
) + 14 > sizeof(codepage_file_name
))
201 DEBUG(0,("load_client_codepage: filename too long to load\n"));
205 pstrcpy(codepage_file_name
, CODEPAGEDIR
);
206 pstrcat(codepage_file_name
, "/");
207 pstrcat(codepage_file_name
, "codepage.");
208 slprintf(&codepage_file_name
[strlen(codepage_file_name
)],
209 sizeof(pstring
)-(strlen(codepage_file_name
)+1),
213 if(sys_stat(codepage_file_name
,&st
)!=0)
215 DEBUG(0,("load_client_codepage: filename %s does not exist.\n",
216 codepage_file_name
));
220 /* Check if it is at least big enough to hold the required
221 data. Should be 2 byte version, 2 byte codepage, 4 byte length,
222 plus zero or more bytes of data. Note that the data cannot be more
223 than 4 * MAXCODEPAGELINES bytes.
227 if( size
< CODEPAGE_HEADER_SIZE
|| size
> (CODEPAGE_HEADER_SIZE
+ 4 * MAXCODEPAGELINES
))
229 DEBUG(0,("load_client_codepage: file %s is an incorrect size for a \
230 code page file (size=%d).\n", codepage_file_name
, (int)size
));
234 /* Read the first 8 bytes of the codepage file - check
235 the version number and code page number. All the data
236 is held in little endian format.
239 if((fp
= sys_fopen( codepage_file_name
, "r")) == NULL
)
241 DEBUG(0,("load_client_codepage: cannot open file %s. Error was %s\n",
242 codepage_file_name
, unix_error_string (errno
)));
246 if(fread( buf
, 1, CODEPAGE_HEADER_SIZE
, fp
)!=CODEPAGE_HEADER_SIZE
)
248 DEBUG(0,("load_client_codepage: cannot read header from file %s. Error was %s\n",
249 codepage_file_name
, unix_error_string (errno
)));
253 /* Check the version value */
254 if(SVAL(buf
,CODEPAGE_VERSION_OFFSET
) != CODEPAGE_FILE_VERSION_ID
)
256 DEBUG(0,("load_client_codepage: filename %s has incorrect version id. \
257 Needed %hu, got %hu.\n",
258 codepage_file_name
, (uint16
)CODEPAGE_FILE_VERSION_ID
,
259 SVAL(buf
,CODEPAGE_VERSION_OFFSET
)));
263 /* Check the codepage matches */
264 if(SVAL(buf
,CODEPAGE_CLIENT_CODEPAGE_OFFSET
) != (uint16
)client_codepage
)
266 DEBUG(0,("load_client_codepage: filename %s has incorrect codepage. \
267 Needed %hu, got %hu.\n",
268 codepage_file_name
, (uint16
)client_codepage
,
269 SVAL(buf
,CODEPAGE_CLIENT_CODEPAGE_OFFSET
)));
273 /* Check the length is correct. */
274 if(IVAL(buf
,CODEPAGE_LENGTH_OFFSET
) != (size
- CODEPAGE_HEADER_SIZE
))
276 DEBUG(0,("load_client_codepage: filename %s has incorrect size headers. \
277 Needed %u, got %u.\n", codepage_file_name
, (uint32
)(size
- CODEPAGE_HEADER_SIZE
),
278 IVAL(buf
,CODEPAGE_LENGTH_OFFSET
)));
282 size
-= CODEPAGE_HEADER_SIZE
; /* Remove header */
284 /* Make sure the size is a multiple of 4. */
287 DEBUG(0,("load_client_codepage: filename %s has a codepage size not a \
288 multiple of 4.\n", codepage_file_name
));
292 /* Allocate space for the code page file and read it all in. */
293 if((cp_p
= (codepage_p
)malloc( size
+ 4 )) == NULL
)
295 DEBUG(0,("load_client_codepage: malloc fail.\n"));
299 if(fread( (char *)cp_p
, 1, size
, fp
)!=size
)
301 DEBUG(0,("load_client_codepage: read fail on file %s. Error was %s.\n",
302 codepage_file_name
, unix_error_string (errno
)));
306 /* Ensure array is correctly terminated. */
307 memset(((char *)cp_p
) + size
, '\0', 4);
314 /* pseudo destructor :-) */
323 /****************************************************************************
324 initialise the client codepage.
325 ****************************************************************************/
326 void codepage_initialise(int client_codepage
)
329 static codepage_p cp
= NULL
;
334 ("codepage_initialise: called twice - ignoring second client code page = %d\n",
339 DEBUG(6,("codepage_initialise: client code page = %d\n", client_codepage
));
342 * Known client codepages - these can be added to.
344 cp
= load_client_codepage( client_codepage
);
349 DEBUG(6,("codepage_initialise: loading dynamic codepage file %s/codepage.%d \
350 for code page %d failed. Using default client codepage 932\n",
351 CODEPAGEDIR
, client_codepage
, client_codepage
));
353 client_codepage
= KANJI_CODEPAGE
;
355 DEBUG(6,("codepage_initialise: loading dynamic codepage file %s/codepage.%d \
356 for code page %d failed. Using default client codepage 850\n",
357 CODEPAGEDIR
, client_codepage
, client_codepage
));
359 client_codepage
= MSDOS_LATIN_1_CODEPAGE
;
364 * Setup the function pointers for the loaded codepage.
366 initialize_multibyte_vectors( client_codepage
);
370 for(i
= 0; !((cp
[i
][0] == '\0') && (cp
[i
][1] == '\0')); i
++)
371 add_dos_char(cp
[i
][0], (BOOL
)cp
[i
][2], cp
[i
][1], (BOOL
)cp
[i
][3]);
375 /*******************************************************************
376 add characters depending on a string passed by the user
377 ********************************************************************/
378 void add_char_string(const char *s
)
380 char *extra_chars
= (char *)strdup(s
);
382 if (!extra_chars
) return;
384 for (t
=strtok(extra_chars
," \t\r\n"); t
; t
=strtok(NULL
," \t\r\n")) {
387 if (isdigit((unsigned char)*t
) || (*t
)=='-') {
388 sscanf(t
,"%i:%i",&i1
,&i2
);
389 add_dos_char(i1
,True
,i2
,True
);
391 sscanf(t
,"%c:%c",&c1
,&c2
);
392 add_dos_char((unsigned char)c1
,True
,(unsigned char)c2
, True
);