[rtl]
[reactos.git] / reactos / lib / rtl / unicode.c
index 7f30600..bafe8c5 100644 (file)
 /*
- * Rtl string functions
- *
- * Copyright (C) 1996-1998 Marcus Meissner
- * Copyright (C) 2000      Alexandre Julliard
- * Copyright (C) 2003      Thomas Mertes
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * COPYRIGHT:         See COPYING in the top level directory
+ * PROJECT:           ReactOS system libraries
+ * PURPOSE:           Unicode Conversion Routines
+ * FILE:              lib/rtl/unicode.c
+ * PROGRAMMER:        Alex Ionescu (alex@relsoft.net)
+ *                    Emanuele Aliberti
+ *                    Gunnar Dalsnes
  */
-#define __NTDRIVER__
+
+/* INCLUDES *****************************************************************/
+
 #include <rtl.h>
 
 #define NDEBUG
 #include <debug.h>
 
+#include <wine/unicode.h>
+
 /* GLOBALS *******************************************************************/
 
 extern BOOLEAN NlsMbCodePageTag;
 extern BOOLEAN NlsMbOemCodePageTag;
+extern PUSHORT NlsLeadByteInfo;
 
-/* FUNCTIONS *****************************************************************/
+extern USHORT NlsOemDefaultChar;
+extern USHORT NlsUnicodeDefaultChar;
 
+/* FUNCTIONS *****************************************************************/
 
 /*
 * @implemented
 */
-WCHAR STDCALL
-RtlAnsiCharToUnicodeChar (IN CHAR AnsiChar)
+WCHAR
+NTAPI
+RtlAnsiCharToUnicodeChar(IN PUCHAR *AnsiChar)
 {
-   ULONG Size;
-   WCHAR UnicodeChar;
+    ULONG Size;
+    NTSTATUS Status;
+    WCHAR UnicodeChar = L' ';
 
-   Size = 1;
-#if 0
+    Size = (NlsLeadByteInfo[**AnsiChar] == 0) ? 1 : 2;
 
-   Size = (NlsLeadByteInfo[AnsiChar] == 0) ? 1 : 2;
-#endif
+    Status = RtlMultiByteToUnicodeN(&UnicodeChar,
+                                    sizeof(WCHAR),
+                                    NULL,
+                                    (PCHAR)*AnsiChar,
+                                    Size);
 
-   RtlMultiByteToUnicodeN (&UnicodeChar,
-                           sizeof(WCHAR),
-                           NULL,
-                           &AnsiChar,
-                           Size);
+    if (!NT_SUCCESS(Status))
+    {
+        UnicodeChar = L' ';
+    }
 
-   return UnicodeChar;
+    *AnsiChar += Size;
+    return UnicodeChar;
 }
 
+/*
+ * @implemented
+ *
+ * NOTES
+ *  This function always writes a terminating '\0'.
+ *  If the dest buffer is too small a partial copy is NOT performed!
+ */
+NTSTATUS
+NTAPI
+RtlAnsiStringToUnicodeString(
+   IN OUT PUNICODE_STRING UniDest,
+   IN PANSI_STRING AnsiSource,
+   IN BOOLEAN AllocateDestinationString)
+{
+    NTSTATUS Status;
+    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)
+    {
+        UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
+        UniDest->MaximumLength = Length;
+        if (!UniDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (UniDest->Length >= UniDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
+
+    Status = RtlMultiByteToUnicodeN(UniDest->Buffer,
+                                    UniDest->Length,
+                                    &Index,
+                                    AnsiSource->Buffer,
+                                    AnsiSource->Length);
+
+    if (!NT_SUCCESS(Status))
+    {
+        if (AllocateDestinationString)
+        {
+            RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
+            UniDest->Buffer = NULL;
+        }
+        return Status;
+    }
+
+    UniDest->Buffer[Index / sizeof(WCHAR)] = UNICODE_NULL;
+    return Status;
+}
 
 /*
  * @implemented
@@ -65,20 +118,20 @@ RtlAnsiCharToUnicodeChar (IN CHAR AnsiChar)
  *  The calculated size in bytes including nullterm.
  */
 ULONG
-STDCALL
+NTAPI
 RtlxAnsiStringToUnicodeSize(IN PCANSI_STRING AnsiString)
 {
-   ULONG Size;
+    ULONG Size;
 
-   RtlMultiByteToUnicodeSize(&Size,
-                             AnsiString->Buffer,
-                             AnsiString->Length);
+    /* Convert from Mb String to Unicode Size */
+    RtlMultiByteToUnicodeSize(&Size,
+                              AnsiString->Buffer,
+                              AnsiString->Length);
 
-   return(Size);
+    /* Return the size plus the null-char */
+    return(Size + sizeof(WCHAR));
 }
 
-
-
 /*
  * @implemented
  *
@@ -87,32 +140,29 @@ RtlxAnsiStringToUnicodeSize(IN PCANSI_STRING AnsiString)
  *  Dest is never nullterminated.
  */
 NTSTATUS
-STDCALL
-RtlAppendStringToString(IN OUT PSTRING Destination,
+NTAPI
+RtlAppendStringToString(IN PSTRING Destination,
                         IN PSTRING Source)
 {
-   PCHAR Ptr;
-
-   if (Source->Length == 0)
-      return(STATUS_SUCCESS);
+    USHORT SourceLength = Source->Length;
 
-   if (Destination->Length + Source->Length >= Destination->MaximumLength)
-      return(STATUS_BUFFER_TOO_SMALL);
+    if (SourceLength)
+    {
+        if (Destination->Length + SourceLength > Destination->MaximumLength)
+        {
+            return STATUS_BUFFER_TOO_SMALL;
+        }
 
-   Ptr = Destination->Buffer + Destination->Length;
-   memmove(Ptr,
-           Source->Buffer,
-           Source->Length);
-   Ptr += Source->Length;
-   *Ptr = 0;
+        RtlMoveMemory(&Destination->Buffer[Destination->Length],
+                      Source->Buffer,
+                      SourceLength);
 
-   Destination->Length += Source->Length;
+        Destination->Length += SourceLength;
+    }
 
-   return(STATUS_SUCCESS);
+    return STATUS_SUCCESS;
 }
 
-
-
 /*
  * @implemented
  *
@@ -122,24 +172,33 @@ RtlAppendStringToString(IN OUT 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)
 {
+    USHORT SourceLength = Source->Length;
+    PWCHAR Buffer = &Destination->Buffer[Destination->Length / sizeof(WCHAR)];
 
-   if ((Source->Length + Destination->Length) > Destination->MaximumLength)
-      return STATUS_BUFFER_TOO_SMALL;
+    if (SourceLength)
+    {
+        if ((SourceLength+ Destination->Length) > Destination->MaximumLength)
+        {
+            return STATUS_BUFFER_TOO_SMALL;
+        }
 
-   memcpy((char*)Destination->Buffer + Destination->Length, Source->Buffer, Source->Length);
-   Destination->Length += Source->Length;
-   /* append terminating '\0' if enough space */
-   if( Destination->MaximumLength > Destination->Length )
-      Destination->Buffer[Destination->Length / sizeof(WCHAR)] = 0;
+        RtlMoveMemory(Buffer, Source->Buffer, SourceLength);
+        Destination->Length += SourceLength;
 
-   return STATUS_SUCCESS;
-}
+        /* append terminating '\0' if enough space */
+        if (Destination->MaximumLength > Destination->Length)
+        {
+            Buffer[SourceLength / sizeof(WCHAR)] = UNICODE_NULL;
+        }
+    }
 
+    return STATUS_SUCCESS;
+}
 
 /**************************************************************************
  *      RtlCharToInteger   (NTDLL.@)
@@ -163,7 +222,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) */
@@ -175,56 +234,56 @@ RtlCharToInteger(
     char bMinus = 0;
 
     while (*str != '\0' && *str <= ' ') {
-       str++;
+    str++;
     } /* while */
 
     if (*str == '+') {
-       str++;
+    str++;
     } else if (*str == '-') {
-       bMinus = 1;
-       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 */
+    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;
+    return STATUS_INVALID_PARAMETER;
     } /* if */
 
     if (value == NULL) {
-       return STATUS_ACCESS_VIOLATION;
+    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++;
+    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;
@@ -235,7 +294,7 @@ RtlCharToInteger(
  * @implemented
  */
 LONG
-STDCALL
+NTAPI
 RtlCompareString(
    IN PSTRING s1,
    IN PSTRING s2,
@@ -261,7 +320,6 @@ RtlCompareString(
    return ret;
 }
 
-
 /*
  * @implemented
  *
@@ -269,17 +327,16 @@ RtlCompareString(
  *  TRUE if strings are equal.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlEqualString(
    IN PSTRING s1,
    IN PSTRING s2,
    IN BOOLEAN CaseInsensitive)
 {
-   if (s1->Length != s2->Length) return FALSE;
-   return !RtlCompareString(s1, s2, CaseInsensitive );
+    if (s1->Length != s2->Length) return FALSE;
+    return !RtlCompareString(s1, s2, CaseInsensitive);
 }
 
-
 /*
  * @implemented
  *
@@ -287,81 +344,108 @@ RtlEqualString(
  *  TRUE if strings are equal.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlEqualUnicodeString(
    IN CONST UNICODE_STRING *s1,
    IN CONST UNICODE_STRING *s2,
    IN BOOLEAN  CaseInsensitive)
 {
-   if (s1->Length != s2->Length) return FALSE;
-   return !RtlCompareUnicodeString((PUNICODE_STRING)s1, (PUNICODE_STRING)s2, CaseInsensitive );
+    if (s1->Length != s2->Length) return FALSE;
+    return !RtlCompareUnicodeString(s1, s2, CaseInsensitive );
 }
 
-
 /*
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlFreeAnsiString(IN PANSI_STRING AnsiString)
 {
-   if (AnsiString->Buffer != NULL)
-   {
-      RtlpFreeStringMemory(AnsiString->Buffer, TAG_ASTR);
+    PAGED_CODE_RTL();
 
-      AnsiString->Buffer = NULL;
-      AnsiString->Length = 0;
-      AnsiString->MaximumLength = 0;
-   }
+    if (AnsiString->Buffer)
+    {
+        RtlpFreeStringMemory(AnsiString->Buffer, TAG_ASTR);
+        RtlZeroMemory(AnsiString, sizeof(ANSI_STRING));
+    }
 }
 
-
 /*
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlFreeOemString(IN POEM_STRING OemString)
 {
-   if (OemString->Buffer != NULL)
-   {
-      RtlpFreeStringMemory(OemString->Buffer, TAG_OSTR);
+   PAGED_CODE_RTL();
 
-      OemString->Buffer = NULL;
-      OemString->Length = 0;
-      OemString->MaximumLength = 0;
-   }
+   if (OemString->Buffer) RtlpFreeStringMemory(OemString->Buffer, TAG_OSTR);
 }
 
-
 /*
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlFreeUnicodeString(IN PUNICODE_STRING UnicodeString)
 {
-   if (UnicodeString->Buffer != NULL)
+    PAGED_CODE_RTL();
+
+    if (UnicodeString->Buffer)
+    {
+        RtlpFreeStringMemory(UnicodeString->Buffer, TAG_USTR);
+        RtlZeroMemory(UnicodeString, sizeof(UNICODE_STRING));
+    }
+}
+
+
+/*
+ * @implemented
+ * 
+ * NOTES
+ *  Check the oem-string to match the uincoded-string.
+ *
+ *  Functions who convert unicode strings to oem strings will set a DefaultChar from 
+ *  the OemCodepage when the character are unknown. So check it against the unicode string
+ *  and return false when the unicode string not contain an TransDefaultChar.
+ */
+BOOLEAN
+NTAPI
+RtlpDidUnicodeToOemWork(IN PCUNICODE_STRING UnicodeString,
+                        IN POEM_STRING OemString)
+{
+   ULONG i = 0;
+
+   /* Go through all characters of a string */
+   while (i < OemString->Length)
    {
-      RtlpFreeStringMemory(UnicodeString->Buffer, TAG_USTR);
+       /* Check if it got translated into '?', but source char
+          wasn't '?' equivalent */
+       if ((OemString->Buffer[i] != 0) &&
+           (OemString->Buffer[i] == NlsOemDefaultChar) &&
+           (UnicodeString->Buffer[i] != NlsUnicodeDefaultChar))
+       {
+           /* Yes, it means unmappable characters were found */
+           return FALSE;
+       }
 
-      UnicodeString->Buffer = NULL;
-      UnicodeString->Length = 0;
-      UnicodeString->MaximumLength = 0;
+       /* Move to the next char */
+       i++;
    }
+
+   /* All chars were translated successfuly */
+   return TRUE;
 }
 
 /*
 * @unimplemented
 */
 BOOLEAN
-STDCALL
-RtlIsValidOemCharacter (
-       IN PWCHAR Char
-       )
+NTAPI
+RtlIsValidOemCharacter(IN PWCHAR Char)
 {
-       UNIMPLEMENTED;
-       return FALSE;
+    UNIMPLEMENTED;
+    return FALSE;
 }
 
 /*
@@ -371,28 +455,51 @@ RtlIsValidOemCharacter (
  *  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)
 {
-   ULONG DestSize;
+    ULONG DestSize;
 
-   if (SourceString == NULL)
-   {
-      DestinationString->Length = 0;
-      DestinationString->MaximumLength = 0;
-   }
-   else
-   {
-      DestSize = strlen ((const char *)SourceString);
-      DestinationString->Length = DestSize;
-      DestinationString->MaximumLength = DestSize + sizeof(CHAR);
-   }
-   DestinationString->Buffer = (PCHAR)SourceString;
+    if(SourceString)
+    {
+        DestSize = strlen(SourceString);
+        DestinationString->Length = (USHORT)DestSize;
+        DestinationString->MaximumLength = (USHORT)DestSize + sizeof(CHAR);
+    }
+    else
+    {
+        DestinationString->Length = 0;
+        DestinationString->MaximumLength = 0;
+    }
+
+    DestinationString->Buffer = (PCHAR)SourceString;
 }
 
+NTSTATUS
+NTAPI
+RtlInitAnsiStringEx(IN OUT PANSI_STRING DestinationString,
+                    IN PCSZ SourceString)
+{
+    ULONG DestSize;
+
+    if(SourceString)
+    {
+        DestSize = strlen(SourceString);
+        if (DestSize >= 0xFFFF) return STATUS_NAME_TOO_LONG;
+        DestinationString->Length = (USHORT)DestSize;
+        DestinationString->MaximumLength = (USHORT)DestSize + sizeof(CHAR);
+    }
+    else
+    {
+        DestinationString->Length = 0;
+        DestinationString->MaximumLength = 0;
+    }
 
+    DestinationString->Buffer = (PCHAR)SourceString;
+    return STATUS_SUCCESS;
 
+}
 /*
  * @implemented
  *
@@ -400,28 +507,14 @@ 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)
 {
-   ULONG DestSize;
-
-   if (SourceString == NULL)
-   {
-      DestinationString->Length = 0;
-      DestinationString->MaximumLength = 0;
-   }
-   else
-   {
-      DestSize = strlen((const char *)SourceString);
-      DestinationString->Length = DestSize;
-      DestinationString->MaximumLength = DestSize + sizeof(CHAR);
-   }
-   DestinationString->Buffer = (PCHAR)SourceString;
+    RtlInitAnsiString(DestinationString, SourceString);
 }
 
-
 /*
  * @implemented
  *
@@ -429,57 +522,52 @@ 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)
 {
-   ULONG DestSize;
+    ULONG DestSize;
 
-   DPRINT("RtlInitUnicodeString(DestinationString 0x%p, SourceString 0x%p)\n",
-          DestinationString,
-          SourceString);
+    if(SourceString)
+    {
+        DestSize = wcslen(SourceString) * sizeof(WCHAR);
+        DestinationString->Length = (USHORT)DestSize;
+        DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR);
+    }
+    else
+    {
+        DestinationString->Length = 0;
+        DestinationString->MaximumLength = 0;
+    }
 
-   if (SourceString == NULL)
-   {
-      DestinationString->Length = 0;
-      DestinationString->MaximumLength = 0;
-   }
-   else
-   {
-      DestSize = wcslen((PWSTR)SourceString) * sizeof(WCHAR);
-      DestinationString->Length = DestSize;
-      DestinationString->MaximumLength = DestSize + sizeof(WCHAR);
-   }
-   DestinationString->Buffer = (PWSTR)SourceString;
+    DestinationString->Buffer = (PWCHAR)SourceString;
 }
 
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS
+NTAPI
 RtlInitUnicodeStringEx(OUT PUNICODE_STRING DestinationString,
                        IN PCWSTR SourceString)
 {
-   ULONG Length;
-
-   if (SourceString != NULL)
-   {
-      Length = wcslen(SourceString) * sizeof(WCHAR);
-      if (Length > 0xFFFC)
-         return STATUS_NAME_TOO_LONG;
+    ULONG DestSize;
 
-      DestinationString->Length = Length;
-      DestinationString->MaximumLength = Length + sizeof(WCHAR);
-      DestinationString->Buffer = (PWSTR)SourceString;
-   }
-   else
-   {
-      DestinationString->Length = 0;
-      DestinationString->MaximumLength = 0;
-      DestinationString->Buffer = NULL;
-   }
+    if(SourceString)
+    {
+        DestSize = wcslen(SourceString) * sizeof(WCHAR);
+        if (DestSize >= 0xFFFC) return STATUS_NAME_TOO_LONG;
+        DestinationString->Length = (USHORT)DestSize;
+        DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR);
+    }
+    else
+    {
+        DestinationString->Length = 0;
+        DestinationString->MaximumLength = 0;
+    }
 
-   return STATUS_SUCCESS;
+    DestinationString->Buffer = (PWCHAR)SourceString;
+    return STATUS_SUCCESS;
 }
 
 /*
@@ -490,62 +578,55 @@ RtlInitUnicodeStringEx(OUT PUNICODE_STRING DestinationString,
  *  Str is nullterminated when length allowes it.
  *  When str fits exactly in length characters the nullterm is ommitted.
  */
-NTSTATUS
-STDCALL
-RtlIntegerToChar(
-   IN ULONG Value,
-   IN ULONG Base,
-   IN ULONG Length,
-   IN OUT PCHAR String)
+NTSTATUS NTAPI RtlIntegerToChar(
+    ULONG value,   /* [I] Value to be converted */
+    ULONG base,    /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
+    ULONG length,  /* [I] Length of the str buffer in bytes */
+    PCHAR str)     /* [O] Destination for the converted value */
 {
-   ULONG Radix;
-   CHAR  temp[33];
-   ULONG v = Value;
-   ULONG i;
-   PCHAR tp;
-   PCHAR sp;
-
-   Radix = Base;
-   if (Radix == 0)
-      Radix = 10;
-
-   if ((Radix != 2) && (Radix != 8) &&
-       (Radix != 10) && (Radix != 16))
-   {
-      return STATUS_INVALID_PARAMETER;
-   }
-
-   tp = temp;
-   while (v || tp == temp)
-   {
-      i = v % Radix;
-      v = v / Radix;
-      if (i < 10)
-         *tp = i + '0';
-      else
-         *tp = i + 'a' - 10;
-      tp++;
-   }
-
-   if ((ULONG)((ULONG_PTR)tp - (ULONG_PTR)temp) >= Length)
-   {
-      return STATUS_BUFFER_TOO_SMALL;
-   }
+    CHAR buffer[33];
+    PCHAR pos;
+    CHAR digit;
+    ULONG len;
 
-   sp = String;
-   while (tp > temp)
-      *sp++ = *--tp;
-   *sp = 0;
+    if (base == 0) {
+    base = 10;
+    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
+    return STATUS_INVALID_PARAMETER;
+    } /* if */
 
-   return STATUS_SUCCESS;
+    pos = &buffer[32];
+    *pos = '\0';
+
+    do {
+    pos--;
+    digit = value % base;
+    value = value / base;
+    if (digit < 10) {
+        *pos = '0' + digit;
+    } else {
+        *pos = 'A' + digit - 10;
+    } /* if */
+    } while (value != 0L);
+
+    len = &buffer[32] - pos;
+    if (len > length) {
+    return STATUS_BUFFER_OVERFLOW;
+    } else if (str == NULL) {
+    return STATUS_ACCESS_VIOLATION;
+    } else if (len == length) {
+    memcpy(str, pos, len);
+    } else {
+    memcpy(str, pos, len + 1);
+    } /* if */
+    return STATUS_SUCCESS;
 }
 
-
 /*
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlIntegerToUnicode(
     IN ULONG Value,
     IN ULONG Base  OPTIONAL,
@@ -595,77 +676,62 @@ RtlIntegerToUnicode(
    return STATUS_SUCCESS;
 }
 
-
-
 /*
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlIntegerToUnicodeString(
-   IN ULONG  Value,
-   IN ULONG  Base, /* optional */
+   IN ULONG Value,
+   IN ULONG Base OPTIONAL,
    IN OUT PUNICODE_STRING String)
 {
-   ANSI_STRING AnsiString;
-   CHAR Buffer[33];
-   NTSTATUS Status;
-
-   Status = RtlIntegerToChar (Value,
-                              Base,
-                              sizeof(Buffer),
-                              Buffer);
-   if (NT_SUCCESS(Status))
-   {
-      AnsiString.Buffer = Buffer;
-      AnsiString.Length = strlen (Buffer);
-      AnsiString.MaximumLength = sizeof(Buffer);
+    ANSI_STRING AnsiString;
+    CHAR Buffer[33];
+    NTSTATUS Status;
 
-      Status = RtlAnsiStringToUnicodeString (String,
-                                             &AnsiString,
-                                             FALSE);
-   }
+    Status = RtlIntegerToChar(Value, Base, sizeof(Buffer), Buffer);
+    if (NT_SUCCESS(Status))
+    {
+        AnsiString.Buffer = Buffer;
+        AnsiString.Length = (USHORT)strlen(Buffer);
+        AnsiString.MaximumLength = sizeof(Buffer);
 
-   return Status;
-}
+        Status = RtlAnsiStringToUnicodeString(String, &AnsiString, FALSE);
+    }
 
+    return Status;
+}
 
 /*
-* @implemented
-*/
+ * @implemented
+ */
 NTSTATUS
-STDCALL
+NTAPI
 RtlInt64ToUnicodeString (
-       IN ULONGLONG Value,
-       IN ULONG Base OPTIONAL,
-       IN OUT PUNICODE_STRING String
-       )
+    IN ULONGLONG Value,
+    IN ULONG Base OPTIONAL,
+    IN OUT PUNICODE_STRING String)
 {
-   LARGE_INTEGER LargeInt;
-   ANSI_STRING AnsiString;
-   CHAR Buffer[33];
-   NTSTATUS Status;
-
-   LargeInt.QuadPart = Value;
-
-   Status = RtlLargeIntegerToChar (&LargeInt,
-                                   Base,
-                                   sizeof(Buffer),
-                                   Buffer);
-   if (NT_SUCCESS(Status))
-   {
-      AnsiString.Buffer = Buffer;
-      AnsiString.Length = strlen (Buffer);
-      AnsiString.MaximumLength = sizeof(Buffer);
+    LARGE_INTEGER LargeInt;
+    ANSI_STRING AnsiString;
+    CHAR Buffer[65];
+    NTSTATUS Status;
 
-      Status = RtlAnsiStringToUnicodeString (String,
-                                             &AnsiString,
-                                             FALSE);
-   }
+    LargeInt.QuadPart = Value;
 
-   return Status;
-}
+    Status = RtlLargeIntegerToChar(&LargeInt, Base, sizeof(Buffer), Buffer);
+    if (NT_SUCCESS(Status))
+    {
+        AnsiString.Buffer = Buffer;
+        AnsiString.Length = (USHORT)strlen(Buffer);
+        AnsiString.MaximumLength = sizeof(Buffer);
+
+        Status = RtlAnsiStringToUnicodeString(String, &AnsiString, FALSE);
+    }
 
+    return Status;
+}
 
 /*
  * @implemented
@@ -674,7 +740,7 @@ RtlInt64ToUnicodeString (
  *  TRUE if String2 contains String1 as a prefix.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlPrefixString(
    PANSI_STRING String1,
    PANSI_STRING String2,
@@ -714,7 +780,6 @@ RtlPrefixString(
    return FALSE;
 }
 
-
 /*
  * @implemented
  *
@@ -722,7 +787,7 @@ RtlPrefixString(
  *  TRUE if String2 contains String1 as a prefix.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlPrefixUnicodeString(
    PCUNICODE_STRING String1,
    PCUNICODE_STRING String2,
@@ -762,119 +827,88 @@ RtlPrefixUnicodeString(
    }
    return FALSE;
 }
-
-/**************************************************************************
- *      RtlUnicodeStringToInteger (NTDLL.@)
+/*
  * @implemented
- * Converts an unicode 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.
- *
- *  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(
-    PCUNICODE_STRING str, /* [I] Unicode string to be converted */
+NTAPI
+RtlUnicodeStringToInteger(const UNICODE_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 */
+    ULONG *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 (CharsRemaining >= 1 && *lpwstr <= ' ') {
+    lpwstr++;
+    CharsRemaining--;
     } /* while */
 
     if (CharsRemaining >= 1) {
-       if (*lpwstr == L'+') {
-           lpwstr++;
-           CharsRemaining--;
-       } else if (*lpwstr == L'-') {
-           bMinus = 1;
-           lpwstr++;
-           CharsRemaining--;
-       } /* if */
+    if (*lpwstr == '+') {
+        lpwstr++;
+        CharsRemaining--;
+    } else if (*lpwstr == '-') {
+        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 (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 */
+    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
+    return STATUS_INVALID_PARAMETER;
     } /* if */
 
     if (value == NULL) {
-       return STATUS_ACCESS_VIOLATION;
+    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--;
+    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;
+    } else {
+        digit = -1;
+    } /* if */
+    if (digit < 0 || digit >= base) {
+        *value = bMinus ? -RunningTotal : RunningTotal;
+        return STATUS_SUCCESS;
+    } /* if */
+
+    RunningTotal = RunningTotal * base + digit;
+    lpwstr++;
+    CharsRemaining--;
     } /* while */
 
     *value = bMinus ? -RunningTotal : RunningTotal;
     return STATUS_SUCCESS;
 }
 
-
 /*
  * @implemented
  *
@@ -882,74 +916,75 @@ RtlUnicodeStringToInteger(
  *  Bytes necessary for the conversion including nullterm.
  */
 ULONG
-STDCALL
-RtlxUnicodeStringToOemSize(
-   IN PCUNICODE_STRING UnicodeString)
+NTAPI
+RtlxUnicodeStringToOemSize(IN PCUNICODE_STRING UnicodeString)
 {
-   ULONG Size;
+    ULONG Size;
 
-   RtlUnicodeToMultiByteSize (&Size,
+    /* Convert the Unicode String to Mb Size */
+    RtlUnicodeToMultiByteSize(&Size,
                               UnicodeString->Buffer,
                               UnicodeString->Length);
 
-   return Size+1; //NB: incl. nullterm
+    /* Return the size + the null char */
+    return (Size + sizeof(CHAR));
 }
 
 /*
  * @implemented
  *
-
  * NOTES
  *  This function always writes a terminating '\0'.
  *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUnicodeStringToAnsiString(
    IN OUT PANSI_STRING AnsiDest,
    IN PCUNICODE_STRING UniSource,
    IN BOOLEAN AllocateDestinationString)
 {
-   NTSTATUS Status = STATUS_SUCCESS;
-   ULONG Length; /* including nullterm */
+    NTSTATUS Status = STATUS_SUCCESS;
+    NTSTATUS RealStatus;
+    ULONG Length;
+    ULONG Index;
 
-   Length = RtlUnicodeStringToAnsiSize(UniSource);
-   AnsiDest->Length = Length - sizeof(CHAR);
+    PAGED_CODE_RTL();
 
-   if (AllocateDestinationString)
-   {
-      AnsiDest->Buffer = RtlpAllocateStringMemory(Length, TAG_ASTR);
-      if (AnsiDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
+    Length = RtlUnicodeStringToAnsiSize(UniSource);
+    if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
-      AnsiDest->MaximumLength = Length;
-   }
-   else if (AnsiDest->MaximumLength == 0)
-   {
-      return STATUS_BUFFER_TOO_SMALL;
-   }
-   else if (Length > AnsiDest->MaximumLength)
-   {
-      /* make room for nullterm */
-      AnsiDest->Length = AnsiDest->MaximumLength - sizeof(CHAR);
-   }
+    AnsiDest->Length = (USHORT)Length - sizeof(CHAR);
 
-   Status = RtlUnicodeToMultiByteN (AnsiDest->Buffer,
-                                    AnsiDest->Length,
-                                    NULL,
-                                    UniSource->Buffer,
-                                    UniSource->Length);
+    if (AllocateDestinationString)
+    {
+        AnsiDest->Buffer = RtlpAllocateStringMemory(Length, TAG_ASTR);
+        AnsiDest->MaximumLength = Length;
+        if (!AnsiDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (AnsiDest->Length >= AnsiDest->MaximumLength)
+    {
+        if (!AnsiDest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
 
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(AnsiDest->Buffer, TAG_ASTR);
-      return Status;
-   }
+        Status = STATUS_BUFFER_OVERFLOW;
+        AnsiDest->Length = AnsiDest->MaximumLength - 1;
+    }
 
-   AnsiDest->Buffer[AnsiDest->Length] = 0;
-   return Status;
-}
+    RealStatus = RtlUnicodeToMultiByteN(AnsiDest->Buffer,
+                                        AnsiDest->Length,
+                                        &Index,
+                                        UniSource->Buffer,
+                                        UniSource->Length);
 
+    if (!NT_SUCCESS(RealStatus) && AllocateDestinationString)
+    {
+        RtlpFreeStringMemory(AnsiDest->Buffer, TAG_ASTR);
+        return RealStatus;
+    }
+
+    AnsiDest->Buffer[Index] = ANSI_NULL;
+    return Status;
+}
 
 /*
  * @implemented
@@ -959,56 +994,51 @@ 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,
    IN BOOLEAN AllocateDestinationString)
 {
-   NTSTATUS Status;
-   ULONG Length; /* including nullterm */
+    NTSTATUS Status;
+    ULONG Length;
+    ULONG Index;
 
-   Length = RtlOemStringToUnicodeSize(OemSource);
-   if (Length > 0xffff)
-      return STATUS_INVALID_PARAMETER_2;
+    PAGED_CODE_RTL();
 
-   UniDest->Length = (WORD)(Length - sizeof(WCHAR));
+    Length = RtlOemStringToUnicodeSize(OemSource);
+    if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
-   if (AllocateDestinationString)
-   {
-      UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
-      if (UniDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
-
-      UniDest->MaximumLength = Length;
-   }
-   else if (Length > UniDest->MaximumLength)
-   {
-      DPRINT("STATUS_BUFFER_TOO_SMALL\n");
-      return STATUS_BUFFER_TOO_SMALL;
-   }
+    UniDest->Length = (USHORT)Length - sizeof(WCHAR);
 
-   /* FIXME: Do we need this????? -Gunnar */
-   RtlZeroMemory (UniDest->Buffer,
-                  UniDest->Length);
+    if (AllocateDestinationString)
+    {
+        UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
+        UniDest->MaximumLength = Length;
+        if (!UniDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (UniDest->Length >= UniDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
 
-   Status = RtlOemToUnicodeN (UniDest->Buffer,
+    Status = RtlOemToUnicodeN(UniDest->Buffer,
                               UniDest->Length,
-                              NULL,
+                              &Index,
                               OemSource->Buffer,
                               OemSource->Length);
 
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
-      return Status;
-   }
+    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    {
+        RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
+        UniDest->Buffer = NULL;
+        return Status;
+    }
 
-   UniDest->Buffer[UniDest->Length / sizeof(WCHAR)] = 0;
-   return STATUS_SUCCESS;
+    UniDest->Buffer[Index / sizeof(WCHAR)] = UNICODE_NULL;
+    return Status;
 }
 
-
 /*
  * @implemented
  *
@@ -1016,114 +1046,161 @@ RtlOemStringToUnicodeString(
  *   This function always '\0' terminates the string returned.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUnicodeStringToOemString(
    IN OUT POEM_STRING OemDest,
    IN PCUNICODE_STRING UniSource,
    IN BOOLEAN  AllocateDestinationString)
 {
-   NTSTATUS Status = STATUS_SUCCESS;
-   ULONG Length; //including nullterm
+    NTSTATUS Status;
+    ULONG Length;
+    ULONG Index;
 
-   Length = RtlUnicodeStringToAnsiSize(UniSource);
-   if (Length > 0x0000FFFF)
-      return STATUS_INVALID_PARAMETER_2;
+    PAGED_CODE_RTL();
 
-   OemDest->Length = (WORD)(Length - sizeof(CHAR));
+    Length = RtlUnicodeStringToOemSize(UniSource);
+    if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
-   if (AllocateDestinationString)
-   {
-      OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
-      if (OemDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
+    OemDest->Length = (USHORT)Length - sizeof(CHAR);
 
-      OemDest->MaximumLength = Length;
-   }
-   else if (OemDest->MaximumLength == 0)
-   {
-      return STATUS_BUFFER_TOO_SMALL;
-   }
-   else if (Length > OemDest->MaximumLength)
-   {
-      //make room for nullterm
-      OemDest->Length = OemDest->MaximumLength - sizeof(CHAR);
-   }
+    if (AllocateDestinationString)
+    {
+        OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
+        OemDest->MaximumLength = Length;
+        if (!OemDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (OemDest->Length >= OemDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
 
-   Status = RtlUnicodeToOemN (OemDest->Buffer,
+    Status = RtlUnicodeToOemN(OemDest->Buffer,
                               OemDest->Length,
-                              NULL,
+                              &Index,
                               UniSource->Buffer,
                               UniSource->Length);
 
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
-      return Status;
-   }
+    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    {
+        RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
+        OemDest->Buffer = NULL;
+        return Status;
+    }
 
-   OemDest->Buffer[OemDest->Length] = 0;
-   return Status;
+    OemDest->Buffer[Index] = ANSI_NULL;
+    return Status;
 }
 
 #define ITU_IMPLEMENTED_TESTS (IS_TEXT_UNICODE_ODD_LENGTH|IS_TEXT_UNICODE_SIGNATURE)
 
-
 /*
  * @implemented
  *
  * RETURNS
  *  The length of the string if all tests were passed, 0 otherwise.
  */
-ULONG STDCALL
-RtlIsTextUnicode (PVOID Buffer,
-                  ULONG Length,
-                  ULONG *Flags)
+BOOLEAN
+NTAPI
+RtlIsTextUnicode( PVOID buf, INT len, INT *pf )
 {
-   PWSTR s = Buffer;
-   ULONG in_flags = (ULONG)-1;
-   ULONG out_flags = 0;
-
-   if (Length == 0)
-      goto done;
-
-   if (Flags != 0)
-      in_flags = *Flags;
-
-   /*
-    * Apply various tests to the text string. According to the
-    * docs, each test "passed" sets the corresponding flag in
-    * the output flags. But some of the tests are mutually
-    * exclusive, so I don't see how you could pass all tests ...
-    */
-
-   /* Check for an odd length ... pass if even. */
-   if (!(Length & 1))
-      out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
-
-   /* Check for the BOM (byte order mark). */
-   if (*s == 0xFEFF)
-      out_flags |= IS_TEXT_UNICODE_SIGNATURE;
+    static const WCHAR std_control_chars[] = {'\r','\n','\t',' ',0x3000,0};
+    static const WCHAR byterev_control_chars[] = {0x0d00,0x0a00,0x0900,0x2000,0};
+    const WCHAR *s = buf;
+    int i;
+    unsigned int flags = MAXULONG, out_flags = 0;
 
-#if 0
-   /* Check for the reverse BOM (byte order mark). */
-   if (*s == 0xFFFE)
-      out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
-#endif
+    if (len < sizeof(WCHAR))
+    {
+        /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
+        if (pf) *pf = 0;
+        return FALSE;
+    }
+    if (pf)
+        flags = *pf;
+    /*
+     * Apply various tests to the text string. According to the
+     * docs, each test "passed" sets the corresponding flag in
+     * the output flags. But some of the tests are mutually
+     * exclusive, so I don't see how you could pass all tests ...
+     */
+
+    /* Check for an odd length ... pass if even. */
+    if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
+
+    if (((char *)buf)[len - 1] == 0)
+        len--;  /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES  */
+
+    len /= sizeof(WCHAR);
+    /* Windows only checks the first 256 characters */
+    if (len > 256) len = 256;
+
+    /* Check for the special byte order unicode marks. */
+    if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
+    if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
+
+    /* apply some statistical analysis */
+    if (flags & IS_TEXT_UNICODE_STATISTICS)
+    {
+        int stats = 0;
+        /* FIXME: checks only for ASCII characters in the unicode stream */
+        for (i = 0; i < len; i++)
+        {
+            if (s[i] <= 255) stats++;
+        }
+        if (stats > len / 2)
+            out_flags |= IS_TEXT_UNICODE_STATISTICS;
+    }
 
-   /* FIXME: Add more tests */
+    /* Check for unicode NULL chars */
+    if (flags & IS_TEXT_UNICODE_NULL_BYTES)
+    {
+        for (i = 0; i < len; i++)
+        {
+            if (!(s[i] & 0xff) || !(s[i] >> 8))
+            {
+                out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
+                break;
+            }
+        }
+    }
 
-   /*
-    * Check whether the string passed all of the tests.
-    */
-   in_flags &= ITU_IMPLEMENTED_TESTS;
-   if ((out_flags & in_flags) != in_flags)
-      Length = 0;
+    if (flags & IS_TEXT_UNICODE_CONTROLS)
+    {
+        for (i = 0; i < len; i++)
+        {
+            if (strchrW(std_control_chars, s[i]))
+            {
+                out_flags |= IS_TEXT_UNICODE_CONTROLS;
+                break;
+            }
+        }
+    }
 
-done:
-   if (Flags != 0)
-      *Flags = out_flags;
+    if (flags & IS_TEXT_UNICODE_REVERSE_CONTROLS)
+    {
+        for (i = 0; i < len; i++)
+        {
+            if (strchrW(byterev_control_chars, s[i]))
+            {
+                out_flags |= IS_TEXT_UNICODE_REVERSE_CONTROLS;
+                break;
+            }
+        }
+    }
 
-   return Length;
+    if (pf)
+    {
+        out_flags &= *pf;
+        *pf = out_flags;
+    }
+    /* check for flags that indicate it's definitely not valid Unicode */
+    if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
+    /* now check for invalid ASCII, and assume Unicode if so */
+    if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
+    /* now check for Unicode flags */
+    if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
+    /* no flags set */
+    return FALSE;
 }
 
 
@@ -1135,47 +1212,62 @@ 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,
    IN BOOLEAN AllocateDestinationString)
 {
-   NTSTATUS Status;
-   ULONG Length; /* excluding nullterm */
+    NTSTATUS Status;
+    ULONG Length;
+    ULONG Index;
 
-   Length = RtlOemStringToCountedUnicodeSize(OemSource);
-   if (Length > 65535)
-      return STATUS_INVALID_PARAMETER_2;
+    PAGED_CODE_RTL();
 
-   if (AllocateDestinationString == TRUE)
-   {
-      UniDest->Buffer = RtlpAllocateStringMemory (Length, TAG_USTR);
-      if (UniDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
+    /* Calculate size of the string */
+    Length = RtlOemStringToCountedUnicodeSize(OemSource);
 
-      UniDest->MaximumLength = Length;
-   }
-   else if (Length > UniDest->MaximumLength)
-   {
-      return STATUS_BUFFER_TOO_SMALL;
-   }
+    /* If it's 0 then zero out dest string and return */
+    if (!Length)
+    {
+        RtlZeroMemory(UniDest, sizeof(UNICODE_STRING));
+        return STATUS_SUCCESS;
+    }
 
-   UniDest->Length = Length;
+    /* Check if length is a sane value */
+    if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
-   Status = RtlOemToUnicodeN (UniDest->Buffer,
+    /* Store it in dest string */
+    UniDest->Length = (USHORT)Length;
+
+    /* If we're asked to alloc the string - do so */
+    if (AllocateDestinationString)
+    {
+        UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
+        UniDest->MaximumLength = Length;
+        if (!UniDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (UniDest->Length > UniDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
+
+    /* Do the conversion */
+    Status = RtlOemToUnicodeN(UniDest->Buffer,
                               UniDest->Length,
-                              NULL,
+                              &Index,
                               OemSource->Buffer,
                               OemSource->Length);
 
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
-      return Status;
-   }
+    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    {
+        /* Conversion failed, free dest string and return status code */
+        RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
+        UniDest->Buffer = NULL;
+        return Status;
+    }
 
-   return Status;
+    return STATUS_SUCCESS;
 }
 
 /*
@@ -1188,26 +1280,30 @@ RtlOemStringToCountedUnicodeString(
  *  The comparison is case insensitive.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlEqualComputerName(
    IN PUNICODE_STRING ComputerName1,
    IN PUNICODE_STRING ComputerName2)
 {
-   OEM_STRING OemString1;
-   OEM_STRING OemString2;
-   BOOLEAN Result = FALSE;
+    OEM_STRING OemString1;
+    OEM_STRING OemString2;
+    BOOLEAN Result = FALSE;
 
-   if (NT_SUCCESS(RtlUpcaseUnicodeStringToOemString( &OemString1, ComputerName1, TRUE )))
-   {
-      if (NT_SUCCESS(RtlUpcaseUnicodeStringToOemString( &OemString2, ComputerName2, TRUE )))
-      {
-         Result = RtlEqualString( &OemString1, &OemString2, TRUE );
-         RtlFreeOemString( &OemString2 );
-      }
-      RtlFreeOemString( &OemString1 );
-   }
+    if (NT_SUCCESS(RtlUpcaseUnicodeStringToOemString(&OemString1,
+                                                     ComputerName1,
+                                                     TRUE)))
+    {
+        if (NT_SUCCESS(RtlUpcaseUnicodeStringToOemString(&OemString2,
+                                                         ComputerName2,
+                                                         TRUE)))
+        {
+            Result = RtlEqualString(&OemString1, &OemString2, FALSE);
+            RtlFreeOemString(&OemString2);
+        }
+        RtlFreeOemString(&OemString1);
+    }
 
-   return Result;
+    return Result;
 }
 
 /*
@@ -1220,50 +1316,15 @@ RtlEqualComputerName(
  *  The comparison is case insensitive.
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlEqualDomainName (
    IN PUNICODE_STRING DomainName1,
    IN PUNICODE_STRING DomainName2
 )
 {
-   return RtlEqualComputerName(DomainName1, DomainName2);
+    return RtlEqualComputerName(DomainName1, DomainName2);
 }
 
-
-/*
- * @implemented
- */
-/*
-BOOLEAN
-STDCALL
-RtlEqualDomainName (
-   IN PUNICODE_STRING DomainName1,
-   IN PUNICODE_STRING DomainName2
-)
-{
-   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;
-
-}
-*/
-
 /*
  * @implemented
  *
@@ -1283,7 +1344,7 @@ RtlEqualDomainName (
  *  See RtlStringFromGUID.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlGUIDFromString(
    IN UNICODE_STRING *str,
    OUT GUID* guid
@@ -1298,7 +1359,7 @@ RtlGUIDFromString(
    /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
     * to memory:       DWORD... WORD WORD BYTES............
     */
-   while (i < 37)
+   while (i <= 37)
    {
       switch (i)
       {
@@ -1397,25 +1458,22 @@ RtlGUIDFromString(
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlEraseUnicodeString(
    IN PUNICODE_STRING String)
 {
-   if (String->Buffer != NULL &&
-       String->MaximumLength != 0)
-   {
-      RtlZeroMemory (String->Buffer,
-                     String->MaximumLength);
-
-      String->Length = 0;
-   }
+    if (String->Buffer && String->MaximumLength)
+    {
+        RtlZeroMemory(String->Buffer, String->MaximumLength);
+        String->Length = 0;
+    }
 }
 
 /*
 * @implemented
 */
 NTSTATUS
-STDCALL
+NTAPI
 RtlHashUnicodeString(
   IN CONST UNICODE_STRING *String,
   IN BOOLEAN CaseInSensitive,
@@ -1471,99 +1529,140 @@ 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,
    IN BOOLEAN AllocateDestinationString)
 {
-   NTSTATUS Status;
-   ULONG Length; //excluding nullterm
+    NTSTATUS Status;
+    ULONG Length;
+    ULONG Index;
 
-   Length = RtlUnicodeStringToCountedOemSize(UniSource);
-   if (Length > 0x0000FFFF)
-      return STATUS_INVALID_PARAMETER_2;
+    PAGED_CODE_RTL();
 
-   OemDest->Length = (WORD)(Length);
+    /* Calculate size of the string */
+    Length = RtlUnicodeStringToCountedOemSize(UniSource);
 
-   if (AllocateDestinationString)
-   {
-      OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
-      if (OemDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
+    /* If it's 0 then zero out dest string and return */
+    if (!Length)
+    {
+        RtlZeroMemory(OemDest, sizeof(OEM_STRING));
+        return STATUS_SUCCESS;
+    }
 
-      OemDest->MaximumLength = Length;
-   }
-   else if (OemDest->MaximumLength == 0)
-   {
-      return STATUS_BUFFER_TOO_SMALL;
-   }
-   else if (Length > OemDest->MaximumLength)
-   {
-      OemDest->Length = OemDest->MaximumLength;
-   }
+    /* Check if length is a sane value */
+    if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
+
+    /* Store it in dest string */
+    OemDest->Length = (USHORT)Length;
 
-   Status = RtlUnicodeToOemN (OemDest->Buffer,
+    /* If we're asked to alloc the string - do so */
+    if (AllocateDestinationString)
+    {
+        OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
+        OemDest->MaximumLength = Length;
+        if (!OemDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (OemDest->Length > OemDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
+
+    /* Do the conversion */
+    Status = RtlUnicodeToOemN(OemDest->Buffer,
                               OemDest->Length,
-                              NULL,
+                              &Index,
                               UniSource->Buffer,
                               UniSource->Length);
 
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
-   }
+    /* Check for unmapped character */
+    if (NT_SUCCESS(Status) && !RtlpDidUnicodeToOemWork(UniSource, OemDest))
+        Status = STATUS_UNMAPPABLE_CHARACTER;
 
-   return Status;
+    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    {
+        /* Conversion failed, free dest string and return status code */
+        RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
+        OemDest->Buffer = NULL;
+        return Status;
+    }
+
+    return Status;
 }
 
 /*
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlLargeIntegerToChar(
    IN PLARGE_INTEGER Value,
    IN ULONG  Base,
    IN ULONG  Length,
    IN OUT PCHAR  String)
 {
-   ULONG Radix;
-   CHAR  temp[65];
-   ULONGLONG v = Value->QuadPart;
-   ULONG i;
-   PCHAR tp;
-   PCHAR sp;
+    ULONGLONG Val = Value->QuadPart;
+    NTSTATUS Status = STATUS_SUCCESS;
+    CHAR Buffer[65];
+    CHAR Digit;
+    ULONG Len;
+    PCHAR Pos;
 
-   Radix = Base;
-   if (Radix == 0)
-      Radix = 10;
+    if (Base == 0) Base = 10;
 
-   if ((Radix != 2) && (Radix != 8) &&
-         (Radix != 10) && (Radix != 16))
-      return STATUS_INVALID_PARAMETER;
+    if ((Base != 2) && (Base != 8) &&
+        (Base != 10) && (Base != 16))
+    {
+        return STATUS_INVALID_PARAMETER;
+    }
 
-   tp = temp;
-   while (v || tp == temp)
-   {
-      i = v % Radix;
-      v = v / Radix;
-      if (i < 10)
-         *tp = i + '0';
-      else
-         *tp = i + 'a' - 10;
-      tp++;
-   }
+    Pos = &Buffer[64];
+    *Pos = '\0';
 
-   if ((ULONG)((ULONG_PTR)tp - (ULONG_PTR)temp) >= Length)
-      return STATUS_BUFFER_TOO_SMALL;
+    do
+    {
+        Pos--;
+        Digit = Val % Base;
+        Val = Val / Base;
+        if (Digit < 10)
+            *Pos = '0' + Digit;
+        else
+            *Pos = 'A' + Digit - 10;
+    }
+    while (Val != 0L);
 
-   sp = String;
-   while (tp > temp)
-      *sp++ = *--tp;
-   *sp = 0;
+    Len = &Buffer[64] - Pos;
 
-   return STATUS_SUCCESS;
+    if (Len > Length)
+        return STATUS_BUFFER_OVERFLOW;
+
+#if 1 /* It needs to be removed, when will probably use SEH in rtl */
+    if (String == NULL)
+    {
+        return STATUS_ACCESS_VIOLATION;
+    }
+#endif
+
+#if 0
+    _SEH2_TRY
+    {
+#endif
+        if (Len == Length)
+            RtlCopyMemory(String, Pos, Len);
+        else
+            RtlCopyMemory(String, Pos, Len + 1);
+#if 0
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        /* Get the error code */
+        Status = _SEH2_GetExceptionCode();
+    }
+    _SEH2_END;
+#endif
+
+    return Status;
 }
 
 /*
@@ -1574,39 +1673,36 @@ 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,
    IN BOOLEAN  AllocateDestinationString)
 {
-   ULONG i;
-   PWCHAR Src, Dest;
+    ULONG i, j;
 
-   if (AllocateDestinationString == TRUE)
-   {
-      UniDest->MaximumLength = UniSource->Length;
-      UniDest->Buffer = RtlpAllocateStringMemory(UniDest->MaximumLength, TAG_USTR);
-      if (UniDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
-   }
-   else if (UniSource->Length > UniDest->MaximumLength)
-   {
-      return STATUS_BUFFER_TOO_SMALL;
-   }
+    PAGED_CODE_RTL();
+
+    if (AllocateDestinationString == TRUE)
+    {
+        UniDest->MaximumLength = UniSource->Length;
+        UniDest->Buffer = RtlpAllocateStringMemory(UniDest->MaximumLength, TAG_USTR);
+        if (UniDest->Buffer == NULL) return STATUS_NO_MEMORY;
+    }
+    else if (UniSource->Length > UniDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
 
-   UniDest->Length = UniSource->Length;
+    j = UniSource->Length / sizeof(WCHAR);
 
-   Src = UniSource->Buffer;
-   Dest = UniDest->Buffer;
-   for (i = 0; i < UniSource->Length / sizeof(WCHAR); i++)
-   {
-      *Dest = RtlUpcaseUnicodeChar (*Src);
-      Dest++;
-      Src++;
-   }
+    for (i = 0; i < j; i++)
+    {
+        UniDest->Buffer[i] = RtlUpcaseUnicodeChar(UniSource->Buffer[i]);
+    }
 
-   return STATUS_SUCCESS;
+    UniDest->Length = UniSource->Length;
+    return STATUS_SUCCESS;
 }
 
 /*
@@ -1617,57 +1713,49 @@ RtlUpcaseUnicodeString(
  *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUpcaseUnicodeStringToAnsiString(
    IN OUT PANSI_STRING AnsiDest,
    IN PUNICODE_STRING UniSource,
    IN BOOLEAN  AllocateDestinationString)
 {
-   NTSTATUS Status;
-   ULONG Length; /* including nullterm */
-
-   Length = RtlUnicodeStringToAnsiSize(UniSource);
-   if (Length > 0x0000FFFF)
-      return STATUS_INVALID_PARAMETER_2;
+    NTSTATUS Status;
+    ULONG Length;
+    ULONG Index;
 
-   AnsiDest->Length = (WORD)(Length - sizeof(CHAR));
+    PAGED_CODE_RTL();
 
-   if (AllocateDestinationString)
-   {
-      AnsiDest->Buffer = RtlpAllocateStringMemory(Length, TAG_ASTR);
-      if (AnsiDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
+    Length = RtlUnicodeStringToAnsiSize(UniSource);
+    if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
-      AnsiDest->MaximumLength = Length;
-   }
-   else if (AnsiDest->MaximumLength == 0)
-   {
-      return STATUS_BUFFER_TOO_SMALL;
-   }
-   else if (Length > AnsiDest->MaximumLength)
-   {
-      /* make room for nullterm */
-      AnsiDest->Length = AnsiDest->MaximumLength - sizeof(CHAR);
-   }
+    AnsiDest->Length = (USHORT)Length - sizeof(CHAR);
 
-   /* FIXME: do we need this??????? -Gunnar */
-   RtlZeroMemory (AnsiDest->Buffer,
-                  AnsiDest->Length);
+    if (AllocateDestinationString)
+    {
+        AnsiDest->Buffer = RtlpAllocateStringMemory(Length, TAG_ASTR);
+        AnsiDest->MaximumLength = Length;
+        if (!AnsiDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (AnsiDest->Length >= AnsiDest->MaximumLength)
+    {
+        if (!AnsiDest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
+    }
 
-   Status = RtlUpcaseUnicodeToMultiByteN (AnsiDest->Buffer,
+    Status = RtlUpcaseUnicodeToMultiByteN(AnsiDest->Buffer,
                                           AnsiDest->Length,
-                                          NULL,
+                                          &Index,
                                           UniSource->Buffer,
                                           UniSource->Length);
 
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(AnsiDest->Buffer, TAG_ASTR);
-      return Status;
-   }
+    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    {
+        RtlpFreeStringMemory(AnsiDest->Buffer, TAG_ASTR);
+        AnsiDest->Buffer = NULL;
+        return Status;
+    }
 
-   AnsiDest->Buffer[AnsiDest->Length] = 0;
-   return Status;
+    AnsiDest->Buffer[Index] = ANSI_NULL;
+    return Status;
 }
 
 /*
@@ -1678,54 +1766,58 @@ RtlUpcaseUnicodeStringToAnsiString(
  *  It performs a partial copy if ansi is too small.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUpcaseUnicodeStringToCountedOemString(
    IN OUT POEM_STRING OemDest,
    IN PCUNICODE_STRING UniSource,
    IN BOOLEAN AllocateDestinationString)
 {
-   NTSTATUS Status;
-   ULONG Length; /* excluding nullterm */
+    NTSTATUS Status;
+    ULONG Length;
+    ULONG Index;
 
-   Length = RtlUnicodeStringToCountedOemSize(UniSource);
-   if (Length > 0x0000FFFF)
-      return(STATUS_INVALID_PARAMETER_2);
+    PAGED_CODE_RTL();
 
-   OemDest->Length = (WORD)(Length);
+    Length = RtlUnicodeStringToCountedOemSize(UniSource);
 
-   if (AllocateDestinationString)
-   {
-      OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
-      if (OemDest->Buffer == NULL)
-         return(STATUS_NO_MEMORY);
+    if (!Length)
+    {
+        RtlZeroMemory(OemDest, sizeof(OEM_STRING));
+    }
 
-      /* FIXME: Do we need this????? */
-      RtlZeroMemory (OemDest->Buffer, Length);
+    if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
-      OemDest->MaximumLength = (WORD)Length;
-   }
-   else if (OemDest->MaximumLength == 0)
-   {
-      return(STATUS_BUFFER_TOO_SMALL);
-   }
-   else if (Length > OemDest->MaximumLength)
-   {
-      OemDest->Length = OemDest->MaximumLength;
-   }
+    OemDest->Length = (USHORT)Length;
 
-   Status = RtlUpcaseUnicodeToOemN(OemDest->Buffer,
-                                   OemDest->Length,
-                                   NULL,
-                                   UniSource->Buffer,
-                                   UniSource->Length);
+    if (AllocateDestinationString)
+    {
+        OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
+        OemDest->MaximumLength = Length;
+        if (!OemDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (OemDest->Length > OemDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
 
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
-      return Status;
-   }
+    Status = RtlUpcaseUnicodeToOemN(OemDest->Buffer,
+                                    OemDest->Length,
+                                    &Index,
+                                    UniSource->Buffer,
+                                    UniSource->Length);
+
+    /* Check for unmapped characters */
+    if (NT_SUCCESS(Status) && !RtlpDidUnicodeToOemWork(UniSource, OemDest))
+        Status = STATUS_UNMAPPABLE_CHARACTER;
 
-   return Status;
+    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    {
+        RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
+        OemDest->Buffer = NULL;
+        return Status;
+    }
+
+    return Status;
 }
 
 /*
@@ -1735,57 +1827,53 @@ RtlUpcaseUnicodeStringToCountedOemString(
  *  It performs a partial copy if oem is too small.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlUpcaseUnicodeStringToOemString (
    IN OUT POEM_STRING OemDest,
    IN PCUNICODE_STRING UniSource,
-   IN BOOLEAN  AllocateDestinationString
-)
+   IN BOOLEAN  AllocateDestinationString)
 {
-   NTSTATUS Status;
-   ULONG Length; /* including nullterm */
-
-   Length = RtlUnicodeStringToAnsiSize(UniSource);
-   if (Length > 0x0000FFFF)
-      return STATUS_INVALID_PARAMETER_2;
+    NTSTATUS Status;
+    ULONG Length;
+    ULONG Index;
 
-   OemDest->Length = (WORD)(Length - sizeof(CHAR));
+    PAGED_CODE_RTL();
 
-   if (AllocateDestinationString)
-   {
-      OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
-      if (OemDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
+    Length = RtlUnicodeStringToOemSize(UniSource);
+    if (Length > MAXUSHORT) return STATUS_INVALID_PARAMETER_2;
 
-      /* FIXME: Do we need this???? */
-      RtlZeroMemory (OemDest->Buffer, Length);
+    OemDest->Length = (USHORT)Length - sizeof(CHAR);
 
-      OemDest->MaximumLength = (WORD)Length;
-   }
-   else if (OemDest->MaximumLength == 0)
-   {
-      return STATUS_BUFFER_OVERFLOW;
-   }
-   else if (Length > OemDest->MaximumLength)
-   {
-      /* make room for nullterm */
-      OemDest->Length = OemDest->MaximumLength - sizeof(CHAR);
-   }
+    if (AllocateDestinationString)
+    {
+        OemDest->Buffer = RtlpAllocateStringMemory(Length, TAG_OSTR);
+        OemDest->MaximumLength = Length;
+        if (!OemDest->Buffer) return STATUS_NO_MEMORY;
+    }
+    else if (OemDest->Length >= OemDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
 
-   Status = RtlUpcaseUnicodeToOemN (OemDest->Buffer,
+    Status = RtlUpcaseUnicodeToOemN(OemDest->Buffer,
                                     OemDest->Length,
-                                    NULL,
+                                    &Index,
                                     UniSource->Buffer,
                                     UniSource->Length);
 
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
-      return Status;
-   }
+    /* Check for unmapped characters */
+    if (NT_SUCCESS(Status) && !RtlpDidUnicodeToOemWork(UniSource, OemDest))
+        Status = STATUS_UNMAPPABLE_CHARACTER;
 
-   OemDest->Buffer[OemDest->Length] = 0;
-   return Status;
+    if (!NT_SUCCESS(Status) && AllocateDestinationString)
+    {
+        RtlpFreeStringMemory(OemDest->Buffer, TAG_OSTR);
+        OemDest->Buffer = NULL;
+        return Status;
+    }
+
+    OemDest->Buffer[Index] = ANSI_NULL;
+    return Status;
 }
 
 /*
@@ -1795,62 +1883,52 @@ RtlUpcaseUnicodeStringToOemString (
  *  Bytes calculated including nullterm
  */
 ULONG
-STDCALL
+NTAPI
 RtlxOemStringToUnicodeSize(IN PCOEM_STRING OemString)
 {
-   ULONG Size;
+    ULONG Size;
 
-   //this function returns size including nullterm
-   RtlMultiByteToUnicodeSize(&Size,
-                             OemString->Buffer,
-                             OemString->Length);
+    /* Convert the Mb String to Unicode Size */
+    RtlMultiByteToUnicodeSize(&Size,
+                              OemString->Buffer,
+                              OemString->Length);
 
-   return(Size);
+    /* Return the size + null-char */
+    return (Size + sizeof(WCHAR));
 }
 
-
-
 /*
  * @implemented
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlStringFromGUID (IN REFGUID Guid,
                    OUT PUNICODE_STRING GuidString)
 {
-   STATIC CONST PWCHAR Hex = L"0123456789ABCDEF";
-   WCHAR Buffer[40];
-   PWCHAR BufferPtr;
-   ULONG i;
-
-   if (Guid == NULL)
-   {
-      return STATUS_INVALID_PARAMETER;
-   }
-
-   swprintf (Buffer,
-             L"{%08lX-%04X-%04X-%02X%02X-",
+    /* Setup the string */
+    GuidString->Length = 38 * sizeof(WCHAR);
+    GuidString->MaximumLength = GuidString->Length + sizeof(UNICODE_NULL);
+    GuidString->Buffer = RtlpAllocateStringMemory(GuidString->MaximumLength,
+                                                  TAG_USTR);
+    if (!GuidString->Buffer) return STATUS_NO_MEMORY;
+
+    /* Now format the GUID */
+    swprintf(GuidString->Buffer,
+             L"{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
              Guid->Data1,
              Guid->Data2,
              Guid->Data3,
              Guid->Data4[0],
-             Guid->Data4[1]);
-   BufferPtr = Buffer + 25;
-
-   /* 6 hex bytes */
-   for (i = 2; i < 8; i++)
-   {
-      *BufferPtr++ = Hex[Guid->Data4[i] >> 4];
-      *BufferPtr++ = Hex[Guid->Data4[i] & 0xf];
-   }
-
-   *BufferPtr++ = L'}';
-   *BufferPtr++ = L'\0';
-
-   return RtlCreateUnicodeString (GuidString, Buffer);
+             Guid->Data4[1],
+             Guid->Data4[2],
+             Guid->Data4[3],
+             Guid->Data4[4],
+             Guid->Data4[5],
+             Guid->Data4[6],
+             Guid->Data4[7]);
+    return STATUS_SUCCESS;
 }
 
-
 /*
  * @implemented
  *
@@ -1858,26 +1936,25 @@ RtlStringFromGUID (IN REFGUID Guid,
  *  Bytes calculated including nullterm
  */
 ULONG
-STDCALL
-RtlxUnicodeStringToAnsiSize(
-   IN PCUNICODE_STRING UnicodeString)
+NTAPI
+RtlxUnicodeStringToAnsiSize(IN PCUNICODE_STRING UnicodeString)
 {
-   ULONG Size;
+    ULONG Size;
 
-   //this function return size without nullterm!
-   RtlUnicodeToMultiByteSize (&Size,
+    /* Convert the Unicode String to Mb Size */
+    RtlUnicodeToMultiByteSize(&Size,
                               UnicodeString->Buffer,
                               UnicodeString->Length);
 
-   return Size + sizeof(CHAR); //NB: incl. nullterm
+    /* Return the size + null-char */
+    return (Size + sizeof(CHAR));
 }
 
-
 /*
  * @implemented
  */
 LONG
-STDCALL
+NTAPI
 RtlCompareUnicodeString(
    IN PCUNICODE_STRING s1,
    IN PCUNICODE_STRING s2,
@@ -1903,63 +1980,77 @@ RtlCompareUnicodeString(
    return ret;
 }
 
-
-
 /*
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlCopyString(
    IN OUT PSTRING DestinationString,
-   IN PSTRING SourceString)
+   IN PSTRING SourceString OPTIONAL)
 {
-   ULONG copylen;
+    ULONG SourceLength;
+    PCHAR p1, p2;
 
-   if(SourceString == NULL)
-   {
-      DestinationString->Length = 0;
-      return;
-   }
-
-   copylen = min (DestinationString->MaximumLength,
-                  SourceString->Length);
+    /* Check if there was no source given */
+    if(!SourceString)
+    {
+        /* Simply return an empty string */
+        DestinationString->Length = 0;
+    }
+    else
+    {
+        /* Choose the smallest length */
+        SourceLength = min(DestinationString->MaximumLength,
+                           SourceString->Length);
 
-   memcpy(DestinationString->Buffer, SourceString->Buffer, copylen);
-   if (DestinationString->MaximumLength >= copylen + sizeof(CHAR))
-   {
-      DestinationString->Buffer[copylen] = 0;
-   }
-   DestinationString->Length = copylen;
-}
+        /* Set it */
+        DestinationString->Length = (USHORT)SourceLength;
 
+        /* Save the pointers to each buffer */
+        p1 = DestinationString->Buffer;
+        p2 = SourceString->Buffer;
 
+        /* Loop the buffer */
+        while (SourceLength)
+        {
+            /* Copy the character and move on */
+            *p1++ = * p2++;
+            SourceLength--;
+        }
+    }
+}
 
 /*
  * @implemented
  */
 VOID
-STDCALL
+NTAPI
 RtlCopyUnicodeString(
    IN OUT PUNICODE_STRING DestinationString,
    IN PCUNICODE_STRING SourceString)
 {
-   ULONG copylen;
+    ULONG SourceLength;
 
-   if (SourceString == NULL)
-   {
-      DestinationString->Length = 0;
-      return;
-   }
+    if(SourceString == NULL)
+    {
+        DestinationString->Length = 0;
+    }
+    else
+    {
+        SourceLength = min(DestinationString->MaximumLength,
+                           SourceString->Length);
+        DestinationString->Length = (USHORT)SourceLength;
 
-   copylen = min (DestinationString->MaximumLength,
-                  SourceString->Length);
-   memcpy(DestinationString->Buffer, SourceString->Buffer, copylen);
-   if (DestinationString->MaximumLength >= copylen + sizeof(WCHAR))
-   {
-     DestinationString->Buffer[copylen / sizeof(WCHAR)] = 0;
-   }
-   DestinationString->Length = copylen;
+        RtlCopyMemory(DestinationString->Buffer,
+                      SourceString->Buffer,
+                      SourceLength);
+
+        if (DestinationString->Length < DestinationString->MaximumLength)
+        {
+            DestinationString->Buffer[SourceLength / sizeof(WCHAR)] = UNICODE_NULL;
+        }
+    }
 }
 
 /*
@@ -1969,48 +2060,48 @@ RtlCopyUnicodeString(
  * Creates a nullterminated UNICODE_STRING
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlCreateUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCWSTR  Source)
 {
-   ULONG Length;
+    ULONG Length;
 
-   Length = (wcslen (Source) + 1) * sizeof(WCHAR);
-   UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
-   if (UniDest->Buffer == NULL)
-      return FALSE;
+    PAGED_CODE_RTL();
 
-   RtlCopyMemory (UniDest->Buffer,
-                  Source,
-                  Length);
+    Length = (wcslen(Source) + 1) * sizeof(WCHAR);
+    if (Length > 0xFFFE) return FALSE;
 
-   UniDest->MaximumLength = Length;
-   UniDest->Length = Length - sizeof (WCHAR);
+    UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
 
-   return TRUE;
+    if (UniDest->Buffer == NULL) return FALSE;
+
+    RtlCopyMemory(UniDest->Buffer, Source, Length);
+    UniDest->MaximumLength = (USHORT)Length;
+    UniDest->Length = Length - sizeof (WCHAR);
+
+    return TRUE;
 }
 
 /*
  * @implemented
  */
 BOOLEAN
-STDCALL
+NTAPI
 RtlCreateUnicodeStringFromAsciiz(
    OUT PUNICODE_STRING Destination,
-   IN PCSZ  Source)
+   IN PCSZ Source)
 {
-   ANSI_STRING AnsiString;
-   NTSTATUS Status;
+    ANSI_STRING AnsiString;
+    NTSTATUS Status;
 
-   RtlInitAnsiString (&AnsiString,
-                      Source);
+    RtlInitAnsiString(&AnsiString, Source);
 
-   Status = RtlAnsiStringToUnicodeString (Destination,
+    Status = RtlAnsiStringToUnicodeString(Destination,
                                           &AnsiString,
                                           TRUE);
 
-   return NT_SUCCESS(Status);
+    return NT_SUCCESS(Status);
 }
 
 /*
@@ -2021,52 +2112,49 @@ RtlCreateUnicodeStringFromAsciiz(
  *  might not be '\0' terminated.
  *  Dest->Length is only set upon success.
  */
-NTSTATUS STDCALL
+NTSTATUS
+NTAPI
 RtlDowncaseUnicodeString(
    IN OUT PUNICODE_STRING UniDest,
    IN PCUNICODE_STRING UniSource,
    IN BOOLEAN AllocateDestinationString)
 {
-   ULONG i;
-   PWCHAR Src, Dest;
+    ULONG i;
+    ULONG StopGap;
 
-   if (AllocateDestinationString)
-   {
-      UniDest->Buffer = RtlpAllocateStringMemory(UniSource->Length, TAG_USTR);
-      if (UniDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
-
-      UniDest->MaximumLength = UniSource->Length;
-   }
-   else if (UniSource->Length > UniDest->MaximumLength)
-   {
-      return STATUS_BUFFER_TOO_SMALL;
-   }
+    PAGED_CODE_RTL();
 
-   UniDest->Length = UniSource->Length;
+    if (AllocateDestinationString)
+    {
+        UniDest->MaximumLength = UniSource->Length;
+        UniDest->Buffer = RtlpAllocateStringMemory(UniSource->Length, TAG_USTR);
+        if (UniDest->Buffer == NULL) return STATUS_NO_MEMORY;
+    }
+    else if (UniSource->Length > UniDest->MaximumLength)
+    {
+        return STATUS_BUFFER_OVERFLOW;
+    }
 
-   Src = UniSource->Buffer;
-   Dest = UniDest->Buffer;
-   for (i=0; i < UniSource->Length / sizeof(WCHAR); i++)
-   {
-      if (*Src < L'A')
-      {
-         *Dest = *Src;
-      }
-      else if (*Src <= L'Z')
-      {
-         *Dest = (*Src + (L'a' - L'A'));
-      }
-      else
-      {
-         *Dest = RtlDowncaseUnicodeChar(*Src);
-      }
+    UniDest->Length = UniSource->Length;
+    StopGap = UniSource->Length / sizeof(WCHAR);
 
-      Dest++;
-      Src++;
-   }
+    for (i= 0 ; i < StopGap; i++)
+    {
+        if (UniSource->Buffer[i] < L'A')
+        {
+            UniDest->Buffer[i] = UniSource->Buffer[i];
+        }
+        else if (UniSource->Buffer[i] <= L'Z')
+        {
+            UniDest->Buffer[i] = (UniSource->Buffer[i] + (L'a' - L'A'));
+        }
+        else
+        {
+            UniDest->Buffer[i] = RtlDowncaseUnicodeChar(UniSource->Buffer[i]);
+        }
+    }
 
-   return STATUS_SUCCESS;
+    return STATUS_SUCCESS;
 }
 
 /*
@@ -2077,81 +2165,38 @@ RtlDowncaseUnicodeString(
  *  dest is '\0' terminated when the MaximumLength allowes it.
  *  When dest fits exactly in MaximumLength characters the '\0' is ommitted.
  */
-NTSTATUS STDCALL
+NTSTATUS
+NTAPI
 RtlAppendUnicodeToString(IN OUT PUNICODE_STRING Destination,
                          IN PCWSTR Source)
 {
-   ULONG slen;
-
-   slen = wcslen(Source) * sizeof(WCHAR);
-
-   if (Destination->Length + slen > Destination->MaximumLength)
-      return(STATUS_BUFFER_TOO_SMALL);
-
-   memcpy((char*)Destination->Buffer + Destination->Length, Source, slen);
-   Destination->Length += slen;
-   /* append terminating '\0' if enough space */
-   if( Destination->MaximumLength > Destination->Length )
-      Destination->Buffer[Destination->Length / sizeof(WCHAR)] = 0;
-
-   return(STATUS_SUCCESS);
-}
-
-/*
- * @implemented
- *
- * NOTES
- *  This function always writes a terminating '\0'.
- *  If the dest buffer is too small a partial copy is NOT performed!
- */
-NTSTATUS
-STDCALL
-RtlAnsiStringToUnicodeString(
-   IN OUT PUNICODE_STRING UniDest,
-   IN PANSI_STRING AnsiSource,
-   IN BOOLEAN AllocateDestinationString)
-{
-   NTSTATUS Status;
-   ULONG Length; //including nullterm
+    USHORT Length;
+    PWCHAR DestBuffer;
 
-   Length = RtlAnsiStringToUnicodeSize(AnsiSource);
-   if (Length > 0xffff)
-      return STATUS_INVALID_PARAMETER_2;
+    if (Source)
+    {
+        UNICODE_STRING UnicodeSource;
 
-   if (AllocateDestinationString == TRUE)
-   {
-      UniDest->Buffer = RtlpAllocateStringMemory(Length, TAG_USTR);
-      if (UniDest->Buffer == NULL)
-         return STATUS_NO_MEMORY;
+        RtlInitUnicodeString(&UnicodeSource, Source);
+        Length = UnicodeSource.Length;
 
-      UniDest->MaximumLength = Length;
-   }
-   else if (Length > UniDest->MaximumLength)
-   {
-      DPRINT("STATUS_BUFFER_TOO_SMALL\n");
-      return STATUS_BUFFER_TOO_SMALL;
-   }
-
-   UniDest->Length = Length - sizeof(WCHAR);
+        if (Destination->Length + Length > Destination->MaximumLength)
+        {
+            return STATUS_BUFFER_TOO_SMALL;
+        }
 
-   //FIXME: We don't need this??? -Gunnar
-   RtlZeroMemory (UniDest->Buffer,
-                  UniDest->Length);
+        DestBuffer = &Destination->Buffer[Destination->Length / sizeof(WCHAR)];
+        RtlMoveMemory(DestBuffer, Source, Length);
+        Destination->Length += Length;
 
-   Status = RtlMultiByteToUnicodeN (UniDest->Buffer,
-                                    UniDest->Length,
-                                    NULL,
-                                    AnsiSource->Buffer,
-                                    AnsiSource->Length);
-
-   if (!NT_SUCCESS(Status) && AllocateDestinationString)
-   {
-      RtlpFreeStringMemory(UniDest->Buffer, TAG_USTR);
-      return Status;
-   }
+        /* append terminating '\0' if enough space */
+        if(Destination->MaximumLength > Destination->Length)
+        {
+            DestBuffer[Length / sizeof(WCHAR)] = UNICODE_NULL;
+        }
+    }
 
-   UniDest->Buffer[UniDest->Length / sizeof(WCHAR)] = 0;
-   return Status;
+    return STATUS_SUCCESS;
 }
 
 /*
@@ -2162,60 +2207,51 @@ RtlAnsiStringToUnicodeString(
  *  dest is never '\0' terminated.
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlAppendAsciizToString(
    IN OUT   PSTRING  Destination,
    IN PCSZ  Source)
 {
-   ULONG Length;
-   PCHAR Ptr;
-
-   if (Source == NULL)
-      return STATUS_SUCCESS;
+    ULONG Length;
 
-   Length = strlen (Source);
-   if (Destination->Length + Length >= Destination->MaximumLength)
-      return STATUS_BUFFER_TOO_SMALL;
+    if (Source)
+    {
+        Length = (USHORT)strlen(Source);
 
-   Ptr = Destination->Buffer + Destination->Length;
-   memmove (Ptr,
-            Source,
-            Length);
-   Ptr += Length;
-   *Ptr = 0;
+        if (Destination->Length + Length > Destination->MaximumLength)
+        {
+            return STATUS_BUFFER_TOO_SMALL;
+        }
 
-   Destination->Length += Length;
+        RtlMoveMemory(&Destination->Buffer[Destination->Length], Source, Length);
+        Destination->Length += Length;
+    }
 
-   return STATUS_SUCCESS;
+    return STATUS_SUCCESS;
 }
 
-
 /*
  * @implemented
  */
-VOID STDCALL
+VOID
+NTAPI
 RtlUpperString(PSTRING DestinationString,
                PSTRING SourceString)
 {
-   ULONG Length;
-   ULONG i;
-   PCHAR Src;
-   PCHAR Dest;
+    ULONG Length;
+    PCHAR Src, Dest;
 
-   Length = min(SourceString->Length,
-                DestinationString->MaximumLength - 1);
+    Length = min(SourceString->Length,
+                 DestinationString->MaximumLength);
 
-   Src = SourceString->Buffer;
-   Dest = DestinationString->Buffer;
-   for (i = 0; i < Length; i++)
-   {
-      *Dest = RtlUpperChar(*Src);
-      Src++;
-      Dest++;
-   }
-   *Dest = 0;
-
-   DestinationString->Length = SourceString->Length;
+    Src = SourceString->Buffer;
+    Dest = DestinationString->Buffer;
+    DestinationString->Length = Length;
+    while (Length)
+    {
+        *Dest++ = RtlUpperChar(*Src++);
+        Length--;
+    }
 }
 
 /*
@@ -2225,18 +2261,24 @@ RtlUpperString(PSTRING DestinationString,
  *  See RtlpDuplicateUnicodeString
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlDuplicateUnicodeString(
    IN ULONG Flags,
    IN PCUNICODE_STRING SourceString,
    OUT PUNICODE_STRING DestinationString)
 {
-   if (SourceString == NULL || DestinationString == NULL)
-      return STATUS_INVALID_PARAMETER;
+   PAGED_CODE_RTL();
 
+    if (SourceString == NULL || DestinationString == NULL ||
+        SourceString->Length > SourceString->MaximumLength ||
+        (SourceString->Length == 0 && SourceString->MaximumLength > 0 && SourceString->Buffer == NULL) ||
+        Flags == RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING || Flags >= 4) {
+        return STATUS_INVALID_PARAMETER;
+    }
 
-   if ((SourceString->Length == 0) && 
-       (Flags != (RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE | 
+
+   if ((SourceString->Length == 0) &&
+       (Flags != (RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE |
                   RTL_DUPLICATE_UNICODE_STRING_ALLOCATE_NULL_STRING)))
    {
       DestinationString->Length = 0;
@@ -2268,9 +2310,9 @@ RtlDuplicateUnicodeString(
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS NTAPI
 RtlValidateUnicodeString(IN ULONG Flags,
-                         IN PUNICODE_STRING UnicodeString)
+                         IN PCUNICODE_STRING UnicodeString)
 {
   /* currently no flags are supported! */
   ASSERT(Flags == 0);
@@ -2293,4 +2335,92 @@ RtlValidateUnicodeString(IN ULONG Flags,
   }
 }
 
-/* EOF */
+NTSTATUS
+NTAPI
+RtlFindCharInUnicodeString(IN ULONG Flags,
+                           IN PUNICODE_STRING SearchString,
+                           IN PCUNICODE_STRING MatchString,
+                           OUT PUSHORT Position)
+{
+    int main_idx;
+    unsigned int search_idx;
+
+    switch (Flags)
+    {
+        case 0:
+        {
+            for (main_idx = 0; main_idx < SearchString->Length / sizeof(WCHAR); main_idx++)
+            {
+                for (search_idx = 0; search_idx < MatchString->Length / sizeof(WCHAR); search_idx++)
+                {
+                    if (SearchString->Buffer[main_idx] == MatchString->Buffer[search_idx])
+                    {
+                        *Position = (main_idx + 1) * sizeof(WCHAR);
+                        return STATUS_SUCCESS;
+                    }
+                }
+            }
+            *Position = 0;
+            return STATUS_NOT_FOUND;
+        }
+
+        case 1:
+        {
+            for (main_idx = SearchString->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--)
+            {
+                for (search_idx = 0; search_idx < MatchString->Length / sizeof(WCHAR); search_idx++)
+                {
+                    if (SearchString->Buffer[main_idx] == MatchString->Buffer[search_idx])
+                    {
+                        *Position = main_idx * sizeof(WCHAR);
+                        return STATUS_SUCCESS;
+                    }
+                }
+            }
+            *Position = 0;
+            return STATUS_NOT_FOUND;
+        }
+
+        case 2:
+        {
+            for (main_idx = 0; main_idx < SearchString->Length / sizeof(WCHAR); main_idx++)
+            {
+                search_idx = 0;
+                while (search_idx < MatchString->Length / sizeof(WCHAR) &&
+                       SearchString->Buffer[main_idx] != MatchString->Buffer[search_idx])
+                {
+                    search_idx++;
+                }
+                if (search_idx >= MatchString->Length / sizeof(WCHAR))
+                {
+                    *Position = (main_idx + 1) * sizeof(WCHAR);
+                    return STATUS_SUCCESS;
+                }
+            }
+            *Position = 0;
+            return STATUS_NOT_FOUND;
+        }
+
+        case 3:
+        {
+            for (main_idx = SearchString->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--)
+            {
+                search_idx = 0;
+                while (search_idx < MatchString->Length / sizeof(WCHAR) &&
+                       SearchString->Buffer[main_idx] != MatchString->Buffer[search_idx])
+                {
+                    search_idx++;
+                }
+                if (search_idx >= MatchString->Length / sizeof(WCHAR))
+                {
+                    *Position = main_idx * sizeof(WCHAR);
+                    return STATUS_SUCCESS;
+                }
+            }
+            *Position = 0;
+            return STATUS_NOT_FOUND;
+        }
+    } /* switch */
+
+    return STATUS_NOT_FOUND;
+}