fixed returned status code in RtlAnsiStringToUnicodeString and properly free allocate...
[reactos.git] / reactos / lib / rtl / unicode.c
index 995d889..9b29db3 100644 (file)
@@ -27,7 +27,7 @@ extern PUSHORT NlsLeadByteInfo;
 * @implemented
 */
 WCHAR
-STDCALL
+NTAPI
 RtlAnsiCharToUnicodeChar(IN PUCHAR *AnsiChar)
 {
     ULONG Size;
@@ -59,7 +59,7 @@ RtlAnsiCharToUnicodeChar(IN PUCHAR *AnsiChar)
  *  If the dest buffer is too small a partial copy is NOT performed!
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlAnsiStringToUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PANSI_STRING AnsiSource,
@@ -69,19 +69,21 @@ RtlAnsiStringToUnicodeString(
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlAnsiStringToUnicodeSize(AnsiSource);
     if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
     UniDest->Length = (USHORT)Length - sizeof(WCHAR);
 
-    if (AllocateDestinationString == TRUE)
+    if (AllocateDestinationString)
     {
         UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
         UniDest->MaximumLength = Length;
         if (!UniDest->Buffer) return STATUS_NO_MEMORY;
     }
-    else if (Length >= UniDest->MaximumLength)
+    else if (UniDest->Length >= UniDest->MaximumLength)
     {
-        return STATUS_BUFFER_TOO_SMALL;
+        return STATUS_BUFFER_OVERFLOW;
     }
 
     Status = RtlMultiByteToUnicodeN(UniDest->Buffer,
@@ -90,9 +92,13 @@ RtlAnsiStringToUnicodeString(
                                     AnsiSource->Buffer,
                                     AnsiSource->Length);
 
-    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    if (!NT_SUCCESS(Status))
     {
-        RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
+        if (AllocateDestinationString)
+        {
+            RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
+            UniDest->Buffer = NULL;
+        }
         return Status;
     }
 
@@ -107,7 +113,7 @@ RtlAnsiStringToUnicodeString(
  *  The calculated size in bytes including nullterm.
  */
 ULONG
-STDCALL
+NTAPI
 RtlxAnsiStringToUnicodeSize(IN PCANSI_STRING AnsiString)
 {
     ULONG Size;
@@ -129,7 +135,7 @@ RtlxAnsiStringToUnicodeSize(IN PCANSI_STRING AnsiString)
  *  Dest is never nullterminated.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlAppendStringToString(IN PSTRING Destination,
                         IN PSTRING Source)
 {
@@ -161,7 +167,7 @@ RtlAppendStringToString(IN PSTRING Destination,
  *  When dest fits exactly in MaximumLength characters the nullterm is ommitted.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlAppendUnicodeStringToString(
    IN OUT PUNICODE_STRING Destination,
    IN PCUNICODE_STRING Source)
@@ -211,7 +217,7 @@ RtlAppendUnicodeStringToString(
  *  This function does not read garbage behind '\0' as the native version does.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlCharToInteger(
     PCSZ str,      /* [I] '\0' terminated single-byte string containing a number */
     ULONG base,    /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
@@ -283,7 +289,7 @@ RtlCharToInteger(
  * @implemented
  */
 LONG
-STDCALL
+NTAPI
 RtlCompareString(
    IN PSTRING s1,
    IN PSTRING s2,
@@ -316,7 +322,7 @@ RtlCompareString(
  *  TRUE if strings are equal.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlEqualString(
    IN PSTRING s1,
    IN PSTRING s2,
@@ -333,7 +339,7 @@ RtlEqualString(
  *  TRUE if strings are equal.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlEqualUnicodeString(
    IN CONST UNICODE_STRING *s1,
    IN CONST UNICODE_STRING *s2,
@@ -347,9 +353,11 @@ RtlEqualUnicodeString(
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlFreeAnsiString(IN PANSI_STRING AnsiString)
 {
+    PAGED_CODE_RTL();
+
     if (AnsiString->Buffer)
     {
         RtlpFreeStringMemory(AnsiString->Buffer, TAG_ASTR);
@@ -361,9 +369,11 @@ RtlFreeAnsiString(IN PANSI_STRING AnsiString)
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlFreeOemString(IN POEM_STRING OemString)
 {
+   PAGED_CODE_RTL();
+
    if (OemString->Buffer) RtlpFreeStringMemory(OemString->Buffer, TAG_OSTR);
 }
 
@@ -371,9 +381,11 @@ RtlFreeOemString(IN POEM_STRING OemString)
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlFreeUnicodeString(IN PUNICODE_STRING UnicodeString)
 {
+    PAGED_CODE_RTL();
+
     if (UnicodeString->Buffer)
     {
         RtlpFreeStringMemory(UnicodeString->Buffer, TAG_ASTR);
@@ -385,7 +397,7 @@ RtlFreeUnicodeString(IN PUNICODE_STRING UnicodeString)
 * @unimplemented
 */
 BOOLEAN
-STDCALL
+NTAPI
 RtlIsValidOemCharacter(IN PWCHAR Char)
 {
     UNIMPLEMENTED;
@@ -399,7 +411,7 @@ RtlIsValidOemCharacter(IN PWCHAR Char)
  *  If source is NULL the length of source is assumed to be 0.
  */
 VOID
-STDCALL
+NTAPI
 RtlInitAnsiString(IN OUT PANSI_STRING DestinationString,
                   IN PCSZ SourceString)
 {
@@ -427,7 +439,7 @@ RtlInitAnsiString(IN OUT PANSI_STRING DestinationString,
  *  If source is NULL the length of source is assumed to be 0.
  */
 VOID
-STDCALL
+NTAPI
 RtlInitString(
    IN OUT PSTRING DestinationString,
    IN PCSZ SourceString)
@@ -442,7 +454,7 @@ RtlInitString(
  *  If source is NULL the length of source is assumed to be 0.
  */
 VOID
-STDCALL
+NTAPI
 RtlInitUnicodeString(IN OUT PUNICODE_STRING DestinationString,
                      IN PCWSTR SourceString)
 {
@@ -467,7 +479,7 @@ RtlInitUnicodeString(IN OUT PUNICODE_STRING DestinationString,
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlInitUnicodeStringEx(OUT PUNICODE_STRING DestinationString,
                        IN PCWSTR SourceString)
 {
@@ -476,7 +488,7 @@ RtlInitUnicodeStringEx(OUT PUNICODE_STRING DestinationString,
     if(SourceString)
     {
         DestSize = wcslen(SourceString) * sizeof(WCHAR);
-        if (DestSize > 0xFFFC) return STATUS_NAME_TOO_LONG;
+        if (DestSize >= 0xFFFC) return STATUS_NAME_TOO_LONG;
         DestinationString->Length = (USHORT)DestSize;
         DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR);
     }
@@ -499,7 +511,7 @@ RtlInitUnicodeStringEx(OUT PUNICODE_STRING DestinationString,
  *  When str fits exactly in length characters the nullterm is ommitted.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlIntegerToChar(
    IN ULONG Value,
    IN ULONG Base,
@@ -552,7 +564,7 @@ RtlIntegerToChar(
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlIntegerToUnicode(
     IN ULONG Value,
     IN ULONG Base  OPTIONAL,
@@ -606,7 +618,7 @@ RtlIntegerToUnicode(
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlIntegerToUnicodeString(
    IN ULONG Value,
    IN ULONG Base OPTIONAL,
@@ -633,7 +645,7 @@ RtlIntegerToUnicodeString(
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlInt64ToUnicodeString (
     IN ULONGLONG Value,
     IN ULONG Base OPTIONAL,
@@ -666,7 +678,7 @@ RtlInt64ToUnicodeString (
  *  TRUE if String2 contains String1 as a prefix.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlPrefixString(
    PANSI_STRING String1,
    PANSI_STRING String2,
@@ -713,7 +725,7 @@ RtlPrefixString(
  *  TRUE if String2 contains String1 as a prefix.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlPrefixUnicodeString(
    PCUNICODE_STRING String1,
    PCUNICODE_STRING String2,
@@ -781,7 +793,7 @@ RtlPrefixUnicodeString(
  *  version does.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUnicodeStringToInteger(
     PCUNICODE_STRING str, /* [I] Unicode string to be converted */
     ULONG base,                /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
@@ -872,7 +884,7 @@ RtlUnicodeStringToInteger(
  *  Bytes necessary for the conversion including nullterm.
  */
 ULONG
-STDCALL
+NTAPI
 RtlxUnicodeStringToOemSize(IN PCUNICODE_STRING UnicodeString)
 {
     ULONG Size;
@@ -894,7 +906,7 @@ RtlxUnicodeStringToOemSize(IN PCUNICODE_STRING UnicodeString)
  *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUnicodeStringToAnsiString(
    IN OUT PANSI_STRING AnsiDest,
    IN PCUNICODE_STRING UniSource,
@@ -905,6 +917,8 @@ RtlUnicodeStringToAnsiString(
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlUnicodeStringToAnsiSize(UniSource);
     if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
@@ -948,7 +962,7 @@ RtlUnicodeStringToAnsiString(
  *  Does NOT perform a partial copy if unicode is too small!
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlOemStringToUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCOEM_STRING OemSource,
@@ -958,6 +972,8 @@ RtlOemStringToUnicodeString(
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlOemStringToUnicodeSize(OemSource);
     if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
@@ -998,7 +1014,7 @@ RtlOemStringToUnicodeString(
  *   This function always '\0' terminates the string returned.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUnicodeStringToOemString(
    IN OUT POEM_STRING OemDest,
    IN PCUNICODE_STRING UniSource,
@@ -1008,6 +1024,8 @@ RtlUnicodeStringToOemString(
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlUnicodeStringToOemSize(UniSource);
     if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
@@ -1049,7 +1067,7 @@ RtlUnicodeStringToOemString(
  * RETURNS
  *  The length of the string if all tests were passed, 0 otherwise.
  */
-ULONG STDCALL
+ULONG NTAPI
 RtlIsTextUnicode (PVOID Buffer,
                   ULONG Length,
                   ULONG *Flags)
@@ -1109,7 +1127,7 @@ done:
  *  A partial copy is NOT performed if the dest buffer is too small!
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlOemStringToCountedUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCOEM_STRING OemSource,
@@ -1119,6 +1137,8 @@ RtlOemStringToCountedUnicodeString(
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlOemStringToCountedUnicodeSize(OemSource);
 
     if (!Length)
@@ -1168,7 +1188,7 @@ RtlOemStringToCountedUnicodeString(
  *  The comparison is case insensitive.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlEqualComputerName(
    IN PUNICODE_STRING ComputerName1,
    IN PUNICODE_STRING ComputerName2)
@@ -1204,7 +1224,7 @@ RtlEqualComputerName(
  *  The comparison is case insensitive.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlEqualDomainName (
    IN PUNICODE_STRING DomainName1,
    IN PUNICODE_STRING DomainName2
@@ -1232,7 +1252,7 @@ RtlEqualDomainName (
  *  See RtlStringFromGUID.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlGUIDFromString(
    IN UNICODE_STRING *str,
    OUT GUID* guid
@@ -1346,7 +1366,7 @@ RtlGUIDFromString(
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlEraseUnicodeString(
    IN PUNICODE_STRING String)
 {
@@ -1361,7 +1381,7 @@ RtlEraseUnicodeString(
 * @implemented
 */
 NTSTATUS
-STDCALL
+NTAPI
 RtlHashUnicodeString(
   IN CONST UNICODE_STRING *String,
   IN BOOLEAN CaseInSensitive,
@@ -1417,7 +1437,7 @@ RtlHashUnicodeString(
  *  Does a partial copy if the dest buffer is too small
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUnicodeStringToCountedOemString(
    IN OUT POEM_STRING OemDest,
    IN PUNICODE_STRING UniSource,
@@ -1427,6 +1447,8 @@ RtlUnicodeStringToCountedOemString(
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlUnicodeStringToCountedOemSize(UniSource);
 
     if (!Length)
@@ -1471,7 +1493,7 @@ RtlUnicodeStringToCountedOemString(
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlLargeIntegerToChar(
    IN PLARGE_INTEGER Value,
    IN ULONG  Base,
@@ -1524,7 +1546,7 @@ RtlLargeIntegerToChar(
  *  might not be '\0' terminated. dest->Length is only set upon success.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUpcaseUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCUNICODE_STRING UniSource,
@@ -1532,6 +1554,8 @@ RtlUpcaseUnicodeString(
 {
     ULONG i, j;
 
+    PAGED_CODE_RTL();
+
     if (AllocateDestinationString == TRUE)
     {
         UniDest->MaximumLength = UniSource->Length;
@@ -1562,7 +1586,7 @@ RtlUpcaseUnicodeString(
  *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUpcaseUnicodeStringToAnsiString(
    IN OUT PANSI_STRING AnsiDest,
    IN PUNICODE_STRING UniSource,
@@ -1572,6 +1596,8 @@ RtlUpcaseUnicodeStringToAnsiString(
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlUnicodeStringToAnsiSize(UniSource);
     if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
@@ -1613,7 +1639,7 @@ RtlUpcaseUnicodeStringToAnsiString(
  *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUpcaseUnicodeStringToCountedOemString(
    IN OUT POEM_STRING OemDest,
    IN PCUNICODE_STRING UniSource,
@@ -1623,6 +1649,8 @@ RtlUpcaseUnicodeStringToCountedOemString(
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlUnicodeStringToCountedOemSize(UniSource);
 
     if (!Length)
@@ -1670,7 +1698,7 @@ RtlUpcaseUnicodeStringToCountedOemString(
  *  It performs a partial copy if oem is too small.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUpcaseUnicodeStringToOemString (
    IN OUT POEM_STRING OemDest,
    IN PCUNICODE_STRING UniSource,
@@ -1680,6 +1708,8 @@ RtlUpcaseUnicodeStringToOemString (
     ULONG Length;
     ULONG Index;
 
+    PAGED_CODE_RTL();
+
     Length = RtlUnicodeStringToOemSize(UniSource);
     if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
@@ -1722,7 +1752,7 @@ RtlUpcaseUnicodeStringToOemString (
  *  Bytes calculated including nullterm
  */
 ULONG
-STDCALL
+NTAPI
 RtlxOemStringToUnicodeSize(IN PCOEM_STRING OemString)
 {
     ULONG Size;
@@ -1740,11 +1770,11 @@ RtlxOemStringToUnicodeSize(IN PCOEM_STRING OemString)
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlStringFromGUID (IN REFGUID Guid,
                    OUT PUNICODE_STRING GuidString)
 {
-   STATIC CONST PWCHAR Hex = L"0123456789ABCDEF";
+   static CONST PWCHAR Hex = L"0123456789ABCDEF";
    WCHAR Buffer[40];
    PWCHAR BufferPtr;
    ULONG i;
@@ -1783,7 +1813,7 @@ RtlStringFromGUID (IN REFGUID Guid,
  *  Bytes calculated including nullterm
  */
 ULONG
-STDCALL
+NTAPI
 RtlxUnicodeStringToAnsiSize(IN PCUNICODE_STRING UnicodeString)
 {
     ULONG Size;
@@ -1801,7 +1831,7 @@ RtlxUnicodeStringToAnsiSize(IN PCUNICODE_STRING UnicodeString)
  * @implemented
  */
 LONG
-STDCALL
+NTAPI
 RtlCompareUnicodeString(
    IN PCUNICODE_STRING s1,
    IN PCUNICODE_STRING s2,
@@ -1831,7 +1861,7 @@ RtlCompareUnicodeString(
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlCopyString(
    IN OUT PSTRING DestinationString,
    IN PSTRING SourceString OPTIONAL)
@@ -1872,7 +1902,7 @@ RtlCopyString(
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlCopyUnicodeString(
    IN OUT PUNICODE_STRING DestinationString,
    IN PCUNICODE_STRING SourceString)
@@ -1907,14 +1937,18 @@ RtlCopyUnicodeString(
  * Creates a nullterminated UNICODE_STRING
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlCreateUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCWSTR  Source)
 {
     ULONG Length;
 
+    PAGED_CODE_RTL();
+
     Length = (wcslen(Source) + 1) * sizeof(WCHAR);
+    if (Length > 0xFFFE) return FALSE;
+
     UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
 
     if (UniDest->Buffer == NULL) return FALSE;
@@ -1930,7 +1964,7 @@ RtlCreateUnicodeString(
  * @implemented
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlCreateUnicodeStringFromAsciiz(
    OUT PUNICODE_STRING Destination,
    IN PCSZ Source)
@@ -1956,7 +1990,7 @@ RtlCreateUnicodeStringFromAsciiz(
  *  Dest->Length is only set upon success.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlDowncaseUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCUNICODE_STRING UniSource,
@@ -1965,6 +1999,8 @@ RtlDowncaseUnicodeString(
     ULONG i;
     ULONG StopGap;
 
+    PAGED_CODE_RTL();
+
     if (AllocateDestinationString)
     {
         UniDest->MaximumLength = UniSource->Length;
@@ -2007,7 +2043,7 @@ RtlDowncaseUnicodeString(
  *  When dest fits exactly in MaximumLength characters the '\0' is ommitted.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlAppendUnicodeToString(IN OUT PUNICODE_STRING Destination,
                          IN PCWSTR Source)
 {
@@ -2048,7 +2084,7 @@ RtlAppendUnicodeToString(IN OUT PUNICODE_STRING Destination,
  *  dest is never '\0' terminated.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlAppendAsciizToString(
    IN OUT   PSTRING  Destination,
    IN PCSZ  Source)
@@ -2075,7 +2111,7 @@ RtlAppendAsciizToString(
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlUpperString(PSTRING DestinationString,
                PSTRING SourceString)
 {
@@ -2102,12 +2138,14 @@ RtlUpperString(PSTRING DestinationString,
  *  See RtlpDuplicateUnicodeString
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlDuplicateUnicodeString(
    IN ULONG Flags,
    IN PCUNICODE_STRING SourceString,
    OUT PUNICODE_STRING DestinationString)
 {
+   PAGED_CODE_RTL();
+
    if (SourceString == NULL || DestinationString == NULL)
       return STATUS_INVALID_PARAMETER;
 
@@ -2145,7 +2183,7 @@ RtlDuplicateUnicodeString(
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlValidateUnicodeString(IN ULONG Flags,
                          IN PUNICODE_STRING UnicodeString)
 {