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

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

Add: RtlUpcaseUnicodeStringToOemString added

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