2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * PURPOSE: Unicode Conversion Routines
5 * FILE: lib/rtl/unicode.c
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
11 /* INCLUDES *****************************************************************/
18 #include <wine/unicode.h>
20 /* GLOBALS *******************************************************************/
22 extern BOOLEAN NlsMbCodePageTag
;
23 extern BOOLEAN NlsMbOemCodePageTag
;
24 extern PUSHORT NlsLeadByteInfo
;
25 extern USHORT NlsOemDefaultChar
;
26 extern USHORT NlsUnicodeDefaultChar
;
29 /* FUNCTIONS *****************************************************************/
33 RtlMultiAppendUnicodeStringBuffer(IN PVOID Unknown
,
38 return STATUS_NOT_IMPLEMENTED
;
46 RtlAnsiCharToUnicodeChar(IN OUT PUCHAR
*AnsiChar
)
50 WCHAR UnicodeChar
= L
' ';
55 Size
= (NlsLeadByteInfo
[**AnsiChar
] == 0) ? 1 : 2;
59 DPRINT("HACK::Shouldn't have happened! Consider fixing Usetup and registry entries it creates on install\n");
63 Status
= RtlMultiByteToUnicodeN(&UnicodeChar
,
69 if (!NT_SUCCESS(Status
))
82 * This function always writes a terminating '\0'.
83 * If the dest buffer is too small a partial copy is NOT performed!
87 RtlAnsiStringToUnicodeString(
88 IN OUT PUNICODE_STRING UniDest
,
89 IN PANSI_STRING AnsiSource
,
90 IN BOOLEAN AllocateDestinationString
)
98 if (NlsMbCodePageTag
== FALSE
)
100 Length
= AnsiSource
->Length
* 2 + sizeof(WCHAR
);
104 Length
= RtlxAnsiStringToUnicodeSize(AnsiSource
);
106 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
107 UniDest
->Length
= (USHORT
)Length
- sizeof(WCHAR
);
109 if (AllocateDestinationString
)
111 UniDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_USTR
);
112 UniDest
->MaximumLength
= (USHORT
)Length
;
113 if (!UniDest
->Buffer
) return STATUS_NO_MEMORY
;
115 else if (UniDest
->Length
>= UniDest
->MaximumLength
)
117 return STATUS_BUFFER_OVERFLOW
;
120 /* UniDest->MaximumLength must be even due to sizeof(WCHAR) being 2 */
121 ASSERT(!(UniDest
->MaximumLength
& 1) && UniDest
->Length
<= UniDest
->MaximumLength
);
123 Status
= RtlMultiByteToUnicodeN(UniDest
->Buffer
,
129 if (!NT_SUCCESS(Status
))
131 if (AllocateDestinationString
)
133 RtlpFreeStringMemory(UniDest
->Buffer
, TAG_USTR
);
134 UniDest
->Buffer
= NULL
;
140 UniDest
->Buffer
[Index
/ sizeof(WCHAR
)] = UNICODE_NULL
;
148 * The calculated size in bytes including nullterm.
152 RtlxAnsiStringToUnicodeSize(IN PCANSI_STRING AnsiString
)
157 /* Convert from Mb String to Unicode Size */
158 RtlMultiByteToUnicodeSize(&Size
,
162 /* Return the size plus the null-char */
163 return(Size
+ sizeof(WCHAR
));
170 * If src->length is zero dest is unchanged.
171 * Dest is never nullterminated.
175 RtlAppendStringToString(IN PSTRING Destination
,
176 IN
const STRING
*Source
)
178 USHORT SourceLength
= Source
->Length
;
182 if (Destination
->Length
+ SourceLength
> Destination
->MaximumLength
)
184 return STATUS_BUFFER_TOO_SMALL
;
187 RtlMoveMemory(&Destination
->Buffer
[Destination
->Length
],
191 Destination
->Length
+= SourceLength
;
194 return STATUS_SUCCESS
;
201 * If src->length is zero dest is unchanged.
202 * Dest is nullterminated when the MaximumLength allowes it.
203 * When dest fits exactly in MaximumLength characters the nullterm is ommitted.
207 RtlAppendUnicodeStringToString(
208 IN OUT PUNICODE_STRING Destination
,
209 IN PCUNICODE_STRING Source
)
211 USHORT SourceLength
= Source
->Length
;
212 PWCHAR Buffer
= &Destination
->Buffer
[Destination
->Length
/ sizeof(WCHAR
)];
216 if ((SourceLength
+ Destination
->Length
) > Destination
->MaximumLength
)
218 return STATUS_BUFFER_TOO_SMALL
;
221 RtlMoveMemory(Buffer
, Source
->Buffer
, SourceLength
);
222 Destination
->Length
+= SourceLength
;
224 /* append terminating '\0' if enough space */
225 if (Destination
->MaximumLength
> Destination
->Length
)
227 Buffer
[SourceLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
231 return STATUS_SUCCESS
;
234 /**************************************************************************
235 * RtlCharToInteger (NTDLL.@)
237 * Converts a character string into its integer equivalent.
240 * Success: STATUS_SUCCESS. value contains the converted number
241 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
242 * STATUS_ACCESS_VIOLATION, if value is NULL.
245 * For base 0 it uses 10 as base and the string should be in the format
246 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
247 * For other bases the string should be in the format
248 * "{whitespace} [+|-] {digits}".
249 * No check is made for value overflow, only the lower 32 bits are assigned.
250 * If str is NULL it crashes, as the native function does.
253 * This function does not read garbage behind '\0' as the native version does.
258 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
259 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
260 PULONG value
) /* [O] Destination for the converted value */
264 ULONG RunningTotal
= 0;
267 /* skip leading whitespaces */
268 while (*str
!= '\0' && *str
<= ' ') str
++;
275 else if (*str
== '-')
281 /* base = 0 means autobase */
293 else if (str
[1] == 'o')
298 else if (str
[1] == 'x')
305 else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16)
307 return STATUS_INVALID_PARAMETER
;
310 if (value
== NULL
) return STATUS_ACCESS_VIOLATION
;
316 if (chCurrent
>= '0' && chCurrent
<= '9')
318 digit
= chCurrent
- '0';
320 else if (chCurrent
>= 'A' && chCurrent
<= 'Z')
322 digit
= chCurrent
- 'A' + 10;
324 else if (chCurrent
>= 'a' && chCurrent
<= 'z')
326 digit
= chCurrent
- 'a' + 10;
333 if (digit
< 0 || digit
>= (int)base
) break;
335 RunningTotal
= RunningTotal
* base
+ digit
;
339 *value
= bMinus
? (0 - RunningTotal
) : RunningTotal
;
340 return STATUS_SUCCESS
;
351 IN BOOLEAN CaseInsensitive
)
357 len
= min(s1
->Length
, s2
->Length
);
363 while (!ret
&& len
--)
364 ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
368 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
371 if (!ret
) ret
= s1
->Length
- s2
->Length
;
380 * TRUE if strings are equal.
387 IN BOOLEAN CaseInsensitive
)
389 if (s1
->Length
!= s2
->Length
) return FALSE
;
390 return !RtlCompareString(s1
, s2
, CaseInsensitive
);
397 * TRUE if strings are equal.
401 RtlEqualUnicodeString(
402 IN CONST UNICODE_STRING
*s1
,
403 IN CONST UNICODE_STRING
*s2
,
404 IN BOOLEAN CaseInsensitive
)
406 if (s1
->Length
!= s2
->Length
) return FALSE
;
407 return !RtlCompareUnicodeString(s1
, s2
, CaseInsensitive
);
415 RtlFreeAnsiString(IN PANSI_STRING AnsiString
)
419 if (AnsiString
->Buffer
)
421 RtlpFreeStringMemory(AnsiString
->Buffer
, TAG_ASTR
);
422 RtlZeroMemory(AnsiString
, sizeof(ANSI_STRING
));
431 RtlFreeOemString(IN POEM_STRING OemString
)
435 if (OemString
->Buffer
) RtlpFreeStringMemory(OemString
->Buffer
, TAG_OSTR
);
443 RtlFreeUnicodeString(IN PUNICODE_STRING UnicodeString
)
447 if (UnicodeString
->Buffer
)
449 RtlpFreeStringMemory(UnicodeString
->Buffer
, TAG_USTR
);
450 RtlZeroMemory(UnicodeString
, sizeof(UNICODE_STRING
));
459 * Check the OEM string to match the Unicode string.
461 * Functions which convert Unicode strings to OEM strings will set a
462 * DefaultChar from the OEM codepage when the characters are unknown.
463 * So check it against the Unicode string and return false when the
464 * Unicode string does not contain a TransDefaultChar.
468 RtlpDidUnicodeToOemWork(IN PCUNICODE_STRING UnicodeString
,
469 IN POEM_STRING OemString
)
473 if (NlsMbOemCodePageTag
== FALSE
)
475 /* single-byte code page */
476 /* Go through all characters of a string */
477 while (i
< OemString
->Length
)
479 /* Check if it got translated into a default char,
480 * but source char wasn't a default char equivalent
482 if ((OemString
->Buffer
[i
] == NlsOemDefaultChar
) &&
483 (UnicodeString
->Buffer
[i
] != NlsUnicodeDefaultChar
))
485 /* Yes, it means unmappable characters were found */
489 /* Move to the next char */
493 /* All chars were translated successfuly */
498 /* multibyte code page */
510 RtlIsValidOemCharacter(IN PWCHAR Char
)
520 * If source is NULL the length of source is assumed to be 0.
524 RtlInitAnsiString(IN OUT PANSI_STRING DestinationString
,
525 IN PCSZ SourceString
)
531 Size
= strlen(SourceString
);
532 if (Size
> (MAXUSHORT
- sizeof(CHAR
))) Size
= MAXUSHORT
- sizeof(CHAR
);
533 DestinationString
->Length
= (USHORT
)Size
;
534 DestinationString
->MaximumLength
= (USHORT
)Size
+ sizeof(CHAR
);
538 DestinationString
->Length
= 0;
539 DestinationString
->MaximumLength
= 0;
542 DestinationString
->Buffer
= (PCHAR
)SourceString
;
547 RtlInitAnsiStringEx(IN OUT PANSI_STRING DestinationString
,
548 IN PCSZ SourceString
)
554 Size
= strlen(SourceString
);
555 if (Size
> (MAXUSHORT
- sizeof(CHAR
))) return STATUS_NAME_TOO_LONG
;
556 DestinationString
->Length
= (USHORT
)Size
;
557 DestinationString
->MaximumLength
= (USHORT
)Size
+ sizeof(CHAR
);
561 DestinationString
->Length
= 0;
562 DestinationString
->MaximumLength
= 0;
565 DestinationString
->Buffer
= (PCHAR
)SourceString
;
566 return STATUS_SUCCESS
;
573 * If source is NULL the length of source is assumed to be 0.
578 IN OUT PSTRING DestinationString
,
579 IN PCSZ SourceString
)
581 RtlInitAnsiString(DestinationString
, SourceString
);
588 * If source is NULL the length of source is assumed to be 0.
592 RtlInitUnicodeString(
593 IN OUT PUNICODE_STRING DestinationString
,
594 IN PCWSTR SourceString
)
597 CONST SIZE_T MaxSize
= (MAXUSHORT
& ~1) - sizeof(UNICODE_NULL
); // an even number
601 Size
= wcslen(SourceString
) * sizeof(WCHAR
);
602 __analysis_assume(Size
<= MaxSize
);
606 DestinationString
->Length
= (USHORT
)Size
;
607 DestinationString
->MaximumLength
= (USHORT
)Size
+ sizeof(UNICODE_NULL
);
611 DestinationString
->Length
= 0;
612 DestinationString
->MaximumLength
= 0;
615 DestinationString
->Buffer
= (PWCHAR
)SourceString
;
623 RtlInitUnicodeStringEx(
624 OUT PUNICODE_STRING DestinationString
,
625 IN PCWSTR SourceString
)
628 CONST SIZE_T MaxSize
= (MAXUSHORT
& ~1) - sizeof(WCHAR
); // an even number
632 Size
= wcslen(SourceString
) * sizeof(WCHAR
);
633 if (Size
> MaxSize
) return STATUS_NAME_TOO_LONG
;
634 DestinationString
->Length
= (USHORT
)Size
;
635 DestinationString
->MaximumLength
= (USHORT
)Size
+ sizeof(WCHAR
);
639 DestinationString
->Length
= 0;
640 DestinationString
->MaximumLength
= 0;
643 DestinationString
->Buffer
= (PWCHAR
)SourceString
;
644 return STATUS_SUCCESS
;
651 * Writes at most length characters to the string str.
652 * Str is nullterminated when length allowes it.
653 * When str fits exactly in length characters the nullterm is ommitted.
655 NTSTATUS NTAPI
RtlIntegerToChar(
656 ULONG value
, /* [I] Value to be converted */
657 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
658 ULONG length
, /* [I] Length of the str buffer in bytes */
659 PCHAR str
) /* [O] Destination for the converted value */
670 else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16)
672 return STATUS_INVALID_PARAMETER
;
681 digit
= (CHAR
)(value
% base
);
682 value
= value
/ base
;
690 *pos
= 'A' + digit
- 10;
695 len
= &buffer
[32] - pos
;
699 return STATUS_BUFFER_OVERFLOW
;
701 else if (str
== NULL
)
703 return STATUS_ACCESS_VIOLATION
;
705 else if (len
== length
)
707 memcpy(str
, pos
, len
);
711 memcpy(str
, pos
, len
+ 1);
714 return STATUS_SUCCESS
;
724 IN ULONG Base OPTIONAL
,
725 IN ULONG Length OPTIONAL
,
726 IN OUT LPWSTR String
)
737 if (Radix
== 0) Radix
= 10;
739 if ((Radix
!= 2) && (Radix
!= 8) &&
740 (Radix
!= 10) && (Radix
!= 16))
742 return STATUS_INVALID_PARAMETER
;
747 while (v
|| tp
== temp
)
752 if (i
< 10) *tp
= (WCHAR
)(i
+ L
'0');
753 else *tp
= (WCHAR
)(i
+ L
'a' - 10);
758 if ((ULONG
)((ULONG_PTR
)tp
- (ULONG_PTR
)temp
) >= Length
)
760 return STATUS_BUFFER_TOO_SMALL
;
765 while (tp
> temp
) *sp
++ = *--tp
;
769 return STATUS_SUCCESS
;
777 RtlIntegerToUnicodeString(
779 IN ULONG Base OPTIONAL
,
780 IN OUT PUNICODE_STRING String
)
782 ANSI_STRING AnsiString
;
786 Status
= RtlIntegerToChar(Value
, Base
, sizeof(Buffer
), Buffer
);
787 if (NT_SUCCESS(Status
))
789 AnsiString
.Buffer
= Buffer
;
790 AnsiString
.Length
= (USHORT
)strlen(Buffer
);
791 AnsiString
.MaximumLength
= sizeof(Buffer
);
793 Status
= RtlAnsiStringToUnicodeString(String
, &AnsiString
, FALSE
);
804 RtlInt64ToUnicodeString (
806 IN ULONG Base OPTIONAL
,
807 IN OUT PUNICODE_STRING String
)
809 LARGE_INTEGER LargeInt
;
810 ANSI_STRING AnsiString
;
814 LargeInt
.QuadPart
= Value
;
816 Status
= RtlLargeIntegerToChar(&LargeInt
, Base
, sizeof(Buffer
), Buffer
);
817 if (NT_SUCCESS(Status
))
819 AnsiString
.Buffer
= Buffer
;
820 AnsiString
.Length
= (USHORT
)strlen(Buffer
);
821 AnsiString
.MaximumLength
= sizeof(Buffer
);
823 Status
= RtlAnsiStringToUnicodeString(String
, &AnsiString
, FALSE
);
833 * TRUE if String2 contains String1 as a prefix.
838 const STRING
*String1
,
839 const STRING
*String2
,
840 BOOLEAN CaseInsensitive
)
846 if (String2
->Length
< String1
->Length
)
849 NumChars
= String1
->Length
;
850 pc1
= String1
->Buffer
;
851 pc2
= String2
->Buffer
;
859 if (RtlUpperChar(*pc1
++) != RtlUpperChar(*pc2
++))
867 if (*pc1
++ != *pc2
++)
882 * TRUE if String2 contains String1 as a prefix.
886 RtlPrefixUnicodeString(
887 PCUNICODE_STRING String1
,
888 PCUNICODE_STRING String2
,
889 BOOLEAN CaseInsensitive
)
895 if (String2
->Length
< String1
->Length
)
898 NumChars
= String1
->Length
/ sizeof(WCHAR
);
899 pc1
= String1
->Buffer
;
900 pc2
= String2
->Buffer
;
908 if (RtlUpcaseUnicodeChar(*pc1
++) !=
909 RtlUpcaseUnicodeChar(*pc2
++))
917 if (*pc1
++ != *pc2
++)
933 RtlUnicodeStringToInteger(
934 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
935 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
936 ULONG
*value
) /* [O] Destination for the converted value */
938 LPWSTR lpwstr
= str
->Buffer
;
939 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
942 ULONG RunningTotal
= 0;
945 while (CharsRemaining
>= 1 && *lpwstr
<= ' ')
951 if (CharsRemaining
>= 1)
958 else if (*lpwstr
== '-')
970 if (CharsRemaining
>= 2 && lpwstr
[0] == '0')
972 if (lpwstr
[1] == 'b')
978 else if (lpwstr
[1] == 'o')
984 else if (lpwstr
[1] == 'x')
992 else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16)
994 return STATUS_INVALID_PARAMETER
;
999 return STATUS_ACCESS_VIOLATION
;
1002 while (CharsRemaining
>= 1)
1004 wchCurrent
= *lpwstr
;
1006 if (wchCurrent
>= '0' && wchCurrent
<= '9')
1008 digit
= wchCurrent
- '0';
1010 else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z')
1012 digit
= wchCurrent
- 'A' + 10;
1014 else if (wchCurrent
>= 'a' && wchCurrent
<= 'z')
1016 digit
= wchCurrent
- 'a' + 10;
1023 if (digit
< 0 || (ULONG
)digit
>= base
) break;
1025 RunningTotal
= RunningTotal
* base
+ digit
;
1030 *value
= bMinus
? (0 - RunningTotal
) : RunningTotal
;
1031 return STATUS_SUCCESS
;
1038 * Bytes necessary for the conversion including nullterm.
1042 RtlxUnicodeStringToOemSize(IN PCUNICODE_STRING UnicodeString
)
1046 /* Convert the Unicode String to Mb Size */
1047 RtlUnicodeToMultiByteSize(&Size
,
1048 UnicodeString
->Buffer
,
1049 UnicodeString
->Length
);
1051 /* Return the size + the null char */
1052 return (Size
+ sizeof(CHAR
));
1059 * This function always writes a terminating '\0'.
1060 * It performs a partial copy if ansi is too small.
1064 RtlUnicodeStringToAnsiString(
1065 IN OUT PANSI_STRING AnsiDest
,
1066 IN PCUNICODE_STRING UniSource
,
1067 IN BOOLEAN AllocateDestinationString
)
1069 NTSTATUS Status
= STATUS_SUCCESS
;
1070 NTSTATUS RealStatus
;
1076 ASSERT(!(UniSource
->Length
& 1));
1078 if (NlsMbCodePageTag
== FALSE
)
1080 Length
= (UniSource
->Length
+ sizeof(WCHAR
)) / sizeof(WCHAR
);
1084 Length
= RtlxUnicodeStringToAnsiSize(UniSource
);
1087 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1089 AnsiDest
->Length
= (USHORT
)Length
- sizeof(CHAR
);
1091 if (AllocateDestinationString
)
1093 AnsiDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_ASTR
);
1094 AnsiDest
->MaximumLength
= (USHORT
)Length
;
1096 if (!AnsiDest
->Buffer
) return STATUS_NO_MEMORY
;
1098 else if (AnsiDest
->Length
>= AnsiDest
->MaximumLength
)
1100 if (!AnsiDest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1102 Status
= STATUS_BUFFER_OVERFLOW
;
1103 AnsiDest
->Length
= AnsiDest
->MaximumLength
- 1;
1106 RealStatus
= RtlUnicodeToMultiByteN(AnsiDest
->Buffer
,
1112 if (!NT_SUCCESS(RealStatus
) && AllocateDestinationString
)
1114 RtlpFreeStringMemory(AnsiDest
->Buffer
, TAG_ASTR
);
1118 AnsiDest
->Buffer
[Index
] = ANSI_NULL
;
1126 * This function always writes a terminating '\0'.
1127 * Does NOT perform a partial copy if unicode is too small!
1131 RtlOemStringToUnicodeString(
1132 IN OUT PUNICODE_STRING UniDest
,
1133 IN PCOEM_STRING OemSource
,
1134 IN BOOLEAN AllocateDestinationString
)
1142 Length
= RtlOemStringToUnicodeSize(OemSource
);
1144 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1146 UniDest
->Length
= (USHORT
)Length
- sizeof(WCHAR
);
1148 if (AllocateDestinationString
)
1150 UniDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_USTR
);
1151 UniDest
->MaximumLength
= (USHORT
)Length
;
1153 if (!UniDest
->Buffer
) return STATUS_NO_MEMORY
;
1155 else if (UniDest
->Length
>= UniDest
->MaximumLength
)
1157 return STATUS_BUFFER_OVERFLOW
;
1160 Status
= RtlOemToUnicodeN(UniDest
->Buffer
,
1166 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1168 RtlpFreeStringMemory(UniDest
->Buffer
, TAG_USTR
);
1169 UniDest
->Buffer
= NULL
;
1173 UniDest
->Buffer
[Index
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1181 * This function always '\0' terminates the string returned.
1185 RtlUnicodeStringToOemString(
1186 IN OUT POEM_STRING OemDest
,
1187 IN PCUNICODE_STRING UniSource
,
1188 IN BOOLEAN AllocateDestinationString
)
1196 Length
= RtlUnicodeStringToOemSize(UniSource
);
1198 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1200 OemDest
->Length
= (USHORT
)Length
- sizeof(CHAR
);
1202 if (AllocateDestinationString
)
1204 OemDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_OSTR
);
1205 OemDest
->MaximumLength
= (USHORT
)Length
;
1207 if (!OemDest
->Buffer
) return STATUS_NO_MEMORY
;
1209 else if (OemDest
->Length
>= OemDest
->MaximumLength
)
1211 return STATUS_BUFFER_OVERFLOW
;
1214 Status
= RtlUnicodeToOemN(OemDest
->Buffer
,
1220 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1222 RtlpFreeStringMemory(OemDest
->Buffer
, TAG_OSTR
);
1223 OemDest
->Buffer
= NULL
;
1227 OemDest
->Buffer
[Index
] = ANSI_NULL
;
1231 #define ITU_IMPLEMENTED_TESTS (IS_TEXT_UNICODE_ODD_LENGTH|IS_TEXT_UNICODE_SIGNATURE)
1237 * The length of the string if all tests were passed, 0 otherwise.
1241 RtlIsTextUnicode(CONST VOID
* buf
, INT len
, INT
* pf
)
1243 static const WCHAR std_control_chars
[] = {'\r', '\n', '\t', ' ', 0x3000, 0};
1244 static const WCHAR byterev_control_chars
[] = {0x0d00, 0x0a00, 0x0900, 0x2000, 0};
1245 const WCHAR
*s
= buf
;
1247 unsigned int flags
= MAXULONG
, out_flags
= 0;
1248 UCHAR last_lo_byte
= 0;
1249 UCHAR last_hi_byte
= 0;
1250 ULONG hi_byte_diff
= 0;
1251 ULONG lo_byte_diff
= 0;
1253 ULONG lead_byte
= 0;
1255 if (len
< sizeof(WCHAR
))
1257 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1267 * Apply various tests to the text string. According to the
1268 * docs, each test "passed" sets the corresponding flag in
1269 * the output flags. But some of the tests are mutually
1270 * exclusive, so I don't see how you could pass all tests ...
1273 /* Check for an odd length ... pass if even. */
1274 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1276 if (((char *)buf
)[len
- 1] == 0)
1277 len
--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1279 len
/= sizeof(WCHAR
);
1281 /* Windows only checks the first 256 characters */
1282 if (len
> 256) len
= 256;
1284 /* Check for the special byte order unicode marks. */
1285 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1286 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1288 for (i
= 0; i
< len
; i
++)
1290 UCHAR lo_byte
= LOBYTE(s
[i
]);
1291 UCHAR hi_byte
= HIBYTE(s
[i
]);
1293 lo_byte_diff
+= max(lo_byte
, last_lo_byte
) - min(lo_byte
, last_lo_byte
);
1294 hi_byte_diff
+= max(hi_byte
, last_hi_byte
) - min(hi_byte
, last_hi_byte
);
1296 last_lo_byte
= lo_byte
;
1297 last_hi_byte
= hi_byte
;
1301 case 0xFFFE: /* Reverse BOM */
1303 case 0x0A0D: /* ASCII CRLF (packed into one word) */
1304 case 0xFFFF: /* Unicode 0xFFFF */
1305 out_flags
|= IS_TEXT_UNICODE_ILLEGAL_CHARS
;
1310 if (lo_byte_diff
< 127 && !hi_byte_diff
)
1312 out_flags
|= IS_TEXT_UNICODE_ASCII16
;
1315 if (NlsMbCodePageTag
)
1317 for (i
= 0; i
< len
; i
++)
1319 if (NlsLeadByteInfo
[s
[i
]])
1328 weight
= (len
/ 2) - 1;
1330 if (lead_byte
< (weight
/ 3))
1332 else if (lead_byte
< ((weight
* 2) / 3))
1339 /* apply some statistical analysis */
1340 if ((flags
& IS_TEXT_UNICODE_STATISTICS
) &&
1341 ((weight
* hi_byte_diff
) < lo_byte_diff
))
1343 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1346 /* Check for unicode NULL chars */
1347 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1349 for (i
= 0; i
< len
; i
++)
1351 if (!(s
[i
] & 0xff) || !(s
[i
] >> 8))
1353 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1359 if (flags
& IS_TEXT_UNICODE_CONTROLS
)
1361 for (i
= 0; i
< len
; i
++)
1363 if (strchrW(std_control_chars
, s
[i
]))
1365 out_flags
|= IS_TEXT_UNICODE_CONTROLS
;
1371 if (flags
& IS_TEXT_UNICODE_REVERSE_CONTROLS
)
1373 for (i
= 0; i
< len
; i
++)
1375 if (strchrW(byterev_control_chars
, s
[i
]))
1377 out_flags
|= IS_TEXT_UNICODE_REVERSE_CONTROLS
;
1382 if (hi_byte_diff
&& !lo_byte_diff
)
1384 out_flags
|= IS_TEXT_UNICODE_REVERSE_ASCII16
;
1387 if ((weight
* lo_byte_diff
) < hi_byte_diff
)
1389 out_flags
|= IS_TEXT_UNICODE_REVERSE_STATISTICS
;
1399 /* check for flags that indicate it's definitely not valid Unicode */
1400 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1402 /* now check for invalid ASCII, and assume Unicode if so */
1403 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1405 /* now check for Unicode flags */
1406 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1417 * Same as RtlOemStringToUnicodeString but doesn't write terminating null
1418 * A partial copy is NOT performed if the dest buffer is too small!
1422 RtlOemStringToCountedUnicodeString(
1423 IN OUT PUNICODE_STRING UniDest
,
1424 IN PCOEM_STRING OemSource
,
1425 IN BOOLEAN AllocateDestinationString
)
1433 /* Calculate size of the string */
1434 Length
= RtlOemStringToCountedUnicodeSize(OemSource
);
1436 /* If it's 0 then zero out dest string and return */
1439 RtlZeroMemory(UniDest
, sizeof(UNICODE_STRING
));
1440 return STATUS_SUCCESS
;
1443 /* Check if length is a sane value */
1444 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1446 /* Store it in dest string */
1447 UniDest
->Length
= (USHORT
)Length
;
1449 /* If we're asked to alloc the string - do so */
1450 if (AllocateDestinationString
)
1452 UniDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_USTR
);
1453 UniDest
->MaximumLength
= (USHORT
)Length
;
1455 if (!UniDest
->Buffer
) return STATUS_NO_MEMORY
;
1457 else if (UniDest
->Length
> UniDest
->MaximumLength
)
1459 return STATUS_BUFFER_OVERFLOW
;
1462 /* Do the conversion */
1463 Status
= RtlOemToUnicodeN(UniDest
->Buffer
,
1469 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1471 /* Conversion failed, free dest string and return status code */
1472 RtlpFreeStringMemory(UniDest
->Buffer
, TAG_USTR
);
1473 UniDest
->Buffer
= NULL
;
1477 return STATUS_SUCCESS
;
1484 * TRUE if the names are equal, FALSE if not
1487 * The comparison is case insensitive.
1491 RtlEqualComputerName(
1492 IN PUNICODE_STRING ComputerName1
,
1493 IN PUNICODE_STRING ComputerName2
)
1495 OEM_STRING OemString1
;
1496 OEM_STRING OemString2
;
1497 BOOLEAN Result
= FALSE
;
1499 if (NT_SUCCESS(RtlUpcaseUnicodeStringToOemString(&OemString1
,
1503 if (NT_SUCCESS(RtlUpcaseUnicodeStringToOemString(&OemString2
,
1507 Result
= RtlEqualString(&OemString1
, &OemString2
, FALSE
);
1508 RtlFreeOemString(&OemString2
);
1511 RtlFreeOemString(&OemString1
);
1521 * TRUE if the names are equal, FALSE if not
1524 * The comparison is case insensitive.
1528 RtlEqualDomainName (
1529 IN PUNICODE_STRING DomainName1
,
1530 IN PUNICODE_STRING DomainName2
)
1532 return RtlEqualComputerName(DomainName1
, DomainName2
);
1538 * RIPPED FROM WINE's ntdll\rtlstr.c rev 1.45
1540 * Convert a string representation of a GUID into a GUID.
1543 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1544 * guid [O] Destination for the converted GUID
1547 * Success: STATUS_SUCCESS. guid contains the converted value.
1548 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1551 * See RtlStringFromGUID.
1556 IN UNICODE_STRING
*str
,
1560 const WCHAR
*lpszCLSID
= str
->Buffer
;
1561 BYTE
* lpOut
= (BYTE
*)guid
;
1563 //TRACE("(%s,%p)\n", debugstr_us(str), guid);
1565 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1566 * to memory: DWORD... WORD WORD BYTES............
1573 if (*lpszCLSID
!= '{')
1574 return STATUS_INVALID_PARAMETER
;
1581 if (*lpszCLSID
!= '-')
1582 return STATUS_INVALID_PARAMETER
;
1586 if (*lpszCLSID
!= '}')
1587 return STATUS_INVALID_PARAMETER
;
1593 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
1596 /* Read two hex digits as a byte value */
1597 if (ch
>= '0' && ch
<= '9')
1599 else if (ch
>= 'a' && ch
<= 'f')
1601 else if (ch
>= 'A' && ch
<= 'F')
1604 return STATUS_INVALID_PARAMETER
;
1606 if (ch2
>= '0' && ch2
<= '9')
1608 else if (ch2
>= 'a' && ch2
<= 'f')
1609 ch2
= ch2
- 'a' + 10;
1610 else if (ch2
>= 'A' && ch2
<= 'F')
1611 ch2
= ch2
- 'A' + 10;
1613 return STATUS_INVALID_PARAMETER
;
1615 byte
= ch
<< 4 | ch2
;
1619 #ifndef WORDS_BIGENDIAN
1620 /* For Big Endian machines, we store the data such that the
1621 * dword/word members can be read as DWORDS and WORDS correctly. */
1654 lpszCLSID
++; /* Skip 2nd character of byte */
1663 return STATUS_SUCCESS
;
1671 RtlEraseUnicodeString(
1672 IN PUNICODE_STRING String
)
1674 if (String
->Buffer
&& String
->MaximumLength
)
1676 RtlZeroMemory(String
->Buffer
, String
->MaximumLength
);
1686 RtlHashUnicodeString(
1687 IN CONST UNICODE_STRING
*String
,
1688 IN BOOLEAN CaseInSensitive
,
1689 IN ULONG HashAlgorithm
,
1690 OUT PULONG HashValue
)
1692 if (String
!= NULL
&& HashValue
!= NULL
)
1694 switch (HashAlgorithm
)
1696 case HASH_STRING_ALGORITHM_DEFAULT
:
1697 case HASH_STRING_ALGORITHM_X65599
:
1702 end
= String
->Buffer
+ (String
->Length
/ sizeof(WCHAR
));
1704 if (CaseInSensitive
)
1706 for (c
= String
->Buffer
; c
!= end
; c
++)
1708 /* only uppercase characters if they are 'a' ... 'z'! */
1709 *HashValue
= ((65599 * (*HashValue
)) +
1710 (ULONG
)(((*c
) >= L
'a' && (*c
) <= L
'z') ?
1711 (*c
) - L
'a' + L
'A' : (*c
)));
1716 for (c
= String
->Buffer
; c
!= end
; c
++)
1718 *HashValue
= ((65599 * (*HashValue
)) + (ULONG
)(*c
));
1722 return STATUS_SUCCESS
;
1727 return STATUS_INVALID_PARAMETER
;
1734 * Same as RtlUnicodeStringToOemString but doesn't write terminating null
1735 * Does a partial copy if the dest buffer is too small
1739 RtlUnicodeStringToCountedOemString(
1740 IN OUT POEM_STRING OemDest
,
1741 IN PUNICODE_STRING UniSource
,
1742 IN BOOLEAN AllocateDestinationString
)
1750 /* Calculate size of the string */
1751 Length
= RtlUnicodeStringToCountedOemSize(UniSource
);
1753 /* If it's 0 then zero out dest string and return */
1756 RtlZeroMemory(OemDest
, sizeof(OEM_STRING
));
1757 return STATUS_SUCCESS
;
1760 /* Check if length is a sane value */
1761 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1763 /* Store it in dest string */
1764 OemDest
->Length
= (USHORT
)Length
;
1766 /* If we're asked to alloc the string - do so */
1767 if (AllocateDestinationString
)
1769 OemDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_OSTR
);
1770 OemDest
->MaximumLength
= (USHORT
)Length
;
1771 if (!OemDest
->Buffer
) return STATUS_NO_MEMORY
;
1773 else if (OemDest
->Length
> OemDest
->MaximumLength
)
1775 return STATUS_BUFFER_OVERFLOW
;
1778 /* Do the conversion */
1779 Status
= RtlUnicodeToOemN(OemDest
->Buffer
,
1785 /* Check for unmapped character */
1786 if (NT_SUCCESS(Status
) && !RtlpDidUnicodeToOemWork(UniSource
, OemDest
))
1787 Status
= STATUS_UNMAPPABLE_CHARACTER
;
1789 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1791 /* Conversion failed, free dest string and return status code */
1792 RtlpFreeStringMemory(OemDest
->Buffer
, TAG_OSTR
);
1793 OemDest
->Buffer
= NULL
;
1805 RtlLargeIntegerToChar(
1806 IN PLARGE_INTEGER Value
,
1809 IN OUT PCHAR String
)
1811 ULONGLONG Val
= Value
->QuadPart
;
1817 if (Base
== 0) Base
= 10;
1819 if ((Base
!= 2) && (Base
!= 8) && (Base
!= 10) && (Base
!= 16))
1821 return STATUS_INVALID_PARAMETER
;
1830 Digit
= (CHAR
)(Val
% Base
);
1836 *Pos
= 'A' + Digit
- 10;
1840 Len
= &Buffer
[64] - Pos
;
1843 return STATUS_BUFFER_OVERFLOW
;
1845 /* If possible, add the 0 termination */
1849 /* Copy the string to the target using SEH */
1850 return RtlpSafeCopyMemory(String
, Pos
, Len
);
1857 * dest is never '\0' terminated because it may be equal to src, and src
1858 * might not be '\0' terminated. dest->Length is only set upon success.
1862 RtlUpcaseUnicodeString(
1863 IN OUT PUNICODE_STRING UniDest
,
1864 IN PCUNICODE_STRING UniSource
,
1865 IN BOOLEAN AllocateDestinationString
)
1871 if (AllocateDestinationString
)
1873 UniDest
->MaximumLength
= UniSource
->Length
;
1874 UniDest
->Buffer
= RtlpAllocateStringMemory(UniDest
->MaximumLength
, TAG_USTR
);
1875 if (UniDest
->Buffer
== NULL
) return STATUS_NO_MEMORY
;
1877 else if (UniSource
->Length
> UniDest
->MaximumLength
)
1879 return STATUS_BUFFER_OVERFLOW
;
1882 j
= UniSource
->Length
/ sizeof(WCHAR
);
1884 for (i
= 0; i
< j
; i
++)
1886 UniDest
->Buffer
[i
] = RtlUpcaseUnicodeChar(UniSource
->Buffer
[i
]);
1889 UniDest
->Length
= UniSource
->Length
;
1890 return STATUS_SUCCESS
;
1897 * This function always writes a terminating '\0'.
1898 * It performs a partial copy if ansi is too small.
1902 RtlUpcaseUnicodeStringToAnsiString(
1903 IN OUT PANSI_STRING AnsiDest
,
1904 IN PUNICODE_STRING UniSource
,
1905 IN BOOLEAN AllocateDestinationString
)
1912 Length
= RtlUnicodeStringToAnsiSize(UniSource
);
1913 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1915 AnsiDest
->Length
= (USHORT
)Length
- sizeof(CHAR
);
1917 if (AllocateDestinationString
)
1919 AnsiDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_ASTR
);
1920 AnsiDest
->MaximumLength
= (USHORT
)Length
;
1921 if (!AnsiDest
->Buffer
) return STATUS_NO_MEMORY
;
1923 else if (AnsiDest
->Length
>= AnsiDest
->MaximumLength
)
1925 if (!AnsiDest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1928 Status
= RtlUpcaseUnicodeToMultiByteN(AnsiDest
->Buffer
,
1934 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1936 RtlpFreeStringMemory(AnsiDest
->Buffer
, TAG_ASTR
);
1937 AnsiDest
->Buffer
= NULL
;
1941 AnsiDest
->Buffer
[Index
] = ANSI_NULL
;
1949 * This function always writes a terminating '\0'.
1950 * It performs a partial copy if ansi is too small.
1954 RtlUpcaseUnicodeStringToCountedOemString(
1955 IN OUT POEM_STRING OemDest
,
1956 IN PCUNICODE_STRING UniSource
,
1957 IN BOOLEAN AllocateDestinationString
)
1964 Length
= RtlUnicodeStringToCountedOemSize(UniSource
);
1968 RtlZeroMemory(OemDest
, sizeof(OEM_STRING
));
1971 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1973 OemDest
->Length
= (USHORT
)Length
;
1975 if (AllocateDestinationString
)
1977 OemDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_OSTR
);
1978 OemDest
->MaximumLength
= (USHORT
)Length
;
1979 if (!OemDest
->Buffer
) return STATUS_NO_MEMORY
;
1981 else if (OemDest
->Length
> OemDest
->MaximumLength
)
1983 return STATUS_BUFFER_OVERFLOW
;
1986 Status
= RtlUpcaseUnicodeToOemN(OemDest
->Buffer
,
1992 /* Check for unmapped characters */
1993 if (NT_SUCCESS(Status
) && !RtlpDidUnicodeToOemWork(UniSource
, OemDest
))
1994 Status
= STATUS_UNMAPPABLE_CHARACTER
;
1996 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1998 RtlpFreeStringMemory(OemDest
->Buffer
, TAG_OSTR
);
1999 OemDest
->Buffer
= NULL
;
2009 * OEM string is always nullterminated
2010 * It performs a partial copy if oem is too small.
2014 RtlUpcaseUnicodeStringToOemString (
2015 IN OUT POEM_STRING OemDest
,
2016 IN PCUNICODE_STRING UniSource
,
2017 IN BOOLEAN AllocateDestinationString
)
2024 Length
= RtlUnicodeStringToOemSize(UniSource
);
2025 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
2027 OemDest
->Length
= (USHORT
)Length
- sizeof(CHAR
);
2029 if (AllocateDestinationString
)
2031 OemDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_OSTR
);
2032 OemDest
->MaximumLength
= (USHORT
)Length
;
2033 if (!OemDest
->Buffer
) return STATUS_NO_MEMORY
;
2035 else if (OemDest
->Length
>= OemDest
->MaximumLength
)
2037 return STATUS_BUFFER_OVERFLOW
;
2040 Status
= RtlUpcaseUnicodeToOemN(OemDest
->Buffer
,
2046 /* Check for unmapped characters */
2047 if (NT_SUCCESS(Status
) && !RtlpDidUnicodeToOemWork(UniSource
, OemDest
))
2048 Status
= STATUS_UNMAPPABLE_CHARACTER
;
2050 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
2052 RtlpFreeStringMemory(OemDest
->Buffer
, TAG_OSTR
);
2053 OemDest
->Buffer
= NULL
;
2057 OemDest
->Buffer
[Index
] = ANSI_NULL
;
2065 * Bytes calculated including nullterm
2069 RtlxOemStringToUnicodeSize(IN PCOEM_STRING OemString
)
2073 /* Convert the Mb String to Unicode Size */
2074 RtlMultiByteToUnicodeSize(&Size
,
2078 /* Return the size + null-char */
2079 return (Size
+ sizeof(WCHAR
));
2087 RtlStringFromGUID (IN REFGUID Guid
,
2088 OUT PUNICODE_STRING GuidString
)
2090 /* Setup the string */
2091 GuidString
->Length
= 38 * sizeof(WCHAR
);
2092 GuidString
->MaximumLength
= GuidString
->Length
+ sizeof(UNICODE_NULL
);
2093 GuidString
->Buffer
= RtlpAllocateStringMemory(GuidString
->MaximumLength
,
2095 if (!GuidString
->Buffer
) return STATUS_NO_MEMORY
;
2097 /* Now format the GUID */
2098 swprintf(GuidString
->Buffer
,
2099 L
"{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
2111 return STATUS_SUCCESS
;
2118 * Bytes calculated including nullterm
2122 RtlxUnicodeStringToAnsiSize(IN PCUNICODE_STRING UnicodeString
)
2127 ASSERT(!(UnicodeString
->Length
& 1));
2129 /* Convert the Unicode String to Mb Size */
2130 RtlUnicodeToMultiByteSize(&Size
,
2131 UnicodeString
->Buffer
,
2132 UnicodeString
->Length
);
2134 /* Return the size + null-char */
2135 return (Size
+ sizeof(CHAR
));
2143 RtlCompareUnicodeString(
2144 IN PCUNICODE_STRING s1
,
2145 IN PCUNICODE_STRING s2
,
2146 IN BOOLEAN CaseInsensitive
)
2152 len
= min(s1
->Length
, s2
->Length
) / sizeof(WCHAR
);
2156 if (CaseInsensitive
)
2158 while (!ret
&& len
--) ret
= RtlUpcaseUnicodeChar(*p1
++) - RtlUpcaseUnicodeChar(*p2
++);
2162 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
2165 if (!ret
) ret
= s1
->Length
- s2
->Length
;
2176 IN OUT PSTRING DestinationString
,
2177 IN
const STRING
*SourceString OPTIONAL
)
2182 /* Check if there was no source given */
2185 /* Simply return an empty string */
2186 DestinationString
->Length
= 0;
2190 /* Choose the smallest length */
2191 SourceLength
= min(DestinationString
->MaximumLength
,
2192 SourceString
->Length
);
2195 DestinationString
->Length
= (USHORT
)SourceLength
;
2197 /* Save the pointers to each buffer */
2198 p1
= DestinationString
->Buffer
;
2199 p2
= SourceString
->Buffer
;
2201 /* Loop the buffer */
2202 while (SourceLength
)
2204 /* Copy the character and move on */
2216 RtlCopyUnicodeString(
2217 IN OUT PUNICODE_STRING DestinationString
,
2218 IN PCUNICODE_STRING SourceString
)
2222 if(SourceString
== NULL
)
2224 DestinationString
->Length
= 0;
2228 SourceLength
= min(DestinationString
->MaximumLength
,
2229 SourceString
->Length
);
2230 DestinationString
->Length
= (USHORT
)SourceLength
;
2232 RtlCopyMemory(DestinationString
->Buffer
,
2233 SourceString
->Buffer
,
2236 if (DestinationString
->Length
< DestinationString
->MaximumLength
)
2238 DestinationString
->Buffer
[SourceLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2247 * Creates a nullterminated UNICODE_STRING
2251 RtlCreateUnicodeString(
2252 IN OUT PUNICODE_STRING UniDest
,
2258 Size
= (wcslen(Source
) + 1) * sizeof(WCHAR
);
2259 if (Size
> MAXUSHORT
) return FALSE
;
2261 UniDest
->Buffer
= RtlpAllocateStringMemory((ULONG
)Size
, TAG_USTR
);
2263 if (UniDest
->Buffer
== NULL
) return FALSE
;
2265 RtlCopyMemory(UniDest
->Buffer
, Source
, Size
);
2266 UniDest
->MaximumLength
= (USHORT
)Size
;
2267 UniDest
->Length
= (USHORT
)Size
- sizeof (WCHAR
);
2277 RtlCreateUnicodeStringFromAsciiz(
2278 OUT PUNICODE_STRING Destination
,
2281 ANSI_STRING AnsiString
;
2284 RtlInitAnsiString(&AnsiString
, Source
);
2286 Status
= RtlAnsiStringToUnicodeString(Destination
,
2290 return NT_SUCCESS(Status
);
2297 * Dest is never '\0' terminated because it may be equal to src, and src
2298 * might not be '\0' terminated.
2299 * Dest->Length is only set upon success.
2303 RtlDowncaseUnicodeString(
2304 IN OUT PUNICODE_STRING UniDest
,
2305 IN PCUNICODE_STRING UniSource
,
2306 IN BOOLEAN AllocateDestinationString
)
2312 if (AllocateDestinationString
)
2314 UniDest
->MaximumLength
= UniSource
->Length
;
2315 UniDest
->Buffer
= RtlpAllocateStringMemory(UniSource
->Length
, TAG_USTR
);
2316 if (UniDest
->Buffer
== NULL
) return STATUS_NO_MEMORY
;
2318 else if (UniSource
->Length
> UniDest
->MaximumLength
)
2320 return STATUS_BUFFER_OVERFLOW
;
2323 UniDest
->Length
= UniSource
->Length
;
2324 StopGap
= UniSource
->Length
/ sizeof(WCHAR
);
2326 for (i
= 0 ; i
< StopGap
; i
++)
2328 if (UniSource
->Buffer
[i
] < L
'A')
2330 UniDest
->Buffer
[i
] = UniSource
->Buffer
[i
];
2332 else if (UniSource
->Buffer
[i
] <= L
'Z')
2334 UniDest
->Buffer
[i
] = (UniSource
->Buffer
[i
] + (L
'a' - L
'A'));
2338 UniDest
->Buffer
[i
] = RtlDowncaseUnicodeChar(UniSource
->Buffer
[i
]);
2342 return STATUS_SUCCESS
;
2349 * if src is NULL dest is unchanged.
2350 * dest is '\0' terminated when the MaximumLength allowes it.
2351 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
2355 RtlAppendUnicodeToString(IN OUT PUNICODE_STRING Destination
,
2363 UNICODE_STRING UnicodeSource
;
2365 RtlInitUnicodeString(&UnicodeSource
, Source
);
2366 Length
= UnicodeSource
.Length
;
2368 if (Destination
->Length
+ Length
> Destination
->MaximumLength
)
2370 return STATUS_BUFFER_TOO_SMALL
;
2373 DestBuffer
= &Destination
->Buffer
[Destination
->Length
/ sizeof(WCHAR
)];
2374 RtlMoveMemory(DestBuffer
, Source
, Length
);
2375 Destination
->Length
+= Length
;
2377 /* append terminating '\0' if enough space */
2378 if(Destination
->MaximumLength
> Destination
->Length
)
2380 DestBuffer
[Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2384 return STATUS_SUCCESS
;
2391 * if src is NULL dest is unchanged.
2392 * dest is never '\0' terminated.
2396 RtlAppendAsciizToString(
2397 IN OUT PSTRING Destination
,
2404 Size
= strlen(Source
);
2406 if (Destination
->Length
+ Size
> Destination
->MaximumLength
)
2408 return STATUS_BUFFER_TOO_SMALL
;
2411 RtlMoveMemory(&Destination
->Buffer
[Destination
->Length
], Source
, Size
);
2412 Destination
->Length
+= (USHORT
)Size
;
2415 return STATUS_SUCCESS
;
2423 RtlUpperString(PSTRING DestinationString
,
2424 const STRING
*SourceString
)
2429 Length
= min(SourceString
->Length
,
2430 DestinationString
->MaximumLength
);
2432 Src
= SourceString
->Buffer
;
2433 Dest
= DestinationString
->Buffer
;
2434 DestinationString
->Length
= Length
;
2438 *Dest
++ = RtlUpperChar(*Src
++);
2447 * See RtlpDuplicateUnicodeString
2451 RtlDuplicateUnicodeString(
2453 IN PCUNICODE_STRING SourceString
,
2454 OUT PUNICODE_STRING DestinationString
)
2458 if (SourceString
== NULL
|| DestinationString
== NULL
||
2459 SourceString
->Length
> SourceString
->MaximumLength
||
2460 (SourceString
->Length
== 0 && SourceString
->MaximumLength
> 0 && SourceString
->Buffer
== NULL
) ||
2461 Flags
== RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING
|| Flags
>= 4)
2463 return STATUS_INVALID_PARAMETER
;
2467 if ((SourceString
->Length
== 0) &&
2468 (Flags
!= (RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
|
2469 RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING
)))
2471 DestinationString
->Length
= 0;
2472 DestinationString
->MaximumLength
= 0;
2473 DestinationString
->Buffer
= NULL
;
2477 UINT DestMaxLength
= SourceString
->Length
;
2479 if (Flags
& RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
)
2480 DestMaxLength
+= sizeof(UNICODE_NULL
);
2482 DestinationString
->Buffer
= RtlpAllocateStringMemory(DestMaxLength
, TAG_USTR
);
2484 if (DestinationString
->Buffer
== NULL
)
2485 return STATUS_NO_MEMORY
;
2487 RtlCopyMemory(DestinationString
->Buffer
, SourceString
->Buffer
, SourceString
->Length
);
2488 DestinationString
->Length
= SourceString
->Length
;
2489 DestinationString
->MaximumLength
= DestMaxLength
;
2491 if (Flags
& RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
)
2492 DestinationString
->Buffer
[DestinationString
->Length
/ sizeof(WCHAR
)] = 0;
2495 return STATUS_SUCCESS
;
2503 RtlValidateUnicodeString(IN ULONG Flags
,
2504 IN PCUNICODE_STRING UnicodeString
)
2506 /* currently no flags are supported! */
2510 ((UnicodeString
== NULL
) ||
2511 ((UnicodeString
->Length
!= 0) &&
2512 (UnicodeString
->Buffer
!= NULL
) &&
2513 ((UnicodeString
->Length
% sizeof(WCHAR
)) == 0) &&
2514 ((UnicodeString
->MaximumLength
% sizeof(WCHAR
)) == 0) &&
2515 (UnicodeString
->MaximumLength
>= UnicodeString
->Length
))))
2517 /* a NULL pointer as a unicode string is considered to be a valid unicode
2519 return STATUS_SUCCESS
;
2523 return STATUS_INVALID_PARAMETER
;
2532 RtlpEnsureBufferSize(ULONG Unknown1
, ULONG Unknown2
, ULONG Unknown3
)
2534 DPRINT1("RtlpEnsureBufferSize: stub\n");
2535 return STATUS_NOT_IMPLEMENTED
;
2540 RtlpIsCharInUnicodeString(
2542 IN PCUNICODE_STRING MatchString
,
2543 IN BOOLEAN CaseInSensitive
)
2547 if (CaseInSensitive
)
2548 Char
= RtlUpcaseUnicodeChar(Char
);
2550 for (i
= 0; i
< MatchString
->Length
/ sizeof(WCHAR
); i
++)
2552 WCHAR OtherChar
= MatchString
->Buffer
[i
];
2553 if (CaseInSensitive
)
2554 OtherChar
= RtlUpcaseUnicodeChar(OtherChar
);
2556 if (Char
== OtherChar
)
2568 RtlFindCharInUnicodeString(
2570 IN PCUNICODE_STRING SearchString
,
2571 IN PCUNICODE_STRING MatchString
,
2572 OUT PUSHORT Position
)
2575 const BOOLEAN WantToFind
= (Flags
& RTL_FIND_CHAR_IN_UNICODE_STRING_COMPLEMENT_CHAR_SET
) == 0;
2576 const BOOLEAN CaseInSensitive
= (Flags
& RTL_FIND_CHAR_IN_UNICODE_STRING_CASE_INSENSITIVE
) != 0;
2579 DPRINT("RtlFindCharInUnicodeString(%u, '%wZ', '%wZ', %p)\n",
2580 Flags
, SearchString
, MatchString
, Position
);
2582 /* Parameter checks */
2583 if (Position
== NULL
)
2584 return STATUS_INVALID_PARAMETER
;
2588 if (Flags
& ~(RTL_FIND_CHAR_IN_UNICODE_STRING_START_AT_END
|
2589 RTL_FIND_CHAR_IN_UNICODE_STRING_COMPLEMENT_CHAR_SET
|
2590 RTL_FIND_CHAR_IN_UNICODE_STRING_CASE_INSENSITIVE
))
2591 return STATUS_INVALID_PARAMETER
;
2594 Length
= SearchString
->Length
/ sizeof(WCHAR
);
2595 if (Flags
& RTL_FIND_CHAR_IN_UNICODE_STRING_START_AT_END
)
2597 for (i
= Length
- 1; (SHORT
)i
>= 0; i
--)
2599 Found
= RtlpIsCharInUnicodeString(SearchString
->Buffer
[i
], MatchString
, CaseInSensitive
);
2600 if (Found
== WantToFind
)
2602 *Position
= i
* sizeof(WCHAR
);
2603 return STATUS_SUCCESS
;
2609 for (i
= 0; i
< Length
; i
++)
2611 Found
= RtlpIsCharInUnicodeString(SearchString
->Buffer
[i
], MatchString
, CaseInSensitive
);
2612 if (Found
== WantToFind
)
2614 *Position
= (i
+ 1) * sizeof(WCHAR
);
2615 return STATUS_SUCCESS
;
2620 return STATUS_NOT_FOUND
;
2627 * Get the maximum of MAX_COMPUTERNAME_LENGTH characters from the dns.host name until the dot is found.
2628 * Convert is to an uppercase oem string and check for unmapped characters.
2629 * Then convert the oem string back to an unicode string.
2633 RtlDnsHostNameToComputerName(PUNICODE_STRING ComputerName
, PUNICODE_STRING DnsHostName
, BOOLEAN AllocateComputerNameString
)
2637 ULONG ComputerNameLength
;
2638 ULONG ComputerNameOemNLength
;
2639 OEM_STRING ComputerNameOem
;
2640 CHAR ComputerNameOemN
[MAX_COMPUTERNAME_LENGTH
+ 1];
2642 Status
= STATUS_INVALID_COMPUTER_NAME
;
2643 ComputerNameLength
= DnsHostName
->Length
;
2645 /* find the first dot in the dns host name */
2646 for (Length
= 0; Length
< DnsHostName
->Length
/ sizeof(WCHAR
); Length
++)
2648 if (DnsHostName
->Buffer
[Length
] == L
'.')
2650 /* dot found, so set the length for the oem translation */
2651 ComputerNameLength
= Length
* sizeof(WCHAR
);
2656 /* the computername must have one character */
2657 if (ComputerNameLength
> 0)
2659 ComputerNameOemNLength
= 0;
2660 /* convert to oem string and use uppercase letters */
2661 Status
= RtlUpcaseUnicodeToOemN(ComputerNameOemN
,
2662 MAX_COMPUTERNAME_LENGTH
,
2663 &ComputerNameOemNLength
,
2664 DnsHostName
->Buffer
,
2665 ComputerNameLength
);
2667 /* status STATUS_BUFFER_OVERFLOW is not a problem since the computername shoud only
2668 have MAX_COMPUTERNAME_LENGTH characters */
2669 if ((Status
== STATUS_SUCCESS
) ||
2670 (Status
== STATUS_BUFFER_OVERFLOW
))
2672 /* set the termination for the oem string */
2673 ComputerNameOemN
[MAX_COMPUTERNAME_LENGTH
] = 0;
2674 /* set status for the case the next function failed */
2675 Status
= STATUS_INVALID_COMPUTER_NAME
;
2676 /* fillup the oem string structure with the converted computername
2677 and check it for unmapped characters */
2678 ComputerNameOem
.Buffer
= ComputerNameOemN
;
2679 ComputerNameOem
.Length
= (USHORT
)ComputerNameOemNLength
;
2680 ComputerNameOem
.MaximumLength
= (USHORT
)(MAX_COMPUTERNAME_LENGTH
+ 1);
2682 if (RtlpDidUnicodeToOemWork(DnsHostName
, &ComputerNameOem
))
2684 /* no unmapped character so convert it back to an unicode string */
2685 Status
= RtlOemStringToUnicodeString(ComputerName
,
2687 AllocateComputerNameString
);