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