- Fix size functions to return the correct results in all cases.
authorAlex Ionescu <aionescu@gmail.com>
Tue, 6 Sep 2005 19:47:06 +0000 (19:47 +0000)
committerAlex Ionescu <aionescu@gmail.com>
Tue, 6 Sep 2005 19:47:06 +0000 (19:47 +0000)
svn path=/trunk/; revision=17705

reactos/lib/rtl/nls.c
reactos/lib/rtl/unicode.c

index d55d8fc..322a945 100644 (file)
@@ -278,35 +278,50 @@ RtlMultiByteToUnicodeN(
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS
+STDCALL
 RtlMultiByteToUnicodeSize(PULONG UnicodeSize,
                           PCSTR MbString,
                           ULONG MbSize)
 {
-   ULONG Length;
-
-   if (NlsMbCodePageTag == FALSE)
-   {
-      /* single-byte code page */
-      *UnicodeSize = MbSize * sizeof (WCHAR);
-   }
-   else
-   {
-      /* multi-byte code page */
-      for (Length = 0; MbSize; MbSize--, MbString++, Length++)
-      {
-         if (NlsLeadByteInfo[(UCHAR)*MbString] != 0)
-         {
-            if (!--MbSize)
-               break;  /* partial char, ignore it */
-            MbString++;
-         }
-      }
-
-      *UnicodeSize = Length * sizeof(WCHAR);
-   }
-
-   return STATUS_SUCCESS;
+    ULONG Length = 0;
+
+    if (!NlsMbCodePageTag)
+    {
+        /* single-byte code page */
+        *UnicodeSize = MbSize * sizeof (WCHAR);
+    }
+    else
+    {
+        /* multi-byte code page */
+        while (MbSize--)
+        {
+            if (NlsLeadByteInfo[*(PUCHAR)MbString++])
+            {
+                if (!MbSize)
+                {
+                    /* partial char, ignore it */
+                    Length++;
+                    break;
+                }
+            }
+            else
+            {
+                /* Move on */
+                MbSize--;
+                MbString++;
+            }
+
+            /* Increase returned size */
+            Length++;
+        }
+
+        /* Return final size */
+        *UnicodeSize = Length * sizeof(WCHAR);
+    }
+
+    /* Success */
+    return STATUS_SUCCESS;
 }
 
 
@@ -472,47 +487,45 @@ RtlUnicodeToMultiByteN (PCHAR MbString,
    return STATUS_SUCCESS;
 }
 
-
 /*
  * @implemented
  */
-NTSTATUS STDCALL
+NTSTATUS
+STDCALL
 RtlUnicodeToMultiByteSize(PULONG MbSize,
                           PWCHAR UnicodeString,
                           ULONG UnicodeSize)
 {
-   ULONG UnicodeLength;
-   ULONG MbLength;
-
-   if (NlsMbCodePageTag == FALSE)
-   {
-      /* single-byte code page */
-      *MbSize = UnicodeSize / sizeof (WCHAR);
-   }
-   else
-   {
-      /* multi-byte code page */
-      UnicodeLength = UnicodeSize / sizeof(WCHAR);
-      MbLength = 0;
-      while (UnicodeLength > 0)
-      {
-         if (NlsLeadByteInfo[(USHORT)*UnicodeString] & 0xff00)
-            MbLength++;
-
-         MbLength++;
-         UnicodeLength--;
-         UnicodeString++;
-      }
-
-      *MbSize = MbLength;
-   }
-
-   return(STATUS_SUCCESS);
+    ULONG UnicodeLength = UnicodeSize / sizeof(WCHAR);
+    ULONG MbLength = 0;
+
+    if (!NlsMbCodePageTag)
+    {
+        /* single-byte code page */
+        *MbSize = UnicodeLength;
+    }
+    else
+    {
+        /* multi-byte code page */
+        while (UnicodeLength--)
+        {
+            if (HIBYTE(NlsUnicodeToAnsiTable[*UnicodeString++]))
+            {
+                MbLength += sizeof(WCHAR);
+            }
+            else
+            {
+                MbLength++;
+            }
+        }
+    
+        *MbSize = MbLength;
+    }
+
+    /* Success */
+    return STATUS_SUCCESS;
 }
 
-
-
-
 /*
  * @unimplemented
  */
index 7f30600..3eec7da 100644 (file)
@@ -68,13 +68,15 @@ ULONG
 STDCALL
 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));
 }
 
 
@@ -883,16 +885,17 @@ RtlUnicodeStringToInteger(
  */
 ULONG
 STDCALL
-RtlxUnicodeStringToOemSize(
-   IN PCUNICODE_STRING UnicodeString)
+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));
 }
 
 /*
@@ -1798,14 +1801,15 @@ ULONG
 STDCALL
 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));
 }
 
 
@@ -1859,17 +1863,17 @@ RtlStringFromGUID (IN REFGUID Guid,
  */
 ULONG
 STDCALL
-RtlxUnicodeStringToAnsiSize(
-   IN PCUNICODE_STRING UnicodeString)
+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));
 }