fixed RtlCompareUnicodeString and RtlCompareString (said stuff were equal when it...
[reactos.git] / reactos / lib / rtl / unicode.c
index 8d99529..b4f10d5 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 #define __NTDRIVER__
-#include "rtl.h"
+#include <rtl.h>
 
 #define NDEBUG
 #include <debug.h>
 
 /* GLOBALS *******************************************************************/
 
-#define TAG_USTR  TAG('U', 'S', 'T', 'R')
-#define TAG_ASTR  TAG('A', 'S', 'T', 'R')
-#define TAG_OSTR  TAG('O', 'S', 'T', 'R')
-
-
 extern BOOLEAN NlsMbCodePageTag;
 extern BOOLEAN NlsMbOemCodePageTag;
 
@@ -223,7 +218,7 @@ RtlCharToInteger(
        } else {
            digit = -1;
        } /* if */
-       if (digit < 0 || digit >= base) {
+       if (digit < 0 || digit >= (int)base) {
            *value = bMinus ? -RunningTotal : RunningTotal;
            return STATUS_SUCCESS;
        } /* if */
@@ -242,47 +237,28 @@ RtlCharToInteger(
 LONG
 STDCALL
 RtlCompareString(
-   IN PSTRING String1,
-   IN PSTRING String2,
+   IN PSTRING s1,
+   IN PSTRING s2,
    IN BOOLEAN CaseInsensitive)
 {
-   ULONG len1, len2;
-   PCHAR s1, s2;
-   CHAR  c1, c2;
+   unsigned int len;
+   LONG ret = 0;
+   LPCSTR p1, p2;
 
-   if (String1 && String2)
-   {
-      len1 = String1->Length;
-      len2 = String2->Length;
-      s1 = String1->Buffer;
-      s2 = String2->Buffer;
+   len = min(s1->Length, s2->Length);
+   p1 = s1->Buffer;
+   p2 = s2->Buffer;
 
-      if (s1 && s2)
-      {
-         if (CaseInsensitive)
-         {
-            for(;;)
-            {
-               c1 = len1-- ? RtlUpperChar (*s1++) : 0;
-               c2 = len2-- ? RtlUpperChar (*s2++) : 0;
-               if (!c1 || !c2 || c1 != c2)
-                  return c1 - c2;
-            }
-         }
-         else
-         {
-            for(;;)
-            {
-               c1 = len1-- ? *s1++ : 0;
-               c2 = len2-- ? *s2++ : 0;
-               if (!c1 || !c2 || c1 != c2)
-                  return c1 - c2;
-            }
-         }
-      }
+   if (CaseInsensitive)
+   {
+     while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
    }
-
-   return 0;
+   else
+   {
+     while (!ret && len--) ret = *p1++ - *p2++;
+   }
+   if (!ret) ret = s1->Length - s2->Length;
+   return ret;
 }
 
 
@@ -295,40 +271,12 @@ RtlCompareString(
 BOOLEAN
 STDCALL
 RtlEqualString(
-   IN PSTRING String1,
-   IN PSTRING String2,
+   IN PSTRING s1,
+   IN PSTRING s2,
    IN BOOLEAN CaseInsensitive)
 {
-   ULONG i;
-   CHAR c1, c2;
-   PCHAR p1, p2;
-
-   if (String1->Length != String2->Length)
-      return FALSE;
-
-   p1 = String1->Buffer;
-   p2 = String2->Buffer;
-   for (i = 0; i < String1->Length; i++)
-   {
-      if (CaseInsensitive == TRUE)
-      {
-         c1 = RtlUpperChar (*p1);
-         c2 = RtlUpperChar (*p2);
-      }
-      else
-      {
-         c1 = *p1;
-         c2 = *p2;
-      }
-
-      if (c1 != c2)
-         return FALSE;
-
-      p1++;
-      p2++;
-   }
-
-   return TRUE;
+   if (s1->Length != s2->Length) return FALSE;
+   return !RtlCompareString(s1, s2, CaseInsensitive );
 }
 
 
@@ -341,41 +289,12 @@ RtlEqualString(
 BOOLEAN
 STDCALL
 RtlEqualUnicodeString(
-   IN CONST UNICODE_STRING *String1,
-   IN CONST UNICODE_STRING *String2,
+   IN CONST UNICODE_STRING *s1,
+   IN CONST UNICODE_STRING *s2,
    IN BOOLEAN  CaseInsensitive)
 {
-   ULONG i;
-   WCHAR wc1, wc2;
-   PWCHAR pw1, pw2;
-
-   if (String1->Length != String2->Length)
-      return FALSE;
-
-   pw1 = String1->Buffer;
-   pw2 = String2->Buffer;
-
-   for (i = 0; i < String1->Length / sizeof(WCHAR); i++)
-   {
-      if (CaseInsensitive == TRUE)
-      {
-         wc1 = RtlUpcaseUnicodeChar (*pw1);
-         wc2 = RtlUpcaseUnicodeChar (*pw2);
-      }
-      else
-      {
-         wc1 = *pw1;
-         wc2 = *pw2;
-      }
-
-      if (wc1 != wc2)
-         return FALSE;
-
-      pw1++;
-      pw2++;
-   }
-
-   return TRUE;
+   if (s1->Length != s2->Length) return FALSE;
+   return !RtlCompareUnicodeString((PUNICODE_STRING)s1, (PUNICODE_STRING)s2, CaseInsensitive );
 }
 
 
@@ -386,14 +305,14 @@ VOID
 STDCALL
 RtlFreeAnsiString(IN PANSI_STRING AnsiString)
 {
-   if (AnsiString->Buffer == NULL)
-      return;
-
-   ExFreePoolWithTag(AnsiString->Buffer, TAG_ASTR);
+   if (AnsiString->Buffer != NULL)
+   {
+      RtlpFreeStringMemory(AnsiString->Buffer, TAG_ASTR);
 
-   AnsiString->Buffer = NULL;
-   AnsiString->Length = 0;
-   AnsiString->MaximumLength = 0;
+      AnsiString->Buffer = NULL;
+      AnsiString->Length = 0;
+      AnsiString->MaximumLength = 0;
+   }
 }
 
 
@@ -404,14 +323,14 @@ VOID
 STDCALL
 RtlFreeOemString(IN POEM_STRING OemString)
 {
-   if (OemString->Buffer == NULL)
-      return;
-
-   ExFreePoolWithTag(OemString->Buffer, TAG_OSTR);
+   if (OemString->Buffer != NULL)
+   {
+      RtlpFreeStringMemory(OemString->Buffer, TAG_OSTR);
 
-   OemString->Buffer = NULL;
-   OemString->Length = 0;
-   OemString->MaximumLength = 0;
+      OemString->Buffer = NULL;
+      OemString->Length = 0;
+      OemString->MaximumLength = 0;
+   }
 }
 
 
@@ -422,14 +341,14 @@ VOID
 STDCALL
 RtlFreeUnicodeString(IN PUNICODE_STRING UnicodeString)
 {
-   if (UnicodeString->Buffer == NULL)
-      return;
-
-   ExFreePoolWithTag(UnicodeString->Buffer, TAG_USTR);
+   if (UnicodeString->Buffer != NULL)
+   {
+      RtlpFreeStringMemory(UnicodeString->Buffer, TAG_USTR);
 
-   UnicodeString->Buffer = NULL;
-   UnicodeString->Length = 0;
-   UnicodeString->MaximumLength = 0;
+      UnicodeString->Buffer = NULL;
+      UnicodeString->Length = 0;
+      UnicodeString->MaximumLength = 0;
+   }
 }
 
 /*
@@ -608,7 +527,7 @@ RtlIntegerToChar(
       tp++;
    }
 
-   if (tp - temp >= Length)
+   if ((ULONG)((ULONG_PTR)tp - (ULONG_PTR)temp) >= Length)
    {
       return STATUS_BUFFER_TOO_SMALL;
    }
@@ -663,7 +582,7 @@ RtlIntegerToUnicode(
       tp++;
    }
 
-   if (tp - temp >= Length)
+   if ((ULONG)((ULONG_PTR)tp - (ULONG_PTR)temp) >= Length)
    {
       return STATUS_BUFFER_TOO_SMALL;
    }
@@ -696,16 +615,16 @@ RtlIntegerToUnicodeString(
                               Base,
                               sizeof(Buffer),
                               Buffer);
-   if (!NT_SUCCESS(Status))
-      return Status;
-
-   AnsiString.Buffer = Buffer;
-   AnsiString.Length = strlen (Buffer);
-   AnsiString.MaximumLength = sizeof(Buffer);
+   if (NT_SUCCESS(Status))
+   {
+      AnsiString.Buffer = Buffer;
+      AnsiString.Length = strlen (Buffer);
+      AnsiString.MaximumLength = sizeof(Buffer);
 
-   Status = RtlAnsiStringToUnicodeString (String,
-                                          &AnsiString,
-                                          FALSE);
+      Status = RtlAnsiStringToUnicodeString (String,
+                                             &AnsiString,
+                                             FALSE);
+   }
 
    return Status;
 }
@@ -733,16 +652,16 @@ RtlInt64ToUnicodeString (
                                    Base,
                                    sizeof(Buffer),
                                    Buffer);
-   if (!NT_SUCCESS(Status))
-      return Status;
-
-   AnsiString.Buffer = Buffer;
-   AnsiString.Length = strlen (Buffer);
-   AnsiString.MaximumLength = sizeof(Buffer);
+   if (NT_SUCCESS(Status))
+   {
+      AnsiString.Buffer = Buffer;
+      AnsiString.Length = strlen (Buffer);
+      AnsiString.MaximumLength = sizeof(Buffer);
 
-   Status = RtlAnsiStringToUnicodeString (String,
-                                          &AnsiString,
-                                          FALSE);
+      Status = RtlAnsiStringToUnicodeString (String,
+                                             &AnsiString,
+                                             FALSE);
+   }
 
    return Status;
 }
@@ -881,44 +800,49 @@ RtlUnicodeStringToInteger(
     USHORT CharsRemaining = str->Length / sizeof(WCHAR);
     WCHAR wchCurrent;
     int digit;
+    ULONG newbase = 0;
     ULONG RunningTotal = 0;
     char bMinus = 0;
 
-    while (CharsRemaining >= 1 && *lpwstr <= ' ') {
+    while (CharsRemaining >= 1 && *lpwstr <= L' ') {
        lpwstr++;
        CharsRemaining--;
     } /* while */
 
     if (CharsRemaining >= 1) {
-       if (*lpwstr == '+') {
+       if (*lpwstr == L'+') {
            lpwstr++;
            CharsRemaining--;
-       } else if (*lpwstr == '-') {
+       } else if (*lpwstr == L'-') {
            bMinus = 1;
            lpwstr++;
            CharsRemaining--;
        } /* if */
     } /* if */
 
-    if (base == 0) {
-       base = 10;
-       if (CharsRemaining >= 2 && lpwstr[0] == '0') {
-           if (lpwstr[1] == 'b') {
-               lpwstr += 2;
-               CharsRemaining -= 2;
-               base = 2;
-           } else if (lpwstr[1] == 'o') {
-               lpwstr += 2;
-               CharsRemaining -= 2;
-               base = 8;
-           } else if (lpwstr[1] == 'x') {
-               lpwstr += 2;
-               CharsRemaining -= 2;
-               base = 16;
-           } /* if */
+    if (CharsRemaining >= 2 && lpwstr[0] == L'0') {
+        if (lpwstr[1] == L'b' || lpwstr[1] == L'B') {
+           lpwstr += 2;
+           CharsRemaining -= 2;
+           newbase = 2;
+       } else if (lpwstr[1] == L'o' || lpwstr[1] == L'O') {
+           lpwstr += 2;
+           CharsRemaining -= 2;
+           newbase = 8;
+        } else if (lpwstr[1] == L'x' || lpwstr[1] == L'X') {
+           lpwstr += 2;
+           CharsRemaining -= 2;
+           newbase = 16;
        } /* if */
-    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
+    }
+    if (base == 0 && newbase == 0) {
+        base = 10;
+    } else if (base == 0 && newbase != 0) {
+        base = newbase;
+    } else if ((newbase != 0 && base != newbase) ||
+               (base != 2 && base != 8 && base != 10 && base != 16)) {
        return STATUS_INVALID_PARAMETER;
+
     } /* if */
 
     if (value == NULL) {
@@ -927,16 +851,16 @@ RtlUnicodeStringToInteger(
 
     while (CharsRemaining >= 1) {
        wchCurrent = *lpwstr;
-       if (wchCurrent >= '0' && wchCurrent <= '9') {
-           digit = wchCurrent - '0';
-       } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
-           digit = wchCurrent - 'A' + 10;
-       } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
-           digit = wchCurrent - 'a' + 10;
+       if (wchCurrent >= L'0' && wchCurrent <= L'9') {
+           digit = wchCurrent - L'0';
+       } else if (wchCurrent >= L'A' && wchCurrent <= L'Z') {
+           digit = wchCurrent - L'A' + 10;
+       } else if (wchCurrent >= L'a' && wchCurrent <= L'z') {
+           digit = wchCurrent - L'a' + 10;
        } else {
            digit = -1;
        } /* if */
-       if (digit < 0 || digit >= base) {
+       if (digit < 0 || digit >= (int)base) {
            *value = bMinus ? -RunningTotal : RunningTotal;
            return STATUS_SUCCESS;
        } /* if */
@@ -972,22 +896,22 @@ RtlUnicodeStringToOemSize(
 }
 
 /*
- * private
+ * @implemented
  *
+
  * NOTES
  *  This function always writes a terminating '\0'.
  *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
-FASTCALL
-RtlpUnicodeStringToAnsiString(
+STDCALL
+RtlUnicodeStringToAnsiString(
    IN OUT PANSI_STRING AnsiDest,
    IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN AllocateDestinationString)
 {
    NTSTATUS Status = STATUS_SUCCESS;
-   ULONG Length; //including nullterm
+   ULONG Length; /* including nullterm */
 
    if (NlsMbCodePageTag == TRUE)
    {
@@ -1000,7 +924,7 @@ RtlpUnicodeStringToAnsiString(
 
    if (AllocateDestinationString)
    {
-      AnsiDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_ASTR);
+      AnsiDest->Buffer = RtlpAllocateStringMemory(Length, TAG_ASTR);
       if (AnsiDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -1012,7 +936,7 @@ RtlpUnicodeStringToAnsiString(
    }
    else if (Length > AnsiDest->MaximumLength)
    {
-      //make room for nullterm
+      /* make room for nullterm */
       AnsiDest->Length = AnsiDest->MaximumLength - sizeof(CHAR);
    }
 
@@ -1024,7 +948,7 @@ RtlpUnicodeStringToAnsiString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(AnsiDest->Buffer, TAG_ASTR);
+      RtlpFreeStringMemory(AnsiDest->Buffer, TAG_ASTR);
       return Status;
    }
 
@@ -1032,44 +956,23 @@ RtlpUnicodeStringToAnsiString(
    return Status;
 }
 
-/*
- * @implemented
- *
-
- * NOTES
- *  See RtlpUnicodeStringToAnsiString
- */
-NTSTATUS
-STDCALL
-RtlUnicodeStringToAnsiString(
-   IN OUT PANSI_STRING AnsiDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpUnicodeStringToAnsiString(
-             AnsiDest,
-             UniSource,
-             AllocateDestinationString,
-             PagedPool);
-}
 
 /*
- * private
+ * @implemented
  *
  * NOTES
  *  This function always writes a terminating '\0'.
  *  Does NOT perform a partial copy if unicode is too small!
  */
 NTSTATUS
-FASTCALL
-RtlpOemStringToUnicodeString(
+STDCALL
+RtlOemStringToUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN POEM_STRING OemSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN AllocateDestinationString)
 {
    NTSTATUS Status;
-   ULONG Length; //including nullterm
+   ULONG Length; /* including nullterm */
 
    if (NlsMbOemCodePageTag == TRUE)
       Length = RtlOemStringToUnicodeSize(OemSource);
@@ -1083,7 +986,7 @@ RtlpOemStringToUnicodeString(
 
    if (AllocateDestinationString)
    {
-      UniDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_USTR);
+      UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
       if (UniDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -1095,7 +998,7 @@ RtlpOemStringToUnicodeString(
       return STATUS_BUFFER_TOO_SMALL;
    }
 
-   //FIXME: Do we need this????? -Gunnar
+   /* FIXME: Do we need this????? -Gunnar */
    RtlZeroMemory (UniDest->Buffer,
                   UniDest->Length);
 
@@ -1107,7 +1010,7 @@ RtlpOemStringToUnicodeString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(UniDest->Buffer, TAG_USTR);
+      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
       return Status;
    }
 
@@ -1120,35 +1023,14 @@ RtlpOemStringToUnicodeString(
  * @implemented
  *
  * NOTES
- *  See RtlpOemStringToUnicodeString
- */
-NTSTATUS
-STDCALL
-RtlOemStringToUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN POEM_STRING OemSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpOemStringToUnicodeString(
-             UniDest,
-             OemSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
-/*
- * private
- *
- * NOTES
  *   This function always '\0' terminates the string returned.
  */
 NTSTATUS
-FASTCALL
-RtlpUnicodeStringToOemString(
+STDCALL
+RtlUnicodeStringToOemString(
    IN OUT POEM_STRING OemDest,
    IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN  AllocateDestinationString)
 {
    NTSTATUS Status = STATUS_SUCCESS;
    ULONG Length; //including nullterm
@@ -1165,7 +1047,7 @@ RtlpUnicodeStringToOemString(
 
    if (AllocateDestinationString)
    {
-      OemDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_OSTR);
+      OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
       if (OemDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -1189,7 +1071,7 @@ RtlpUnicodeStringToOemString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(OemDest->Buffer, TAG_OSTR);
+      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
       return Status;
    }
 
@@ -1197,26 +1079,6 @@ RtlpUnicodeStringToOemString(
    return Status;
 }
 
-/*
- * @implemented
- *
- * NOTES
- *  See RtlpUnicodeStringToOemString.
- */
-NTSTATUS
-STDCALL
-RtlUnicodeStringToOemString(
-   IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString)
-{
-   return RtlpUnicodeStringToOemString(
-             OemDest,
-             UniSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
 #define ITU_IMPLEMENTED_TESTS (IS_TEXT_UNICODE_ODD_LENGTH|IS_TEXT_UNICODE_SIGNATURE)
 
 
@@ -1278,23 +1140,23 @@ done:
    return Length;
 }
 
+
 /*
- * private
+ * @implemented
  *
  * NOTES
  *  Same as RtlOemStringToUnicodeString but doesn't write terminating null
  *  A partial copy is NOT performed if the dest buffer is too small!
  */
 NTSTATUS
-FASTCALL
-RtlpOemStringToCountedUnicodeString(
+STDCALL
+RtlOemStringToCountedUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN POEM_STRING OemSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN AllocateDestinationString)
 {
    NTSTATUS Status;
-   ULONG Length; //excluding nullterm
+   ULONG Length; /* excluding nullterm */
 
    if (NlsMbCodePageTag == TRUE)
       Length = RtlOemStringToUnicodeSize(OemSource) - sizeof(WCHAR);
@@ -1306,7 +1168,7 @@ RtlpOemStringToCountedUnicodeString(
 
    if (AllocateDestinationString == TRUE)
    {
-      UniDest->Buffer = ExAllocatePoolWithTag (PoolType, Length, TAG_USTR);
+      UniDest->Buffer = RtlpAllocateStringMemory (Length, TAG_USTR);
       if (UniDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -1327,33 +1189,13 @@ RtlpOemStringToCountedUnicodeString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(UniDest->Buffer, TAG_USTR);
+      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
       return Status;
    }
 
    return Status;
 }
 
-/*
- * @implemented
- *
- * NOTES
- *  See RtlpOemStringToCountedUnicodeString
- */
-NTSTATUS
-STDCALL
-RtlOemStringToCountedUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN POEM_STRING OemSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpOemStringToCountedUnicodeString(
-             UniDest,
-             OemSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
 /*
  * @implemented
  *
@@ -1577,17 +1419,14 @@ STDCALL
 RtlEraseUnicodeString(
    IN PUNICODE_STRING String)
 {
-   if (String->Buffer == NULL)
-      return;
-
-   if (String->MaximumLength == 0)
-      return;
-
-   memset (String->Buffer,
-           0,
-           String->MaximumLength);
+   if (String->Buffer != NULL &&
+       String->MaximumLength != 0)
+   {
+      RtlZeroMemory (String->Buffer,
+                     String->MaximumLength);
 
-   String->Length = 0;
+      String->Length = 0;
+   }
 }
 
 /*
@@ -1643,19 +1482,18 @@ RtlHashUnicodeString(
 }
 
 /*
- * private
+ * @implemented
  *
  * NOTES
  *  Same as RtlUnicodeStringToOemString but doesn't write terminating null
  *  Does a partial copy if the dest buffer is too small
  */
 NTSTATUS
-FASTCALL
-RtlpUnicodeStringToCountedOemString(
+STDCALL
+RtlUnicodeStringToCountedOemString(
    IN OUT POEM_STRING OemDest,
    IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN AllocateDestinationString)
 {
    NTSTATUS Status;
    ULONG Length; //excluding nullterm
@@ -1672,7 +1510,7 @@ RtlpUnicodeStringToCountedOemString(
 
    if (AllocateDestinationString)
    {
-      OemDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_OSTR);
+      OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
       if (OemDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -1695,32 +1533,12 @@ RtlpUnicodeStringToCountedOemString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(OemDest->Buffer, TAG_OSTR);
+      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
    }
 
    return Status;
 }
 
-/*
- * @implemented
- *
- * NOTES
- *  See RtlpUnicodeStringToCountedOemString.
- */
-NTSTATUS
-STDCALL
-RtlUnicodeStringToCountedOemString(
-   IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpUnicodeStringToCountedOemString(
-             OemDest,
-             UniSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
 /*
  * @implemented
  */
@@ -1759,7 +1577,7 @@ RtlLargeIntegerToChar(
       tp++;
    }
 
-   if (tp - temp >= Length)
+   if ((ULONG)((ULONG_PTR)tp - (ULONG_PTR)temp) >= Length)
       return STATUS_BUFFER_TOO_SMALL;
 
    sp = String;
@@ -1771,19 +1589,18 @@ RtlLargeIntegerToChar(
 }
 
 /*
- * private
+ * @implemented
  *
  * NOTES
  *  dest is never '\0' terminated because it may be equal to src, and src
  *  might not be '\0' terminated. dest->Length is only set upon success.
  */
 NTSTATUS
-FASTCALL
-RtlpUpcaseUnicodeString(
+STDCALL
+RtlUpcaseUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN  AllocateDestinationString)
 {
    ULONG i;
    PWCHAR Src, Dest;
@@ -1791,7 +1608,7 @@ RtlpUpcaseUnicodeString(
    if (AllocateDestinationString == TRUE)
    {
       UniDest->MaximumLength = UniSource->Length;
-      UniDest->Buffer = ExAllocatePoolWithTag(PoolType, UniDest->MaximumLength, TAG_USTR);
+      UniDest->Buffer = RtlpAllocateStringMemory(UniDest->MaximumLength, TAG_USTR);
       if (UniDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
    }
@@ -1818,40 +1635,18 @@ RtlpUpcaseUnicodeString(
  * @implemented
  *
  * NOTES
- *  See RtlpUpcaseUnicodeString
- */
-NTSTATUS
-STDCALL
-RtlUpcaseUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PCUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString)
-{
-
-   return RtlpUpcaseUnicodeString(
-             UniDest,
-             UniSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
-/*
- * private
- *
- * NOTES
  *  This function always writes a terminating '\0'.
  *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
-FASTCALL
-RtlpUpcaseUnicodeStringToAnsiString(
+STDCALL
+RtlUpcaseUnicodeStringToAnsiString(
    IN OUT PANSI_STRING AnsiDest,
    IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN  AllocateDestinationString)
 {
    NTSTATUS Status;
-   ULONG Length; //including nullterm
+   ULONG Length; /* including nullterm */
 
    if (NlsMbCodePageTag == TRUE)
       Length = RtlUnicodeStringToAnsiSize(UniSource);
@@ -1865,7 +1660,7 @@ RtlpUpcaseUnicodeStringToAnsiString(
 
    if (AllocateDestinationString)
    {
-      AnsiDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_ASTR);
+      AnsiDest->Buffer = RtlpAllocateStringMemory(Length, TAG_ASTR);
       if (AnsiDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -1877,11 +1672,11 @@ RtlpUpcaseUnicodeStringToAnsiString(
    }
    else if (Length > AnsiDest->MaximumLength)
    {
-      //make room for nullterm
+      /* make room for nullterm */
       AnsiDest->Length = AnsiDest->MaximumLength - sizeof(CHAR);
    }
 
-   //FIXME: do we need this??????? -Gunnar
+   /* FIXME: do we need this??????? -Gunnar */
    RtlZeroMemory (AnsiDest->Buffer,
                   AnsiDest->Length);
 
@@ -1893,7 +1688,7 @@ RtlpUpcaseUnicodeStringToAnsiString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(AnsiDest->Buffer, TAG_ASTR);
+      RtlpFreeStringMemory(AnsiDest->Buffer, TAG_ASTR);
       return Status;
    }
 
@@ -1905,39 +1700,18 @@ RtlpUpcaseUnicodeStringToAnsiString(
  * @implemented
  *
  * NOTES
- *  See RtlpUpcaseUnicodeStringToAnsiString
+ *  This function always writes a terminating '\0'.
+ *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
 STDCALL
-RtlUpcaseUnicodeStringToAnsiString(
-   IN OUT PANSI_STRING AnsiDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString)
-{
-   return RtlpUpcaseUnicodeStringToAnsiString(
-             AnsiDest,
-             UniSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
-/*
- * private
- *
- * NOTES
- *  Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
- *  It performs a partial copy if oem is too small.
- */
-NTSTATUS
-FASTCALL
-RtlpUpcaseUnicodeStringToCountedOemString(
+RtlUpcaseUnicodeStringToCountedOemString(
    IN OUT POEM_STRING OemDest,
    IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN AllocateDestinationString)
 {
    NTSTATUS Status;
-   ULONG Length; //excluding nullterm
+   ULONG Length; /* excluding nullterm */
 
    if (NlsMbCodePageTag == TRUE)
       Length = RtlUnicodeStringToAnsiSize(UniSource) - sizeof(CHAR);
@@ -1951,11 +1725,11 @@ RtlpUpcaseUnicodeStringToCountedOemString(
 
    if (AllocateDestinationString)
    {
-      OemDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_OSTR);
+      OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
       if (OemDest->Buffer == NULL)
          return(STATUS_NO_MEMORY);
 
-      //FIXME: Do we need this?????
+      /* FIXME: Do we need this????? */
       RtlZeroMemory (OemDest->Buffer, Length);
 
       OemDest->MaximumLength = (WORD)Length;
@@ -1977,7 +1751,7 @@ RtlpUpcaseUnicodeStringToCountedOemString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(OemDest->Buffer, TAG_OSTR);
+      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
       return Status;
    }
 
@@ -1986,42 +1760,20 @@ RtlpUpcaseUnicodeStringToCountedOemString(
 
 /*
  * @implemented
- *
- * NOTES
- *  See RtlpUpcaseUnicodeStringToCountedOemString
- */
-NTSTATUS
-STDCALL
-RtlUpcaseUnicodeStringToCountedOemString(
-   IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpUpcaseUnicodeStringToCountedOemString(
-             OemDest,
-             UniSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
-/*
- * private
- *
  * NOTES
  *  Oem string is allways nullterminated
  *  It performs a partial copy if oem is too small.
  */
 NTSTATUS
-FASTCALL
-RtlpUpcaseUnicodeStringToOemString (
+STDCALL
+RtlUpcaseUnicodeStringToOemString (
    IN OUT POEM_STRING OemDest,
    IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString,
-   IN POOL_TYPE PoolType
+   IN BOOLEAN  AllocateDestinationString
 )
 {
    NTSTATUS Status;
-   ULONG Length; //including nullterm
+   ULONG Length; /* including nullterm */
 
    if (NlsMbOemCodePageTag == TRUE)
       Length = RtlUnicodeStringToAnsiSize(UniSource);
@@ -2035,11 +1787,11 @@ RtlpUpcaseUnicodeStringToOemString (
 
    if (AllocateDestinationString)
    {
-      OemDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_OSTR);
+      OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
       if (OemDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
-      //FIXME: Do we need this????
+      /* FIXME: Do we need this???? */
       RtlZeroMemory (OemDest->Buffer, Length);
 
       OemDest->MaximumLength = (WORD)Length;
@@ -2050,7 +1802,7 @@ RtlpUpcaseUnicodeStringToOemString (
    }
    else if (Length > OemDest->MaximumLength)
    {
-      //make room for nullterm
+      /* make room for nullterm */
       OemDest->Length = OemDest->MaximumLength - sizeof(CHAR);
    }
 
@@ -2062,7 +1814,7 @@ RtlpUpcaseUnicodeStringToOemString (
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(OemDest->Buffer, TAG_OSTR);
+      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
       return Status;
    }
 
@@ -2070,26 +1822,6 @@ RtlpUpcaseUnicodeStringToOemString (
    return Status;
 }
 
-/*
- * @implemented
- * NOTES
- *  See RtlpUpcaseUnicodeStringToOemString
- */
-NTSTATUS
-STDCALL
-RtlUpcaseUnicodeStringToOemString (
-   IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString
-)
-{
-   return RtlpUpcaseUnicodeStringToOemString(
-             OemDest,
-             UniSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
 /*
  * @implemented
  *
@@ -2175,58 +1907,38 @@ RtlUnicodeStringToAnsiSize(
 }
 
 
-
-
 /*
  * @implemented
  */
 LONG
 STDCALL
 RtlCompareUnicodeString(
-   IN PUNICODE_STRING String1,
-   IN PUNICODE_STRING String2,
+   IN PUNICODE_STRING s1,
+   IN PUNICODE_STRING s2,
    IN BOOLEAN  CaseInsensitive)
 {
-   ULONG len1, len2;
-   PWCHAR s1, s2;
-   WCHAR  c1, c2;
+   unsigned int len;
+   LONG ret = 0;
+   LPCWSTR p1, p2;
 
-   if (String1 && String2)
-   {
-      len1 = String1->Length / sizeof(WCHAR);
-      len2 = String2->Length / sizeof(WCHAR);
-      s1 = String1->Buffer;
-      s2 = String2->Buffer;
+   len = min(s1->Length, s2->Length) / sizeof(WCHAR);
+   p1 = s1->Buffer;
+   p2 = s2->Buffer;
 
-      if (s1 && s2)
-      {
-         if (CaseInsensitive)
-         {
-            while (1)
-            {
-               c1 = len1-- ? RtlUpcaseUnicodeChar (*s1++) : 0;
-               c2 = len2-- ? RtlUpcaseUnicodeChar (*s2++) : 0;
-               if (!c1 || !c2 || c1 != c2)
-                  return c1 - c2;
-            }
-         }
-         else
-         {
-            while (1)
-            {
-               c1 = len1-- ? *s1++ : 0;
-               c2 = len2-- ? *s2++ : 0;
-               if (!c1 || !c2 || c1 != c2)
-                  return c1 - c2;
-            }
-         }
-      }
+   if (CaseInsensitive)
+   {
+     while (!ret && len--) ret = RtlUpcaseUnicodeChar(*p1++) - RtlUpcaseUnicodeChar(*p2++);
    }
-
-   return 0;
+   else
+   {
+     while (!ret && len--) ret = *p1++ - *p2++;
+   }
+   if (!ret) ret = s1->Length - s2->Length;
+   return ret;
 }
 
 
+
 /*
  * @implemented
  */
@@ -2285,28 +1997,27 @@ RtlCopyUnicodeString(
 }
 
 /*
- * private
+ * @implemented
  *
+ * NOTES
  * Creates a nullterminated UNICODE_STRING
  */
 BOOLEAN
-FASTCALL
-RtlpCreateUnicodeString(
+STDCALL
+RtlCreateUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
-   IN PCWSTR  Source,
-   IN POOL_TYPE PoolType)
+   IN PCWSTR  Source)
 {
    ULONG Length;
 
    Length = (wcslen (Source) + 1) * sizeof(WCHAR);
-   PoolType = PagedPool;
-   UniDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_USTR);
+   UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
    if (UniDest->Buffer == NULL)
       return FALSE;
 
-   memmove (UniDest->Buffer,
-            Source,
-            Length);
+   RtlCopyMemory (UniDest->Buffer,
+                  Source,
+                  Length);
 
    UniDest->MaximumLength = Length;
    UniDest->Length = Length - sizeof (WCHAR);
@@ -2314,23 +2025,6 @@ RtlpCreateUnicodeString(
    return TRUE;
 }
 
-/*
- * @implemented
- *
- * NOTES
- *  See RtlpCreateUnicodeString
- */
-BOOLEAN
-STDCALL
-RtlCreateUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PCWSTR  Source)
-{
-
-   DPRINT("RtlCreateUnicodeString\n");
-   return RtlpCreateUnicodeString(UniDest, Source, PagedPool);
-}
-
 /*
  * @implemented
  */
@@ -2354,27 +2048,25 @@ RtlCreateUnicodeStringFromAsciiz(
 }
 
 /*
- * private
+ * @implemented
  *
  * NOTES
  *  Dest is never '\0' terminated because it may be equal to src, and src
  *  might not be '\0' terminated.
  *  Dest->Length is only set upon success.
  */
-NTSTATUS
-FASTCALL
-RtlpDowncaseUnicodeString(
+NTSTATUS STDCALL
+RtlDowncaseUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN AllocateDestinationString)
 {
    ULONG i;
    PWCHAR Src, Dest;
 
    if (AllocateDestinationString)
    {
-      UniDest->Buffer = ExAllocatePoolWithTag(PoolType, UniSource->Length, TAG_USTR);
+      UniDest->Buffer = RtlpAllocateStringMemory(UniSource->Length, TAG_USTR);
       if (UniDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -2411,25 +2103,6 @@ RtlpDowncaseUnicodeString(
    return STATUS_SUCCESS;
 }
 
-/*
- * @implemented
- *
- * NOTES
- *  See RtlpDowncaseUnicodeString
- */
-NTSTATUS STDCALL
-RtlDowncaseUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpDowncaseUnicodeString(
-             UniDest,
-             UniSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
 /*
  * @implemented
  *
@@ -2459,19 +2132,18 @@ RtlAppendUnicodeToString(IN OUT PUNICODE_STRING Destination,
 }
 
 /*
- * private
+ * @implemented
  *
  * NOTES
  *  This function always writes a terminating '\0'.
  *  If the dest buffer is too small a partial copy is NOT performed!
  */
 NTSTATUS
-FASTCALL
-RtlpAnsiStringToUnicodeString(
+STDCALL
+RtlAnsiStringToUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PANSI_STRING AnsiSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
+   IN BOOLEAN AllocateDestinationString)
 {
    NTSTATUS Status;
    ULONG Length; //including nullterm
@@ -2486,7 +2158,7 @@ RtlpAnsiStringToUnicodeString(
 
    if (AllocateDestinationString == TRUE)
    {
-      UniDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_USTR);
+      UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
       if (UniDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -2512,7 +2184,7 @@ RtlpAnsiStringToUnicodeString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePoolWithTag(UniDest->Buffer, TAG_USTR);
+      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
       return Status;
    }
 
@@ -2520,26 +2192,6 @@ RtlpAnsiStringToUnicodeString(
    return Status;
 }
 
-/*
- * @implemented
- *
- * NOTES
- *  See RtlpAnsiStringToUnicodeString
- */
-NTSTATUS
-STDCALL
-RtlAnsiStringToUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PANSI_STRING AnsiSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpAnsiStringToUnicodeString(
-             UniDest,
-             AnsiSource,
-             AllocateDestinationString,
-             PagedPool);
-}
-
 /*
  * @implemented
  *
@@ -2621,7 +2273,7 @@ RtlxAnsiStringToUnicodeSize(IN PANSI_STRING AnsiString)
 ULONG STDCALL
 RtlxOemStringToUnicodeSize(IN POEM_STRING OemString)
 {
-   return RtlOemStringToUnicodeSize((PANSI_STRING)OemString);
+   return RtlOemStringToUnicodeSize(OemString);
 }
 
 
@@ -2647,13 +2299,15 @@ RtlxUnicodeStringToOemSize(IN PUNICODE_STRING UnicodeString)
 
 /*
  * @implemented
+ *
+ * NOTES
+ *  See RtlpDuplicateUnicodeString
  */
 NTSTATUS STDCALL
-RtlpDuplicateUnicodeString(
+RtlDuplicateUnicodeString(
    INT AddNull,
    IN PUNICODE_STRING SourceString,
-   PUNICODE_STRING DestinationString,
-   POOL_TYPE PoolType)
+   PUNICODE_STRING DestinationString)
 {
    if (SourceString == NULL || DestinationString == NULL)
       return STATUS_INVALID_PARAMETER;
@@ -2667,12 +2321,12 @@ RtlpDuplicateUnicodeString(
    }
    else
    {
-      unsigned int DestMaxLength = SourceString->Length;
+      UINT DestMaxLength = SourceString->Length;
 
       if (AddNull)
          DestMaxLength += sizeof(UNICODE_NULL);
 
-      DestinationString->Buffer = ExAllocatePool(PoolType, DestMaxLength);
+      DestinationString->Buffer = RtlpAllocateStringMemory(DestMaxLength, TAG_USTR);
       if (DestinationString->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -2687,25 +2341,6 @@ RtlpDuplicateUnicodeString(
    return STATUS_SUCCESS;
 }
 
-/*
- * @implemented
- *
- * NOTES
- *  See RtlpDuplicateUnicodeString
- */
-NTSTATUS STDCALL
-RtlDuplicateUnicodeString(
-   INT AddNull,
-   IN PUNICODE_STRING SourceString,
-   PUNICODE_STRING DestinationString)
-{
-   return RtlpDuplicateUnicodeString(
-            AddNull,
-            SourceString,
-            DestinationString,
-            PagedPool);
-}
-
 /*
  * @implemented
  */