Remove LOCAL_*() calls from USER, use Local*16() instead.
[wine] / dlls / ntdll / rtlstr.c
1 /*
2  * Rtl string functions
3  *
4  * Copyright (C) 1996-1998 Marcus Meissner
5  * Copyright (C) 2000      Alexandre Julliard
6  * Copyright (C) 2003      Thomas Mertes
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36 #include "ntdll_misc.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
39
40 #define GUID_STRING_LENGTH    38
41
42 UINT NlsAnsiCodePage = 0;
43 BYTE NlsMbCodePageTag = 0;
44 BYTE NlsMbOemCodePageTag = 0;
45
46 static const union cptable *ansi_table;
47 static const union cptable *oem_table;
48 static const union cptable* unix_table; /* NULL if UTF8 */
49
50
51 /**************************************************************************
52  *      __wine_init_codepages   (NTDLL.@)
53  *
54  * Set the code page once kernel32 is loaded. Should be done differently.
55  */
56 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
57                             const union cptable *ucp)
58 {
59     ansi_table = ansi;
60     oem_table = oem;
61     unix_table = ucp;
62     NlsAnsiCodePage = ansi->info.codepage;
63 }
64
65 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
66
67     return (unix_table) ?
68         wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
69         wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
70 }
71
72 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
73                     const char* defchar, int *used )
74 {
75     if (unix_table)
76         return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
77     if (used) *used = 0;  /* all chars are valid for UTF-8 */
78     return wine_utf8_wcstombs( src, srclen, dst, dstlen );
79 }
80
81 /**************************************************************************
82  *      RtlInitAnsiString   (NTDLL.@)
83  *
84  * Initializes a buffered ansi string.
85  *
86  * RETURNS
87  *  Nothing.
88  *
89  * NOTES
90  *  Assigns source to target->Buffer. The length of source is assigned to
91  *  target->Length and target->MaximumLength. If source is NULL the length
92  *  of source is assumed to be 0.
93  */
94 void WINAPI RtlInitAnsiString(
95     PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
96     PCSZ source)         /* [I]   '\0' terminated string used to initialize target */
97 {
98     if ((target->Buffer = (PCHAR) source))
99     {
100         target->Length = strlen(source);
101         target->MaximumLength = target->Length + 1;
102     }
103     else target->Length = target->MaximumLength = 0;
104 }
105
106
107 /**************************************************************************
108  *      RtlInitString   (NTDLL.@)
109  *
110  * Initializes a buffered string.
111  *
112  * RETURNS
113  *  Nothing.
114  *
115  * NOTES
116  *  Assigns source to target->Buffer. The length of source is assigned to
117  *  target->Length and target->MaximumLength. If source is NULL the length
118  *  of source is assumed to be 0.
119  */
120 void WINAPI RtlInitString(
121     PSTRING target, /* [I/O] Buffered string to be initialized */
122     PCSZ source)    /* [I]   '\0' terminated string used to initialize target */
123 {
124     RtlInitAnsiString( target, source );
125 }
126
127
128 /**************************************************************************
129  *      RtlFreeAnsiString   (NTDLL.@)
130  */
131 void WINAPI RtlFreeAnsiString( PSTRING str )
132 {
133     if (str->Buffer) RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
134 }
135
136
137 /**************************************************************************
138  *      RtlFreeOemString   (NTDLL.@)
139  */
140 void WINAPI RtlFreeOemString( PSTRING str )
141 {
142     RtlFreeAnsiString( str );
143 }
144
145
146 /**************************************************************************
147  *      RtlCopyString   (NTDLL.@)
148  */
149 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
150 {
151     if (src)
152     {
153         unsigned int len = min( src->Length, dst->MaximumLength );
154         memcpy( dst->Buffer, src->Buffer, len );
155         dst->Length = len;
156     }
157     else dst->Length = 0;
158 }
159
160
161 /**************************************************************************
162  *      RtlInitUnicodeString   (NTDLL.@)
163  *
164  * Initializes a buffered unicode string.
165  *
166  * RETURNS
167  *  Nothing.
168  *
169  * NOTES
170  *  Assigns source to target->Buffer. The length of source is assigned to
171  *  target->Length and target->MaximumLength. If source is NULL the length
172  *  of source is assumed to be 0.
173  */
174 void WINAPI RtlInitUnicodeString(
175     PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
176     PCWSTR source)          /* [I]   '\0' terminated unicode string used to initialize target */
177 {
178     if ((target->Buffer = (PWSTR) source))
179     {
180         target->Length = strlenW(source) * sizeof(WCHAR);
181         target->MaximumLength = target->Length + sizeof(WCHAR);
182     }
183     else target->Length = target->MaximumLength = 0;
184 }
185
186
187 /**************************************************************************
188  *      RtlInitUnicodeStringEx   (NTDLL.@)
189  *
190  * Initializes a buffered unicode string.
191  *
192  * RETURNS
193  *  Success: STATUS_SUCCESS. target is initialized.
194  *  Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
195  *
196  * NOTES
197  *  Assigns source to target->Buffer. The length of source is assigned to
198  *  target->Length and target->MaximumLength. If source is NULL the length
199  *  of source is assumed to be 0.
200  */
201 NTSTATUS WINAPI RtlInitUnicodeStringEx(
202     PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
203     PCWSTR source)          /* [I]   '\0' terminated unicode string used to initialize target */
204 {
205     if (source != NULL) {
206         unsigned int len = strlenW(source) * sizeof(WCHAR);
207
208         if (len > 0xFFFC) {
209             return STATUS_NAME_TOO_LONG;
210         } else {
211             target->Length = len;
212             target->MaximumLength = len + sizeof(WCHAR);
213             target->Buffer = (PWSTR) source;
214         } /* if */
215     } else {
216         target->Length = 0;
217         target->MaximumLength = 0;
218         target->Buffer = NULL;
219     } /* if */
220     return STATUS_SUCCESS;
221 }
222
223
224 /**************************************************************************
225  *      RtlCreateUnicodeString   (NTDLL.@)
226  *
227  * Creates a UNICODE_STRING from a null-terminated Unicode string.
228  *
229  * RETURNS
230  *     Success: TRUE
231  *     Failure: FALSE
232  */
233 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
234 {
235     int len = (strlenW(src) + 1) * sizeof(WCHAR);
236     if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
237     memcpy( target->Buffer, src, len );
238     target->MaximumLength = len;
239     target->Length = len - sizeof(WCHAR);
240     return TRUE;
241 }
242
243
244 /**************************************************************************
245  *      RtlCreateUnicodeStringFromAsciiz   (NTDLL.@)
246  *
247  * Creates a UNICODE_STRING from a null-terminated Ascii string.
248  *
249  * RETURNS
250  *     Success: TRUE
251  *     Failure: FALSE
252  */
253 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
254 {
255     STRING ansi;
256     RtlInitAnsiString( &ansi, src );
257     return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
258 }
259
260
261 /**************************************************************************
262  *      RtlFreeUnicodeString   (NTDLL.@)
263  *
264  * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or 
265  * RtlCreateUnicodeStringFromAsciiz().
266  *
267  * RETURNS
268  *     nothing
269  */
270 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
271 {
272     if (str->Buffer) RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
273 }
274
275
276 /**************************************************************************
277  *      RtlCopyUnicodeString   (NTDLL.@)
278  *
279  * Copies from one UNICODE_STRING to another.
280  *
281  * RETURNS
282  *     nothing
283  */
284 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
285 {
286     if (src)
287     {
288         unsigned int len = min( src->Length, dst->MaximumLength );
289         memcpy( dst->Buffer, src->Buffer, len );
290         dst->Length = len;
291         /* append terminating '\0' if enough space */
292         if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
293     }
294     else dst->Length = 0;
295 }
296
297
298 /**************************************************************************
299  *      RtlDuplicateUnicodeString   (NTDLL.@)
300  *
301  * Duplicates an unicode string.
302  *
303  * RETURNS
304  *  Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
305  *  Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
306  *           STATUS_NO_MEMORY, if the allocation fails.
307  *
308  * NOTES
309  *  For add_nul there are several possible values:
310  *  0 = destination will not be '\0' terminated,
311  *  1 = destination will be '\0' terminated,
312  *  3 = like 1 but for an empty source string produce '\0' terminated empty
313  *     Buffer instead of assigning NULL to the Buffer.
314  *  Other add_nul values are invalid.
315  */
316 NTSTATUS WINAPI RtlDuplicateUnicodeString(
317     int add_nul,                  /* [I] flag */
318     const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
319     UNICODE_STRING *destination)  /* [O] destination for the duplicated unicode string */
320 {
321     if (source == NULL || destination == NULL ||
322         source->Length > source->MaximumLength ||
323         (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
324         add_nul == 2 || add_nul >= 4 || add_nul < 0) {
325         return STATUS_INVALID_PARAMETER;
326     } else {
327         if (source->Length == 0 && add_nul != 3) {
328             destination->Length = 0;
329             destination->MaximumLength = 0;
330             destination->Buffer = NULL;
331         } else {
332             unsigned int destination_max_len = source->Length;
333
334             if (add_nul) {
335                 destination_max_len += sizeof(WCHAR);
336             } /* if */
337             destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
338             if (destination->Buffer == NULL) {
339                 return STATUS_NO_MEMORY;
340             } else {
341                 memcpy(destination->Buffer, source->Buffer, source->Length);
342                 destination->Length = source->Length;
343                 destination->MaximumLength = source->Length;
344                 /* append terminating '\0' if enough space */
345                 if (add_nul) {
346                     destination->MaximumLength = destination_max_len;
347                     destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
348                 } /* if */
349             } /* if */
350         } /* if */
351     } /* if */
352     return STATUS_SUCCESS;
353 }
354
355
356 /**************************************************************************
357  *      RtlEraseUnicodeString   (NTDLL.@)
358  *
359  * Overwrites a UNICODE_STRING with zeros.
360  *
361  * RETURNS
362  *     nothing
363  */
364 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
365 {
366     if (str->Buffer)
367     {
368         memset( str->Buffer, 0, str->MaximumLength );
369         str->Length = 0;
370     }
371 }
372
373
374 /*
375     COMPARISON FUNCTIONS
376 */
377
378
379 /******************************************************************************
380  *      RtlCompareString   (NTDLL.@)
381  */
382 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
383 {
384     unsigned int len;
385     LONG ret = 0;
386     LPCSTR p1, p2;
387
388     len = min(s1->Length, s2->Length);
389     p1 = s1->Buffer;
390     p2 = s2->Buffer;
391
392     if (CaseInsensitive)
393     {
394         while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
395     }
396     else
397     {
398         while (!ret && len--) ret = *p1++ - *p2++;
399     }
400     if (!ret) ret = s1->Length - s2->Length;
401     return ret;
402 }
403
404
405 /******************************************************************************
406  *      RtlCompareUnicodeString   (NTDLL.@)
407  */
408 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
409                                      BOOLEAN CaseInsensitive )
410 {
411     unsigned int len;
412     LONG ret = 0;
413     LPCWSTR p1, p2;
414
415     len = min(s1->Length, s2->Length) / sizeof(WCHAR);
416     p1 = s1->Buffer;
417     p2 = s2->Buffer;
418
419     if (CaseInsensitive)
420     {
421         while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
422     }
423     else
424     {
425         while (!ret && len--) ret = *p1++ - *p2++;
426     }
427     if (!ret) ret = s1->Length - s2->Length;
428     return ret;
429 }
430
431
432 /**************************************************************************
433  *      RtlEqualString   (NTDLL.@)
434  *
435  * Determine if two strings are equal.
436  *
437  * PARAMS
438  *  s1              [I] Source string
439  *  s2              [I] String to compare to s1
440  *  CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
441  *
442  * RETURNS
443  *  Non-zero if s1 is equal to s2, 0 otherwise.
444  */
445 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
446 {
447     if (s1->Length != s2->Length) return FALSE;
448     return !RtlCompareString( s1, s2, CaseInsensitive );
449 }
450
451
452 /**************************************************************************
453  *      RtlEqualUnicodeString   (NTDLL.@)
454  *
455  * Unicode version of RtlEqualString.
456  */
457 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
458                                       BOOLEAN CaseInsensitive )
459 {
460     if (s1->Length != s2->Length) return FALSE;
461     return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
462 }
463
464
465 /**************************************************************************
466  *      RtlPrefixString   (NTDLL.@)
467  *
468  * Determine if one string is a prefix of another.
469  *
470  * PARAMS
471  *  s1          [I] Prefix to look for in s2
472  *  s2          [I] String that may contain s1 as a prefix
473  *  ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
474  *
475  * RETURNS
476  *  TRUE if s2 contains s1 as a prefix, FALSE otherwise.
477  */
478 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
479 {
480     unsigned int i;
481
482     if (s1->Length > s2->Length) return FALSE;
483     if (ignore_case)
484     {
485         for (i = 0; i < s1->Length; i++)
486             if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
487     }
488     else
489     {
490         for (i = 0; i < s1->Length; i++)
491             if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
492     }
493     return TRUE;
494 }
495
496
497 /**************************************************************************
498  *      RtlPrefixUnicodeString   (NTDLL.@)
499  *
500  * Unicode version of RtlPrefixString.
501  */
502 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
503                                        const UNICODE_STRING *s2,
504                                        BOOLEAN ignore_case )
505 {
506     unsigned int i;
507
508     if (s1->Length > s2->Length) return FALSE;
509     if (ignore_case)
510     {
511         for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
512             if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
513     }
514     else
515     {
516         for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
517             if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
518     }
519     return TRUE;
520 }
521
522
523 /**************************************************************************
524  *      RtlEqualComputerName   (NTDLL.@)
525  *
526  * Determine if two computer names are the same.
527  *
528  * PARAMS
529  *  left  [I] First computer name
530  *  right [I] Second computer name
531  *
532  * RETURNS
533  *  0 if the names are equal, non-zero otherwise.
534  *
535  * NOTES
536  *  The comparison is case insensitive.
537  */
538 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
539                                      const UNICODE_STRING *right)
540 {
541     NTSTATUS ret;
542     STRING upLeft, upRight;
543
544     if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
545     {
546        if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
547        {
548          ret = RtlEqualString( &upLeft, &upRight, FALSE );
549          RtlFreeOemString( &upRight );
550        }
551        RtlFreeOemString( &upLeft );
552     }
553     return ret;
554 }
555
556
557 /**************************************************************************
558  *      RtlEqualDomainName   (NTDLL.@)
559  *
560  * Determine if two domain names are the same.
561  *
562  * PARAMS
563  *  left  [I] First domain name
564  *  right [I] Second domain name
565  *
566  * RETURNS
567  *  0 if the names are equal, non-zero otherwise.
568  *
569  * NOTES
570  *  The comparison is case insensitive.
571  */
572 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
573                                    const UNICODE_STRING *right)
574 {
575     return RtlEqualComputerName(left, right);
576 }
577
578
579 /*
580         COPY BETWEEN ANSI_STRING or UNICODE_STRING
581         there is no parameter checking, it just crashes
582 */
583
584
585 /**************************************************************************
586  *      RtlAnsiStringToUnicodeString   (NTDLL.@)
587  *
588  * Converts an ansi string to an unicode string.
589  *
590  * RETURNS
591  *  Success: STATUS_SUCCESS. uni contains the converted string
592  *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
593  *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
594  *           STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
595  *
596  * NOTES
597  *  This function always writes a terminating '\0'.
598  */
599 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
600     PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
601     PCANSI_STRING ansi,  /* [I]   Ansi string to be converted */
602     BOOLEAN doalloc)     /* [I]   TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
603 {
604     DWORD total = RtlAnsiStringToUnicodeSize( ansi );
605
606     if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
607     uni->Length = total - sizeof(WCHAR);
608     if (doalloc)
609     {
610         uni->MaximumLength = total;
611         if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
612             return STATUS_NO_MEMORY;
613     }
614     else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
615
616     RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
617     uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
618     return STATUS_SUCCESS;
619 }
620
621
622 /**************************************************************************
623  *      RtlOemStringToUnicodeString   (NTDLL.@)
624  *
625  * Converts an oem string to an unicode string.
626  *
627  * RETURNS
628  *  Success: STATUS_SUCCESS. uni contains the converted string
629  *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
630  *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
631  *           STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
632  *
633  * NOTES
634  *  This function always writes a terminating '\0'.
635  */
636 NTSTATUS WINAPI RtlOemStringToUnicodeString(
637     UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
638     const STRING *oem,   /* [I]   Oem string to be converted */
639     BOOLEAN doalloc)     /* [I]   TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
640 {
641     DWORD total = RtlOemStringToUnicodeSize( oem );
642
643     if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
644     uni->Length = total - sizeof(WCHAR);
645     if (doalloc)
646     {
647         uni->MaximumLength = total;
648         if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
649             return STATUS_NO_MEMORY;
650     }
651     else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
652
653     RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
654     uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
655     return STATUS_SUCCESS;
656 }
657
658
659 /**************************************************************************
660  *      RtlUnicodeStringToAnsiString   (NTDLL.@)
661  *
662  * Converts an unicode string to an ansi string.
663  *
664  * RETURNS
665  *  Success: STATUS_SUCCESS. ansi contains the converted string
666  *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
667  *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
668  *
669  * NOTES
670  *  This function always writes a terminating '\0'.
671  *  It performs a partial copy if ansi is too small.
672  */
673 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
674     STRING *ansi,              /* [I/O] Destination for the ansi string */
675     const UNICODE_STRING *uni, /* [I]   Unicode string to be converted */
676     BOOLEAN doalloc)           /* [I]   TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
677 {
678     NTSTATUS ret = STATUS_SUCCESS;
679     DWORD len = RtlUnicodeStringToAnsiSize( uni );
680
681     ansi->Length = len - 1;
682     if (doalloc)
683     {
684         ansi->MaximumLength = len;
685         if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
686             return STATUS_NO_MEMORY;
687     }
688     else if (ansi->MaximumLength < len)
689     {
690         if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
691         ansi->Length = ansi->MaximumLength - 1;
692         ret = STATUS_BUFFER_OVERFLOW;
693     }
694
695     RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
696     ansi->Buffer[ansi->Length] = 0;
697     return ret;
698 }
699
700
701 /**************************************************************************
702  *      RtlUnicodeStringToOemString   (NTDLL.@)
703  *
704  * Converts a Rtl Unicode string to an OEM string.
705  *
706  * PARAMS
707  *  oem     [O] Destination for OEM string
708  *  uni     [I] Source Unicode string
709  *  doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
710  *
711  * RETURNS
712  *  Success: STATUS_SUCCESS. oem contains the converted string
713  *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
714  *           STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
715  *
716  * NOTES
717  *   If doalloc is TRUE, the length allocated is uni->Length + 1.
718  *   This function always '\0' terminates the string returned.
719  */
720 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
721                                              const UNICODE_STRING *uni,
722                                              BOOLEAN doalloc )
723 {
724     NTSTATUS ret = STATUS_SUCCESS;
725     DWORD len = RtlUnicodeStringToOemSize( uni );
726
727     oem->Length = len - 1;
728     if (doalloc)
729     {
730         oem->MaximumLength = len;
731         if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
732             return STATUS_NO_MEMORY;
733     }
734     else if (oem->MaximumLength < len)
735     {
736         if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
737         oem->Length = oem->MaximumLength - 1;
738         ret = STATUS_BUFFER_OVERFLOW;
739     }
740
741     RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
742     oem->Buffer[oem->Length] = 0;
743     return ret;
744 }
745
746
747 /**************************************************************************
748  *      RtlMultiByteToUnicodeN   (NTDLL.@)
749  *
750  * Converts a multi-byte string to a Unicode string.
751  *
752  * RETURNS
753  *  NTSTATUS code
754  *
755  * NOTES
756  *  Performs a partial copy if dst is too small.
757  */
758 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
759                                         LPCSTR src, DWORD srclen )
760 {
761
762     int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
763     if (reslen)
764         *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
765     return STATUS_SUCCESS;
766 }
767
768
769 /**************************************************************************
770  *      RtlOemToUnicodeN   (NTDLL.@)
771  *
772  * Converts a multi-byte string in the OEM code page to a Unicode string.
773  *
774  * RETURNS
775  *  NTSTATUS code
776  */
777 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
778                                   LPCSTR src, DWORD srclen )
779 {
780     int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
781     if (reslen)
782         *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
783     return STATUS_SUCCESS;
784 }
785
786
787 /**************************************************************************
788  *      RtlUnicodeToMultiByteN   (NTDLL.@)
789  *
790  * Converts a Unicode string to a multi-byte string in the ANSI code page.
791  *
792  * RETURNS
793  *  NTSTATUS code
794  */
795 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
796                                         LPCWSTR src, DWORD srclen )
797 {
798     int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
799                                 dst, dstlen, NULL, NULL );
800     if (reslen)
801         *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
802     return STATUS_SUCCESS;
803 }
804
805
806 /**************************************************************************
807  *      RtlUnicodeToOemN   (NTDLL.@)
808  *
809  * Converts a Unicode string to a multi-byte string in the OEM code page.
810  *
811  * RETURNS
812  *  NTSTATUS code
813  */
814 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
815                                   LPCWSTR src, DWORD srclen )
816 {
817     int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
818                                 dst, dstlen, NULL, NULL );
819     if (reslen)
820         *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
821     return STATUS_SUCCESS;
822 }
823
824
825 /*
826      CASE CONVERSIONS
827 */
828
829
830 /**************************************************************************
831  *      RtlUpperChar   (NTDLL.@)
832  *
833  * Converts an Ascii character to uppercase.
834  *
835  * PARAMS
836  *  ch [I] Character to convert
837  *
838  * RETURNS
839  *  The uppercase character value.
840  *
841  * NOTES
842  *  For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
843  *  All other input characters are returned unchanged. The locale and
844  *  multibyte characters are not taken into account (as native DLL).
845  */
846 CHAR WINAPI RtlUpperChar( CHAR ch )
847 {
848     if (ch >= 'a' && ch <= 'z') {
849         return ch - 'a' + 'A';
850     } else {
851         return ch;
852     } /* if */
853 }
854
855
856 /**************************************************************************
857  *      RtlUpperString   (NTDLL.@)
858  *
859  * Converts an Ascii string to uppercase.
860  *
861  * PARAMS
862  *  dst [O] Destination for converted string
863  *  src [I] Source string to convert
864  *
865  * RETURNS
866  *  Nothing.
867  *
868  * NOTES
869  *  For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
870  *  All other src characters are copied unchanged to dst. The locale and
871  *  multibyte characters are not taken into account (as native DLL).
872  *  The number of character copied is the minimum of src->Length and
873  *  the dst->MaximumLength.
874  */
875 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
876 {
877     unsigned int i, len = min(src->Length, dst->MaximumLength);
878
879     for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
880     dst->Length = len;
881 }
882
883
884 /**************************************************************************
885  *      RtlUpcaseUnicodeChar   (NTDLL.@)
886  *
887  * Converts an Unicode character to uppercase.
888  *
889  * PARAMS
890  *  wch [I] Character to convert
891  *
892  * RETURNS
893  *  The uppercase character value.
894  */
895 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
896 {
897     return toupperW(wch);
898 }
899
900
901 /**************************************************************************
902  *      RtlDowncaseUnicodeChar   (NTDLL.@)
903  *
904  * Converts an Unicode character to lowercase.
905  *
906  * PARAMS
907  *  wch [I] Character to convert
908  *
909  * RETURNS
910  *  The lowercase character value.
911  */
912 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
913 {
914     return tolowerW(wch);
915 }
916
917
918 /**************************************************************************
919  *      RtlUpcaseUnicodeString   (NTDLL.@)
920  *
921  * Converts an Unicode string to uppercase.
922  *
923  * PARAMS
924  *  dest    [O] Destination for converted string
925  *  src     [I] Source string to convert
926  *  doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
927  *
928  * RETURNS
929  *  Success: STATUS_SUCCESS. dest contains the converted string.
930  *  Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
931  *           STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
932  *
933  * NOTES
934  *  dest is never '\0' terminated because it may be equal to src, and src
935  *  might not be '\0' terminated. dest->Length is only set upon success.
936  */
937 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
938                                         const UNICODE_STRING *src,
939                                         BOOLEAN doalloc)
940 {
941     DWORD i, len = src->Length;
942
943     if (doalloc)
944     {
945         dest->MaximumLength = len;
946         if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
947             return STATUS_NO_MEMORY;
948     }
949     else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
950
951     for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
952     dest->Length = len;
953     return STATUS_SUCCESS;
954 }
955
956
957 /**************************************************************************
958  *      RtlDowncaseUnicodeString   (NTDLL.@)
959  *
960  * Converts an Unicode string to lowercase.
961  *
962  * PARAMS
963  *  dest    [O] Destination for converted string
964  *  src     [I] Source string to convert
965  *  doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
966  *
967  * RETURNS
968  *  Success: STATUS_SUCCESS. dest contains the converted string.
969  *  Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
970  *           STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
971  *
972  * NOTES
973  *  dest is never '\0' terminated because it may be equal to src, and src
974  *  might not be '\0' terminated. dest->Length is only set upon success.
975  */
976 NTSTATUS WINAPI RtlDowncaseUnicodeString(
977     UNICODE_STRING *dest,
978     const UNICODE_STRING *src,
979     BOOLEAN doalloc)
980 {
981     DWORD i;
982     DWORD len = src->Length;
983
984     if (doalloc) {
985         dest->MaximumLength = len;
986         if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
987             return STATUS_NO_MEMORY;
988         } /* if */
989     } else if (len > dest->MaximumLength) {
990         return STATUS_BUFFER_OVERFLOW;
991     } /* if */
992
993     for (i = 0; i < len/sizeof(WCHAR); i++) {
994         dest->Buffer[i] = tolowerW(src->Buffer[i]);
995     } /* for */
996     dest->Length = len;
997     return STATUS_SUCCESS;
998 }
999
1000
1001 /**************************************************************************
1002  *      RtlUpcaseUnicodeStringToAnsiString   (NTDLL.@)
1003  *
1004  * Converts a Unicode string to the equivalent ANSI upper-case representation.
1005  *
1006  * RETURNS
1007  *  NTSTATUS code
1008  *
1009  * NOTES
1010  *  writes terminating 0
1011  */
1012 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1013                                                     const UNICODE_STRING *src,
1014                                                     BOOLEAN doalloc )
1015 {
1016     NTSTATUS ret;
1017     UNICODE_STRING upcase;
1018
1019     if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1020     {
1021         ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1022         RtlFreeUnicodeString( &upcase );
1023     }
1024     return ret;
1025 }
1026
1027
1028 /**************************************************************************
1029  *      RtlUpcaseUnicodeStringToOemString   (NTDLL.@)
1030  *
1031  * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1032  * stored in STRING format.
1033  *
1034  * RETURNS
1035  *  NTSTATUS code
1036  *
1037  * NOTES
1038  *  writes terminating 0
1039  */
1040 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1041                                                    const UNICODE_STRING *src,
1042                                                    BOOLEAN doalloc )
1043 {
1044     NTSTATUS ret;
1045     UNICODE_STRING upcase;
1046
1047     if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1048     {
1049         ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1050         RtlFreeUnicodeString( &upcase );
1051     }
1052     return ret;
1053 }
1054
1055
1056 /**************************************************************************
1057  *      RtlUpcaseUnicodeStringToCountedOemString   (NTDLL.@)
1058  *
1059  * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1060  * stored in STRING format.
1061  *
1062  * RETURNS
1063  *  NTSTATUS code
1064  *
1065  * NOTES
1066  *  Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1067  */
1068 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1069                                                           const UNICODE_STRING *uni,
1070                                                           BOOLEAN doalloc )
1071 {
1072     NTSTATUS ret;
1073     UNICODE_STRING upcase;
1074     WCHAR tmp[32];
1075
1076     upcase.Buffer = tmp;
1077     upcase.MaximumLength = sizeof(tmp);
1078     ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
1079     if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );
1080
1081     if (!ret)
1082     {
1083         DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1084         oem->Length = len;
1085         if (doalloc)
1086         {
1087             oem->MaximumLength = len;
1088             if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1089             {
1090                 ret = STATUS_NO_MEMORY;
1091                 goto done;
1092             }
1093         }
1094         else if (oem->MaximumLength < len)
1095         {
1096             ret = STATUS_BUFFER_OVERFLOW;
1097             oem->Length = oem->MaximumLength;
1098             if (!oem->MaximumLength) goto done;
1099         }
1100         RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1101     done:
1102         if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1103     }
1104     return ret;
1105 }
1106
1107
1108 /**************************************************************************
1109  *      RtlUpcaseUnicodeToMultiByteN   (NTDLL.@)
1110  *
1111  * Converts a Unicode string to the equivalent ANSI upper-case representation.
1112  *
1113  * RETURNS
1114  *  NTSTATUS code
1115  */
1116 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1117                                               LPCWSTR src, DWORD srclen )
1118 {
1119     NTSTATUS ret;
1120     LPWSTR upcase;
1121     DWORD i;
1122
1123     if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1124     for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1125     ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1126     RtlFreeHeap( GetProcessHeap(), 0, upcase );
1127     return ret;
1128 }
1129
1130
1131 /**************************************************************************
1132  *      RtlUpcaseUnicodeToOemN   (NTDLL.@)
1133  *
1134  * Converts a Unicode string to the equivalent OEM upper-case representation.
1135  *
1136  * RETURNS
1137  *  NTSTATUS code
1138  */
1139 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1140                                         LPCWSTR src, DWORD srclen )
1141 {
1142     NTSTATUS ret;
1143     LPWSTR upcase;
1144     DWORD i;
1145
1146     if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1147     for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1148     ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1149     RtlFreeHeap( GetProcessHeap(), 0, upcase );
1150     return ret;
1151 }
1152
1153
1154 /*
1155         STRING SIZE
1156 */
1157
1158
1159 /**************************************************************************
1160  *      RtlOemStringToUnicodeSize   (NTDLL.@)
1161  *      RtlxOemStringToUnicodeSize  (NTDLL.@)
1162  *
1163  * Calculate the size in bytes necessary for the Unicode conversion of str,
1164  * including the terminating '\0'.
1165  *
1166  * PARAMS
1167  *  str [I] String to calculate the size of
1168  *
1169  * RETURNS
1170  *  The calculated size.
1171  */
1172 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1173 {
1174     int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1175     return (ret + 1) * sizeof(WCHAR);
1176 }
1177
1178
1179 /**************************************************************************
1180  *      RtlAnsiStringToUnicodeSize   (NTDLL.@)
1181  *      RtlxAnsiStringToUnicodeSize  (NTDLL.@)
1182  *
1183  * Calculate the size in bytes necessary for the Unicode conversion of str,
1184  * including the terminating '\0'.
1185  *
1186  * PARAMS
1187  *  str [I] String to calculate the size of
1188  *
1189  * RETURNS
1190  *  The calculated size.
1191  */
1192 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1193 {
1194     DWORD ret;
1195     RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1196     return ret + sizeof(WCHAR);
1197 }
1198
1199
1200 /**************************************************************************
1201  *      RtlMultiByteToUnicodeSize   (NTDLL.@)
1202  *
1203  * Compute the size in bytes necessary for the Unicode conversion of str,
1204  * without the terminating '\0'.
1205  *
1206  * PARAMS
1207  *  size [O] Destination for size
1208  *  str  [I] String to calculate the size of
1209  *  len  [I] Length of str
1210  *
1211  * RETURNS
1212  *  STATUS_SUCCESS.
1213  */
1214 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1215 {
1216     *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1217     return STATUS_SUCCESS;
1218 }
1219
1220
1221 /**************************************************************************
1222  *      RtlUnicodeToMultiByteSize   (NTDLL.@)
1223  *
1224  * Calculate the size in bytes necessary for the multibyte conversion of str,
1225  * without the terminating '\0'.
1226  *
1227  * PARAMS
1228  *  size [O] Destination for size
1229  *  str  [I] String to calculate the size of
1230  *  len  [I] Length of str
1231  *
1232  * RETURNS
1233  *  STATUS_SUCCESS.
1234  */
1235 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1236 {
1237     *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1238     return STATUS_SUCCESS;
1239 }
1240
1241
1242 /**************************************************************************
1243  *      RtlUnicodeStringToAnsiSize   (NTDLL.@)
1244  *      RtlxUnicodeStringToAnsiSize  (NTDLL.@)
1245  *
1246  * Calculate the size in bytes necessary for the Ansi conversion of str,
1247  * including the terminating '\0'.
1248  *
1249  * PARAMS
1250  *  str [I] String to calculate the size of
1251  *
1252  * RETURNS
1253  *  The calculated size.
1254  */
1255 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1256 {
1257     DWORD ret;
1258     RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1259     return ret + 1;
1260 }
1261
1262
1263 /**************************************************************************
1264  *      RtlUnicodeStringToOemSize   (NTDLL.@)
1265  *      RtlxUnicodeStringToOemSize  (NTDLL.@)
1266  *
1267  * Calculate the size in bytes necessary for the OEM conversion of str,
1268  * including the terminating '\0'.
1269  *
1270  * PARAMS
1271  *  str [I] String to calculate the size of
1272  *
1273  * RETURNS
1274  *  The calculated size.
1275  */
1276 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1277 {
1278     return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1279                              NULL, 0, NULL, NULL ) + 1;
1280 }
1281
1282
1283 /**************************************************************************
1284  *      RtlAppendAsciizToString   (NTDLL.@)
1285  *
1286  * Concatenates a buffered character string and a '\0' terminated character
1287  * string
1288  *
1289  * RETURNS
1290  *  Success: STATUS_SUCCESS. src is appended to dest.
1291  *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1292  *                  to hold the concatenated string.
1293  *
1294  * NOTES
1295  *  if src is NULL dest is unchanged.
1296  *  dest is never '\0' terminated.
1297  */
1298 NTSTATUS WINAPI RtlAppendAsciizToString(
1299     STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1300     LPCSTR src)   /* [I]   '\0' terminated character string to be concatenated */
1301 {
1302     if (src != NULL) {
1303         unsigned int src_len = strlen(src);
1304         unsigned int dest_len  = src_len + dest->Length;
1305
1306         if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1307         memcpy(dest->Buffer + dest->Length, src, src_len);
1308         dest->Length = dest_len;
1309     } /* if */
1310     return STATUS_SUCCESS;
1311 }
1312
1313
1314 /**************************************************************************
1315  *      RtlAppendStringToString   (NTDLL.@)
1316  *
1317  * Concatenates two buffered character strings
1318  *
1319  * RETURNS
1320  *  Success: STATUS_SUCCESS. src is appended to dest.
1321  *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1322  *                  to hold the concatenated string.
1323  *
1324  * NOTES
1325  *  if src->length is zero dest is unchanged.
1326  *  dest is never '\0' terminated.
1327  */
1328 NTSTATUS WINAPI RtlAppendStringToString(
1329     STRING *dest,       /* [I/O] Buffered character string to which src is concatenated */
1330     const STRING *src)  /* [I]   Buffered character string to be concatenated */
1331 {
1332     if (src->Length != 0) {
1333         unsigned int dest_len = src->Length + dest->Length;
1334
1335         if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1336         memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1337         dest->Length = dest_len;
1338     } /* if */
1339     return STATUS_SUCCESS;
1340 }
1341
1342
1343 /**************************************************************************
1344  *      RtlAppendUnicodeToString   (NTDLL.@)
1345  *
1346  * Concatenates a buffered unicode string and a '\0' terminated unicode 
1347  * string
1348  *
1349  * RETURNS
1350  *  Success: STATUS_SUCCESS. src is appended to dest.
1351  *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1352  *                  to hold the concatenated string.
1353  *
1354  * NOTES
1355  *  if src is NULL dest is unchanged.
1356  *  dest is '\0' terminated when the MaximumLength allows it.
1357  *  When dest fits exactly in MaximumLength characters the '\0' is omitted.
1358  *
1359  * DIFFERENCES
1360  *  Does not write in the src->Buffer beyond MaximumLength when
1361  *  MaximumLength is odd as the native function does.
1362  */
1363 NTSTATUS WINAPI RtlAppendUnicodeToString(
1364     UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1365     LPCWSTR src)          /* [I]   '\0' terminated unicode string to be concatenated */
1366 {
1367     if (src != NULL) {
1368         unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1369         unsigned int dest_len  = src_len + dest->Length;
1370
1371         if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1372         memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1373         dest->Length = dest_len;
1374         /* append terminating '\0' if enough space */
1375         if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1376             dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1377         } /* if */
1378     } /* if */
1379     return STATUS_SUCCESS;
1380 }
1381
1382
1383 /**************************************************************************
1384  *      RtlAppendUnicodeStringToString   (NTDLL.@)
1385  *
1386  * Concatenates two buffered unicode strings
1387  *
1388  * RETURNS
1389  *  Success: STATUS_SUCCESS. src is appended to dest.
1390  *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1391  *                  to hold the concatenated string.
1392  *
1393  * NOTES
1394  *  if src->length is zero dest is unchanged.
1395  *  dest is '\0' terminated when the MaximumLength allows it.
1396  *  When dest fits exactly in MaximumLength characters the '\0' is omitted.
1397  *
1398  * DIFFERENCES
1399  *  Does not write in the src->Buffer beyond MaximumLength when
1400  *  MaximumLength is odd as the native function does.
1401  */
1402 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1403     UNICODE_STRING *dest,      /* [I/O] Buffered unicode string to which src is concatenated */
1404     const UNICODE_STRING *src) /* [I]   Buffered unicode string to be concatenated */
1405 {
1406     if (src->Length != 0) {
1407         unsigned int dest_len = src->Length + dest->Length;
1408
1409         if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1410         memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1411         dest->Length = dest_len;
1412         /* append terminating '\0' if enough space */
1413         if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1414             dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1415         } /* if */
1416     } /* if */
1417     return STATUS_SUCCESS;
1418 }
1419
1420
1421 /**************************************************************************
1422  *      RtlFindCharInUnicodeString   (NTDLL.@)
1423  *
1424  * Searches for one of several unicode characters in an unicode string.
1425  *
1426  * RETURNS
1427  *  Success: STATUS_SUCCESS. pos contains the position after the character found.
1428  *  Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1429  */
1430 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1431     int flags,                          /* [I] Flags */
1432     const UNICODE_STRING *main_str,     /* [I] Unicode string in which one or more characters are searched */
1433     const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1434     USHORT *pos)                        /* [O] Position of the first character found + 2 */
1435 {
1436     int main_idx;
1437     unsigned int search_idx;
1438
1439     switch (flags) {
1440         case 0:
1441             for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1442                 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1443                     if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1444                         *pos = (main_idx + 1) * sizeof(WCHAR);
1445                         return STATUS_SUCCESS;
1446                     }
1447                 }
1448             }
1449             *pos = 0;
1450             return STATUS_NOT_FOUND;
1451         case 1:
1452             for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1453                 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1454                     if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1455                         *pos = main_idx * sizeof(WCHAR);
1456                         return STATUS_SUCCESS;
1457                     }
1458                 }
1459             }
1460             *pos = 0;
1461             return STATUS_NOT_FOUND;
1462         case 2:
1463             for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1464                 search_idx = 0;
1465                 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1466                          main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1467                     search_idx++;
1468                 }
1469                 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1470                     *pos = (main_idx + 1) * sizeof(WCHAR);
1471                     return STATUS_SUCCESS;
1472                 }
1473             }
1474             *pos = 0;
1475             return STATUS_NOT_FOUND;
1476         case 3:
1477             for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1478                 search_idx = 0;
1479                 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1480                          main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1481                     search_idx++;
1482                 }
1483                 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1484                     *pos = main_idx * sizeof(WCHAR);
1485                     return STATUS_SUCCESS;
1486                 }
1487             }
1488             *pos = 0;
1489             return STATUS_NOT_FOUND;
1490     } /* switch */
1491     return STATUS_NOT_FOUND;
1492 }
1493
1494
1495 /*
1496         MISC
1497 */
1498 /* Tests that we currently implement */
1499 #define ITU_IMPLEMENTED_TESTS \
1500     (IS_TEXT_UNICODE_SIGNATURE | \
1501      IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1502      IS_TEXT_UNICODE_ODD_LENGTH | \
1503      IS_TEXT_UNICODE_STATISTICS | \
1504      IS_TEXT_UNICODE_NULL_BYTES)
1505
1506 /**************************************************************************
1507  *      RtlIsTextUnicode (NTDLL.@)
1508  *
1509  * Attempt to guess whether a text buffer is Unicode.
1510  *
1511  * PARAMS
1512  *  buf [I] Text buffer to test
1513  *  len [I] Length of buf
1514  *  pf  [O] Destination for test results
1515  *
1516  * RETURNS
1517  *  The length of the string if all tests were passed, 0 otherwise.
1518  *
1519  * FIXME
1520  *  Should implement more tests.
1521  */
1522 DWORD WINAPI RtlIsTextUnicode(
1523         LPVOID buf,
1524         DWORD len,
1525         DWORD *pf)
1526 {
1527         LPWSTR s = buf;
1528         DWORD flags = -1, out_flags = 0;
1529
1530         if (!len)
1531                 goto out;
1532         if (pf)
1533                 flags = *pf;
1534         /*
1535          * Apply various tests to the text string. According to the
1536          * docs, each test "passed" sets the corresponding flag in
1537          * the output flags. But some of the tests are mutually
1538          * exclusive, so I don't see how you could pass all tests ...
1539          */
1540
1541         /* Check for an odd length ... pass if even. */
1542         if ((flags & IS_TEXT_UNICODE_ODD_LENGTH) && (len & 1))
1543                 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1544
1545         /* Check for the special byte order unicode marks. */
1546         if ((flags & IS_TEXT_UNICODE_SIGNATURE) && *s == 0xFEFF)
1547                 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1548
1549     if ((flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE) && *s == 0xFFFE)
1550         out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1551
1552     /* apply some statistical analysis */
1553     if (flags & IS_TEXT_UNICODE_STATISTICS)
1554     {
1555         DWORD i, stats = 0;
1556         /* FIXME: checks only for ASCII characters in the unicode stream */
1557         for (i = 0; i < len / sizeof(WCHAR); i++)
1558         {
1559             if (s[i] <= 255) stats++;
1560         }
1561         if (stats > len / sizeof(WCHAR) / 2)
1562             out_flags |= IS_TEXT_UNICODE_STATISTICS;
1563     }
1564
1565     /* Check for unicode NULL chars */
1566     if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1567     {
1568         DWORD i;
1569         for (i = 0; i < len / sizeof(WCHAR); i++)
1570         {
1571             if (!s[i])
1572             {
1573                 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1574                 break;
1575             }
1576         }
1577     }
1578
1579         /*
1580          * Check whether the string passed all of the tests.
1581          */
1582         flags &= ITU_IMPLEMENTED_TESTS;
1583         if ((out_flags & flags) != flags)
1584                 len = 0;
1585 out:
1586         if (pf)
1587                 *pf = out_flags;
1588         return len;
1589 }
1590
1591
1592 /**************************************************************************
1593  *      RtlCharToInteger   (NTDLL.@)
1594  *
1595  * Converts a character string into its integer equivalent.
1596  *
1597  * RETURNS
1598  *  Success: STATUS_SUCCESS. value contains the converted number
1599  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1600  *           STATUS_ACCESS_VIOLATION, if value is NULL.
1601  *
1602  * NOTES
1603  *  For base 0 it uses 10 as base and the string should be in the format
1604  *      "{whitespace} [+|-] [0[x|o|b]] {digits}".
1605  *  For other bases the string should be in the format
1606  *      "{whitespace} [+|-] {digits}".
1607  *  No check is made for value overflow, only the lower 32 bits are assigned.
1608  *  If str is NULL it crashes, as the native function does.
1609  *
1610  * DIFFERENCES
1611  *  This function does not read garbage behind '\0' as the native version does.
1612  */
1613 NTSTATUS WINAPI RtlCharToInteger(
1614     PCSZ str,      /* [I] '\0' terminated single-byte string containing a number */
1615     ULONG base,    /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1616     ULONG *value)  /* [O] Destination for the converted value */
1617 {
1618     CHAR chCurrent;
1619     int digit;
1620     ULONG RunningTotal = 0;
1621     char bMinus = 0;
1622
1623     while (*str != '\0' && *str <= ' ') {
1624         str++;
1625     } /* while */
1626
1627     if (*str == '+') {
1628         str++;
1629     } else if (*str == '-') {
1630         bMinus = 1;
1631         str++;
1632     } /* if */
1633
1634     if (base == 0) {
1635         base = 10;
1636         if (str[0] == '0') {
1637             if (str[1] == 'b') {
1638                 str += 2;
1639                 base = 2;
1640             } else if (str[1] == 'o') {
1641                 str += 2;
1642                 base = 8;
1643             } else if (str[1] == 'x') {
1644                 str += 2;
1645                 base = 16;
1646             } /* if */
1647         } /* if */
1648     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1649         return STATUS_INVALID_PARAMETER;
1650     } /* if */
1651
1652     if (value == NULL) {
1653         return STATUS_ACCESS_VIOLATION;
1654     } /* if */
1655
1656     while (*str != '\0') {
1657         chCurrent = *str;
1658         if (chCurrent >= '0' && chCurrent <= '9') {
1659             digit = chCurrent - '0';
1660         } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1661             digit = chCurrent - 'A' + 10;
1662         } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1663             digit = chCurrent - 'a' + 10;
1664         } else {
1665             digit = -1;
1666         } /* if */
1667         if (digit < 0 || digit >= base) {
1668             *value = bMinus ? -RunningTotal : RunningTotal;
1669             return STATUS_SUCCESS;
1670         } /* if */
1671
1672         RunningTotal = RunningTotal * base + digit;
1673         str++;
1674     } /* while */
1675
1676     *value = bMinus ? -RunningTotal : RunningTotal;
1677     return STATUS_SUCCESS;
1678 }
1679
1680
1681 /**************************************************************************
1682  *      RtlIntegerToChar   (NTDLL.@)
1683  *
1684  * Converts an unsigned integer to a character string.
1685  *
1686  * RETURNS
1687  *  Success: STATUS_SUCCESS. str contains the converted number
1688  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1689  *           STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1690  *           STATUS_ACCESS_VIOLATION, if str is NULL.
1691  *
1692  * NOTES
1693  *  Instead of base 0 it uses 10 as base.
1694  *  Writes at most length characters to the string str.
1695  *  Str is '\0' terminated when length allows it.
1696  *  When str fits exactly in length characters the '\0' is omitted.
1697  */
1698 NTSTATUS WINAPI RtlIntegerToChar(
1699     ULONG value,   /* [I] Value to be converted */
1700     ULONG base,    /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1701     ULONG length,  /* [I] Length of the str buffer in bytes */
1702     PCHAR str)     /* [O] Destination for the converted value */
1703 {
1704     CHAR buffer[33];
1705     PCHAR pos;
1706     CHAR digit;
1707     ULONG len;
1708
1709     if (base == 0) {
1710         base = 10;
1711     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1712         return STATUS_INVALID_PARAMETER;
1713     } /* if */
1714
1715     pos = &buffer[32];
1716     *pos = '\0';
1717
1718     do {
1719         pos--;
1720         digit = value % base;
1721         value = value / base;
1722         if (digit < 10) {
1723             *pos = '0' + digit;
1724         } else {
1725             *pos = 'A' + digit - 10;
1726         } /* if */
1727     } while (value != 0L);
1728
1729     len = &buffer[32] - pos;
1730     if (len > length) {
1731         return STATUS_BUFFER_OVERFLOW;
1732     } else if (str == NULL) {
1733         return STATUS_ACCESS_VIOLATION;
1734     } else if (len == length) {
1735         memcpy(str, pos, len);
1736     } else {
1737         memcpy(str, pos, len + 1);
1738     } /* if */
1739     return STATUS_SUCCESS;
1740 }
1741
1742
1743 /**************************************************************************
1744  *      RtlUnicodeStringToInteger (NTDLL.@)
1745  *
1746  * Converts an unicode string into its integer equivalent.
1747  *
1748  * RETURNS
1749  *  Success: STATUS_SUCCESS. value contains the converted number
1750  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1751  *           STATUS_ACCESS_VIOLATION, if value is NULL.
1752  *
1753  * NOTES
1754  *  For base 0 it uses 10 as base and the string should be in the format
1755  *      "{whitespace} [+|-] [0[x|o|b]] {digits}".
1756  *  For other bases the string should be in the format
1757  *      "{whitespace} [+|-] {digits}".
1758  *  No check is made for value overflow, only the lower 32 bits are assigned.
1759  *  If str is NULL it crashes, as the native function does.
1760  *
1761  * DIFFERENCES
1762  *  This function does not read garbage on string length 0 as the native
1763  *  version does.
1764  */
1765 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1766     const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1767     ULONG base,                /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1768     ULONG *value)              /* [O] Destination for the converted value */
1769 {
1770     LPWSTR lpwstr = str->Buffer;
1771     USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1772     WCHAR wchCurrent;
1773     int digit;
1774     ULONG RunningTotal = 0;
1775     char bMinus = 0;
1776
1777     while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1778         lpwstr++;
1779         CharsRemaining--;
1780     } /* while */
1781
1782     if (CharsRemaining >= 1) {
1783         if (*lpwstr == '+') {
1784             lpwstr++;
1785             CharsRemaining--;
1786         } else if (*lpwstr == '-') {
1787             bMinus = 1;
1788             lpwstr++;
1789             CharsRemaining--;
1790         } /* if */
1791     } /* if */
1792
1793     if (base == 0) {
1794         base = 10;
1795         if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1796             if (lpwstr[1] == 'b') {
1797                 lpwstr += 2;
1798                 CharsRemaining -= 2;
1799                 base = 2;
1800             } else if (lpwstr[1] == 'o') {
1801                 lpwstr += 2;
1802                 CharsRemaining -= 2;
1803                 base = 8;
1804             } else if (lpwstr[1] == 'x') {
1805                 lpwstr += 2;
1806                 CharsRemaining -= 2;
1807                 base = 16;
1808             } /* if */
1809         } /* if */
1810     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1811         return STATUS_INVALID_PARAMETER;
1812     } /* if */
1813
1814     if (value == NULL) {
1815         return STATUS_ACCESS_VIOLATION;
1816     } /* if */
1817
1818     while (CharsRemaining >= 1) {
1819         wchCurrent = *lpwstr;
1820         if (wchCurrent >= '0' && wchCurrent <= '9') {
1821             digit = wchCurrent - '0';
1822         } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1823             digit = wchCurrent - 'A' + 10;
1824         } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1825             digit = wchCurrent - 'a' + 10;
1826         } else {
1827             digit = -1;
1828         } /* if */
1829         if (digit < 0 || digit >= base) {
1830             *value = bMinus ? -RunningTotal : RunningTotal;
1831             return STATUS_SUCCESS;
1832         } /* if */
1833
1834         RunningTotal = RunningTotal * base + digit;
1835         lpwstr++;
1836         CharsRemaining--;
1837     } /* while */
1838
1839     *value = bMinus ? -RunningTotal : RunningTotal;
1840     return STATUS_SUCCESS;
1841 }
1842
1843
1844 /**************************************************************************
1845  *      RtlIntegerToUnicodeString (NTDLL.@)
1846  *
1847  * Converts an unsigned integer to a '\0' terminated unicode string.
1848  *
1849  * RETURNS
1850  *  Success: STATUS_SUCCESS. str contains the converted number
1851  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1852  *           STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1853  *                  (with the '\0' termination). In this case str->Length
1854  *                  is set to the length, the string would have (which can
1855  *                  be larger than the MaximumLength).
1856  *
1857  * NOTES
1858  *  Instead of base 0 it uses 10 as base.
1859  *  If str is NULL it crashes, as the native function does.
1860  *
1861  * DIFFERENCES
1862  *  Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1863  *  The native function does this when the string would be longer than 16
1864  *  characters even when the string parameter is long enough.
1865  */
1866 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1867     ULONG value,         /* [I] Value to be converted */
1868     ULONG base,          /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1869     UNICODE_STRING *str) /* [O] Destination for the converted value */
1870 {
1871     WCHAR buffer[33];
1872     PWCHAR pos;
1873     WCHAR digit;
1874
1875     if (base == 0) {
1876         base = 10;
1877     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1878         return STATUS_INVALID_PARAMETER;
1879     } /* if */
1880
1881     pos = &buffer[32];
1882     *pos = '\0';
1883
1884     do {
1885         pos--;
1886         digit = value % base;
1887         value = value / base;
1888         if (digit < 10) {
1889             *pos = '0' + digit;
1890         } else {
1891             *pos = 'A' + digit - 10;
1892         } /* if */
1893     } while (value != 0L);
1894
1895     str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1896     if (str->Length >= str->MaximumLength) {
1897         return STATUS_BUFFER_OVERFLOW;
1898     } else {
1899         memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1900     } /* if */
1901     return STATUS_SUCCESS;
1902 }
1903
1904
1905 /*************************************************************************
1906  * RtlGUIDFromString (NTDLL.@)
1907  *
1908  * Convert a string representation of a GUID into a GUID.
1909  *
1910  * PARAMS
1911  *  str  [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1912  *  guid [O] Destination for the converted GUID
1913  *
1914  * RETURNS
1915  *  Success: STATUS_SUCCESS. guid contains the converted value.
1916  *  Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1917  *
1918  * SEE ALSO
1919  *  See RtlStringFromGUID.
1920  */
1921 NTSTATUS WINAPI RtlGUIDFromString(const UNICODE_STRING *str, GUID* guid)
1922 {
1923   int i = 0;
1924   const WCHAR *lpszCLSID = str->Buffer;
1925   BYTE* lpOut = (BYTE*)guid;
1926
1927   TRACE("(%s,%p)\n", debugstr_us(str), guid);
1928
1929   /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1930    * to memory:       DWORD... WORD WORD BYTES............
1931    */
1932   while (i < 37)
1933   {
1934     switch (i)
1935     {
1936     case 0:
1937       if (*lpszCLSID != '{')
1938         return STATUS_INVALID_PARAMETER;
1939       break;
1940
1941     case 9: case 14: case 19: case 24:
1942       if (*lpszCLSID != '-')
1943         return STATUS_INVALID_PARAMETER;
1944       break;
1945
1946     case 37:
1947       if (*lpszCLSID != '}')
1948         return STATUS_INVALID_PARAMETER;
1949       break;
1950
1951     default:
1952       {
1953         WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1954         unsigned char byte;
1955
1956         /* Read two hex digits as a byte value */
1957         if      (ch >= '0' && ch <= '9') ch = ch - '0';
1958         else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1959         else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1960         else return STATUS_INVALID_PARAMETER;
1961
1962         if      (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1963         else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1964         else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1965         else return STATUS_INVALID_PARAMETER;
1966
1967         byte = ch << 4 | ch2;
1968
1969         switch (i)
1970         {
1971 #ifndef WORDS_BIGENDIAN
1972         /* For Big Endian machines, we store the data such that the
1973          * dword/word members can be read as DWORDS and WORDS correctly. */
1974         /* Dword */
1975         case 1:  lpOut[3] = byte; break;
1976         case 3:  lpOut[2] = byte; break;
1977         case 5:  lpOut[1] = byte; break;
1978         case 7:  lpOut[0] = byte; lpOut += 4;  break;
1979         /* Word */
1980         case 10: case 15: lpOut[1] = byte; break;
1981         case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
1982 #endif
1983         /* Byte */
1984         default: lpOut[0] = byte; lpOut++; break;
1985         }
1986         lpszCLSID++; /* Skip 2nd character of byte */
1987         i++;
1988       }
1989     }
1990     lpszCLSID++;
1991     i++;
1992   }
1993
1994   return STATUS_SUCCESS;
1995 }
1996
1997 /*************************************************************************
1998  * RtlStringFromGUID (NTDLL.@)
1999  *
2000  * Convert a GUID into a string representation of a GUID.
2001  *
2002  * PARAMS
2003  *  guid [I] GUID to convert
2004  *  str  [O] Destination for the converted string
2005  *
2006  * RETURNS
2007  *  Success: STATUS_SUCCESS. str contains the converted value.
2008  *  Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2009  *
2010  * SEE ALSO
2011  *  See RtlGUIDFromString.
2012  */
2013 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2014 {
2015   static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2016     '%','0','4','X','-',  '%','0','4','X','-','%','0','2','X','%','0','2','X',
2017     '-',   '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2018     '%','0','2','X','%','0','2','X','}','\0' };
2019
2020   TRACE("(%p,%p)\n", guid, str);
2021
2022   str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2023   str->MaximumLength = str->Length + sizeof(WCHAR);
2024   str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2025   if (!str->Buffer)
2026   {
2027     str->Length = str->MaximumLength = 0;
2028     return STATUS_NO_MEMORY;
2029   }
2030   sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2031           guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2032           guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2033
2034   return STATUS_SUCCESS;
2035 }