source: trunk/src/NTDLL/rtlstr.cpp@ 83

Last change on this file since 83 was 83, checked in by phaller, 26 years ago

Fix: various unicode fixes

File size: 14.0 KB
Line 
1/*
2 * Project Odin Software License can be found in LICENSE.TXT
3 * Win32 NT Runtime / NTDLL for OS/2
4 * Rtl string functions
5 *
6 * Copyright 1996-1998 Marcus Meissner
7 * Copyright 1998, 1999 Patrick Haller (phaller@gmx.net)
8 */
9
10#include "config.h"
11
12#include <stdlib.h>
13#include <string.h>
14#include <ctype.h>
15
16#include <os2win.h>
17#include <unicode.h>
18
19#include "ntdll.h"
20#include "windef.h"
21
22#ifdef HAVE_WCTYPE_H
23# include <wctype.h>
24#endif
25#include "wine/winestring.h"
26#include "winnls.h"
27
28
29
30/*
31 * STRING FUNCTIONS
32 */
33
34/**************************************************************************
35 * RtlAnsiStringToUnicodeString [NTDLL.269]
36 */
37DWORD WINAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING uni,
38 PANSI_STRING ansi,
39 BOOLEAN doalloc)
40{
41 DWORD unilen = (ansi->Length+1)*sizeof(WCHAR);
42
43 dprintf(("NTDLL: RtlAnsiStringToUnicodeString(%08xh,%08xh,%08xh)\n",
44 uni,
45 ansi,
46 doalloc));
47
48 if (unilen>0xFFFF)
49 return STATUS_INVALID_PARAMETER_2;
50
51 uni->Length = unilen;
52 if (doalloc)
53 {
54 uni->MaximumLength = unilen;
55 uni->Buffer = (WCHAR *)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,unilen);
56 if (!uni->Buffer)
57 return STATUS_NO_MEMORY;
58 }
59
60 if (unilen>uni->MaximumLength)
61 return STATUS_BUFFER_OVERFLOW;
62
63 AsciiToUnicodeN(ansi->Buffer,
64 uni->Buffer,
65 unilen/2);
66
67 return STATUS_SUCCESS;
68}
69
70
71/**************************************************************************
72 * RtlOemStringToUnicodeString [NTDLL.447]
73 */
74DWORD WINAPI RtlOemStringToUnicodeString(PUNICODE_STRING uni,
75 PSTRING ansi,
76 BOOLEAN doalloc)
77{
78 DWORD unilen = (ansi->Length+1)*sizeof(WCHAR);
79
80 dprintf(("NTDLL: RtlOemStringToUnicodeString(%08xh,%08xh,%08xh)\n",
81 uni,
82 ansi,
83 doalloc));
84
85 if (unilen>0xFFFF)
86 return STATUS_INVALID_PARAMETER_2;
87
88 uni->Length = unilen;
89 if (doalloc)
90 {
91 uni->MaximumLength = unilen;
92 uni->Buffer = (WCHAR *)HeapAlloc(GetProcessHeap(),
93 HEAP_ZERO_MEMORY,
94 unilen);
95
96 if (!uni->Buffer)
97 return STATUS_NO_MEMORY;
98 }
99
100 if (unilen>uni->MaximumLength)
101 return STATUS_BUFFER_OVERFLOW;
102
103 AsciiToUnicodeN(ansi->Buffer,
104 uni->Buffer,
105 unilen/2);
106 return STATUS_SUCCESS;
107}
108
109
110/**************************************************************************
111 * RtlMultiByteToUnicodeN [NTDLL.436]
112 * FIXME: multibyte support
113 */
114DWORD WINAPI RtlMultiByteToUnicodeN(LPWSTR unistr,
115 DWORD unilen,
116 LPDWORD reslen,
117 LPSTR oemstr,
118 DWORD oemlen)
119{
120 DWORD len;
121 LPWSTR x;
122
123 dprintf(("NTDLL: RtlMultiByteToUnicodeN(%08xh,%08xh,%08xh,%08xh,%08xh)\n",
124 unistr,
125 unilen,
126 reslen,
127 oemstr,
128 oemlen));
129
130 len = oemlen;
131
132 if (unilen/2 < len)
133 len = unilen/2;
134
135 x=(LPWSTR)HeapAlloc(GetProcessHeap(),
136 HEAP_ZERO_MEMORY,
137 (len+1)*sizeof(WCHAR));
138
139 AsciiToUnicodeN(oemstr,
140 x,
141 len+1);
142
143 memcpy(unistr,
144 x,
145 len*2);
146
147 if (reslen)
148 *reslen = len*2;
149
150 return 0;
151}
152
153
154/**************************************************************************
155 * RtlOemToUnicodeN [NTDLL.448]
156 */
157DWORD WINAPI RtlOemToUnicodeN(LPWSTR unistr,
158 DWORD unilen,
159 LPDWORD reslen,
160 LPSTR oemstr,
161 DWORD oemlen)
162{
163 DWORD len;
164 LPWSTR x;
165
166 dprintf(("NTDLL: RtlOemToUnicodeN(%08xh,%08xh,%08xh,%08xh,%08xh)\n",
167 unistr,
168 unilen,
169 reslen,
170 oemstr,
171 oemlen));
172
173 len = oemlen;
174
175 if (unilen/2 < len)
176 len = unilen/2;
177
178 x=(LPWSTR)HeapAlloc(GetProcessHeap(),
179 HEAP_ZERO_MEMORY,
180 (len+1)*sizeof(WCHAR));
181
182 AsciiToUnicodeN(oemstr,
183 x,
184 len+1);
185
186 memcpy(unistr,
187 x,
188 len*2);
189
190 if (reslen)
191 *reslen = len*2;
192
193 return 0;
194}
195
196
197/**************************************************************************
198 * RtlInitAnsiString [NTDLL.399]
199 */
200VOID WINAPI RtlInitAnsiString(PANSI_STRING target,
201 LPCSTR source)
202{
203 dprintf(("NTDLL: RtlInitAnsiString(%08xh, %08xh)\n",
204 target,
205 source));
206
207 target->Length = target->MaximumLength = 0;
208 target->Buffer = (LPSTR)source;
209 if (!source)
210 return;
211
212 target->MaximumLength = lstrlenA(target->Buffer);
213 target->Length = target->MaximumLength+1;
214}
215
216
217/**************************************************************************
218 * RtlInitOemString
219 */
220VOID WINAPI RtlInitOemString(POEM_STRING target,
221 LPCSTR source)
222{
223 dprintf(("NTDLL: RtlInitOemString(%08xh, %08xh)\n",
224 target,
225 source));
226
227 target->Length = target->MaximumLength = 0;
228 target->Buffer = (LPSTR)source;
229 if (!source)
230 return;
231
232 target->MaximumLength = lstrlenA(target->Buffer);
233 target->Length = target->MaximumLength+1;
234}
235
236
237
238/**************************************************************************
239 * RtlInitString [NTDLL.402]
240 */
241VOID WINAPI RtlInitString(PSTRING target,
242 LPCSTR source)
243{
244 dprintf (("NTDLL: RtlInitString(%08xh, %08xh)\n",
245 target,
246 source));
247
248 target->Length = target->MaximumLength = 0;
249 target->Buffer = (LPSTR)source;
250 if (!source)
251 return;
252
253 target->MaximumLength = lstrlenA(target->Buffer);
254 target->Length = target->MaximumLength+1;
255}
256
257
258/**************************************************************************
259 * RtlInitUnicodeString [NTDLL.403]
260 */
261VOID WINAPI RtlInitUnicodeString(PUNICODE_STRING target,
262 LPCWSTR source)
263{
264 dprintf(("NTDLL: RtlInitUnicodeString(%08xh,%08xh)\n",
265 target,
266 source));
267
268 target->Length = target->MaximumLength = 0;
269 target->Buffer = (LPWSTR)source;
270 if (!source)
271 return;
272
273 target->MaximumLength = lstrlenW(target->Buffer)*2;
274 target->Length = target->MaximumLength+2;
275}
276
277
278/**************************************************************************
279 * RtlFreeUnicodeString [NTDLL.377]
280 */
281VOID WINAPI RtlFreeUnicodeString(PUNICODE_STRING str)
282{
283 dprintf(("NTDLL: RtlFreeUnicodeString(%08xh)\n",
284 str));
285
286 if (str->Buffer)
287 HeapFree(GetProcessHeap(),
288 0,
289 str->Buffer);
290}
291
292
293/**************************************************************************
294 * RtlFreeAnsiString [NTDLL.373]
295 */
296VOID WINAPI RtlFreeAnsiString(PANSI_STRING AnsiString)
297{
298 dprintf(("NTDLL: RtlFreeAnsiString(%08xh)\n",
299 AnsiString));
300
301 if( AnsiString->Buffer )
302 HeapFree(GetProcessHeap(),
303 0,
304 AnsiString->Buffer);
305}
306
307
308/**************************************************************************
309 * RtlFreeOemString
310 */
311VOID WINAPI RtlFreeOemString(POEM_STRING OemString)
312{
313 dprintf(("NTDLL: RtlFreeOemString(%08xh)\n",
314 OemString));
315
316 if( OemString->Buffer )
317 HeapFree(GetProcessHeap(),
318 0,
319 OemString->Buffer);
320}
321
322
323
324/**************************************************************************
325 * RtlUnicodeToOemN [NTDLL.515]
326 */
327DWORD WINAPI RtlUnicodeToOemN(LPSTR oemstr,
328 DWORD oemlen,
329 LPDWORD reslen,
330 LPWSTR unistr,
331 DWORD unilen)
332{
333 DWORD len;
334 LPSTR x;
335
336 dprintf(("NTDLL: RtlUnicodeToOemN(%08xh,%08xh,%08xh,%08xh,%08xh)\n",
337 oemstr,
338 oemlen,
339 reslen,
340 unistr,
341 unilen));
342
343 len = oemlen;
344
345 if (unilen/2 < len)
346 len = unilen/2;
347
348 x=(LPSTR)HeapAlloc(GetProcessHeap(),
349 HEAP_ZERO_MEMORY,
350 len+1);
351
352 UnicodeToAsciiN(unistr,
353 x,
354 len+1);
355
356 memcpy(oemstr,
357 x,
358 len);
359
360 if (reslen)
361 *reslen = len;
362
363 return 0;
364}
365
366
367/**************************************************************************
368 * RtlUnicodeStringToOemString [NTDLL.511]
369 */
370DWORD WINAPI RtlUnicodeStringToOemString(PANSI_STRING oem,
371 PUNICODE_STRING uni,
372 BOOLEAN alloc)
373{
374 dprintf(("NTDLL: RtlUnicodeStringToOemString(%08xh,%08xh,%08xh)\n",
375 oem,
376 uni,
377 alloc));
378
379 if (alloc)
380 {
381 oem->Buffer = (LPSTR)HeapAlloc(GetProcessHeap(),
382 HEAP_ZERO_MEMORY,
383 uni->Length/2)+1;
384 oem->MaximumLength = uni->Length/2+1;
385 }
386
387 oem->Length = uni->Length/2;
388 UnicodeToAsciiN(uni->Buffer,
389 oem->Buffer,
390 oem->MaximumLength);
391 return 0;
392}
393
394/**************************************************************************
395 * RtlUnicodeStringToAnsiString [NTDLL.507]
396 */
397DWORD WINAPI RtlUnicodeStringToAnsiString(PANSI_STRING oem,
398 PUNICODE_STRING uni,
399 BOOLEAN alloc)
400{
401 dprintf(("NTDLL: RtlUnicodeStringToAnsiString(%08xh,%08xh,%08xh)\n",
402 oem,
403 uni,
404 alloc));
405
406 if (alloc)
407 {
408 oem->Buffer = (LPSTR)HeapAlloc(GetProcessHeap(),
409 HEAP_ZERO_MEMORY,
410 uni->Length/2)+1;
411 oem->MaximumLength = uni->Length/2+1;
412 }
413
414 oem->Length = uni->Length/2;
415 UnicodeToAsciiN(uni->Buffer,
416 oem->Buffer,
417 oem->MaximumLength);
418
419 return 0;
420}
421
422/**************************************************************************
423 * RtlEqualUnicodeString [NTDLL]
424 */
425DWORD WINAPI RtlEqualUnicodeString(PUNICODE_STRING s1,
426 PUNICODE_STRING s2,
427 DWORD x)
428{
429 dprintf(("NTDLL: RtlEqualUnicodeString(%08xh,%08xh,%08xh)\n",
430 s1,
431 s2,
432 x));
433
434 if (s1->Length != s2->Length)
435 return 1;
436
437 return !lstrncmpW(s1->Buffer,
438 s2->Buffer,
439 s1->Length/2);
440}
441
442
443/**************************************************************************
444 * RtlUpcaseUnicodeString [NTDLL.520]
445 */
446DWORD WINAPI RtlUpcaseUnicodeString(PUNICODE_STRING dest,
447 PUNICODE_STRING src,
448 BOOLEAN doalloc)
449{
450 LPWSTR s,t;
451 DWORD i,len;
452
453 dprintf(("NTDLL: RtlUpcaseUnicodeString(%08xh,%08xh,%08xh)\n",
454 dest,
455 src,
456 doalloc));
457
458 len = src->Length;
459 if (doalloc)
460 {
461 dest->MaximumLength = len;
462 dest->Buffer = (LPWSTR)HeapAlloc(GetProcessHeap(),
463 HEAP_ZERO_MEMORY,
464 len);
465 if (!dest->Buffer)
466 return STATUS_NO_MEMORY;
467 }
468
469 if (dest->MaximumLength < len)
470 return STATUS_BUFFER_OVERFLOW;
471
472 s=dest->Buffer;
473 t=src->Buffer;
474
475 /* len is in bytes */
476#if 0
477 for (i = 0;
478 i < len/2;
479 i++)
480 s[i] = towupper(t[i]);
481#endif
482 return STATUS_SUCCESS;
483}
484
485
486/**************************************************************************
487 * RtlxOemStringToUnicodeSize [NTDLL.549]
488 */
489UINT WINAPI RtlxOemStringToUnicodeSize(PSTRING str)
490{
491 dprintf(("NTDLL: RtlxOemStringToUnicodeSize(%08xh)\n",
492 str));
493
494 return str->Length*2+2;
495}
496
497
498/**************************************************************************
499 * RtlxAnsiStringToUnicodeSize [NTDLL.548]
500 */
501UINT WINAPI RtlxAnsiStringToUnicodeSize(PANSI_STRING str)
502{
503 dprintf(("NTDLL: RtlxAnsiStringToUnicodeSize(%08xh)\n",
504 str));
505
506 return str->Length*2+2;
507}
508
509
510/**************************************************************************
511 * RtlIsTextUnicode [NTDLL.417]
512 *
513 * Apply various feeble heuristics to guess whether
514 * the text buffer contains Unicode.
515 * FIXME: should implement more tests.
516 */
517DWORD WINAPI RtlIsTextUnicode(LPVOID buf,
518 DWORD len,
519 DWORD *pf)
520{
521 LPWSTR s = (LPWSTR)buf;
522 DWORD flags = -1,
523 out_flags = 0;
524
525 dprintf(("NTDLL: RtlIsTextUnicode(%08xh,%08xh,%08xh)\n",
526 buf,
527 len,
528 pf));
529
530 if (!len)
531 goto out;
532
533 if (pf)
534 flags = *pf;
535
536 /*
537 * Apply various tests to the text string. According to the
538 * docs, each test "passed" sets the corresponding flag in
539 * the output flags. But some of the tests are mutually
540 * exclusive, so I don't see how you could pass all tests ...
541 */
542
543 /* Check for an odd length ... pass if even. */
544 if (!(len & 1))
545 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
546
547 /* Check for the special unicode marker byte. */
548 if (*s == 0xFEFF)
549 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
550
551 /*
552 * Check whether the string passed all of the tests.
553 */
554 flags &= ITU_IMPLEMENTED_TESTS;
555 if ((out_flags & flags) != flags)
556 len = 0;
557
558out:
559 if (pf)
560 *pf = out_flags;
561
562 return len;
563}
564
565
566/******************************************************************************
567 * RtlCompareUnicodeString [NTDLL]
568 */
569DWORD WINAPI RtlCompareUnicodeString(PUNICODE_STRING String1,
570 PUNICODE_STRING String2,
571 BOOLEAN CaseInSensitive)
572{
573 dprintf(("NTDLL: RtlCompareUnicodeString(%08xh,%08xh,%08xh) not implemented.\n",
574 String1,
575 String2,
576 CaseInSensitive));
577
578 return 0;
579}
580
581
Note: See TracBrowser for help on using the repository browser.