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 *****************************************************************/
36 RtlAnsiCharToUnicodeChar(IN OUT PUCHAR
*AnsiChar
)
40 WCHAR UnicodeChar
= L
' ';
45 Size
= (NlsLeadByteInfo
[**AnsiChar
] == 0) ? 1 : 2;
49 DPRINT("HACK::Shouldn't have happened! Consider fixing Usetup and registry entries it creates on install\n");
53 Status
= RtlMultiByteToUnicodeN(&UnicodeChar
,
59 if (!NT_SUCCESS(Status
))
72 * This function always writes a terminating '\0'.
73 * If the dest buffer is too small a partial copy is NOT performed!
77 RtlAnsiStringToUnicodeString(
78 IN OUT PUNICODE_STRING UniDest
,
79 IN PANSI_STRING AnsiSource
,
80 IN BOOLEAN AllocateDestinationString
)
88 if (NlsMbCodePageTag
== FALSE
)
90 Length
= AnsiSource
->Length
* 2 + sizeof(WCHAR
);
94 Length
= RtlxAnsiStringToUnicodeSize(AnsiSource
);
96 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
97 UniDest
->Length
= (USHORT
)Length
- sizeof(WCHAR
);
99 if (AllocateDestinationString
)
101 UniDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_USTR
);
102 UniDest
->MaximumLength
= (USHORT
)Length
;
103 if (!UniDest
->Buffer
) return STATUS_NO_MEMORY
;
105 else if (UniDest
->Length
>= UniDest
->MaximumLength
)
107 return STATUS_BUFFER_OVERFLOW
;
110 /* UniDest->MaximumLength must be even due to sizeof(WCHAR) being 2 */
111 ASSERT(!(UniDest
->MaximumLength
& 1) && UniDest
->Length
<= UniDest
->MaximumLength
);
113 Status
= RtlMultiByteToUnicodeN(UniDest
->Buffer
,
119 if (!NT_SUCCESS(Status
))
121 if (AllocateDestinationString
)
123 RtlpFreeStringMemory(UniDest
->Buffer
, TAG_USTR
);
124 UniDest
->Buffer
= NULL
;
130 UniDest
->Buffer
[Index
/ sizeof(WCHAR
)] = UNICODE_NULL
;
138 * The calculated size in bytes including nullterm.
142 RtlxAnsiStringToUnicodeSize(IN PCANSI_STRING AnsiString
)
147 /* Convert from Mb String to Unicode Size */
148 RtlMultiByteToUnicodeSize(&Size
,
152 /* Return the size plus the null-char */
153 return(Size
+ sizeof(WCHAR
));
160 * If src->length is zero dest is unchanged.
161 * Dest is never nullterminated.
165 RtlAppendStringToString(IN PSTRING Destination
,
168 USHORT SourceLength
= Source
->Length
;
172 if (Destination
->Length
+ SourceLength
> Destination
->MaximumLength
)
174 return STATUS_BUFFER_TOO_SMALL
;
177 RtlMoveMemory(&Destination
->Buffer
[Destination
->Length
],
181 Destination
->Length
+= SourceLength
;
184 return STATUS_SUCCESS
;
191 * If src->length is zero dest is unchanged.
192 * Dest is nullterminated when the MaximumLength allowes it.
193 * When dest fits exactly in MaximumLength characters the nullterm is ommitted.
197 RtlAppendUnicodeStringToString(
198 IN OUT PUNICODE_STRING Destination
,
199 IN PCUNICODE_STRING Source
)
201 USHORT SourceLength
= Source
->Length
;
202 PWCHAR Buffer
= &Destination
->Buffer
[Destination
->Length
/ sizeof(WCHAR
)];
206 if ((SourceLength
+ Destination
->Length
) > Destination
->MaximumLength
)
208 return STATUS_BUFFER_TOO_SMALL
;
211 RtlMoveMemory(Buffer
, Source
->Buffer
, SourceLength
);
212 Destination
->Length
+= SourceLength
;
214 /* append terminating '\0' if enough space */
215 if (Destination
->MaximumLength
> Destination
->Length
)
217 Buffer
[SourceLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
221 return STATUS_SUCCESS
;
224 /**************************************************************************
225 * RtlCharToInteger (NTDLL.@)
227 * Converts a character string into its integer equivalent.
230 * Success: STATUS_SUCCESS. value contains the converted number
231 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
232 * STATUS_ACCESS_VIOLATION, if value is NULL.
235 * For base 0 it uses 10 as base and the string should be in the format
236 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
237 * For other bases the string should be in the format
238 * "{whitespace} [+|-] {digits}".
239 * No check is made for value overflow, only the lower 32 bits are assigned.
240 * If str is NULL it crashes, as the native function does.
243 * This function does not read garbage behind '\0' as the native version does.
248 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
249 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
250 PULONG value
) /* [O] Destination for the converted value */
254 ULONG RunningTotal
= 0;
257 /* skip leading whitespaces */
258 while (*str
!= '\0' && *str
<= ' ') str
++;
265 else if (*str
== '-')
271 /* base = 0 means autobase */
283 else if (str
[1] == 'o')
288 else if (str
[1] == 'x')
295 else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16)
297 return STATUS_INVALID_PARAMETER
;
300 if (value
== NULL
) return STATUS_ACCESS_VIOLATION
;
306 if (chCurrent
>= '0' && chCurrent
<= '9')
308 digit
= chCurrent
- '0';
310 else if (chCurrent
>= 'A' && chCurrent
<= 'Z')
312 digit
= chCurrent
- 'A' + 10;
314 else if (chCurrent
>= 'a' && chCurrent
<= 'z')
316 digit
= chCurrent
- 'a' + 10;
323 if (digit
< 0 || digit
>= (int)base
) break;
325 RunningTotal
= RunningTotal
* base
+ digit
;
329 *value
= bMinus
? (0 - RunningTotal
) : RunningTotal
;
330 return STATUS_SUCCESS
;
341 IN BOOLEAN CaseInsensitive
)
347 len
= min(s1
->Length
, s2
->Length
);
353 while (!ret
&& len
--)
354 ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
358 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
361 if (!ret
) ret
= s1
->Length
- s2
->Length
;
370 * TRUE if strings are equal.
377 IN BOOLEAN CaseInsensitive
)
379 if (s1
->Length
!= s2
->Length
) return FALSE
;
380 return !RtlCompareString(s1
, s2
, CaseInsensitive
);
387 * TRUE if strings are equal.
391 RtlEqualUnicodeString(
392 IN CONST UNICODE_STRING
*s1
,
393 IN CONST UNICODE_STRING
*s2
,
394 IN BOOLEAN CaseInsensitive
)
396 if (s1
->Length
!= s2
->Length
) return FALSE
;
397 return !RtlCompareUnicodeString(s1
, s2
, CaseInsensitive
);
405 RtlFreeAnsiString(IN PANSI_STRING AnsiString
)
409 if (AnsiString
->Buffer
)
411 RtlpFreeStringMemory(AnsiString
->Buffer
, TAG_ASTR
);
412 RtlZeroMemory(AnsiString
, sizeof(ANSI_STRING
));
421 RtlFreeOemString(IN POEM_STRING OemString
)
425 if (OemString
->Buffer
) RtlpFreeStringMemory(OemString
->Buffer
, TAG_OSTR
);
433 RtlFreeUnicodeString(IN PUNICODE_STRING UnicodeString
)
437 if (UnicodeString
->Buffer
)
439 RtlpFreeStringMemory(UnicodeString
->Buffer
, TAG_USTR
);
440 RtlZeroMemory(UnicodeString
, sizeof(UNICODE_STRING
));
449 * Check the OEM string to match the Unicode string.
451 * Functions which convert Unicode strings to OEM strings will set a
452 * DefaultChar from the OEM codepage when the characters are unknown.
453 * So check it against the Unicode string and return false when the
454 * Unicode string does not contain a TransDefaultChar.
458 RtlpDidUnicodeToOemWork(IN PCUNICODE_STRING UnicodeString
,
459 IN POEM_STRING OemString
)
463 if (NlsMbOemCodePageTag
== FALSE
)
465 /* single-byte code page */
466 /* Go through all characters of a string */
467 while (i
< OemString
->Length
)
469 /* Check if it got translated into a default char,
470 * but source char wasn't a default char equivalent
472 if ((OemString
->Buffer
[i
] == NlsOemDefaultChar
) &&
473 (UnicodeString
->Buffer
[i
] != NlsUnicodeDefaultChar
))
475 /* Yes, it means unmappable characters were found */
479 /* Move to the next char */
483 /* All chars were translated successfuly */
488 /* multibyte code page */
500 RtlIsValidOemCharacter(IN PWCHAR Char
)
510 * If source is NULL the length of source is assumed to be 0.
514 RtlInitAnsiString(IN OUT PANSI_STRING DestinationString
,
515 IN PCSZ SourceString
)
521 Size
= strlen(SourceString
);
522 if (Size
> (MAXUSHORT
- sizeof(CHAR
))) Size
= MAXUSHORT
- sizeof(CHAR
);
523 DestinationString
->Length
= (USHORT
)Size
;
524 DestinationString
->MaximumLength
= (USHORT
)Size
+ sizeof(CHAR
);
528 DestinationString
->Length
= 0;
529 DestinationString
->MaximumLength
= 0;
532 DestinationString
->Buffer
= (PCHAR
)SourceString
;
537 RtlInitAnsiStringEx(IN OUT PANSI_STRING DestinationString
,
538 IN PCSZ SourceString
)
544 Size
= strlen(SourceString
);
545 if (Size
> (MAXUSHORT
- sizeof(CHAR
))) return STATUS_NAME_TOO_LONG
;
546 DestinationString
->Length
= (USHORT
)Size
;
547 DestinationString
->MaximumLength
= (USHORT
)Size
+ sizeof(CHAR
);
551 DestinationString
->Length
= 0;
552 DestinationString
->MaximumLength
= 0;
555 DestinationString
->Buffer
= (PCHAR
)SourceString
;
556 return STATUS_SUCCESS
;
563 * If source is NULL the length of source is assumed to be 0.
568 IN OUT PSTRING DestinationString
,
569 IN PCSZ SourceString
)
571 RtlInitAnsiString(DestinationString
, SourceString
);
578 * If source is NULL the length of source is assumed to be 0.
582 RtlInitUnicodeString(
583 IN OUT PUNICODE_STRING DestinationString
,
584 IN PCWSTR SourceString
)
590 Size
= wcslen(SourceString
) * sizeof(WCHAR
);
591 if (Size
> (MAXUSHORT
- sizeof(WCHAR
))) Size
= MAXUSHORT
- sizeof(WCHAR
);
592 DestinationString
->Length
= (USHORT
)Size
;
593 DestinationString
->MaximumLength
= (USHORT
)Size
+ sizeof(WCHAR
);
597 DestinationString
->Length
= 0;
598 DestinationString
->MaximumLength
= 0;
601 DestinationString
->Buffer
= (PWCHAR
)SourceString
;
609 RtlInitUnicodeStringEx(
610 OUT PUNICODE_STRING DestinationString
,
611 IN PCWSTR SourceString
)
617 Size
= wcslen(SourceString
) * sizeof(WCHAR
);
618 if (Size
> (MAXUSHORT
- sizeof(WCHAR
))) return STATUS_NAME_TOO_LONG
;
619 DestinationString
->Length
= (USHORT
)Size
;
620 DestinationString
->MaximumLength
= (USHORT
)Size
+ sizeof(WCHAR
);
624 DestinationString
->Length
= 0;
625 DestinationString
->MaximumLength
= 0;
628 DestinationString
->Buffer
= (PWCHAR
)SourceString
;
629 return STATUS_SUCCESS
;
636 * Writes at most length characters to the string str.
637 * Str is nullterminated when length allowes it.
638 * When str fits exactly in length characters the nullterm is ommitted.
640 NTSTATUS NTAPI
RtlIntegerToChar(
641 ULONG value
, /* [I] Value to be converted */
642 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
643 ULONG length
, /* [I] Length of the str buffer in bytes */
644 PCHAR str
) /* [O] Destination for the converted value */
655 else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16)
657 return STATUS_INVALID_PARAMETER
;
666 digit
= (CHAR
)(value
% base
);
667 value
= value
/ base
;
675 *pos
= 'A' + digit
- 10;
680 len
= &buffer
[32] - pos
;
684 return STATUS_BUFFER_OVERFLOW
;
686 else if (str
== NULL
)
688 return STATUS_ACCESS_VIOLATION
;
690 else if (len
== length
)
692 memcpy(str
, pos
, len
);
696 memcpy(str
, pos
, len
+ 1);
699 return STATUS_SUCCESS
;
709 IN ULONG Base OPTIONAL
,
710 IN ULONG Length OPTIONAL
,
711 IN OUT LPWSTR String
)
722 if (Radix
== 0) Radix
= 10;
724 if ((Radix
!= 2) && (Radix
!= 8) &&
725 (Radix
!= 10) && (Radix
!= 16))
727 return STATUS_INVALID_PARAMETER
;
732 while (v
|| tp
== temp
)
737 if (i
< 10) *tp
= (WCHAR
)(i
+ L
'0');
738 else *tp
= (WCHAR
)(i
+ L
'a' - 10);
743 if ((ULONG
)((ULONG_PTR
)tp
- (ULONG_PTR
)temp
) >= Length
)
745 return STATUS_BUFFER_TOO_SMALL
;
750 while (tp
> temp
) *sp
++ = *--tp
;
754 return STATUS_SUCCESS
;
762 RtlIntegerToUnicodeString(
764 IN ULONG Base OPTIONAL
,
765 IN OUT PUNICODE_STRING String
)
767 ANSI_STRING AnsiString
;
771 Status
= RtlIntegerToChar(Value
, Base
, sizeof(Buffer
), Buffer
);
772 if (NT_SUCCESS(Status
))
774 AnsiString
.Buffer
= Buffer
;
775 AnsiString
.Length
= (USHORT
)strlen(Buffer
);
776 AnsiString
.MaximumLength
= sizeof(Buffer
);
778 Status
= RtlAnsiStringToUnicodeString(String
, &AnsiString
, FALSE
);
789 RtlInt64ToUnicodeString (
791 IN ULONG Base OPTIONAL
,
792 IN OUT PUNICODE_STRING String
)
794 LARGE_INTEGER LargeInt
;
795 ANSI_STRING AnsiString
;
799 LargeInt
.QuadPart
= Value
;
801 Status
= RtlLargeIntegerToChar(&LargeInt
, Base
, sizeof(Buffer
), Buffer
);
802 if (NT_SUCCESS(Status
))
804 AnsiString
.Buffer
= Buffer
;
805 AnsiString
.Length
= (USHORT
)strlen(Buffer
);
806 AnsiString
.MaximumLength
= sizeof(Buffer
);
808 Status
= RtlAnsiStringToUnicodeString(String
, &AnsiString
, FALSE
);
818 * TRUE if String2 contains String1 as a prefix.
823 PANSI_STRING String1
,
824 PANSI_STRING String2
,
825 BOOLEAN CaseInsensitive
)
831 if (String2
->Length
< String1
->Length
) return FALSE
;
833 Length
= String1
->Length
;
834 pc1
= String1
->Buffer
;
835 pc2
= String2
->Buffer
;
843 if (RtlUpperChar (*pc1
++) != RtlUpperChar (*pc2
++))
851 if (*pc1
++ != *pc2
++)
866 * TRUE if String2 contains String1 as a prefix.
870 RtlPrefixUnicodeString(
871 PCUNICODE_STRING String1
,
872 PCUNICODE_STRING String2
,
873 BOOLEAN CaseInsensitive
)
879 if (String2
->Length
< String1
->Length
)
882 Length
= String1
->Length
/ 2;
883 pc1
= String1
->Buffer
;
884 pc2
= String2
->Buffer
;
892 if (RtlUpcaseUnicodeChar(*pc1
++) !=
893 RtlUpcaseUnicodeChar(*pc2
++))
901 if( *pc1
++ != *pc2
++ )
916 RtlUnicodeStringToInteger(
917 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
918 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
919 ULONG
*value
) /* [O] Destination for the converted value */
921 LPWSTR lpwstr
= str
->Buffer
;
922 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
925 ULONG RunningTotal
= 0;
928 while (CharsRemaining
>= 1 && *lpwstr
<= ' ')
934 if (CharsRemaining
>= 1)
941 else if (*lpwstr
== '-')
953 if (CharsRemaining
>= 2 && lpwstr
[0] == '0')
955 if (lpwstr
[1] == 'b')
961 else if (lpwstr
[1] == 'o')
967 else if (lpwstr
[1] == 'x')
975 else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16)
977 return STATUS_INVALID_PARAMETER
;
982 return STATUS_ACCESS_VIOLATION
;
985 while (CharsRemaining
>= 1)
987 wchCurrent
= *lpwstr
;
989 if (wchCurrent
>= '0' && wchCurrent
<= '9')
991 digit
= wchCurrent
- '0';
993 else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z')
995 digit
= wchCurrent
- 'A' + 10;
997 else if (wchCurrent
>= 'a' && wchCurrent
<= 'z')
999 digit
= wchCurrent
- 'a' + 10;
1006 if (digit
< 0 || (ULONG
)digit
>= base
) break;
1008 RunningTotal
= RunningTotal
* base
+ digit
;
1013 *value
= bMinus
? (0 - RunningTotal
) : RunningTotal
;
1014 return STATUS_SUCCESS
;
1021 * Bytes necessary for the conversion including nullterm.
1025 RtlxUnicodeStringToOemSize(IN PCUNICODE_STRING UnicodeString
)
1029 /* Convert the Unicode String to Mb Size */
1030 RtlUnicodeToMultiByteSize(&Size
,
1031 UnicodeString
->Buffer
,
1032 UnicodeString
->Length
);
1034 /* Return the size + the null char */
1035 return (Size
+ sizeof(CHAR
));
1042 * This function always writes a terminating '\0'.
1043 * It performs a partial copy if ansi is too small.
1047 RtlUnicodeStringToAnsiString(
1048 IN OUT PANSI_STRING AnsiDest
,
1049 IN PCUNICODE_STRING UniSource
,
1050 IN BOOLEAN AllocateDestinationString
)
1052 NTSTATUS Status
= STATUS_SUCCESS
;
1053 NTSTATUS RealStatus
;
1059 ASSERT(!(UniSource
->Length
& 1));
1061 if (NlsMbCodePageTag
== FALSE
)
1063 Length
= (UniSource
->Length
+ sizeof(WCHAR
)) / sizeof(WCHAR
);
1067 Length
= RtlxUnicodeStringToAnsiSize(UniSource
);
1070 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1072 AnsiDest
->Length
= (USHORT
)Length
- sizeof(CHAR
);
1074 if (AllocateDestinationString
)
1076 AnsiDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_ASTR
);
1077 AnsiDest
->MaximumLength
= (USHORT
)Length
;
1079 if (!AnsiDest
->Buffer
) return STATUS_NO_MEMORY
;
1081 else if (AnsiDest
->Length
>= AnsiDest
->MaximumLength
)
1083 if (!AnsiDest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1085 Status
= STATUS_BUFFER_OVERFLOW
;
1086 AnsiDest
->Length
= AnsiDest
->MaximumLength
- 1;
1089 RealStatus
= RtlUnicodeToMultiByteN(AnsiDest
->Buffer
,
1095 if (!NT_SUCCESS(RealStatus
) && AllocateDestinationString
)
1097 RtlpFreeStringMemory(AnsiDest
->Buffer
, TAG_ASTR
);
1101 AnsiDest
->Buffer
[Index
] = ANSI_NULL
;
1109 * This function always writes a terminating '\0'.
1110 * Does NOT perform a partial copy if unicode is too small!
1114 RtlOemStringToUnicodeString(
1115 IN OUT PUNICODE_STRING UniDest
,
1116 IN PCOEM_STRING OemSource
,
1117 IN BOOLEAN AllocateDestinationString
)
1125 Length
= RtlOemStringToUnicodeSize(OemSource
);
1127 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1129 UniDest
->Length
= (USHORT
)Length
- sizeof(WCHAR
);
1131 if (AllocateDestinationString
)
1133 UniDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_USTR
);
1134 UniDest
->MaximumLength
= (USHORT
)Length
;
1136 if (!UniDest
->Buffer
) return STATUS_NO_MEMORY
;
1138 else if (UniDest
->Length
>= UniDest
->MaximumLength
)
1140 return STATUS_BUFFER_OVERFLOW
;
1143 Status
= RtlOemToUnicodeN(UniDest
->Buffer
,
1149 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1151 RtlpFreeStringMemory(UniDest
->Buffer
, TAG_USTR
);
1152 UniDest
->Buffer
= NULL
;
1156 UniDest
->Buffer
[Index
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1164 * This function always '\0' terminates the string returned.
1168 RtlUnicodeStringToOemString(
1169 IN OUT POEM_STRING OemDest
,
1170 IN PCUNICODE_STRING UniSource
,
1171 IN BOOLEAN AllocateDestinationString
)
1179 Length
= RtlUnicodeStringToOemSize(UniSource
);
1181 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1183 OemDest
->Length
= (USHORT
)Length
- sizeof(CHAR
);
1185 if (AllocateDestinationString
)
1187 OemDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_OSTR
);
1188 OemDest
->MaximumLength
= (USHORT
)Length
;
1190 if (!OemDest
->Buffer
) return STATUS_NO_MEMORY
;
1192 else if (OemDest
->Length
>= OemDest
->MaximumLength
)
1194 return STATUS_BUFFER_OVERFLOW
;
1197 Status
= RtlUnicodeToOemN(OemDest
->Buffer
,
1203 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1205 RtlpFreeStringMemory(OemDest
->Buffer
, TAG_OSTR
);
1206 OemDest
->Buffer
= NULL
;
1210 OemDest
->Buffer
[Index
] = ANSI_NULL
;
1214 #define ITU_IMPLEMENTED_TESTS (IS_TEXT_UNICODE_ODD_LENGTH|IS_TEXT_UNICODE_SIGNATURE)
1220 * The length of the string if all tests were passed, 0 otherwise.
1224 RtlIsTextUnicode( PVOID buf
, INT len
, INT
*pf
)
1226 static const WCHAR std_control_chars
[] = {'\r', '\n', '\t', ' ', 0x3000, 0};
1227 static const WCHAR byterev_control_chars
[] = {0x0d00, 0x0a00, 0x0900, 0x2000, 0};
1228 const WCHAR
*s
= buf
;
1230 unsigned int flags
= MAXULONG
, out_flags
= 0;
1232 if (len
< sizeof(WCHAR
))
1234 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1244 * Apply various tests to the text string. According to the
1245 * docs, each test "passed" sets the corresponding flag in
1246 * the output flags. But some of the tests are mutually
1247 * exclusive, so I don't see how you could pass all tests ...
1250 /* Check for an odd length ... pass if even. */
1251 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1253 if (((char *)buf
)[len
- 1] == 0)
1254 len
--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1256 len
/= sizeof(WCHAR
);
1258 /* Windows only checks the first 256 characters */
1259 if (len
> 256) len
= 256;
1261 /* Check for the special byte order unicode marks. */
1262 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1263 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1265 /* apply some statistical analysis */
1266 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1270 /* FIXME: checks only for ASCII characters in the unicode stream */
1271 for (i
= 0; i
< len
; i
++)
1273 if (s
[i
] <= 255) stats
++;
1276 if (stats
> len
/ 2)
1277 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1280 /* Check for unicode NULL chars */
1281 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1283 for (i
= 0; i
< len
; i
++)
1285 if (!(s
[i
] & 0xff) || !(s
[i
] >> 8))
1287 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1293 if (flags
& IS_TEXT_UNICODE_CONTROLS
)
1295 for (i
= 0; i
< len
; i
++)
1297 if (strchrW(std_control_chars
, s
[i
]))
1299 out_flags
|= IS_TEXT_UNICODE_CONTROLS
;
1305 if (flags
& IS_TEXT_UNICODE_REVERSE_CONTROLS
)
1307 for (i
= 0; i
< len
; i
++)
1309 if (strchrW(byterev_control_chars
, s
[i
]))
1311 out_flags
|= IS_TEXT_UNICODE_REVERSE_CONTROLS
;
1323 /* check for flags that indicate it's definitely not valid Unicode */
1324 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1326 /* now check for invalid ASCII, and assume Unicode if so */
1327 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1329 /* now check for Unicode flags */
1330 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1341 * Same as RtlOemStringToUnicodeString but doesn't write terminating null
1342 * A partial copy is NOT performed if the dest buffer is too small!
1346 RtlOemStringToCountedUnicodeString(
1347 IN OUT PUNICODE_STRING UniDest
,
1348 IN PCOEM_STRING OemSource
,
1349 IN BOOLEAN AllocateDestinationString
)
1357 /* Calculate size of the string */
1358 Length
= RtlOemStringToCountedUnicodeSize(OemSource
);
1360 /* If it's 0 then zero out dest string and return */
1363 RtlZeroMemory(UniDest
, sizeof(UNICODE_STRING
));
1364 return STATUS_SUCCESS
;
1367 /* Check if length is a sane value */
1368 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1370 /* Store it in dest string */
1371 UniDest
->Length
= (USHORT
)Length
;
1373 /* If we're asked to alloc the string - do so */
1374 if (AllocateDestinationString
)
1376 UniDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_USTR
);
1377 UniDest
->MaximumLength
= (USHORT
)Length
;
1379 if (!UniDest
->Buffer
) return STATUS_NO_MEMORY
;
1381 else if (UniDest
->Length
> UniDest
->MaximumLength
)
1383 return STATUS_BUFFER_OVERFLOW
;
1386 /* Do the conversion */
1387 Status
= RtlOemToUnicodeN(UniDest
->Buffer
,
1393 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1395 /* Conversion failed, free dest string and return status code */
1396 RtlpFreeStringMemory(UniDest
->Buffer
, TAG_USTR
);
1397 UniDest
->Buffer
= NULL
;
1401 return STATUS_SUCCESS
;
1408 * TRUE if the names are equal, FALSE if not
1411 * The comparison is case insensitive.
1415 RtlEqualComputerName(
1416 IN PUNICODE_STRING ComputerName1
,
1417 IN PUNICODE_STRING ComputerName2
)
1419 OEM_STRING OemString1
;
1420 OEM_STRING OemString2
;
1421 BOOLEAN Result
= FALSE
;
1423 if (NT_SUCCESS(RtlUpcaseUnicodeStringToOemString(&OemString1
,
1427 if (NT_SUCCESS(RtlUpcaseUnicodeStringToOemString(&OemString2
,
1431 Result
= RtlEqualString(&OemString1
, &OemString2
, FALSE
);
1432 RtlFreeOemString(&OemString2
);
1435 RtlFreeOemString(&OemString1
);
1445 * TRUE if the names are equal, FALSE if not
1448 * The comparison is case insensitive.
1452 RtlEqualDomainName (
1453 IN PUNICODE_STRING DomainName1
,
1454 IN PUNICODE_STRING DomainName2
)
1456 return RtlEqualComputerName(DomainName1
, DomainName2
);
1462 * RIPPED FROM WINE's ntdll\rtlstr.c rev 1.45
1464 * Convert a string representation of a GUID into a GUID.
1467 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1468 * guid [O] Destination for the converted GUID
1471 * Success: STATUS_SUCCESS. guid contains the converted value.
1472 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1475 * See RtlStringFromGUID.
1480 IN UNICODE_STRING
*str
,
1484 const WCHAR
*lpszCLSID
= str
->Buffer
;
1485 BYTE
* lpOut
= (BYTE
*)guid
;
1487 //TRACE("(%s,%p)\n", debugstr_us(str), guid);
1489 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1490 * to memory: DWORD... WORD WORD BYTES............
1497 if (*lpszCLSID
!= '{')
1498 return STATUS_INVALID_PARAMETER
;
1505 if (*lpszCLSID
!= '-')
1506 return STATUS_INVALID_PARAMETER
;
1510 if (*lpszCLSID
!= '}')
1511 return STATUS_INVALID_PARAMETER
;
1517 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
1520 /* Read two hex digits as a byte value */
1521 if (ch
>= '0' && ch
<= '9')
1523 else if (ch
>= 'a' && ch
<= 'f')
1525 else if (ch
>= 'A' && ch
<= 'F')
1528 return STATUS_INVALID_PARAMETER
;
1530 if (ch2
>= '0' && ch2
<= '9')
1532 else if (ch2
>= 'a' && ch2
<= 'f')
1533 ch2
= ch2
- 'a' + 10;
1534 else if (ch2
>= 'A' && ch2
<= 'F')
1535 ch2
= ch2
- 'A' + 10;
1537 return STATUS_INVALID_PARAMETER
;
1539 byte
= ch
<< 4 | ch2
;
1543 #ifndef WORDS_BIGENDIAN
1544 /* For Big Endian machines, we store the data such that the
1545 * dword/word members can be read as DWORDS and WORDS correctly. */
1578 lpszCLSID
++; /* Skip 2nd character of byte */
1587 return STATUS_SUCCESS
;
1595 RtlEraseUnicodeString(
1596 IN PUNICODE_STRING String
)
1598 if (String
->Buffer
&& String
->MaximumLength
)
1600 RtlZeroMemory(String
->Buffer
, String
->MaximumLength
);
1610 RtlHashUnicodeString(
1611 IN CONST UNICODE_STRING
*String
,
1612 IN BOOLEAN CaseInSensitive
,
1613 IN ULONG HashAlgorithm
,
1614 OUT PULONG HashValue
)
1616 if (String
!= NULL
&& HashValue
!= NULL
)
1618 switch (HashAlgorithm
)
1620 case HASH_STRING_ALGORITHM_DEFAULT
:
1621 case HASH_STRING_ALGORITHM_X65599
:
1626 end
= String
->Buffer
+ (String
->Length
/ sizeof(WCHAR
));
1628 if (CaseInSensitive
)
1630 for (c
= String
->Buffer
; c
!= end
; c
++)
1632 /* only uppercase characters if they are 'a' ... 'z'! */
1633 *HashValue
= ((65599 * (*HashValue
)) +
1634 (ULONG
)(((*c
) >= L
'a' && (*c
) <= L
'z') ?
1635 (*c
) - L
'a' + L
'A' : (*c
)));
1640 for (c
= String
->Buffer
; c
!= end
; c
++)
1642 *HashValue
= ((65599 * (*HashValue
)) + (ULONG
)(*c
));
1646 return STATUS_SUCCESS
;
1651 return STATUS_INVALID_PARAMETER
;
1658 * Same as RtlUnicodeStringToOemString but doesn't write terminating null
1659 * Does a partial copy if the dest buffer is too small
1663 RtlUnicodeStringToCountedOemString(
1664 IN OUT POEM_STRING OemDest
,
1665 IN PUNICODE_STRING UniSource
,
1666 IN BOOLEAN AllocateDestinationString
)
1674 /* Calculate size of the string */
1675 Length
= RtlUnicodeStringToCountedOemSize(UniSource
);
1677 /* If it's 0 then zero out dest string and return */
1680 RtlZeroMemory(OemDest
, sizeof(OEM_STRING
));
1681 return STATUS_SUCCESS
;
1684 /* Check if length is a sane value */
1685 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1687 /* Store it in dest string */
1688 OemDest
->Length
= (USHORT
)Length
;
1690 /* If we're asked to alloc the string - do so */
1691 if (AllocateDestinationString
)
1693 OemDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_OSTR
);
1694 OemDest
->MaximumLength
= (USHORT
)Length
;
1695 if (!OemDest
->Buffer
) return STATUS_NO_MEMORY
;
1697 else if (OemDest
->Length
> OemDest
->MaximumLength
)
1699 return STATUS_BUFFER_OVERFLOW
;
1702 /* Do the conversion */
1703 Status
= RtlUnicodeToOemN(OemDest
->Buffer
,
1709 /* Check for unmapped character */
1710 if (NT_SUCCESS(Status
) && !RtlpDidUnicodeToOemWork(UniSource
, OemDest
))
1711 Status
= STATUS_UNMAPPABLE_CHARACTER
;
1713 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1715 /* Conversion failed, free dest string and return status code */
1716 RtlpFreeStringMemory(OemDest
->Buffer
, TAG_OSTR
);
1717 OemDest
->Buffer
= NULL
;
1729 RtlLargeIntegerToChar(
1730 IN PLARGE_INTEGER Value
,
1733 IN OUT PCHAR String
)
1735 ULONGLONG Val
= Value
->QuadPart
;
1736 NTSTATUS Status
= STATUS_SUCCESS
;
1742 if (Base
== 0) Base
= 10;
1744 if ((Base
!= 2) && (Base
!= 8) && (Base
!= 10) && (Base
!= 16))
1746 return STATUS_INVALID_PARAMETER
;
1755 Digit
= (CHAR
)(Val
% Base
);
1761 *Pos
= 'A' + Digit
- 10;
1765 Len
= &Buffer
[64] - Pos
;
1768 return STATUS_BUFFER_OVERFLOW
;
1770 #if 1 /* It needs to be removed, when will probably use SEH in rtl */
1774 return STATUS_ACCESS_VIOLATION
;
1785 RtlCopyMemory(String
, Pos
, Len
);
1787 RtlCopyMemory(String
, Pos
, Len
+ 1);
1791 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
1793 /* Get the error code */
1794 Status
= _SEH2_GetExceptionCode();
1806 * dest is never '\0' terminated because it may be equal to src, and src
1807 * might not be '\0' terminated. dest->Length is only set upon success.
1811 RtlUpcaseUnicodeString(
1812 IN OUT PUNICODE_STRING UniDest
,
1813 IN PCUNICODE_STRING UniSource
,
1814 IN BOOLEAN AllocateDestinationString
)
1820 if (AllocateDestinationString
== TRUE
)
1822 UniDest
->MaximumLength
= UniSource
->Length
;
1823 UniDest
->Buffer
= RtlpAllocateStringMemory(UniDest
->MaximumLength
, TAG_USTR
);
1824 if (UniDest
->Buffer
== NULL
) return STATUS_NO_MEMORY
;
1826 else if (UniSource
->Length
> UniDest
->MaximumLength
)
1828 return STATUS_BUFFER_OVERFLOW
;
1831 j
= UniSource
->Length
/ sizeof(WCHAR
);
1833 for (i
= 0; i
< j
; i
++)
1835 UniDest
->Buffer
[i
] = RtlUpcaseUnicodeChar(UniSource
->Buffer
[i
]);
1838 UniDest
->Length
= UniSource
->Length
;
1839 return STATUS_SUCCESS
;
1846 * This function always writes a terminating '\0'.
1847 * It performs a partial copy if ansi is too small.
1851 RtlUpcaseUnicodeStringToAnsiString(
1852 IN OUT PANSI_STRING AnsiDest
,
1853 IN PUNICODE_STRING UniSource
,
1854 IN BOOLEAN AllocateDestinationString
)
1861 Length
= RtlUnicodeStringToAnsiSize(UniSource
);
1862 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1864 AnsiDest
->Length
= (USHORT
)Length
- sizeof(CHAR
);
1866 if (AllocateDestinationString
)
1868 AnsiDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_ASTR
);
1869 AnsiDest
->MaximumLength
= (USHORT
)Length
;
1870 if (!AnsiDest
->Buffer
) return STATUS_NO_MEMORY
;
1872 else if (AnsiDest
->Length
>= AnsiDest
->MaximumLength
)
1874 if (!AnsiDest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1877 Status
= RtlUpcaseUnicodeToMultiByteN(AnsiDest
->Buffer
,
1883 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1885 RtlpFreeStringMemory(AnsiDest
->Buffer
, TAG_ASTR
);
1886 AnsiDest
->Buffer
= NULL
;
1890 AnsiDest
->Buffer
[Index
] = ANSI_NULL
;
1898 * This function always writes a terminating '\0'.
1899 * It performs a partial copy if ansi is too small.
1903 RtlUpcaseUnicodeStringToCountedOemString(
1904 IN OUT POEM_STRING OemDest
,
1905 IN PCUNICODE_STRING UniSource
,
1906 IN BOOLEAN AllocateDestinationString
)
1913 Length
= RtlUnicodeStringToCountedOemSize(UniSource
);
1917 RtlZeroMemory(OemDest
, sizeof(OEM_STRING
));
1920 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1922 OemDest
->Length
= (USHORT
)Length
;
1924 if (AllocateDestinationString
)
1926 OemDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_OSTR
);
1927 OemDest
->MaximumLength
= (USHORT
)Length
;
1928 if (!OemDest
->Buffer
) return STATUS_NO_MEMORY
;
1930 else if (OemDest
->Length
> OemDest
->MaximumLength
)
1932 return STATUS_BUFFER_OVERFLOW
;
1935 Status
= RtlUpcaseUnicodeToOemN(OemDest
->Buffer
,
1941 /* Check for unmapped characters */
1942 if (NT_SUCCESS(Status
) && !RtlpDidUnicodeToOemWork(UniSource
, OemDest
))
1943 Status
= STATUS_UNMAPPABLE_CHARACTER
;
1945 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
1947 RtlpFreeStringMemory(OemDest
->Buffer
, TAG_OSTR
);
1948 OemDest
->Buffer
= NULL
;
1958 * Oem string is allways nullterminated
1959 * It performs a partial copy if oem is too small.
1963 RtlUpcaseUnicodeStringToOemString (
1964 IN OUT POEM_STRING OemDest
,
1965 IN PCUNICODE_STRING UniSource
,
1966 IN BOOLEAN AllocateDestinationString
)
1973 Length
= RtlUnicodeStringToOemSize(UniSource
);
1974 if (Length
> MAXUSHORT
) return STATUS_INVALID_PARAMETER_2
;
1976 OemDest
->Length
= (USHORT
)Length
- sizeof(CHAR
);
1978 if (AllocateDestinationString
)
1980 OemDest
->Buffer
= RtlpAllocateStringMemory(Length
, TAG_OSTR
);
1981 OemDest
->MaximumLength
= (USHORT
)Length
;
1982 if (!OemDest
->Buffer
) return STATUS_NO_MEMORY
;
1984 else if (OemDest
->Length
>= OemDest
->MaximumLength
)
1986 return STATUS_BUFFER_OVERFLOW
;
1989 Status
= RtlUpcaseUnicodeToOemN(OemDest
->Buffer
,
1995 /* Check for unmapped characters */
1996 if (NT_SUCCESS(Status
) && !RtlpDidUnicodeToOemWork(UniSource
, OemDest
))
1997 Status
= STATUS_UNMAPPABLE_CHARACTER
;
1999 if (!NT_SUCCESS(Status
) && AllocateDestinationString
)
2001 RtlpFreeStringMemory(OemDest
->Buffer
, TAG_OSTR
);
2002 OemDest
->Buffer
= NULL
;
2006 OemDest
->Buffer
[Index
] = ANSI_NULL
;
2014 * Bytes calculated including nullterm
2018 RtlxOemStringToUnicodeSize(IN PCOEM_STRING OemString
)
2022 /* Convert the Mb String to Unicode Size */
2023 RtlMultiByteToUnicodeSize(&Size
,
2027 /* Return the size + null-char */
2028 return (Size
+ sizeof(WCHAR
));
2036 RtlStringFromGUID (IN REFGUID Guid
,
2037 OUT PUNICODE_STRING GuidString
)
2039 /* Setup the string */
2040 GuidString
->Length
= 38 * sizeof(WCHAR
);
2041 GuidString
->MaximumLength
= GuidString
->Length
+ sizeof(UNICODE_NULL
);
2042 GuidString
->Buffer
= RtlpAllocateStringMemory(GuidString
->MaximumLength
,
2044 if (!GuidString
->Buffer
) return STATUS_NO_MEMORY
;
2046 /* Now format the GUID */
2047 swprintf(GuidString
->Buffer
,
2048 L
"{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
2060 return STATUS_SUCCESS
;
2067 * Bytes calculated including nullterm
2071 RtlxUnicodeStringToAnsiSize(IN PCUNICODE_STRING UnicodeString
)
2076 ASSERT(!(UnicodeString
->Length
& 1));
2078 /* Convert the Unicode String to Mb Size */
2079 RtlUnicodeToMultiByteSize(&Size
,
2080 UnicodeString
->Buffer
,
2081 UnicodeString
->Length
);
2083 /* Return the size + null-char */
2084 return (Size
+ sizeof(CHAR
));
2092 RtlCompareUnicodeString(
2093 IN PCUNICODE_STRING s1
,
2094 IN PCUNICODE_STRING s2
,
2095 IN BOOLEAN CaseInsensitive
)
2101 len
= min(s1
->Length
, s2
->Length
) / sizeof(WCHAR
);
2105 if (CaseInsensitive
)
2107 while (!ret
&& len
--) ret
= RtlUpcaseUnicodeChar(*p1
++) - RtlUpcaseUnicodeChar(*p2
++);
2111 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
2114 if (!ret
) ret
= s1
->Length
- s2
->Length
;
2125 IN OUT PSTRING DestinationString
,
2126 IN PSTRING SourceString OPTIONAL
)
2131 /* Check if there was no source given */
2134 /* Simply return an empty string */
2135 DestinationString
->Length
= 0;
2139 /* Choose the smallest length */
2140 SourceLength
= min(DestinationString
->MaximumLength
,
2141 SourceString
->Length
);
2144 DestinationString
->Length
= (USHORT
)SourceLength
;
2146 /* Save the pointers to each buffer */
2147 p1
= DestinationString
->Buffer
;
2148 p2
= SourceString
->Buffer
;
2150 /* Loop the buffer */
2151 while (SourceLength
)
2153 /* Copy the character and move on */
2165 RtlCopyUnicodeString(
2166 IN OUT PUNICODE_STRING DestinationString
,
2167 IN PCUNICODE_STRING SourceString
)
2171 if(SourceString
== NULL
)
2173 DestinationString
->Length
= 0;
2177 SourceLength
= min(DestinationString
->MaximumLength
,
2178 SourceString
->Length
);
2179 DestinationString
->Length
= (USHORT
)SourceLength
;
2181 RtlCopyMemory(DestinationString
->Buffer
,
2182 SourceString
->Buffer
,
2185 if (DestinationString
->Length
< DestinationString
->MaximumLength
)
2187 DestinationString
->Buffer
[SourceLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2196 * Creates a nullterminated UNICODE_STRING
2200 RtlCreateUnicodeString(
2201 IN OUT PUNICODE_STRING UniDest
,
2207 Size
= (wcslen(Source
) + 1) * sizeof(WCHAR
);
2208 if (Size
> MAXUSHORT
) return FALSE
;
2210 UniDest
->Buffer
= RtlpAllocateStringMemory((ULONG
)Size
, TAG_USTR
);
2212 if (UniDest
->Buffer
== NULL
) return FALSE
;
2214 RtlCopyMemory(UniDest
->Buffer
, Source
, Size
);
2215 UniDest
->MaximumLength
= (USHORT
)Size
;
2216 UniDest
->Length
= (USHORT
)Size
- sizeof (WCHAR
);
2226 RtlCreateUnicodeStringFromAsciiz(
2227 OUT PUNICODE_STRING Destination
,
2230 ANSI_STRING AnsiString
;
2233 RtlInitAnsiString(&AnsiString
, Source
);
2235 Status
= RtlAnsiStringToUnicodeString(Destination
,
2239 return NT_SUCCESS(Status
);
2246 * Dest is never '\0' terminated because it may be equal to src, and src
2247 * might not be '\0' terminated.
2248 * Dest->Length is only set upon success.
2252 RtlDowncaseUnicodeString(
2253 IN OUT PUNICODE_STRING UniDest
,
2254 IN PCUNICODE_STRING UniSource
,
2255 IN BOOLEAN AllocateDestinationString
)
2261 if (AllocateDestinationString
)
2263 UniDest
->MaximumLength
= UniSource
->Length
;
2264 UniDest
->Buffer
= RtlpAllocateStringMemory(UniSource
->Length
, TAG_USTR
);
2265 if (UniDest
->Buffer
== NULL
) return STATUS_NO_MEMORY
;
2267 else if (UniSource
->Length
> UniDest
->MaximumLength
)
2269 return STATUS_BUFFER_OVERFLOW
;
2272 UniDest
->Length
= UniSource
->Length
;
2273 StopGap
= UniSource
->Length
/ sizeof(WCHAR
);
2275 for (i
= 0 ; i
< StopGap
; i
++)
2277 if (UniSource
->Buffer
[i
] < L
'A')
2279 UniDest
->Buffer
[i
] = UniSource
->Buffer
[i
];
2281 else if (UniSource
->Buffer
[i
] <= L
'Z')
2283 UniDest
->Buffer
[i
] = (UniSource
->Buffer
[i
] + (L
'a' - L
'A'));
2287 UniDest
->Buffer
[i
] = RtlDowncaseUnicodeChar(UniSource
->Buffer
[i
]);
2291 return STATUS_SUCCESS
;
2298 * if src is NULL dest is unchanged.
2299 * dest is '\0' terminated when the MaximumLength allowes it.
2300 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
2304 RtlAppendUnicodeToString(IN OUT PUNICODE_STRING Destination
,
2312 UNICODE_STRING UnicodeSource
;
2314 RtlInitUnicodeString(&UnicodeSource
, Source
);
2315 Length
= UnicodeSource
.Length
;
2317 if (Destination
->Length
+ Length
> Destination
->MaximumLength
)
2319 return STATUS_BUFFER_TOO_SMALL
;
2322 DestBuffer
= &Destination
->Buffer
[Destination
->Length
/ sizeof(WCHAR
)];
2323 RtlMoveMemory(DestBuffer
, Source
, Length
);
2324 Destination
->Length
+= Length
;
2326 /* append terminating '\0' if enough space */
2327 if(Destination
->MaximumLength
> Destination
->Length
)
2329 DestBuffer
[Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2333 return STATUS_SUCCESS
;
2340 * if src is NULL dest is unchanged.
2341 * dest is never '\0' terminated.
2345 RtlAppendAsciizToString(
2346 IN OUT PSTRING Destination
,
2353 Size
= strlen(Source
);
2355 if (Destination
->Length
+ Size
> Destination
->MaximumLength
)
2357 return STATUS_BUFFER_TOO_SMALL
;
2360 RtlMoveMemory(&Destination
->Buffer
[Destination
->Length
], Source
, Size
);
2361 Destination
->Length
+= (USHORT
)Size
;
2364 return STATUS_SUCCESS
;
2372 RtlUpperString(PSTRING DestinationString
,
2373 PSTRING SourceString
)
2378 Length
= min(SourceString
->Length
,
2379 DestinationString
->MaximumLength
);
2381 Src
= SourceString
->Buffer
;
2382 Dest
= DestinationString
->Buffer
;
2383 DestinationString
->Length
= Length
;
2387 *Dest
++ = RtlUpperChar(*Src
++);
2396 * See RtlpDuplicateUnicodeString
2400 RtlDuplicateUnicodeString(
2402 IN PCUNICODE_STRING SourceString
,
2403 OUT PUNICODE_STRING DestinationString
)
2407 if (SourceString
== NULL
|| DestinationString
== NULL
||
2408 SourceString
->Length
> SourceString
->MaximumLength
||
2409 (SourceString
->Length
== 0 && SourceString
->MaximumLength
> 0 && SourceString
->Buffer
== NULL
) ||
2410 Flags
== RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING
|| Flags
>= 4)
2412 return STATUS_INVALID_PARAMETER
;
2416 if ((SourceString
->Length
== 0) &&
2417 (Flags
!= (RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
|
2418 RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING
)))
2420 DestinationString
->Length
= 0;
2421 DestinationString
->MaximumLength
= 0;
2422 DestinationString
->Buffer
= NULL
;
2426 UINT DestMaxLength
= SourceString
->Length
;
2428 if (Flags
& RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
)
2429 DestMaxLength
+= sizeof(UNICODE_NULL
);
2431 DestinationString
->Buffer
= RtlpAllocateStringMemory(DestMaxLength
, TAG_USTR
);
2433 if (DestinationString
->Buffer
== NULL
)
2434 return STATUS_NO_MEMORY
;
2436 RtlCopyMemory(DestinationString
->Buffer
, SourceString
->Buffer
, SourceString
->Length
);
2437 DestinationString
->Length
= SourceString
->Length
;
2438 DestinationString
->MaximumLength
= DestMaxLength
;
2440 if (Flags
& RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
)
2441 DestinationString
->Buffer
[DestinationString
->Length
/ sizeof(WCHAR
)] = 0;
2444 return STATUS_SUCCESS
;
2452 RtlValidateUnicodeString(IN ULONG Flags
,
2453 IN PCUNICODE_STRING UnicodeString
)
2455 /* currently no flags are supported! */
2459 ((UnicodeString
== NULL
) ||
2460 ((UnicodeString
->Length
!= 0) &&
2461 (UnicodeString
->Buffer
!= NULL
) &&
2462 ((UnicodeString
->Length
% sizeof(WCHAR
)) == 0) &&
2463 ((UnicodeString
->MaximumLength
% sizeof(WCHAR
)) == 0) &&
2464 (UnicodeString
->MaximumLength
>= UnicodeString
->Length
))))
2466 /* a NULL pointer as a unicode string is considered to be a valid unicode
2468 return STATUS_SUCCESS
;
2472 return STATUS_INVALID_PARAMETER
;
2481 RtlpEnsureBufferSize(ULONG Unknown1
, ULONG Unknown2
, ULONG Unknown3
)
2483 DPRINT1("RtlpEnsureBufferSize: stub\n");
2484 return STATUS_NOT_IMPLEMENTED
;
2492 RtlFindCharInUnicodeString(
2494 IN PUNICODE_STRING SearchString
,
2495 IN PCUNICODE_STRING MatchString
,
2496 OUT PUSHORT Position
)
2505 for (i
= 0; i
< SearchString
->Length
/ sizeof(WCHAR
); i
++)
2507 for (j
= 0; j
< MatchString
->Length
/ sizeof(WCHAR
); j
++)
2509 if (SearchString
->Buffer
[i
] == MatchString
->Buffer
[j
])
2511 *Position
= (i
+ 1) * sizeof(WCHAR
);
2512 return STATUS_SUCCESS
;
2518 return STATUS_NOT_FOUND
;
2523 for (i
= SearchString
->Length
/ sizeof(WCHAR
) - 1; i
>= 0; i
--)
2525 for (j
= 0; j
< MatchString
->Length
/ sizeof(WCHAR
); j
++)
2527 if (SearchString
->Buffer
[i
] == MatchString
->Buffer
[j
])
2529 *Position
= i
* sizeof(WCHAR
);
2530 return STATUS_SUCCESS
;
2536 return STATUS_NOT_FOUND
;
2541 for (i
= 0; i
< SearchString
->Length
/ sizeof(WCHAR
); i
++)
2545 while (j
< MatchString
->Length
/ sizeof(WCHAR
) &&
2546 SearchString
->Buffer
[i
] != MatchString
->Buffer
[j
])
2551 if (j
>= MatchString
->Length
/ sizeof(WCHAR
))
2553 *Position
= (i
+ 1) * sizeof(WCHAR
);
2554 return STATUS_SUCCESS
;
2559 return STATUS_NOT_FOUND
;
2564 for (i
= SearchString
->Length
/ sizeof(WCHAR
) - 1; i
>= 0; i
--)
2568 while (j
< MatchString
->Length
/ sizeof(WCHAR
) &&
2569 SearchString
->Buffer
[i
] != MatchString
->Buffer
[j
])
2574 if (j
>= MatchString
->Length
/ sizeof(WCHAR
))
2576 *Position
= i
* sizeof(WCHAR
);
2577 return STATUS_SUCCESS
;
2582 return STATUS_NOT_FOUND
;
2586 return STATUS_NOT_FOUND
;
2593 * Get the maximum of MAX_COMPUTERNAME_LENGTH characters from the dns.host name until the dot is found.
2594 * Convert is to an uppercase oem string and check for unmapped characters.
2595 * Then convert the oem string back to an unicode string.
2599 RtlDnsHostNameToComputerName(PUNICODE_STRING ComputerName
, PUNICODE_STRING DnsHostName
, BOOLEAN AllocateComputerNameString
)
2603 ULONG ComputerNameLength
;
2604 ULONG ComputerNameOemNLength
;
2605 OEM_STRING ComputerNameOem
;
2606 CHAR ComputerNameOemN
[MAX_COMPUTERNAME_LENGTH
+ 1];
2608 Status
= STATUS_INVALID_COMPUTER_NAME
;
2609 ComputerNameLength
= DnsHostName
->Length
;
2611 /* find the first dot in the dns host name */
2612 for (Length
= 0; Length
< DnsHostName
->Length
/ sizeof(WCHAR
); Length
++)
2614 if (DnsHostName
->Buffer
[Length
] == L
'.')
2616 /* dot found, so set the length for the oem translation */
2617 ComputerNameLength
= Length
* sizeof(WCHAR
);
2622 /* the computername must have one character */
2623 if (ComputerNameLength
> 0)
2625 ComputerNameOemNLength
= 0;
2626 /* convert to oem string and use uppercase letters */
2627 Status
= RtlUpcaseUnicodeToOemN(ComputerNameOemN
,
2628 MAX_COMPUTERNAME_LENGTH
,
2629 &ComputerNameOemNLength
,
2630 DnsHostName
->Buffer
,
2631 ComputerNameLength
);
2633 /* status STATUS_BUFFER_OVERFLOW is not a problem since the computername shoud only
2634 have MAX_COMPUTERNAME_LENGTH characters */
2635 if ((Status
== STATUS_SUCCESS
) ||
2636 (Status
== STATUS_BUFFER_OVERFLOW
))
2638 /* set the termination for the oem string */
2639 ComputerNameOemN
[MAX_COMPUTERNAME_LENGTH
] = 0;
2640 /* set status for the case the next function failed */
2641 Status
= STATUS_INVALID_COMPUTER_NAME
;
2642 /* fillup the oem string structure with the converted computername
2643 and check it for unmapped characters */
2644 ComputerNameOem
.Buffer
= ComputerNameOemN
;
2645 ComputerNameOem
.Length
= (USHORT
)ComputerNameOemNLength
;
2646 ComputerNameOem
.MaximumLength
= (USHORT
)(MAX_COMPUTERNAME_LENGTH
+ 1);
2648 if (RtlpDidUnicodeToOemWork(DnsHostName
, &ComputerNameOem
) == TRUE
)
2650 /* no unmapped character so convert it back to an unicode string */
2651 Status
= RtlOemStringToUnicodeString(ComputerName
,
2653 AllocateComputerNameString
);