From d56b10f252fc4ef17d8386960421773685a2a99f Mon Sep 17 00:00:00 2001 From: belliash Date: Mon, 5 Sep 2022 15:17:12 +0200 Subject: [PATCH] Various fixes to the existing routines: * Export RtlWideStringTokenize() * Allow to compare whole strings in RtlWideStringCompare() when no length specified * RtlSameMemory() returns boolean now * Source bytes can be constant value in RtlCopyMemory() --- sdk/xtdk/rtlfuncs.h | 6 ++++-- xtoskrnl/rtl/memory.c | 23 +++++++++-------------- xtoskrnl/rtl/widestr.c | 20 +++++++++++++++----- xtoskrnl/xtoskrnl.spec | 3 ++- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/sdk/xtdk/rtlfuncs.h b/sdk/xtdk/rtlfuncs.h index f508582..6039fea 100644 --- a/sdk/xtdk/rtlfuncs.h +++ b/sdk/xtdk/rtlfuncs.h @@ -15,7 +15,7 @@ XTAPI -PVOID +VOID RtlCopyMemory(IN PVOID Destination, IN PCVOID Source, IN SIZE_T Length); @@ -38,16 +38,18 @@ BOOLEAN RtlListEmpty(PLIST_ENTRY ListHead); XTAPI -SIZE_T +BOOLEAN RtlSameMemory(IN PCVOID LeftBuffer, IN PCVOID RightBuffer, IN SIZE_T Length); +XTCDECL INT RtlWideStringCompare(IN CONST PWCHAR String1, IN CONST PWCHAR String2, IN CONST ULONG Length); +XTCDECL PWCHAR RtlWideStringTokenize(IN PWCHAR String, IN CONST PWCHAR Delimiter, diff --git a/xtoskrnl/rtl/memory.c b/xtoskrnl/rtl/memory.c index 901c5a8..54fd932 100644 --- a/xtoskrnl/rtl/memory.c +++ b/xtoskrnl/rtl/memory.c @@ -21,27 +21,24 @@ * @param Length * Specifies the number of bytes to copy. * - * @return Returns the destination pointer. + * @return This routine does not return any value. * * @since NT 3.5 */ XTAPI -PVOID +VOID RtlCopyMemory(IN PVOID Destination, IN PCVOID Source, IN SIZE_T Length) { PCHAR DestinationBytes = (PCHAR)Destination; - PCHAR SourceBytes = (PCHAR)Source; + PCCHAR SourceBytes = (PCHAR)Source; /* Forward buffer copy */ while(Length--) { *DestinationBytes++ = *SourceBytes++; } - - /* Return pointer to destination buffer */ - return Destination; } /** @@ -56,20 +53,18 @@ RtlCopyMemory(IN PVOID Destination, * @param Length * Specifies a number of bytes to compare. * - * @return A value indicating the relationship between the content of the memory blocks. - * It returns zero (0) if both memory blocks are equal or a value different than - * zero representing which is greater if they do not match. + * @return Returns TRUE if both buffers are equal up to the specified length, or FALSE otherwise. * * @since XT 1.0 */ XTAPI -SIZE_T +BOOLEAN RtlSameMemory(IN PCVOID LeftBuffer, IN PCVOID RightBuffer, IN SIZE_T Length) { - CONST UCHAR *Left = (PUCHAR)LeftBuffer; - CONST UCHAR *Right = (PUCHAR)RightBuffer; + CONST CHAR *Left = (PCHAR)LeftBuffer; + CONST CHAR *Right = (PCHAR)RightBuffer; /* Check if there is anything to compare */ if(Length) @@ -81,7 +76,7 @@ RtlSameMemory(IN PCVOID LeftBuffer, if(*Left != *Right) { /* Buffers differ */ - return (*Left - *Right); + return FALSE; } /* Advance to next byte */ @@ -91,5 +86,5 @@ RtlSameMemory(IN PCVOID LeftBuffer, } /* Buffers equal */ - return 0; + return TRUE; } diff --git a/xtoskrnl/rtl/widestr.c b/xtoskrnl/rtl/widestr.c index ff138c3..8e6baa6 100644 --- a/xtoskrnl/rtl/widestr.c +++ b/xtoskrnl/rtl/widestr.c @@ -25,6 +25,7 @@ * * @since XT 1.0 */ +XTCDECL INT RtlWideStringCompare(IN CONST PWCHAR String1, IN CONST PWCHAR String2, @@ -33,7 +34,14 @@ RtlWideStringCompare(IN CONST PWCHAR String1, ULONG Index; /* Iterate through the strings */ - for(Index = 0; Index < Length; Index++) { + for(Index = 0; ; Index++) { + /* Check if length limit reached */ + if(Index != 0 && Index == Length) + { + /* Skip checking next characters */ + break; + } + /* Check if string characters are equal */ if(String1[Index] != String2[Index]) { @@ -42,9 +50,9 @@ RtlWideStringCompare(IN CONST PWCHAR String1, } /* Check if end of string reached */ - if(!String1[Index]) + if(!String1[Index] || !String2[Index]) { - /* Equal strings until the end of String1 */ + /* Equal strings until the end of one of them */ return 0; } } @@ -69,6 +77,7 @@ RtlWideStringCompare(IN CONST PWCHAR String1, * * @since: XT 1.0 */ +XTCDECL PWCHAR RtlWideStringTokenize(IN PWCHAR String, IN CONST PWCHAR Delimiter, @@ -81,7 +90,7 @@ RtlWideStringTokenize(IN PWCHAR String, if(String == NULL && (String = *SavePtr) == NULL) { /* Empty string given */ - return (NULL); + return NULL; } /* Check non-delimiter characters */ @@ -89,7 +98,7 @@ RtlWideStringTokenize(IN PWCHAR String, if(Char == L'\0') { *SavePtr = NULL; - return (NULL); + return NULL; } Token = String - 1; @@ -110,6 +119,7 @@ RtlWideStringTokenize(IN PWCHAR String, { String[-1] = L'\0'; } + /* Store pointer to the next token */ *SavePtr = String; diff --git a/xtoskrnl/xtoskrnl.spec b/xtoskrnl/xtoskrnl.spec index d977863..1749d09 100644 --- a/xtoskrnl/xtoskrnl.spec +++ b/xtoskrnl/xtoskrnl.spec @@ -2,4 +2,5 @@ @ stdcall HlIoPortOutByte(ptr long) @ stdcall RtlCopyMemory(ptr ptr long) @ stdcall RtlSameMemory(ptr ptr long) -@ stdcall RtlWideStringCompare(wstr wstr long) \ No newline at end of file +@ stdcall RtlWideStringCompare(wstr wstr long) +@ stdcall RtlWideStringTokenize(wstr wstr wstr) \ No newline at end of file