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