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