source: trunk/src/NTDLL/rtlstr.c@ 33

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

Port: NTDLL from Wine-990508 (almost) completely ported. 64-bit integer arithmtic missing.

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