Calculate the size of BI_BITFIELDS dib sections via the width and
[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 #include <ctype.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
40
41 UINT NlsAnsiCodePage = 0;
42 BYTE NlsMbCodePageTag = 0;
43 BYTE NlsMbOemCodePageTag = 0;
44
45 static const union cptable *ansi_table;
46 static const union cptable *oem_table;
47 static const union cptable* unix_table; /* NULL if UTF8 */
48
49
50 /**************************************************************************
51  *      __wine_init_codepages   (NTDLL.@)
52  *
53  * Set the code page once kernel32 is loaded. Should be done differently.
54  */
55 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
56                             const union cptable *ucp)
57 {
58     ansi_table = ansi;
59     oem_table = oem;
60     unix_table = ucp;
61     NlsAnsiCodePage = ansi->info.codepage;
62 }
63
64 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
65
66     return (unix_table) ?
67         wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
68         wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
69 }
70
71 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
72                     const char* defchar, int *used )
73
74     return (unix_table) ?
75         wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used ) :
76         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( ntdll_get_process_heap(), 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( ntdll_get_process_heap(), 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( ntdll_get_process_heap(), 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(ntdll_get_process_heap(), 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 = toupper(*p1++) - toupper(*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 (toupper(s1->Buffer[i]) != toupper(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 comparason 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 comparason 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         COPY BETWEEN ANSI_STRING or UNICODE_STRING
579         there is no parameter checking, it just crashes
580 */
581
582
583 /**************************************************************************
584  *      RtlAnsiStringToUnicodeString   (NTDLL.@)
585  *
586  * Converts an ansi string to an unicode string.
587  *
588  * RETURNS
589  *  Success: STATUS_SUCCESS. uni contains the converted string
590  *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
591  *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
592  *           STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
593  *
594  * NOTES
595  *  This function always writes a terminating '\0'.
596  */
597 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
598     PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
599     PCANSI_STRING ansi,  /* [I]   Ansi string to be converted */
600     BOOLEAN doalloc)     /* [I]   TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
601 {
602     DWORD total = RtlAnsiStringToUnicodeSize( ansi );
603
604     if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
605     uni->Length = total - sizeof(WCHAR);
606     if (doalloc)
607     {
608         uni->MaximumLength = total;
609         if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
610             return STATUS_NO_MEMORY;
611     }
612     else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
613
614     RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
615     uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
616     return STATUS_SUCCESS;
617 }
618
619
620 /**************************************************************************
621  *      RtlOemStringToUnicodeString   (NTDLL.@)
622  *
623  * Converts an oem string to an unicode string.
624  *
625  * RETURNS
626  *  Success: STATUS_SUCCESS. uni contains the converted string
627  *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
628  *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
629  *           STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
630  *
631  * NOTES
632  *  This function always writes a terminating '\0'.
633  */
634 NTSTATUS WINAPI RtlOemStringToUnicodeString(
635     UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
636     const STRING *oem,   /* [I]   Oem string to be converted */
637     BOOLEAN doalloc)     /* [I]   TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
638 {
639     DWORD total = RtlOemStringToUnicodeSize( oem );
640
641     if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
642     uni->Length = total - sizeof(WCHAR);
643     if (doalloc)
644     {
645         uni->MaximumLength = total;
646         if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
647             return STATUS_NO_MEMORY;
648     }
649     else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
650
651     RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
652     uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
653     return STATUS_SUCCESS;
654 }
655
656
657 /**************************************************************************
658  *      RtlUnicodeStringToAnsiString   (NTDLL.@)
659  *
660  * Converts an unicode string to an ansi string.
661  *
662  * RETURNS
663  *  Success: STATUS_SUCCESS. ansi contains the converted string
664  *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
665  *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
666  *
667  * NOTES
668  *  This function always writes a terminating '\0'.
669  *  It performs a partial copy if ansi is too small.
670  */
671 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
672     STRING *ansi,              /* [I/O] Destination for the ansi string */
673     const UNICODE_STRING *uni, /* [I]   Unicode string to be converted */
674     BOOLEAN doalloc)           /* [I]   TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
675 {
676     NTSTATUS ret = STATUS_SUCCESS;
677     DWORD len = RtlUnicodeStringToAnsiSize( uni );
678
679     ansi->Length = len - 1;
680     if (doalloc)
681     {
682         ansi->MaximumLength = len;
683         if (!(ansi->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
684             return STATUS_NO_MEMORY;
685     }
686     else if (ansi->MaximumLength < len)
687     {
688         if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
689         ansi->Length = ansi->MaximumLength - 1;
690         ret = STATUS_BUFFER_OVERFLOW;
691     }
692
693     RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
694     ansi->Buffer[ansi->Length] = 0;
695     return ret;
696 }
697
698
699 /**************************************************************************
700  *      RtlUnicodeStringToOemString   (NTDLL.@)
701  *
702  * Converts a Rtl Unicode string to an OEM string.
703  *
704  * PARAMS
705  *  oem     [O] Destination for OEM string
706  *  uni     [I] Source Unicode string
707  *  doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
708  *
709  * RETURNS
710  *  Success: STATUS_SUCCESS. oem contains the converted string
711  *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
712  *           STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
713  *
714  * NOTES
715  *   If doalloc is TRUE, the length allocated is uni->Length + 1.
716  *   This function always '\0' terminates the string returned.
717  */
718 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
719                                              const UNICODE_STRING *uni,
720                                              BOOLEAN doalloc )
721 {
722     NTSTATUS ret = STATUS_SUCCESS;
723     DWORD len = RtlUnicodeStringToOemSize( uni );
724
725     oem->Length = len - 1;
726     if (doalloc)
727     {
728         oem->MaximumLength = len;
729         if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
730             return STATUS_NO_MEMORY;
731     }
732     else if (oem->MaximumLength < len)
733     {
734         if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
735         oem->Length = oem->MaximumLength - 1;
736         ret = STATUS_BUFFER_OVERFLOW;
737     }
738
739     RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
740     oem->Buffer[oem->Length] = 0;
741     return ret;
742 }
743
744
745 /**************************************************************************
746  *      RtlMultiByteToUnicodeN   (NTDLL.@)
747  *
748  * Converts a multi-byte string to a Unicode string.
749  *
750  * RETURNS
751  *  NTSTATUS code
752  *
753  * NOTES
754  *  Performs a partial copy if dst is too small.
755  */
756 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
757                                         LPCSTR src, DWORD srclen )
758 {
759
760     int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
761     if (reslen)
762         *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
763     return STATUS_SUCCESS;
764 }
765
766
767 /**************************************************************************
768  *      RtlOemToUnicodeN   (NTDLL.@)
769  *
770  * Converts a multi-byte string in the OEM code page to a Unicode string.
771  *
772  * RETURNS
773  *  NTSTATUS code
774  */
775 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
776                                   LPCSTR src, DWORD srclen )
777 {
778     int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
779     if (reslen)
780         *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
781     return STATUS_SUCCESS;
782 }
783
784
785 /**************************************************************************
786  *      RtlUnicodeToMultiByteN   (NTDLL.@)
787  *
788  * Converts a Unicode string to a multi-byte string in the ANSI code page.
789  *
790  * RETURNS
791  *  NTSTATUS code
792  */
793 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
794                                         LPCWSTR src, DWORD srclen )
795 {
796     int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
797                                 dst, dstlen, NULL, NULL );
798     if (reslen)
799         *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
800     return STATUS_SUCCESS;
801 }
802
803
804 /**************************************************************************
805  *      RtlUnicodeToOemN   (NTDLL.@)
806  *
807  * Converts a Unicode string to a multi-byte string in the OEM code page.
808  *
809  * RETURNS
810  *  NTSTATUS code
811  */
812 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
813                                   LPCWSTR src, DWORD srclen )
814 {
815     int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
816                                 dst, dstlen, NULL, NULL );
817     if (reslen)
818         *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
819     return STATUS_SUCCESS;
820 }
821
822
823 /*
824      CASE CONVERSIONS
825 */
826
827
828 /**************************************************************************
829  *      RtlUpperChar   (NTDLL.@)
830  *
831  * Converts an Ascii character to uppercase.
832  *
833  * PARAMS
834  *  ch [I] Character to convert
835  *
836  * RETURNS
837  *  The uppercase character value.
838  *
839  * NOTES
840  *  For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
841  *  All other input characters are returned unchanged. The locale and
842  *  multibyte characters are not taken into account (as native DLL).
843  */
844 CHAR WINAPI RtlUpperChar( CHAR ch )
845 {
846     if (ch >= 'a' && ch <= 'z') {
847         return ch - 'a' + 'A';
848     } else {
849         return ch;
850     } /* if */
851 }
852
853
854 /**************************************************************************
855  *      RtlUpperString   (NTDLL.@)
856  *
857  * Converts an Ascii string to uppercase.
858  *
859  * PARAMS
860  *  dst [O] Destination for converted string
861  *  src [I] Source string to convert
862  *
863  * RETURNS
864  *  Nothing.
865  *
866  * NOTES
867  *  For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
868  *  All other src characters are copied unchanged to dst. The locale and
869  *  multibyte characters are not taken into account (as native DLL).
870  *  The number of character copied is the minimum of src->Length and
871  *  the dst->MaximumLength.
872  */
873 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
874 {
875     unsigned int i, len = min(src->Length, dst->MaximumLength);
876
877     for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
878     dst->Length = len;
879 }
880
881
882 /**************************************************************************
883  *      RtlUpcaseUnicodeChar   (NTDLL.@)
884  *
885  * Converts an Unicode character to uppercase.
886  *
887  * PARAMS
888  *  wch [I] Character to convert
889  *
890  * RETURNS
891  *  The uppercase character value.
892  */
893 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
894 {
895     return toupperW(wch);
896 }
897
898
899 /**************************************************************************
900  *      RtlDowncaseUnicodeChar   (NTDLL.@)
901  *
902  * Converts an Unicode character to lowercase.
903  *
904  * PARAMS
905  *  wch [I] Character to convert
906  *
907  * RETURNS
908  *  The lowercase character value.
909  */
910 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
911 {
912     return tolowerW(wch);
913 }
914
915
916 /**************************************************************************
917  *      RtlUpcaseUnicodeString   (NTDLL.@)
918  *
919  * Converts an Unicode string to uppercase.
920  *
921  * PARAMS
922  *  dest    [O] Destination for converted string
923  *  src     [I] Source string to convert
924  *  doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
925  *
926  * RETURNS
927  *  Success: STATUS_SUCCESS. dest contains the converted string.
928  *  Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
929  *           STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
930  *
931  * NOTES
932  *  dest is never '\0' terminated because it may be equal to src, and src
933  *  might not be '\0' terminated. dest->Length is only set upon success.
934  */
935 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
936                                         const UNICODE_STRING *src,
937                                         BOOLEAN doalloc)
938 {
939     DWORD i, len = src->Length;
940
941     if (doalloc)
942     {
943         dest->MaximumLength = len;
944         if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
945             return STATUS_NO_MEMORY;
946     }
947     else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
948
949     for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
950     dest->Length = len;
951     return STATUS_SUCCESS;
952 }
953
954
955 /**************************************************************************
956  *      RtlDowncaseUnicodeString   (NTDLL.@)
957  *
958  * Converts an Unicode string to lowercase.
959  *
960  * PARAMS
961  *  dest    [O] Destination for converted string
962  *  src     [I] Source string to convert
963  *  doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
964  *
965  * RETURNS
966  *  Success: STATUS_SUCCESS. dest contains the converted string.
967  *  Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
968  *           STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
969  *
970  * NOTES
971  *  dest is never '\0' terminated because it may be equal to src, and src
972  *  might not be '\0' terminated. dest->Length is only set upon success.
973  */
974 NTSTATUS WINAPI RtlDowncaseUnicodeString(
975     UNICODE_STRING *dest,
976     const UNICODE_STRING *src,
977     BOOLEAN doalloc)
978 {
979     DWORD i;
980     DWORD len = src->Length;
981
982     if (doalloc) {
983         dest->MaximumLength = len;
984         if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) {
985             return STATUS_NO_MEMORY;
986         } /* if */
987     } else if (len > dest->MaximumLength) {
988         return STATUS_BUFFER_OVERFLOW;
989     } /* if */
990
991     for (i = 0; i < len/sizeof(WCHAR); i++) {
992         dest->Buffer[i] = tolowerW(src->Buffer[i]);
993     } /* for */
994     dest->Length = len;
995     return STATUS_SUCCESS;
996 }
997
998
999 /**************************************************************************
1000  *      RtlUpcaseUnicodeStringToAnsiString   (NTDLL.@)
1001  *
1002  * Converts a Unicode string to the equivalent ANSI upper-case representation.
1003  *
1004  * RETURNS
1005  *  NTSTATUS code
1006  *
1007  * NOTES
1008  *  writes terminating 0
1009  */
1010 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1011                                                     const UNICODE_STRING *src,
1012                                                     BOOLEAN doalloc )
1013 {
1014     NTSTATUS ret;
1015     UNICODE_STRING upcase;
1016
1017     if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1018     {
1019         ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1020         RtlFreeUnicodeString( &upcase );
1021     }
1022     return ret;
1023 }
1024
1025
1026 /**************************************************************************
1027  *      RtlUpcaseUnicodeStringToOemString   (NTDLL.@)
1028  *
1029  * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1030  * stored in STRING format.
1031  *
1032  * RETURNS
1033  *  NTSTATUS code
1034  *
1035  * NOTES
1036  *  writes terminating 0
1037  */
1038 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1039                                                    const UNICODE_STRING *src,
1040                                                    BOOLEAN doalloc )
1041 {
1042     NTSTATUS ret;
1043     UNICODE_STRING upcase;
1044
1045     if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1046     {
1047         ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1048         RtlFreeUnicodeString( &upcase );
1049     }
1050     return ret;
1051 }
1052
1053
1054 /**************************************************************************
1055  *      RtlUpcaseUnicodeStringToCountedOemString   (NTDLL.@)
1056  *
1057  * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1058  * stored in STRING format.
1059  *
1060  * RETURNS
1061  *  NTSTATUS code
1062  *
1063  * NOTES
1064  *  Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1065  */
1066 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1067                                                           const UNICODE_STRING *uni,
1068                                                           BOOLEAN doalloc )
1069 {
1070     NTSTATUS ret;
1071     UNICODE_STRING upcase;
1072
1073     if (!(ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE )))
1074     {
1075         DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1076         oem->Length = len;
1077         if (doalloc)
1078         {
1079             oem->MaximumLength = len;
1080             if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
1081             {
1082                 ret = STATUS_NO_MEMORY;
1083                 goto done;
1084             }
1085         }
1086         else if (oem->MaximumLength < len)
1087         {
1088             ret = STATUS_BUFFER_OVERFLOW;
1089             oem->Length = oem->MaximumLength;
1090             if (!oem->MaximumLength) goto done;
1091         }
1092         RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1093     done:
1094         RtlFreeUnicodeString( &upcase );
1095     }
1096     return ret;
1097 }
1098
1099
1100 /**************************************************************************
1101  *      RtlUpcaseUnicodeToMultiByteN   (NTDLL.@)
1102  *
1103  * Converts a Unicode string to the equivalent ANSI upper-case representation.
1104  *
1105  * RETURNS
1106  *  NTSTATUS code
1107  */
1108 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1109                                               LPCWSTR src, DWORD srclen )
1110 {
1111     NTSTATUS ret;
1112     LPWSTR upcase;
1113     DWORD i;
1114
1115     if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
1116     for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1117     ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1118     RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
1119     return ret;
1120 }
1121
1122
1123 /**************************************************************************
1124  *      RtlUpcaseUnicodeToOemN   (NTDLL.@)
1125  *
1126  * Converts a Unicode string to the equivalent OEM upper-case representation.
1127  *
1128  * RETURNS
1129  *  NTSTATUS code
1130  */
1131 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1132                                         LPCWSTR src, DWORD srclen )
1133 {
1134     NTSTATUS ret;
1135     LPWSTR upcase;
1136     DWORD i;
1137
1138     if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
1139     for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1140     ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1141     RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
1142     return ret;
1143 }
1144
1145
1146 /*
1147         STRING SIZE
1148 */
1149
1150
1151 /**************************************************************************
1152  *      RtlOemStringToUnicodeSize   (NTDLL.@)
1153  *      RtlxOemStringToUnicodeSize  (NTDLL.@)
1154  *
1155  * Calculate the size in bytes necessary for the Unicode conversion of str,
1156  * including the terminating '\0'.
1157  *
1158  * PARAMS
1159  *  str [I] String to calculate the size of
1160  *
1161  * RETURNS
1162  *  The calculated size.
1163  */
1164 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1165 {
1166     int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1167     return (ret + 1) * sizeof(WCHAR);
1168 }
1169
1170
1171 /**************************************************************************
1172  *      RtlAnsiStringToUnicodeSize   (NTDLL.@)
1173  *      RtlxAnsiStringToUnicodeSize  (NTDLL.@)
1174  *
1175  * Calculate the size in bytes necessary for the Unicode conversion of str,
1176  * including the terminating '\0'.
1177  *
1178  * PARAMS
1179  *  str [I] String to calculate the size of
1180  *
1181  * RETURNS
1182  *  The calculated size.
1183  */
1184 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1185 {
1186     DWORD ret;
1187     RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1188     return ret + sizeof(WCHAR);
1189 }
1190
1191
1192 /**************************************************************************
1193  *      RtlMultiByteToUnicodeSize   (NTDLL.@)
1194  *
1195  * Compute the size in bytes necessary for the Unicode conversion of str,
1196  * without the terminating '\0'.
1197  *
1198  * PARAMS
1199  *  size [O] Destination for size
1200  *  str  [I] String to calculate the size of
1201  *  len  [I] Length of str
1202  *
1203  * RETURNS
1204  *  STATUS_SUCCESS.
1205  */
1206 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1207 {
1208     *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1209     return STATUS_SUCCESS;
1210 }
1211
1212
1213 /**************************************************************************
1214  *      RtlUnicodeToMultiByteSize   (NTDLL.@)
1215  *
1216  * Calculate the size in bytes necessary for the multibyte conversion of str,
1217  * without the terminating '\0'.
1218  *
1219  * PARAMS
1220  *  size [O] Destination for size
1221  *  str  [I] String to calculate the size of
1222  *  len  [I] Length of str
1223  *
1224  * RETURNS
1225  *  STATUS_SUCCESS.
1226  */
1227 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1228 {
1229     *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1230     return STATUS_SUCCESS;
1231 }
1232
1233
1234 /**************************************************************************
1235  *      RtlUnicodeStringToAnsiSize   (NTDLL.@)
1236  *      RtlxUnicodeStringToAnsiSize  (NTDLL.@)
1237  *
1238  * Calculate the size in bytes necessary for the Ansi conversion of str,
1239  * including the terminating '\0'.
1240  *
1241  * PARAMS
1242  *  str [I] String to calculate the size of
1243  *
1244  * RETURNS
1245  *  The calculated size.
1246  */
1247 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1248 {
1249     DWORD ret;
1250     RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1251     return ret + 1;
1252 }
1253
1254
1255 /**************************************************************************
1256  *      RtlUnicodeStringToOemSize   (NTDLL.@)
1257  *      RtlxUnicodeStringToOemSize  (NTDLL.@)
1258  *
1259  * Calculate the size in bytes necessary for the OEM conversion of str,
1260  * including the terminating '\0'.
1261  *
1262  * PARAMS
1263  *  str [I] String to calculate the size of
1264  *
1265  * RETURNS
1266  *  The calculated size.
1267  */
1268 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1269 {
1270     return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1271                              NULL, 0, NULL, NULL ) + 1;
1272 }
1273
1274
1275 /**************************************************************************
1276  *      RtlAppendAsciizToString   (NTDLL.@)
1277  *
1278  * Concatenates a buffered character string and a '\0' terminated character
1279  * string
1280  *
1281  * RETURNS
1282  *  Success: STATUS_SUCCESS. src is appended to dest.
1283  *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1284  *                  to hold the concatenated string.
1285  *
1286  * NOTES
1287  *  if src is NULL dest is unchanged.
1288  *  dest is never '\0' terminated.
1289  */
1290 NTSTATUS WINAPI RtlAppendAsciizToString(
1291     STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1292     LPCSTR src)   /* [I]   '\0' terminated character string to be concatenated */
1293 {
1294     if (src != NULL) {
1295         unsigned int src_len = strlen(src);
1296         unsigned int dest_len  = src_len + dest->Length;
1297
1298         if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1299         memcpy(dest->Buffer + dest->Length, src, src_len);
1300         dest->Length = dest_len;
1301     } /* if */
1302     return STATUS_SUCCESS;
1303 }
1304
1305
1306 /**************************************************************************
1307  *      RtlAppendStringToString   (NTDLL.@)
1308  *
1309  * Concatenates two buffered character strings
1310  *
1311  * RETURNS
1312  *  Success: STATUS_SUCCESS. src is appended to dest.
1313  *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1314  *                  to hold the concatenated string.
1315  *
1316  * NOTES
1317  *  if src->length is zero dest is unchanged.
1318  *  dest is never '\0' terminated.
1319  */
1320 NTSTATUS WINAPI RtlAppendStringToString(
1321     STRING *dest,       /* [I/O] Buffered character string to which src is concatenated */
1322     const STRING *src)  /* [I]   Buffered character string to be concatenated */
1323 {
1324     if (src->Length != 0) {
1325         unsigned int dest_len = src->Length + dest->Length;
1326
1327         if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1328         memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1329         dest->Length = dest_len;
1330     } /* if */
1331     return STATUS_SUCCESS;
1332 }
1333
1334
1335 /**************************************************************************
1336  *      RtlAppendUnicodeToString   (NTDLL.@)
1337  *
1338  * Concatenates an buffered unicode string and a '\0' terminated unicode 
1339  * string
1340  *
1341  * RETURNS
1342  *  Success: STATUS_SUCCESS. src is appended to dest.
1343  *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1344  *                  to hold the concatenated string.
1345  *
1346  * NOTES
1347  *  if src is NULL dest is unchanged.
1348  *  dest is '\0' terminated when the MaximumLength allowes it.
1349  *  When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1350  *
1351  * DIFFERENCES
1352  *  Does not write in the src->Buffer beyond MaximumLength when
1353  *  MaximumLength is odd as the native function does.
1354  */
1355 NTSTATUS WINAPI RtlAppendUnicodeToString(
1356     UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1357     LPCWSTR src)          /* [I]   '\0' terminated unicode string to be concatenated */
1358 {
1359     if (src != NULL) {
1360         unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1361         unsigned int dest_len  = src_len + dest->Length;
1362
1363         if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1364         memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1365         dest->Length = dest_len;
1366         /* append terminating '\0' if enough space */
1367         if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1368             dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1369         } /* if */
1370     } /* if */
1371     return STATUS_SUCCESS;
1372 }
1373
1374
1375 /**************************************************************************
1376  *      RtlAppendUnicodeStringToString   (NTDLL.@)
1377  *
1378  * Concatenates two buffered unicode strings
1379  *
1380  * RETURNS
1381  *  Success: STATUS_SUCCESS. src is appended to dest.
1382  *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1383  *                  to hold the concatenated string.
1384  *
1385  * NOTES
1386  *  if src->length is zero dest is unchanged.
1387  *  dest is '\0' terminated when the MaximumLength allowes it.
1388  *  When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1389  *
1390  * DIFFERENCES
1391  *  Does not write in the src->Buffer beyond MaximumLength when
1392  *  MaximumLength is odd as the native function does.
1393  */
1394 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1395     UNICODE_STRING *dest,      /* [I/O] Buffered unicode string to which src is concatenated */
1396     const UNICODE_STRING *src) /* [I]   Buffered unicode string to be concatenated */
1397 {
1398     if (src->Length != 0) {
1399         unsigned int dest_len = src->Length + dest->Length;
1400
1401         if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1402         memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1403         dest->Length = dest_len;
1404         /* append terminating '\0' if enough space */
1405         if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1406             dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1407         } /* if */
1408     } /* if */
1409     return STATUS_SUCCESS;
1410 }
1411
1412
1413 /**************************************************************************
1414  *      RtlFindCharInUnicodeString   (NTDLL.@)
1415  *
1416  * Searches for one of several unicode characters in an unicode string.
1417  *
1418  * RETURNS
1419  *  Success: STATUS_SUCCESS. pos contains the position after the character found.
1420  *  Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1421  */
1422 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1423     int flags,                          /* [I] Flags */
1424     const UNICODE_STRING *main_str,     /* [I] Unicode string in which one or more characters are searched */
1425     const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1426     USHORT *pos)                        /* [O] Position of the first character found + 2 */
1427 {
1428     int main_idx;
1429     unsigned int search_idx;
1430
1431     switch (flags) {
1432         case 0:
1433             for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1434                 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1435                     if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1436                         *pos = (main_idx + 1) * sizeof(WCHAR);
1437                         return STATUS_SUCCESS;
1438                     }
1439                 }
1440             }
1441             *pos = 0;
1442             return STATUS_NOT_FOUND;
1443         case 1:
1444             for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1445                 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1446                     if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1447                         *pos = main_idx * sizeof(WCHAR);
1448                         return STATUS_SUCCESS;
1449                     }
1450                 }
1451             }
1452             *pos = 0;
1453             return STATUS_NOT_FOUND;
1454         case 2:
1455             for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1456                 search_idx = 0;
1457                 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1458                          main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1459                     search_idx++;
1460                 }
1461                 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1462                     *pos = (main_idx + 1) * sizeof(WCHAR);
1463                     return STATUS_SUCCESS;
1464                 }
1465             }
1466             *pos = 0;
1467             return STATUS_NOT_FOUND;
1468         case 3:
1469             for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1470                 search_idx = 0;
1471                 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1472                          main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1473                     search_idx++;
1474                 }
1475                 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1476                     *pos = main_idx * sizeof(WCHAR);
1477                     return STATUS_SUCCESS;
1478                 }
1479             }
1480             *pos = 0;
1481             return STATUS_NOT_FOUND;
1482     } /* switch */
1483     return STATUS_NOT_FOUND;
1484 }
1485
1486
1487 /*
1488         MISC
1489 */
1490 /* Tests that we currently implement */
1491 #define ITU_IMPLEMENTED_TESTS \
1492     (IS_TEXT_UNICODE_SIGNATURE | \
1493      IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1494      IS_TEXT_UNICODE_ODD_LENGTH | \
1495      IS_TEXT_UNICODE_STATISTICS | \
1496      IS_TEXT_UNICODE_NULL_BYTES)
1497
1498 /**************************************************************************
1499  *      RtlIsTextUnicode (NTDLL.@)
1500  *
1501  * Attempt to guess whether a text buffer is Unicode.
1502  *
1503  * PARAMS
1504  *  buf [I] Text buffer to test
1505  *  len [I] Length of buf
1506  *  pf  [O] Destination for test results
1507  *
1508  * RETURNS
1509  *  The length of the string if all tests were passed, 0 otherwise.
1510  *
1511  * FIXME
1512  *  Should implement more tests.
1513  */
1514 DWORD WINAPI RtlIsTextUnicode(
1515         LPVOID buf,
1516         DWORD len,
1517         DWORD *pf)
1518 {
1519         LPWSTR s = buf;
1520         DWORD flags = -1, out_flags = 0;
1521
1522         if (!len)
1523                 goto out;
1524         if (pf)
1525                 flags = *pf;
1526         /*
1527          * Apply various tests to the text string. According to the
1528          * docs, each test "passed" sets the corresponding flag in
1529          * the output flags. But some of the tests are mutually
1530          * exclusive, so I don't see how you could pass all tests ...
1531          */
1532
1533         /* Check for an odd length ... pass if even. */
1534         if ((flags & IS_TEXT_UNICODE_ODD_LENGTH) && (len & 1))
1535                 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1536
1537         /* Check for the special byte order unicode marks. */
1538         if ((flags & IS_TEXT_UNICODE_SIGNATURE) && *s == 0xFEFF)
1539                 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1540
1541     if ((flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE) && *s == 0xFFFE)
1542         out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1543
1544     /* apply some statistical analysis */
1545     if (flags & IS_TEXT_UNICODE_STATISTICS)
1546     {
1547         DWORD i, stats = 0;
1548         /* FIXME: checks only for ASCII characters in the unicode stream */
1549         for (i = 0; i < len / sizeof(WCHAR); i++)
1550         {
1551             if (s[i] <= 255) stats++;
1552         }
1553         if (stats > len / sizeof(WCHAR) / 2)
1554             out_flags |= IS_TEXT_UNICODE_STATISTICS;
1555     }
1556
1557     /* Check for unicode NULL chars */
1558     if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1559     {
1560         DWORD i;
1561         for (i = 0; i < len / sizeof(WCHAR); i++)
1562         {
1563             if (!s[i])
1564             {
1565                 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1566                 break;
1567             }
1568         }
1569     }
1570
1571         /*
1572          * Check whether the string passed all of the tests.
1573          */
1574         flags &= ITU_IMPLEMENTED_TESTS;
1575         if ((out_flags & flags) != flags)
1576                 len = 0;
1577 out:
1578         if (pf)
1579                 *pf = out_flags;
1580         return len;
1581 }
1582
1583
1584 /**************************************************************************
1585  *      RtlCharToInteger   (NTDLL.@)
1586  *
1587  * Converts a character string into its integer equivalent.
1588  *
1589  * RETURNS
1590  *  Success: STATUS_SUCCESS. value contains the converted number
1591  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1592  *           STATUS_ACCESS_VIOLATION, if value is NULL.
1593  *
1594  * NOTES
1595  *  For base 0 it uses 10 as base and the string should be in the format
1596  *      "{whitespace} [+|-] [0[x|o|b]] {digits}".
1597  *  For other bases the string should be in the format
1598  *      "{whitespace} [+|-] {digits}".
1599  *  No check is made for value overflow, only the lower 32 bits are assigned.
1600  *  If str is NULL it crashes, as the native function does.
1601  *
1602  * DIFFERENCES
1603  *  This function does not read garbage behind '\0' as the native version does.
1604  */
1605 NTSTATUS WINAPI RtlCharToInteger(
1606     PCSZ str,      /* [I] '\0' terminated single-byte string containing a number */
1607     ULONG base,    /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1608     ULONG *value)  /* [O] Destination for the converted value */
1609 {
1610     CHAR chCurrent;
1611     int digit;
1612     ULONG RunningTotal = 0;
1613     char bMinus = 0;
1614
1615     while (*str != '\0' && *str <= ' ') {
1616         str++;
1617     } /* while */
1618
1619     if (*str == '+') {
1620         str++;
1621     } else if (*str == '-') {
1622         bMinus = 1;
1623         str++;
1624     } /* if */
1625
1626     if (base == 0) {
1627         base = 10;
1628         if (str[0] == '0') {
1629             if (str[1] == 'b') {
1630                 str += 2;
1631                 base = 2;
1632             } else if (str[1] == 'o') {
1633                 str += 2;
1634                 base = 8;
1635             } else if (str[1] == 'x') {
1636                 str += 2;
1637                 base = 16;
1638             } /* if */
1639         } /* if */
1640     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1641         return STATUS_INVALID_PARAMETER;
1642     } /* if */
1643
1644     if (value == NULL) {
1645         return STATUS_ACCESS_VIOLATION;
1646     } /* if */
1647
1648     while (*str != '\0') {
1649         chCurrent = *str;
1650         if (chCurrent >= '0' && chCurrent <= '9') {
1651             digit = chCurrent - '0';
1652         } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1653             digit = chCurrent - 'A' + 10;
1654         } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1655             digit = chCurrent - 'a' + 10;
1656         } else {
1657             digit = -1;
1658         } /* if */
1659         if (digit < 0 || digit >= base) {
1660             *value = bMinus ? -RunningTotal : RunningTotal;
1661             return STATUS_SUCCESS;
1662         } /* if */
1663
1664         RunningTotal = RunningTotal * base + digit;
1665         str++;
1666     } /* while */
1667
1668     *value = bMinus ? -RunningTotal : RunningTotal;
1669     return STATUS_SUCCESS;
1670 }
1671
1672
1673 /**************************************************************************
1674  *      RtlIntegerToChar   (NTDLL.@)
1675  *
1676  * Converts an unsigned integer to a character string.
1677  *
1678  * RETURNS
1679  *  Success: STATUS_SUCCESS. str contains the converted number
1680  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1681  *           STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1682  *           STATUS_ACCESS_VIOLATION, if str is NULL.
1683  *
1684  * NOTES
1685  *  Instead of base 0 it uses 10 as base.
1686  *  Writes at most length characters to the string str.
1687  *  Str is '\0' terminated when length allowes it.
1688  *  When str fits exactly in length characters the '\0' is ommitted.
1689  */
1690 NTSTATUS WINAPI RtlIntegerToChar(
1691     ULONG value,   /* [I] Value to be converted */
1692     ULONG base,    /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1693     ULONG length,  /* [I] Length of the str buffer in bytes */
1694     PCHAR str)     /* [O] Destination for the converted value */
1695 {
1696     CHAR buffer[33];
1697     PCHAR pos;
1698     CHAR digit;
1699     ULONG len;
1700
1701     if (base == 0) {
1702         base = 10;
1703     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1704         return STATUS_INVALID_PARAMETER;
1705     } /* if */
1706
1707     pos = &buffer[32];
1708     *pos = '\0';
1709
1710     do {
1711         pos--;
1712         digit = value % base;
1713         value = value / base;
1714         if (digit < 10) {
1715             *pos = '0' + digit;
1716         } else {
1717             *pos = 'A' + digit - 10;
1718         } /* if */
1719     } while (value != 0L);
1720
1721     len = &buffer[32] - pos;
1722     if (len > length) {
1723         return STATUS_BUFFER_OVERFLOW;
1724     } else if (str == NULL) {
1725         return STATUS_ACCESS_VIOLATION;
1726     } else if (len == length) {
1727         memcpy(str, pos, len);
1728     } else {
1729         memcpy(str, pos, len + 1);
1730     } /* if */
1731     return STATUS_SUCCESS;
1732 }
1733
1734
1735 /**************************************************************************
1736  *      RtlUnicodeStringToInteger (NTDLL.@)
1737  *
1738  * Converts an unicode string into its integer equivalent.
1739  *
1740  * RETURNS
1741  *  Success: STATUS_SUCCESS. value contains the converted number
1742  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1743  *           STATUS_ACCESS_VIOLATION, if value is NULL.
1744  *
1745  * NOTES
1746  *  For base 0 it uses 10 as base and the string should be in the format
1747  *      "{whitespace} [+|-] [0[x|o|b]] {digits}".
1748  *  For other bases the string should be in the format
1749  *      "{whitespace} [+|-] {digits}".
1750  *  No check is made for value overflow, only the lower 32 bits are assigned.
1751  *  If str is NULL it crashes, as the native function does.
1752  *
1753  * DIFFERENCES
1754  *  This function does not read garbage on string length 0 as the native
1755  *  version does.
1756  */
1757 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1758     const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1759     ULONG base,                /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1760     ULONG *value)              /* [O] Destination for the converted value */
1761 {
1762     LPWSTR lpwstr = str->Buffer;
1763     USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1764     WCHAR wchCurrent;
1765     int digit;
1766     ULONG RunningTotal = 0;
1767     char bMinus = 0;
1768
1769     while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1770         lpwstr++;
1771         CharsRemaining--;
1772     } /* while */
1773
1774     if (CharsRemaining >= 1) {
1775         if (*lpwstr == '+') {
1776             lpwstr++;
1777             CharsRemaining--;
1778         } else if (*lpwstr == '-') {
1779             bMinus = 1;
1780             lpwstr++;
1781             CharsRemaining--;
1782         } /* if */
1783     } /* if */
1784
1785     if (base == 0) {
1786         base = 10;
1787         if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1788             if (lpwstr[1] == 'b') {
1789                 lpwstr += 2;
1790                 CharsRemaining -= 2;
1791                 base = 2;
1792             } else if (lpwstr[1] == 'o') {
1793                 lpwstr += 2;
1794                 CharsRemaining -= 2;
1795                 base = 8;
1796             } else if (lpwstr[1] == 'x') {
1797                 lpwstr += 2;
1798                 CharsRemaining -= 2;
1799                 base = 16;
1800             } /* if */
1801         } /* if */
1802     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1803         return STATUS_INVALID_PARAMETER;
1804     } /* if */
1805
1806     if (value == NULL) {
1807         return STATUS_ACCESS_VIOLATION;
1808     } /* if */
1809
1810     while (CharsRemaining >= 1) {
1811         wchCurrent = *lpwstr;
1812         if (wchCurrent >= '0' && wchCurrent <= '9') {
1813             digit = wchCurrent - '0';
1814         } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1815             digit = wchCurrent - 'A' + 10;
1816         } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1817             digit = wchCurrent - 'a' + 10;
1818         } else {
1819             digit = -1;
1820         } /* if */
1821         if (digit < 0 || digit >= base) {
1822             *value = bMinus ? -RunningTotal : RunningTotal;
1823             return STATUS_SUCCESS;
1824         } /* if */
1825
1826         RunningTotal = RunningTotal * base + digit;
1827         lpwstr++;
1828         CharsRemaining--;
1829     } /* while */
1830
1831     *value = bMinus ? -RunningTotal : RunningTotal;
1832     return STATUS_SUCCESS;
1833 }
1834
1835
1836 /**************************************************************************
1837  *      RtlIntegerToUnicodeString (NTDLL.@)
1838  *
1839  * Converts an unsigned integer to a '\0' terminated unicode string.
1840  *
1841  * RETURNS
1842  *  Success: STATUS_SUCCESS. str contains the converted number
1843  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1844  *           STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1845  *                  (with the '\0' termination). In this case str->Length
1846  *                  is set to the length, the string would have (which can
1847  *                  be larger than the MaximumLength).
1848  *
1849  * NOTES
1850  *  Instead of base 0 it uses 10 as base.
1851  *  If str is NULL it crashes, as the native function does.
1852  *
1853  * DIFFERENCES
1854  *  Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1855  *  The native function does this when the string would be longer than 16
1856  *  characters even when the string parameter is long enough.
1857  */
1858 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1859     ULONG value,         /* [I] Value to be converted */
1860     ULONG base,          /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1861     UNICODE_STRING *str) /* [O] Destination for the converted value */
1862 {
1863     WCHAR buffer[33];
1864     PWCHAR pos;
1865     WCHAR digit;
1866
1867     if (base == 0) {
1868         base = 10;
1869     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1870         return STATUS_INVALID_PARAMETER;
1871     } /* if */
1872
1873     pos = &buffer[32];
1874     *pos = '\0';
1875
1876     do {
1877         pos--;
1878         digit = value % base;
1879         value = value / base;
1880         if (digit < 10) {
1881             *pos = '0' + digit;
1882         } else {
1883             *pos = 'A' + digit - 10;
1884         } /* if */
1885     } while (value != 0L);
1886
1887     str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1888     if (str->Length >= str->MaximumLength) {
1889         return STATUS_BUFFER_OVERFLOW;
1890     } else {
1891         memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1892     } /* if */
1893     return STATUS_SUCCESS;
1894 }
1895
1896
1897 /*************************************************************************
1898  * RtlGUIDFromString (NTDLL.@)
1899  *
1900  * Convert a string representation of a GUID into a GUID.
1901  *
1902  * PARAMS
1903  *  str  [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1904  *  guid [O] Destination for the converted GUID
1905  *
1906  * RETURNS
1907  *  Success: STATUS_SUCCESS. guid contains the converted value.
1908  *  Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1909  *
1910  * SEE ALSO
1911  *  See RtlStringFromGUID.
1912  */
1913 NTSTATUS WINAPI RtlGUIDFromString(const UNICODE_STRING *str, GUID* guid)
1914 {
1915   int i = 0;
1916   const WCHAR *lpszCLSID = str->Buffer;
1917   BYTE* lpOut = (BYTE*)guid;
1918
1919   TRACE("(%s,%p)\n", debugstr_us(str), guid);
1920
1921   /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1922    * to memory:       DWORD... WORD WORD BYTES............
1923    */
1924   while (i < 37)
1925   {
1926     switch (i)
1927     {
1928     case 0:
1929       if (*lpszCLSID != '{')
1930         return STATUS_INVALID_PARAMETER;
1931       break;
1932
1933     case 9: case 14: case 19: case 24:
1934       if (*lpszCLSID != '-')
1935         return STATUS_INVALID_PARAMETER;
1936       break;
1937
1938     case 37:
1939       if (*lpszCLSID != '}')
1940         return STATUS_INVALID_PARAMETER;
1941       break;
1942
1943     default:
1944       {
1945         WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1946         unsigned char byte;
1947
1948         /* Read two hex digits as a byte value */
1949         if      (ch >= '0' && ch <= '9') ch = ch - '0';
1950         else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1951         else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1952         else return STATUS_INVALID_PARAMETER;
1953
1954         if      (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1955         else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1956         else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1957         else return STATUS_INVALID_PARAMETER;
1958
1959         byte = ch << 4 | ch2;
1960
1961         switch (i)
1962         {
1963 #ifndef WORDS_BIGENDIAN
1964         /* For Big Endian machines, we store the data such that the
1965          * dword/word members can be read as DWORDS and WORDS correctly. */
1966         /* Dword */
1967         case 1:  lpOut[3] = byte; break;
1968         case 3:  lpOut[2] = byte; break;
1969         case 5:  lpOut[1] = byte; break;
1970         case 7:  lpOut[0] = byte; lpOut += 4;  break;
1971         /* Word */
1972         case 10: case 15: lpOut[1] = byte; break;
1973         case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
1974 #endif
1975         /* Byte */
1976         default: lpOut[0] = byte; lpOut++; break;
1977         }
1978         lpszCLSID++; /* Skip 2nd character of byte */
1979         i++;
1980       }
1981     }
1982     lpszCLSID++;
1983     i++;
1984   }
1985
1986   return STATUS_SUCCESS;
1987 }
1988
1989 /*************************************************************************
1990  * RtlStringFromGUID (NTDLL.@)
1991  *
1992  * Convert a GUID into a string representation of a GUID.
1993  *
1994  * PARAMS
1995  *  guid [I] GUID to convert
1996  *  str  [O] Destination for the converted string
1997  *
1998  * RETURNS
1999  *  Success: STATUS_SUCCESS. str contains the converted value.
2000  *  Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2001  *
2002  * SEE ALSO
2003  *  See RtlGUIDFromString.
2004  */
2005 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2006 {
2007   static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2008     '%','0','4','X','-',  '%','0','4','X','-','%','0','2','X','%','0','2','X',
2009     '-',   '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2010     '%','0','2','X','%','0','2','X','}','\0' };
2011
2012   TRACE("(%p,%p)\n", guid, str);
2013
2014   str->Buffer = (WCHAR*)RtlAllocateHeap( ntdll_get_process_heap(), 0, 40 * sizeof(WCHAR));
2015   if (!str->Buffer)
2016   {
2017     str->Length = str->MaximumLength = 0;
2018     return STATUS_NO_MEMORY;
2019   }
2020   str->Length = str->MaximumLength = 40;
2021   sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2022           guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2023           guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2024
2025   return STATUS_SUCCESS;
2026 }