- Use Rtl macros instead of manually handling the cases.
[reactos.git] / reactos / lib / rtl / unicode.c
index 00158fb..7f30600 100644 (file)
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-
-
 #define __NTDRIVER__
-#include <ddk/ntddk.h>
-
-#include <ntdll/rtl.h>
-
-#include <ntos/minmax.h>
-#define __NO_CTYPE_INLINES
-#include <ctype.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;
 
@@ -79,7 +66,7 @@ RtlAnsiCharToUnicodeChar (IN CHAR AnsiChar)
  */
 ULONG
 STDCALL
-RtlAnsiStringToUnicodeSize(IN PANSI_STRING AnsiString)
+RtlxAnsiStringToUnicodeSize(IN PCANSI_STRING AnsiString)
 {
    ULONG Size;
 
@@ -138,7 +125,7 @@ NTSTATUS
 STDCALL
 RtlAppendUnicodeStringToString(
    IN OUT PUNICODE_STRING Destination,
-   IN PUNICODE_STRING Source)
+   IN PCUNICODE_STRING Source)
 {
 
    if ((Source->Length + Destination->Length) > Destination->MaximumLength)
@@ -154,98 +141,124 @@ RtlAppendUnicodeStringToString(
 }
 
 
-/*
+/**************************************************************************
+ *      RtlCharToInteger   (NTDLL.@)
  * @implemented
+ * Converts a character string into its integer equivalent.
+ *
+ * RETURNS
+ *  Success: STATUS_SUCCESS. value contains the converted number
+ *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
+ *           STATUS_ACCESS_VIOLATION, if value is NULL.
+ *
+ * NOTES
+ *  For base 0 it uses 10 as base and the string should be in the format
+ *      "{whitespace} [+|-] [0[x|o|b]] {digits}".
+ *  For other bases the string should be in the format
+ *      "{whitespace} [+|-] {digits}".
+ *  No check is made for value overflow, only the lower 32 bits are assigned.
+ *  If str is NULL it crashes, as the native function does.
+ *
+ * DIFFERENCES
+ *  This function does not read garbage behind '\0' as the native version does.
  */
 NTSTATUS
 STDCALL
 RtlCharToInteger(
-   IN PCSZ String,
-   IN ULONG Base,
-   IN OUT PULONG Value)
-{
-   ULONG Val;
-
-   *Value = 0;
-
-   if (Base == 0)
-   {
-      Base = 10;
-      if (*String == '0')
-      {
-         Base = 8;
-         String++;
-         if ((*String == 'x') && isxdigit (String[1]))
-         {
-            String++;
-            Base = 16;
-         }
-      }
-   }
-
-   if (!isxdigit (*String))
-      return STATUS_INVALID_PARAMETER;
-
-   while (isxdigit (*String) &&
-          (Val = isdigit (*String) ? * String - '0' : (islower (*String)
-                 ? toupper (*String) : *String) - 'A' + 10) < Base)
-   {
-      *Value = *Value * Base + Val;
-      String++;
-   }
-
-   return STATUS_SUCCESS;
+    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) */
+    PULONG value)  /* [O] Destination for the converted value */
+{
+    CHAR chCurrent;
+    int digit;
+    ULONG RunningTotal = 0;
+    char bMinus = 0;
+
+    while (*str != '\0' && *str <= ' ') {
+       str++;
+    } /* while */
+
+    if (*str == '+') {
+       str++;
+    } else if (*str == '-') {
+       bMinus = 1;
+       str++;
+    } /* if */
+
+    if (base == 0) {
+       base = 10;
+       if (str[0] == '0') {
+           if (str[1] == 'b') {
+               str += 2;
+               base = 2;
+           } else if (str[1] == 'o') {
+               str += 2;
+               base = 8;
+           } else if (str[1] == 'x') {
+               str += 2;
+               base = 16;
+           } /* if */
+       } /* if */
+    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
+       return STATUS_INVALID_PARAMETER;
+    } /* if */
+
+    if (value == NULL) {
+       return STATUS_ACCESS_VIOLATION;
+    } /* if */
+
+    while (*str != '\0') {
+       chCurrent = *str;
+       if (chCurrent >= '0' && chCurrent <= '9') {
+           digit = chCurrent - '0';
+       } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
+           digit = chCurrent - 'A' + 10;
+       } else if (chCurrent >= 'a' && chCurrent <= 'z') {
+           digit = chCurrent - 'a' + 10;
+       } else {
+           digit = -1;
+       } /* if */
+       if (digit < 0 || digit >= (int)base) {
+           *value = bMinus ? -RunningTotal : RunningTotal;
+           return STATUS_SUCCESS;
+       } /* if */
+
+       RunningTotal = RunningTotal * base + digit;
+       str++;
+    } /* while */
+
+    *value = bMinus ? -RunningTotal : RunningTotal;
+    return STATUS_SUCCESS;
 }
 
-
-
 /*
  * @implemented
  */
 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;
 }
 
 
@@ -258,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 );
 }
 
 
@@ -304,41 +289,12 @@ RtlEqualString(
 BOOLEAN
 STDCALL
 RtlEqualUnicodeString(
-   IN PUNICODE_STRING String1,
-   IN PUNICODE_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 );
 }
 
 
@@ -349,14 +305,14 @@ VOID
 STDCALL
 RtlFreeAnsiString(IN PANSI_STRING AnsiString)
 {
-   if (AnsiString->Buffer == NULL)
-      return;
-
-   ExFreePool(AnsiString->Buffer);
+   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;
+   }
 }
 
 
@@ -367,14 +323,14 @@ VOID
 STDCALL
 RtlFreeOemString(IN POEM_STRING OemString)
 {
-   if (OemString->Buffer == NULL)
-      return;
-
-   ExFreePool(OemString->Buffer);
+   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;
+   }
 }
 
 
@@ -385,14 +341,14 @@ VOID
 STDCALL
 RtlFreeUnicodeString(IN PUNICODE_STRING UnicodeString)
 {
-   if (UnicodeString->Buffer == NULL)
-      return;
-
-   ExFreePool(UnicodeString->Buffer);
+   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;
+   }
 }
 
 /*
@@ -479,7 +435,7 @@ RtlInitUnicodeString(IN OUT PUNICODE_STRING DestinationString,
 {
    ULONG DestSize;
 
-   DPRINT("RtlInitUnicodeString(DestinationString %x, SourceString %x)\n",
+   DPRINT("RtlInitUnicodeString(DestinationString 0x%p, SourceString 0x%p)\n",
           DestinationString,
           SourceString);
 
@@ -571,7 +527,7 @@ RtlIntegerToChar(
       tp++;
    }
 
-   if (tp - temp >= Length)
+   if ((ULONG)((ULONG_PTR)tp - (ULONG_PTR)temp) >= Length)
    {
       return STATUS_BUFFER_TOO_SMALL;
    }
@@ -626,7 +582,7 @@ RtlIntegerToUnicode(
       tp++;
    }
 
-   if (tp - temp >= Length)
+   if ((ULONG)((ULONG_PTR)tp - (ULONG_PTR)temp) >= Length)
    {
       return STATUS_BUFFER_TOO_SMALL;
    }
@@ -659,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;
 }
@@ -689,23 +645,23 @@ RtlInt64ToUnicodeString (
    ANSI_STRING AnsiString;
    CHAR Buffer[33];
    NTSTATUS Status;
-   
+
    LargeInt.QuadPart = Value;
 
    Status = RtlLargeIntegerToChar (&LargeInt,
                                    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;
 }
@@ -768,8 +724,8 @@ RtlPrefixString(
 BOOLEAN
 STDCALL
 RtlPrefixUnicodeString(
-   PUNICODE_STRING String1,
-   PUNICODE_STRING String2,
+   PCUNICODE_STRING String1,
+   PCUNICODE_STRING String2,
    BOOLEAN  CaseInsensitive)
 {
    PWCHAR pc1;
@@ -807,113 +763,118 @@ RtlPrefixUnicodeString(
    return FALSE;
 }
 
-
-
-/*
+/**************************************************************************
+ *      RtlUnicodeStringToInteger (NTDLL.@)
  * @implemented
+ * Converts an unicode string into its integer equivalent.
  *
- * Note that regardless of success or failure status, we should leave the
- * partial value in Value.  An error is never returned based on the chars
- * in the string.
+ * RETURNS
+ *  Success: STATUS_SUCCESS. value contains the converted number
+ *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
+ *           STATUS_ACCESS_VIOLATION, if value is NULL.
  *
- * This function does check the base.  Only 2, 8, 10, 16 are permitted,
- * else STATUS_INVALID_PARAMETER is returned.
+ * NOTES
+ *  For base 0 it uses 10 as base and the string should be in the format
+ *      "{whitespace} [+|-] [0[x|o|b]] {digits}".
+ *  For other bases the string should be in the format
+ *      "{whitespace} [+|-] {digits}".
+ *  No check is made for value overflow, only the lower 32 bits are assigned.
+ *  If str is NULL it crashes, as the native function does.
+ *
+ *  Note that regardless of success or failure status, we should leave the
+ *  partial value in Value.  An error is never returned based on the chars
+ *  in the string.
+ *
+ * DIFFERENCES
+ *  This function does not read garbage on string length 0 as the native
+ *  version does.
  */
 NTSTATUS
 STDCALL
 RtlUnicodeStringToInteger(
-   IN PUNICODE_STRING String,
-   IN ULONG  Base,
-   OUT PULONG  Value)
-{
-   PWCHAR Str;
-   ULONG lenmin = 0;
-   ULONG i;
-   ULONG Val;
-   BOOLEAN addneg = FALSE;
-   NTSTATUS Status = STATUS_SUCCESS;
-
-   *Value = 0;
-   Str = String->Buffer;
-
-   if( Base && Base != 2 && Base != 8 && Base != 10 && Base != 16 )
-       return STATUS_INVALID_PARAMETER;
-
-   for (i = 0; i < String->Length / sizeof(WCHAR); i++)
-   {
-      if (*Str == L'b')
-      {
-         Base = 2;
-         lenmin++;
-      }
-      else if (*Str == L'o')
-      {
-         Base = 8;
-         lenmin++;
-      }
-      else if (*Str == L'd')
-      {
-         Base = 10;
-         lenmin++;
-      }
-      else if (*Str == L'x')
-      {
-         Base = 16;
-         lenmin++;
-      }
-      else if (*Str == L'+')
-      {
-         lenmin++;
-      }
-      else if (*Str == L'-')
-      {
-         addneg = TRUE;
-         lenmin++;
-      }
-      else if ((*Str > L'1') && (Base == 2))
-      {
-         break;
-      }
-      else if (((*Str > L'7') || (*Str < L'0')) && (Base == 8))
-      {
-         break;
-      }
-      else if (((*Str > L'9') || (*Str < L'0')) && (Base == 10))
-      {
-         break;
-      }
-      else if (  ((*Str > L'9') || (*Str < L'0')) &&
-                 ((towupper (*Str) > L'F') || (towupper (*Str) < L'A')) &&
-                 (Base == 16))
-      {
-         break;
-      }
-      Str++;
-   }
-
-   Str = String->Buffer + lenmin;
-
-   if (Base == 0)
-      Base = 10;
-
-   while (iswxdigit (*Str) &&
-          (Val = 
-          iswdigit (*Str) ? 
-          *Str - L'0' : 
-          (towupper (*Str) - L'A' + 10)) < Base)
-   {
-      *Value = *Value * Base + Val;
-      Str++;
-   }
-
-   if (addneg == TRUE)
-      *Value *= -1;
-
-   return Status;
+    PCUNICODE_STRING str, /* [I] Unicode string to be converted */
+    ULONG base,                /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
+    PULONG value)              /* [O] Destination for the converted value */
+{
+    LPWSTR lpwstr = str->Buffer;
+    USHORT CharsRemaining = str->Length / sizeof(WCHAR);
+    WCHAR wchCurrent;
+    int digit;
+    ULONG newbase = 0;
+    ULONG RunningTotal = 0;
+    char bMinus = 0;
+
+    while (CharsRemaining >= 1 && *lpwstr <= L' ') {
+       lpwstr++;
+       CharsRemaining--;
+    } /* while */
+
+    if (CharsRemaining >= 1) {
+       if (*lpwstr == L'+') {
+           lpwstr++;
+           CharsRemaining--;
+       } else if (*lpwstr == L'-') {
+           bMinus = 1;
+           lpwstr++;
+           CharsRemaining--;
+       } /* if */
+    } /* 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 */
+    }
+    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) {
+       return STATUS_ACCESS_VIOLATION;
+    } /* if */
+
+    while (CharsRemaining >= 1) {
+       wchCurrent = *lpwstr;
+       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 >= (int)base) {
+           *value = bMinus ? -RunningTotal : RunningTotal;
+           return STATUS_SUCCESS;
+       } /* if */
+
+       RunningTotal = RunningTotal * base + digit;
+       lpwstr++;
+       CharsRemaining--;
+    } /* while */
+
+    *value = bMinus ? -RunningTotal : RunningTotal;
+    return STATUS_SUCCESS;
 }
 
 
-
 /*
  * @implemented
  *
@@ -922,8 +883,8 @@ RtlUnicodeStringToInteger(
  */
 ULONG
 STDCALL
-RtlUnicodeStringToOemSize(
-   IN PUNICODE_STRING UnicodeString)
+RtlxUnicodeStringToOemSize(
+   IN PCUNICODE_STRING UnicodeString)
 {
    ULONG Size;
 
@@ -934,59 +895,30 @@ RtlUnicodeStringToOemSize(
    return Size+1; //NB: incl. nullterm
 }
 
-
 /*
  * @implemented
  *
- * NOTES
- *  See RtlpUnicodeStringToAnsiString
- */
-NTSTATUS
-STDCALL
-RtlUnicodeStringToAnsiString(
-   IN OUT PANSI_STRING AnsiDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpUnicodeStringToAnsiString(
-             AnsiDest,
-             UniSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
 
-
-/*
- * private
- *
  * 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 PCUNICODE_STRING UniSource,
+   IN BOOLEAN AllocateDestinationString)
 {
    NTSTATUS Status = STATUS_SUCCESS;
-   ULONG Length; //including nullterm
-
-   if (NlsMbCodePageTag == TRUE)
-   {
-      Length = RtlUnicodeStringToAnsiSize(UniSource);
-   }
-   else
-      Length = (UniSource->Length / sizeof(WCHAR)) + sizeof(CHAR);
+   ULONG Length; /* including nullterm */
 
+   Length = RtlUnicodeStringToAnsiSize(UniSource);
    AnsiDest->Length = Length - sizeof(CHAR);
 
    if (AllocateDestinationString)
    {
-      AnsiDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_ASTR);
+      AnsiDest->Buffer = RtlpAllocateStringMemory(Length, TAG_ASTR);
       if (AnsiDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -998,7 +930,7 @@ RtlpUnicodeStringToAnsiString(
    }
    else if (Length > AnsiDest->MaximumLength)
    {
-      //make room for nullterm
+      /* make room for nullterm */
       AnsiDest->Length = AnsiDest->MaximumLength - sizeof(CHAR);
    }
 
@@ -1010,7 +942,7 @@ RtlpUnicodeStringToAnsiString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool (AnsiDest->Buffer);
+      RtlpFreeStringMemory(AnsiDest->Buffer, TAG_ASTR);
       return Status;
    }
 
@@ -1023,46 +955,20 @@ RtlpUnicodeStringToAnsiString(
  * @implemented
  *
  * NOTES
- *  See RtlpOemStringToUnicodeString 
+ *  This function always writes a terminating '\0'.
+ *  Does NOT perform a partial copy if unicode is too small!
  */
 NTSTATUS
 STDCALL
 RtlOemStringToUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
-   IN POEM_STRING OemSource,
+   IN PCOEM_STRING OemSource,
    IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpOemStringToUnicodeString(
-             UniDest,
-             OemSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-
-/*
- * private
- *
- * NOTES
- *  This function always writes a terminating '\0'.
- *  Does NOT perform a partial copy if unicode is too small!
- */
-NTSTATUS
-FASTCALL
-RtlpOemStringToUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN POEM_STRING OemSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
 {
    NTSTATUS Status;
-   ULONG Length; //including nullterm
-
-   if (NlsMbOemCodePageTag == TRUE)
-      Length = RtlOemStringToUnicodeSize(OemSource);
-   else
-      Length = (OemSource->Length * sizeof(WCHAR)) + sizeof(WCHAR);
+   ULONG Length; /* including nullterm */
 
+   Length = RtlOemStringToUnicodeSize(OemSource);
    if (Length > 0xffff)
       return STATUS_INVALID_PARAMETER_2;
 
@@ -1070,7 +976,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;
 
@@ -1082,7 +988,7 @@ RtlpOemStringToUnicodeString(
       return STATUS_BUFFER_TOO_SMALL;
    }
 
-   //FIXME: Do we need this????? -Gunnar
+   /* FIXME: Do we need this????? -Gunnar */
    RtlZeroMemory (UniDest->Buffer,
                   UniDest->Length);
 
@@ -1094,7 +1000,7 @@ RtlpOemStringToUnicodeString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool (UniDest->Buffer);
+      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
       return Status;
    }
 
@@ -1107,46 +1013,19 @@ RtlpOemStringToUnicodeString(
  * @implemented
  *
  * NOTES
- *  See RtlpUnicodeStringToOemString.
+ *   This function always '\0' terminates the string returned.
  */
 NTSTATUS
 STDCALL
 RtlUnicodeStringToOemString(
    IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
+   IN PCUNICODE_STRING UniSource,
    IN BOOLEAN  AllocateDestinationString)
-{
-   return RtlpUnicodeStringToOemString(
-             OemDest,
-             UniSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-
-
-/*
- * private
- *
- * NOTES
- *   This function always '\0' terminates the string returned.
- */
-NTSTATUS
-FASTCALL
-RtlpUnicodeStringToOemString(
-   IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString,
-   IN POOL_TYPE PoolType)
 {
    NTSTATUS Status = STATUS_SUCCESS;
    ULONG Length; //including nullterm
 
-   if (NlsMbOemCodePageTag == TRUE)
-      Length = RtlUnicodeStringToAnsiSize (UniSource);
-   else
-      Length = (UniSource->Length / sizeof(WCHAR)) + sizeof(CHAR);
-
+   Length = RtlUnicodeStringToAnsiSize(UniSource);
    if (Length > 0x0000FFFF)
       return STATUS_INVALID_PARAMETER_2;
 
@@ -1154,7 +1033,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;
 
@@ -1178,7 +1057,7 @@ RtlpUnicodeStringToOemString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool(OemDest->Buffer);
+      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
       return Status;
    }
 
@@ -1186,8 +1065,6 @@ RtlpUnicodeStringToOemString(
    return Status;
 }
 
-
-
 #define ITU_IMPLEMENTED_TESTS (IS_TEXT_UNICODE_ODD_LENGTH|IS_TEXT_UNICODE_SIGNATURE)
 
 
@@ -1254,52 +1131,26 @@ done:
  * @implemented
  *
  * NOTES
- *  See RtlpOemStringToCountedUnicodeString
+ *  Same as RtlOemStringToUnicodeString but doesn't write terminating null
+ *  A partial copy is NOT performed if the dest buffer is too small!
  */
 NTSTATUS
 STDCALL
 RtlOemStringToCountedUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
-   IN POEM_STRING OemSource,
+   IN PCOEM_STRING OemSource,
    IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpOemStringToCountedUnicodeString(
-             UniDest,
-             OemSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-
-/*
- * private
- *
- * 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(
-   IN OUT PUNICODE_STRING UniDest,
-   IN POEM_STRING OemSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
 {
    NTSTATUS Status;
-   ULONG Length; //excluding nullterm
-
-   if (NlsMbCodePageTag == TRUE)
-      Length = RtlOemStringToUnicodeSize(OemSource) - sizeof(WCHAR);
-   else
-      Length = OemSource->Length * sizeof(WCHAR);
+   ULONG Length; /* excluding nullterm */
 
+   Length = RtlOemStringToCountedUnicodeSize(OemSource);
    if (Length > 65535)
       return STATUS_INVALID_PARAMETER_2;
 
    if (AllocateDestinationString == TRUE)
    {
-      UniDest->Buffer = ExAllocatePoolWithTag (PoolType, Length, TAG_USTR);
+      UniDest->Buffer = RtlpAllocateStringMemory (Length, TAG_USTR);
       if (UniDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -1320,14 +1171,13 @@ RtlpOemStringToCountedUnicodeString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool (UniDest->Buffer);
+      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
       return Status;
    }
 
    return Status;
 }
 
-
 /*
  * @implemented
  *
@@ -1394,23 +1244,23 @@ RtlEqualDomainName (
    OEM_STRING OemString1;
    OEM_STRING OemString2;
    BOOLEAN Result;
+
    RtlUpcaseUnicodeStringToOemString (&OemString1,
                                       DomainName1,
                                       TRUE);
    RtlUpcaseUnicodeStringToOemString (&OemString2,
                                       DomainName2,
                                       TRUE);
+
    Result = RtlEqualString (&OemString1,
                             &OemString2,
                             FALSE);
+
    RtlFreeOemString (&OemString1);
    RtlFreeOemString (&OemString2);
+
    return Result;
-   
+
 }
 */
 
@@ -1551,40 +1401,74 @@ 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;
+   }
 }
 
 /*
-* @unimplemented
+* @implemented
 */
 NTSTATUS
 STDCALL
 RtlHashUnicodeString(
-       IN const UNICODE_STRING *String,
-       IN BOOLEAN CaseInSensitive,
-       IN ULONG HashAlgorithm,
-       OUT PULONG HashValue
-       )
-{
-       UNIMPLEMENTED;
-       return STATUS_NOT_IMPLEMENTED;
+  IN CONST UNICODE_STRING *String,
+  IN BOOLEAN CaseInSensitive,
+  IN ULONG HashAlgorithm,
+  OUT PULONG HashValue)
+{
+    if (String != NULL && HashValue != NULL)
+    {
+        switch (HashAlgorithm)
+        {
+            case HASH_STRING_ALGORITHM_DEFAULT:
+            case HASH_STRING_ALGORITHM_X65599:
+            {
+                WCHAR *c, *end;
+
+                *HashValue = 0;
+                end = String->Buffer + (String->Length / sizeof(WCHAR));
+
+                if (CaseInSensitive)
+                {
+                    for (c = String->Buffer;
+                         c != end;
+                         c++)
+                    {
+                        /* only uppercase characters if they are 'a' ... 'z'! */
+                        *HashValue = ((65599 * (*HashValue)) +
+                                      (ULONG)(((*c) >= L'a' && (*c) <= L'z') ?
+                                              (*c) - L'a' + L'A' : (*c)));
+                    }
+                }
+                else
+                {
+                    for (c = String->Buffer;
+                         c != end;
+                         c++)
+                    {
+                        *HashValue = ((65599 * (*HashValue)) + (ULONG)(*c));
+                    }
+                }
+                return STATUS_SUCCESS;
+            }
+        }
+    }
+
+    return STATUS_INVALID_PARAMETER;
 }
 
 /*
  * @implemented
  *
  * NOTES
- *  See RtlpUnicodeStringToCountedOemString.
+ *  Same as RtlUnicodeStringToOemString but doesn't write terminating null
+ *  Does a partial copy if the dest buffer is too small
  */
 NTSTATUS
 STDCALL
@@ -1592,37 +1476,11 @@ RtlUnicodeStringToCountedOemString(
    IN OUT POEM_STRING OemDest,
    IN PUNICODE_STRING UniSource,
    IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpUnicodeStringToCountedOemString(
-             OemDest,
-             UniSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-/*
- * private
- *
- * NOTES
- *  Same as RtlUnicodeStringToOemString but doesn't write terminating null
- *  Does a partial copy if the dest buffer is too small
- */
-NTSTATUS
-FASTCALL
-RtlpUnicodeStringToCountedOemString(
-   IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
 {
    NTSTATUS Status;
    ULONG Length; //excluding nullterm
 
-   if (NlsMbOemCodePageTag == TRUE)
-      Length = RtlUnicodeStringToAnsiSize(UniSource) - sizeof(CHAR);
-   else
-      Length = (UniSource->Length / sizeof(WCHAR));
-
+   Length = RtlUnicodeStringToCountedOemSize(UniSource);
    if (Length > 0x0000FFFF)
       return STATUS_INVALID_PARAMETER_2;
 
@@ -1630,7 +1488,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;
 
@@ -1653,13 +1511,12 @@ RtlpUnicodeStringToCountedOemString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool(OemDest->Buffer);
+      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
    }
 
    return Status;
 }
 
-
 /*
  * @implemented
  */
@@ -1698,7 +1555,7 @@ RtlLargeIntegerToChar(
       tp++;
    }
 
-   if (tp - temp >= Length)
+   if ((ULONG)((ULONG_PTR)tp - (ULONG_PTR)temp) >= Length)
       return STATUS_BUFFER_TOO_SMALL;
 
    sp = String;
@@ -1709,13 +1566,12 @@ RtlLargeIntegerToChar(
    return STATUS_SUCCESS;
 }
 
-
-
 /*
  * @implemented
  *
  * NOTES
- *  See RtlpUpcaseUnicodeString
+ *  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
 STDCALL
@@ -1724,37 +1580,13 @@ RtlUpcaseUnicodeString(
    IN PCUNICODE_STRING UniSource,
    IN BOOLEAN  AllocateDestinationString)
 {
-
-   return RtlpUpcaseUnicodeString(
-             UniDest,
-             UniSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-
-/*
- * private
- *
- * 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(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PCUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString,
-   IN POOL_TYPE PoolType)
-{
    ULONG i;
    PWCHAR Src, Dest;
 
    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;
    }
@@ -1777,13 +1609,12 @@ RtlpUpcaseUnicodeString(
    return STATUS_SUCCESS;
 }
 
-
-
 /*
  * @implemented
  *
  * NOTES
- *  See RtlpUpcaseUnicodeStringToAnsiString
+ *  This function always writes a terminating '\0'.
+ *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
 STDCALL
@@ -1791,37 +1622,11 @@ RtlUpcaseUnicodeStringToAnsiString(
    IN OUT PANSI_STRING AnsiDest,
    IN PUNICODE_STRING UniSource,
    IN BOOLEAN  AllocateDestinationString)
-{
-   return RtlpUpcaseUnicodeStringToAnsiString(
-             AnsiDest,
-             UniSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-/*
- * private
- *
- * NOTES
- *  This function always writes a terminating '\0'.
- *  It performs a partial copy if ansi is too small.
- */
-NTSTATUS
-FASTCALL
-RtlpUpcaseUnicodeStringToAnsiString(
-   IN OUT PANSI_STRING AnsiDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString,
-   IN POOL_TYPE PoolType)
 {
    NTSTATUS Status;
-   ULONG Length; //including nullterm
-
-   if (NlsMbCodePageTag == TRUE)
-      Length = RtlUnicodeStringToAnsiSize(UniSource);
-   else
-      Length = (UniSource->Length / sizeof(WCHAR)) + sizeof(CHAR);
+   ULONG Length; /* including nullterm */
 
+   Length = RtlUnicodeStringToAnsiSize(UniSource);
    if (Length > 0x0000FFFF)
       return STATUS_INVALID_PARAMETER_2;
 
@@ -1829,7 +1634,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;
 
@@ -1841,11 +1646,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);
 
@@ -1857,7 +1662,7 @@ RtlpUpcaseUnicodeStringToAnsiString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool(AnsiDest->Buffer);
+      RtlpFreeStringMemory(AnsiDest->Buffer, TAG_ASTR);
       return Status;
    }
 
@@ -1865,53 +1670,24 @@ RtlpUpcaseUnicodeStringToAnsiString(
    return Status;
 }
 
-
-
 /*
  * @implemented
  *
  * NOTES
- *  See RtlpUpcaseUnicodeStringToCountedOemString
+ *  This function always writes a terminating '\0'.
+ *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
 STDCALL
 RtlUpcaseUnicodeStringToCountedOemString(
    IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
+   IN PCUNICODE_STRING UniSource,
    IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpUpcaseUnicodeStringToCountedOemString(
-             OemDest,
-             UniSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-
-
-/*
- * private
- *
- * NOTES
- *  Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
- *  It performs a partial copy if oem is too small.
- */
-NTSTATUS
-FASTCALL
-RtlpUpcaseUnicodeStringToCountedOemString(
-   IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
 {
    NTSTATUS Status;
-   ULONG Length; //excluding nullterm
-
-   if (NlsMbCodePageTag == TRUE)
-      Length = RtlUnicodeStringToAnsiSize(UniSource) - sizeof(CHAR);
-   else
-      Length = UniSource->Length / sizeof(WCHAR);
+   ULONG Length; /* excluding nullterm */
 
+   Length = RtlUnicodeStringToCountedOemSize(UniSource);
    if (Length > 0x0000FFFF)
       return(STATUS_INVALID_PARAMETER_2);
 
@@ -1919,11 +1695,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;
@@ -1945,59 +1721,31 @@ RtlpUpcaseUnicodeStringToCountedOemString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool(OemDest->Buffer);
+      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
       return Status;
    }
 
    return Status;
 }
 
-
 /*
  * @implemented
  * NOTES
- *  See RtlpUpcaseUnicodeStringToOemString
+ *  Oem string is allways nullterminated
+ *  It performs a partial copy if oem is too small.
  */
 NTSTATUS
 STDCALL
 RtlUpcaseUnicodeStringToOemString (
    IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
+   IN PCUNICODE_STRING UniSource,
    IN BOOLEAN  AllocateDestinationString
 )
-{
-   return RtlpUpcaseUnicodeStringToOemString(
-             OemDest,
-             UniSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-
-/*
- * private
- *
- * NOTES
- *  Oem string is allways nullterminated 
- *  It performs a partial copy if oem is too small.
- */
-NTSTATUS
-FASTCALL
-RtlpUpcaseUnicodeStringToOemString (
-   IN OUT POEM_STRING OemDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString,
-   IN POOL_TYPE PoolType
-)
 {
    NTSTATUS Status;
-   ULONG Length; //including nullterm
-
-   if (NlsMbOemCodePageTag == TRUE)
-      Length = RtlUnicodeStringToAnsiSize(UniSource);
-   else
-      Length = (UniSource->Length / sizeof(WCHAR)) + sizeof(CHAR);
+   ULONG Length; /* including nullterm */
 
+   Length = RtlUnicodeStringToAnsiSize(UniSource);
    if (Length > 0x0000FFFF)
       return STATUS_INVALID_PARAMETER_2;
 
@@ -2005,11 +1753,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;
@@ -2020,7 +1768,7 @@ RtlpUpcaseUnicodeStringToOemString (
    }
    else if (Length > OemDest->MaximumLength)
    {
-      //make room for nullterm
+      /* make room for nullterm */
       OemDest->Length = OemDest->MaximumLength - sizeof(CHAR);
    }
 
@@ -2032,7 +1780,7 @@ RtlpUpcaseUnicodeStringToOemString (
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool(OemDest->Buffer);
+      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
       return Status;
    }
 
@@ -2040,7 +1788,6 @@ RtlpUpcaseUnicodeStringToOemString (
    return Status;
 }
 
-
 /*
  * @implemented
  *
@@ -2049,7 +1796,7 @@ RtlpUpcaseUnicodeStringToOemString (
  */
 ULONG
 STDCALL
-RtlOemStringToUnicodeSize(IN POEM_STRING OemString)
+RtlxOemStringToUnicodeSize(IN PCOEM_STRING OemString)
 {
    ULONG Size;
 
@@ -2112,8 +1859,8 @@ RtlStringFromGUID (IN REFGUID Guid,
  */
 ULONG
 STDCALL
-RtlUnicodeStringToAnsiSize(
-   IN PUNICODE_STRING UnicodeString)
+RtlxUnicodeStringToAnsiSize(
+   IN PCUNICODE_STRING UnicodeString)
 {
    ULONG Size;
 
@@ -2126,58 +1873,38 @@ RtlUnicodeStringToAnsiSize(
 }
 
 
-
-
 /*
  * @implemented
  */
 LONG
 STDCALL
 RtlCompareUnicodeString(
-   IN PUNICODE_STRING String1,
-   IN PUNICODE_STRING String2,
+   IN PCUNICODE_STRING s1,
+   IN PCUNICODE_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
  */
@@ -2195,11 +1922,14 @@ RtlCopyString(
       return;
    }
 
-   copylen = min (DestinationString->MaximumLength - sizeof(CHAR),
+   copylen = min (DestinationString->MaximumLength,
                   SourceString->Length);
 
    memcpy(DestinationString->Buffer, SourceString->Buffer, copylen);
-   DestinationString->Buffer[copylen] = 0;
+   if (DestinationString->MaximumLength >= copylen + sizeof(CHAR))
+   {
+      DestinationString->Buffer[copylen] = 0;
+   }
    DestinationString->Length = copylen;
 }
 
@@ -2212,7 +1942,7 @@ VOID
 STDCALL
 RtlCopyUnicodeString(
    IN OUT PUNICODE_STRING DestinationString,
-   IN PUNICODE_STRING SourceString)
+   IN PCUNICODE_STRING SourceString)
 {
    ULONG copylen;
 
@@ -2222,19 +1952,21 @@ RtlCopyUnicodeString(
       return;
    }
 
-   copylen = min (DestinationString->MaximumLength - sizeof(WCHAR),
+   copylen = min (DestinationString->MaximumLength,
                   SourceString->Length);
    memcpy(DestinationString->Buffer, SourceString->Buffer, copylen);
-   DestinationString->Buffer[copylen / sizeof(WCHAR)] = 0;
+   if (DestinationString->MaximumLength >= copylen + sizeof(WCHAR))
+   {
+     DestinationString->Buffer[copylen / sizeof(WCHAR)] = 0;
+   }
    DestinationString->Length = copylen;
 }
 
-
 /*
  * @implemented
  *
  * NOTES
- *  See RtlpCreateUnicodeString
+ * Creates a nullterminated UNICODE_STRING
  */
 BOOLEAN
 STDCALL
@@ -2242,33 +1974,16 @@ RtlCreateUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCWSTR  Source)
 {
-
-   DPRINT("RtlCreateUnicodeString\n");
-   return RtlpCreateUnicodeString(UniDest, Source, NonPagedPool);
-}
-
-
-/*
- * private
- */
-BOOLEAN
-FASTCALL
-RtlpCreateUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PCWSTR  Source,
-   IN POOL_TYPE PoolType)
-{
    ULONG Length;
 
    Length = (wcslen (Source) + 1) * sizeof(WCHAR);
-
-   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);
@@ -2276,8 +1991,6 @@ RtlpCreateUnicodeString(
    return TRUE;
 }
 
-
-
 /*
  * @implemented
  */
@@ -2300,50 +2013,26 @@ RtlCreateUnicodeStringFromAsciiz(
    return NT_SUCCESS(Status);
 }
 
-
-
 /*
  * @implemented
  *
  * NOTES
- *  See RtlpDowncaseUnicodeString
+ *  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 STDCALL
 RtlDowncaseUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
-   IN PUNICODE_STRING UniSource,
+   IN PCUNICODE_STRING UniSource,
    IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpDowncaseUnicodeString(
-             UniDest,
-             UniSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-
-/*
- * private
- *
- * 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(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PUNICODE_STRING UniSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
 {
    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;
 
@@ -2380,8 +2069,6 @@ RtlpDowncaseUnicodeString(
    return STATUS_SUCCESS;
 }
 
-
-
 /*
  * @implemented
  *
@@ -2410,12 +2097,12 @@ RtlAppendUnicodeToString(IN OUT PUNICODE_STRING Destination,
    return(STATUS_SUCCESS);
 }
 
-
 /*
  * @implemented
  *
  * NOTES
- *  See RtlpAnsiStringToUnicodeString
+ *  This function always writes a terminating '\0'.
+ *  If the dest buffer is too small a partial copy is NOT performed!
  */
 NTSTATUS
 STDCALL
@@ -2423,45 +2110,17 @@ RtlAnsiStringToUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PANSI_STRING AnsiSource,
    IN BOOLEAN AllocateDestinationString)
-{
-   return RtlpAnsiStringToUnicodeString(
-             UniDest,
-             AnsiSource,
-             AllocateDestinationString,
-             NonPagedPool);
-}
-
-
-
-/*
- * private
- *
- * NOTES
- *  This function always writes a terminating '\0'.
- *  If the dest buffer is too small a partial copy is NOT performed!
- */
-NTSTATUS
-FASTCALL
-RtlpAnsiStringToUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PANSI_STRING AnsiSource,
-   IN BOOLEAN AllocateDestinationString,
-   IN POOL_TYPE PoolType)
 {
    NTSTATUS Status;
    ULONG Length; //including nullterm
 
-   if (NlsMbCodePageTag == TRUE)
-      Length = RtlAnsiStringToUnicodeSize(AnsiSource);
-   else
-      Length = (AnsiSource->Length * sizeof(WCHAR)) + sizeof(WCHAR);
-
+   Length = RtlAnsiStringToUnicodeSize(AnsiSource);
    if (Length > 0xffff)
       return STATUS_INVALID_PARAMETER_2;
 
    if (AllocateDestinationString == TRUE)
    {
-      UniDest->Buffer = ExAllocatePoolWithTag(PoolType, Length, TAG_USTR);
+      UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
       if (UniDest->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -2487,7 +2146,7 @@ RtlpAnsiStringToUnicodeString(
 
    if (!NT_SUCCESS(Status) && AllocateDestinationString)
    {
-      ExFreePool(UniDest->Buffer);
+      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
       return Status;
    }
 
@@ -2495,8 +2154,6 @@ RtlpAnsiStringToUnicodeString(
    return Status;
 }
 
-
-
 /*
  * @implemented
  *
@@ -2561,83 +2218,26 @@ RtlUpperString(PSTRING DestinationString,
    DestinationString->Length = SourceString->Length;
 }
 
-
-/*
- * @implemented
- */
-ULONG STDCALL
-RtlxAnsiStringToUnicodeSize(IN PANSI_STRING AnsiString)
-{
-   return RtlAnsiStringToUnicodeSize(AnsiString);
-}
-
-
-/*
- * @implemented
- */
-ULONG STDCALL
-RtlxOemStringToUnicodeSize(IN POEM_STRING OemString)
-{
-   return RtlOemStringToUnicodeSize((PANSI_STRING)OemString);
-}
-
-
-
-/*
- * @implemented
- */
-ULONG STDCALL
-RtlxUnicodeStringToAnsiSize(IN PUNICODE_STRING UnicodeString)
-{
-   return RtlUnicodeStringToAnsiSize(UnicodeString);
-}
-
-
-/*
- * @implemented
- */
-ULONG STDCALL
-RtlxUnicodeStringToOemSize(IN PUNICODE_STRING UnicodeString)
-{
-   return RtlUnicodeStringToOemSize(UnicodeString);
-}
-
-
 /*
  * @implemented
  *
  * NOTES
  *  See RtlpDuplicateUnicodeString
  */
-NTSTATUS STDCALL
+NTSTATUS
+STDCALL
 RtlDuplicateUnicodeString(
-   INT AddNull,
-   IN PUNICODE_STRING SourceString,
-   PUNICODE_STRING DestinationString)
-{
-   return RtlpDuplicateUnicodeString(
-            AddNull,
-            SourceString,
-            DestinationString,
-            NonPagedPool);
-}
-   
-   
-/*
- * @implemented
- */
-NTSTATUS STDCALL
-RtlpDuplicateUnicodeString(
-   INT AddNull,
-   IN PUNICODE_STRING SourceString,
-   PUNICODE_STRING DestinationString,
-   POOL_TYPE PoolType)
+   IN ULONG Flags,
+   IN PCUNICODE_STRING SourceString,
+   OUT PUNICODE_STRING DestinationString)
 {
    if (SourceString == NULL || DestinationString == NULL)
       return STATUS_INVALID_PARAMETER;
 
 
-   if (SourceString->Length == 0 && AddNull != 3)
+   if ((SourceString->Length == 0) && 
+       (Flags != (RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE | 
+                  RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING)))
    {
       DestinationString->Length = 0;
       DestinationString->MaximumLength = 0;
@@ -2645,12 +2245,12 @@ RtlpDuplicateUnicodeString(
    }
    else
    {
-      unsigned int DestMaxLength = SourceString->Length;
+      UINT DestMaxLength = SourceString->Length;
 
-      if (AddNull)
+      if (Flags & RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE)
          DestMaxLength += sizeof(UNICODE_NULL);
 
-      DestinationString->Buffer = ExAllocatePool(PoolType, DestMaxLength);
+      DestinationString->Buffer = RtlpAllocateStringMemory(DestMaxLength, TAG_USTR);
       if (DestinationString->Buffer == NULL)
          return STATUS_NO_MEMORY;
 
@@ -2658,11 +2258,39 @@ RtlpDuplicateUnicodeString(
       DestinationString->Length = SourceString->Length;
       DestinationString->MaximumLength = DestMaxLength;
 
-      if (AddNull)
+      if (Flags & RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE)
          DestinationString->Buffer[DestinationString->Length / sizeof(WCHAR)] = 0;
    }
 
    return STATUS_SUCCESS;
 }
 
+/*
+ * @implemented
+ */
+NTSTATUS STDCALL
+RtlValidateUnicodeString(IN ULONG Flags,
+                         IN PUNICODE_STRING UnicodeString)
+{
+  /* currently no flags are supported! */
+  ASSERT(Flags == 0);
+
+  if ((Flags == 0) &&
+      ((UnicodeString == NULL) ||
+       ((UnicodeString->Length != 0) &&
+        (UnicodeString->Buffer != NULL) &&
+        ((UnicodeString->Length % sizeof(WCHAR)) == 0) &&
+        ((UnicodeString->MaximumLength % sizeof(WCHAR)) == 0) &&
+        (UnicodeString->MaximumLength >= UnicodeString->Length))))
+  {
+    /* a NULL pointer as a unicode string is considered to be a valid unicode
+       string! */
+    return STATUS_SUCCESS;
+  }
+  else
+  {
+    return STATUS_INVALID_PARAMETER;
+  }
+}
+
 /* EOF */