From 84cbfb9d8e46127f7b539f2d31655378130601ca Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Tue, 6 Sep 2005 19:47:06 +0000 Subject: [PATCH] - Fix size functions to return the correct results in all cases. svn path=/trunk/; revision=17705 --- reactos/lib/rtl/nls.c | 127 +++++++++++++++++++++----------------- reactos/lib/rtl/unicode.c | 48 +++++++------- 2 files changed, 96 insertions(+), 79 deletions(-) diff --git a/reactos/lib/rtl/nls.c b/reactos/lib/rtl/nls.c index d55d8fcc47f..322a9453850 100644 --- a/reactos/lib/rtl/nls.c +++ b/reactos/lib/rtl/nls.c @@ -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 */ diff --git a/reactos/lib/rtl/unicode.c b/reactos/lib/rtl/unicode.c index 7f306000f23..3eec7dae791 100644 --- a/reactos/lib/rtl/unicode.c +++ b/reactos/lib/rtl/unicode.c @@ -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)); } -- 2.17.1