1805ac5ab1956eb5caa0bb929da273d4b2a90a15
[reactos.git] / reactos / lib / ntdll / rtl / nls.c
1 /* $Id: nls.c,v 1.7 2003/03/16 13:07:02 chorns Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/ntdll/rtl/nls.c
6 * PURPOSE: National Language Support (NLS) functions
7 * UPDATE HISTORY:
8 * 20/08/99 Created by Emanuele Aliberti
9 * 10/11/99 Added translation functions.
10 *
11 * NOTE:
12 * Multi-byte code pages are not supported yet. Even single-byte code
13 * pages are not supported properly. Only stupid CHAR->WCHAR and
14 * WCHAR->CHAR (Attention: data loss!!!) translation is done.
15 *
16 * TODO:
17 * 1) Implement code to initialize the translation tables.
18 * 2) Use fixed translation table for translation.
19 * 3) Add loading of translation tables (NLS files).
20 * 4) Implement unicode upcase and downcase handling.
21 * 5) Add multi-byte translation code.
22 */
23
24 #include <ddk/ntddk.h>
25
26 #define NDEBUG
27 #include <debug.h>
28
29
30 BOOLEAN
31 NlsMbCodePageTag = FALSE;
32
33 BOOLEAN
34 NlsMbOemCodePageTag = FALSE;
35
36 BYTE
37 NlsLeadByteInfo = 0; /* ? */
38
39 USHORT
40 NlsOemLeadByteInfo = 0;
41
42 USHORT
43 NlsAnsiCodePage = 0;
44
45 USHORT
46 NlsOemCodePage = 0; /* not exported */
47
48 #if 0
49 PWCHAR NlsAnsiToUnicodeTable = NULL;
50 PWCHAR NlsOemToUnicodeTable = NULL;
51
52 PCHAR NlsUnicodeToAnsiTable = NULL;
53 PCHAR NlsUnicodeToOemTable = NULL;
54
55 PWCHAR NlsUnicodeUpcaseTable = NULL;
56 PWCHAR NlsUnicodeLowercaseTable = NULL;
57 #endif
58
59
60 /* FUNCTIONS *****************************************************************/
61
62 /*
63 * Missing functions:
64 * RtlInitCodePageTable
65 * RtlInitNlsTables
66 * RtlResetRtlTranslations
67 */
68
69 /*
70 * RtlConsoleMultiByteToUnicodeN@24
71 */
72
73 NTSTATUS
74 STDCALL
75 RtlCustomCPToUnicodeN (
76 PRTL_NLS_DATA NlsData,
77 PWCHAR UnicodeString,
78 ULONG UnicodeSize,
79 PULONG ResultSize,
80 PCHAR CustomString,
81 ULONG CustomSize)
82 {
83 ULONG Size = 0;
84 ULONG i;
85
86 if (NlsData->DbcsFlag == FALSE)
87 {
88 /* single-byte code page */
89 if (CustomSize > (UnicodeSize / sizeof(WCHAR)))
90 Size = UnicodeSize / sizeof(WCHAR);
91 else
92 Size = CustomSize;
93
94 if (ResultSize != NULL)
95 *ResultSize = Size * sizeof(WCHAR);
96
97 for (i = 0; i < Size; i++)
98 {
99 *UnicodeString = NlsData->MultiByteToUnicode[(int)*CustomString];
100 UnicodeString++;
101 CustomString++;
102 }
103 }
104 else
105 {
106 /* multi-byte code page */
107 /* FIXME */
108 assert(FALSE);
109 }
110
111 return STATUS_SUCCESS;
112 }
113
114
115 VOID
116 STDCALL
117 RtlGetDefaultCodePage (
118 PUSHORT AnsiCodePage,
119 PUSHORT OemCodePage
120 )
121 {
122 *AnsiCodePage = NlsAnsiCodePage;
123 *OemCodePage = NlsOemCodePage;
124 }
125
126
127 NTSTATUS
128 STDCALL
129 RtlMultiByteToUnicodeN (
130 PWCHAR UnicodeString,
131 ULONG UnicodeSize,
132 PULONG ResultSize,
133 PCHAR MbString,
134 ULONG MbSize
135 )
136 {
137 ULONG Size = 0;
138 ULONG i;
139
140 if (NlsMbCodePageTag == FALSE)
141 {
142 /* single-byte code page */
143 if (MbSize > (UnicodeSize / sizeof(WCHAR)))
144 Size = UnicodeSize / sizeof(WCHAR);
145 else
146 Size = MbSize;
147
148 if (ResultSize != NULL)
149 *ResultSize = Size * sizeof(WCHAR);
150
151 for (i = 0; i < Size; i++)
152 {
153 *UnicodeString = *MbString;
154 #if 0
155 *UnicodeString = NlsAnsiToUnicodeTable[*MbString];
156 #endif
157
158 UnicodeString++;
159 MbString++;
160 }
161 }
162 else
163 {
164 /* multi-byte code page */
165 /* FIXME */
166 assert(FALSE);
167 }
168
169 return STATUS_SUCCESS;
170 }
171
172
173 NTSTATUS
174 STDCALL
175 RtlMultiByteToUnicodeSize (
176 PULONG UnicodeSize,
177 PCHAR MbString,
178 ULONG MbSize
179 )
180 {
181 if (NlsMbCodePageTag == FALSE)
182 {
183 /* single-byte code page */
184 *UnicodeSize = MbSize * sizeof (WCHAR);
185 }
186 else
187 {
188 /* multi-byte code page */
189 /* FIXME */
190
191 }
192
193 return STATUS_SUCCESS;
194 }
195
196
197 NTSTATUS
198 STDCALL
199 RtlOemToUnicodeN (
200 PWCHAR UnicodeString,
201 ULONG UnicodeSize,
202 PULONG ResultSize,
203 PCHAR OemString,
204 ULONG OemSize
205 )
206 {
207 ULONG Size = 0;
208 ULONG i;
209
210 if (NlsMbOemCodePageTag == FALSE)
211 {
212 /* single-byte code page */
213 if (OemSize > (UnicodeSize / sizeof(WCHAR)))
214 Size = UnicodeSize / sizeof(WCHAR);
215 else
216 Size = OemSize;
217
218 if (ResultSize != NULL)
219 *ResultSize = Size * sizeof(WCHAR);
220
221 for (i = 0; i < Size; i++)
222 {
223 *UnicodeString = *OemString;
224 #if 0
225 *UnicodeString = NlsOemToUnicodeTable[*OemString];
226 #endif
227
228 UnicodeString++;
229 OemString++;
230 };
231 }
232 else
233 {
234 /* multi-byte code page */
235 /* FIXME */
236 assert(FALSE);
237 }
238
239 return STATUS_SUCCESS;
240 }
241
242
243 NTSTATUS
244 STDCALL
245 RtlUnicodeToCustomCPN (
246 PRTL_NLS_DATA NlsData,
247 PCHAR CustomString,
248 ULONG CustomSize,
249 PULONG ResultSize,
250 PWCHAR UnicodeString,
251 ULONG UnicodeSize
252 )
253 {
254 ULONG Size = 0;
255 ULONG i;
256
257 if (NlsData->DbcsFlag == 0)
258 {
259 /* single-byte code page */
260 if (UnicodeSize > (CustomSize * sizeof(WCHAR)))
261 Size = CustomSize;
262 else
263 Size = UnicodeSize / sizeof(WCHAR);
264
265 if (ResultSize != NULL)
266 *ResultSize = Size;
267
268 for (i = 0; i < Size; i++)
269 {
270 *CustomString = NlsData->UnicodeToMultiByte[*UnicodeString];
271 CustomString++;
272 UnicodeString++;
273 }
274 }
275 else
276 {
277 /* multi-byte code page */
278 /* FIXME */
279 assert(FALSE);
280 }
281
282 return STATUS_SUCCESS;
283 }
284
285
286 NTSTATUS
287 STDCALL
288 RtlUnicodeToMultiByteN (
289 PCHAR MbString,
290 ULONG MbSize,
291 PULONG ResultSize,
292 PWCHAR UnicodeString,
293 ULONG UnicodeSize
294 )
295 {
296 ULONG Size = 0;
297 ULONG i;
298
299 if (NlsMbCodePageTag == FALSE)
300 {
301 /* single-byte code page */
302 if (UnicodeSize > (MbSize * sizeof(WCHAR)))
303 Size = MbSize;
304 else
305 Size = UnicodeSize / sizeof(WCHAR);
306
307 if (ResultSize != NULL)
308 *ResultSize = Size;
309
310 for (i = 0; i < Size; i++)
311 {
312 *MbString = *UnicodeString;
313 #if 0
314 *MbString = UnicodeToAnsiTable[*UnicodeString];
315 #endif
316
317 MbString++;
318 UnicodeString++;
319 }
320 }
321 else
322 {
323 /* multi-byte code page */
324 /* FIXME */
325 assert(FALSE);
326 }
327
328 return STATUS_SUCCESS;
329 }
330
331
332 NTSTATUS
333 STDCALL
334 RtlUnicodeToMultiByteSize (
335 PULONG MbSize,
336 PWCHAR UnicodeString,
337 ULONG UnicodeSize
338 )
339 {
340 if (NlsMbCodePageTag == FALSE)
341 {
342 /* single-byte code page */
343 *MbSize = UnicodeSize / sizeof (WCHAR);
344 }
345 else
346 {
347 /* multi-byte code page */
348 /* FIXME */
349 *MbSize = 0;
350 assert(FALSE);
351 }
352
353 return STATUS_SUCCESS;
354 }
355
356
357 NTSTATUS
358 STDCALL
359 RtlUnicodeToOemN (
360 PCHAR OemString,
361 ULONG OemSize,
362 PULONG ResultSize,
363 PWCHAR UnicodeString,
364 ULONG UnicodeSize
365 )
366 {
367 ULONG Size = 0;
368 ULONG i;
369
370 if (NlsMbOemCodePageTag == FALSE)
371 {
372 /* single-byte code page */
373 if (UnicodeSize > (OemSize * sizeof(WCHAR)))
374 Size = OemSize;
375 else
376 Size = UnicodeSize / sizeof(WCHAR);
377
378 if (ResultSize != NULL)
379 *ResultSize = Size;
380
381 for (i = 0; i < Size; i++)
382 {
383 *OemString = *UnicodeString;
384 #if 0
385 *OemString = UnicodeToOemTable[*UnicodeString];
386 #endif
387
388 OemString++;
389 UnicodeString++;
390 };
391 }
392 else
393 {
394 /* multi-byte code page */
395 /* FIXME */
396 assert(FALSE);
397 }
398
399 return STATUS_SUCCESS;
400 }
401
402
403 NTSTATUS
404 STDCALL
405 RtlUpcaseUnicodeToCustomCPN (
406 PRTL_NLS_DATA NlsData,
407 PCHAR CustomString,
408 ULONG CustomSize,
409 PULONG ResultSize,
410 PWCHAR UnicodeString,
411 ULONG UnicodeSize
412 )
413 {
414 WCHAR UpcaseChar;
415 ULONG Size = 0;
416 ULONG i;
417
418 if (NlsData->DbcsFlag == 0)
419 {
420 /* single-byte code page */
421 if (UnicodeSize > (CustomSize * sizeof(WCHAR)))
422 Size = CustomSize;
423 else
424 Size = UnicodeSize / sizeof(WCHAR);
425
426 if (ResultSize != NULL)
427 *ResultSize = Size;
428
429 for (i = 0; i < Size; i++)
430 {
431 *CustomString = NlsData->UnicodeToMultiByte[*UnicodeString];
432 #if 0
433 UpcaseChar = NlsUnicodeUpcaseTable[*UnicodeString];
434 *CustomString = NlsData->UnicodeToMultiByte[UpcaseChar];
435 #endif
436 CustomString++;
437 UnicodeString++;
438 }
439 }
440 else
441 {
442 /* multi-byte code page */
443 /* FIXME */
444 assert(FALSE);
445 }
446
447 return STATUS_SUCCESS;
448 }
449
450
451 NTSTATUS
452 STDCALL
453 RtlUpcaseUnicodeToMultiByteN (
454 PCHAR MbString,
455 ULONG MbSize,
456 PULONG ResultSize,
457 PWCHAR UnicodeString,
458 ULONG UnicodeSize
459 )
460 {
461 WCHAR UpcaseChar;
462 ULONG Size = 0;
463 ULONG i;
464
465 if (NLS_MB_CODE_PAGE_TAG == FALSE)
466 {
467 /* single-byte code page */
468 if (UnicodeSize > (MbSize * sizeof(WCHAR)))
469 Size = MbSize;
470 else
471 Size = UnicodeSize / sizeof(WCHAR);
472
473 if (ResultSize != NULL)
474 *ResultSize = Size;
475
476 for (i = 0; i < Size; i++)
477 {
478 *MbString = *UnicodeString;
479 #if 0
480 UpcaseChar = NlsUnicodeUpcaseTable[*UnicodeString];
481 *MbString = NlsUnicodeToAnsiTable[UpcaseChar];
482 #endif
483
484 MbString++;
485 UnicodeString++;
486 }
487 }
488 else
489 {
490 /* multi-byte code page */
491 /* FIXME */
492 assert(FALSE);
493 }
494
495 return STATUS_SUCCESS;
496 }
497
498
499 NTSTATUS
500 STDCALL
501 RtlUpcaseUnicodeToOemN (
502 PCHAR OemString,
503 ULONG OemSize,
504 PULONG ResultSize,
505 PWCHAR UnicodeString,
506 ULONG UnicodeSize
507 )
508 {
509 WCHAR UpcaseChar;
510 ULONG Size = 0;
511 ULONG i;
512
513 if (NLS_MB_OEM_CODE_PAGE_TAG == FALSE)
514 {
515 /* single-byte code page */
516 if (UnicodeSize > (OemSize * sizeof(WCHAR)))
517 Size = OemSize;
518 else
519 Size = UnicodeSize / sizeof(WCHAR);
520
521 if (ResultSize != NULL)
522 *ResultSize = Size;
523
524 for (i = 0; i < Size; i++)
525 {
526 *OemString = *UnicodeString;
527 #if 0
528 UpcaseChar = NlsUnicodeUpcaseTable[*UnicodeString];
529 *OemString = UnicodeToOemTable[UpcaseChar];
530 #endif
531
532 OemString++;
533 UnicodeString++;
534 }
535 }
536 else
537 {
538 /* multi-byte code page */
539 /* FIXME */
540 assert(FALSE);
541 }
542
543 return STATUS_SUCCESS;
544 }
545
546 /* EOF */