From 7e23484252dd8a3ada87df7723477a784d090b5c Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Wed, 14 Feb 2024 14:46:34 +0100 Subject: [PATCH] Implement RtlReverseWideString(), RtlTrimLeftWideString(), RtlTrimRightWideString() and RtlTrimWideString() routines --- sdk/xtdk/rtlfuncs.h | 17 ++++++ xtoskrnl/rtl/widestr.c | 127 ++++++++++++++++++++++++++++++++++++++--- xtoskrnl/xtoskrnl.spec | 4 ++ 3 files changed, 140 insertions(+), 8 deletions(-) diff --git a/sdk/xtdk/rtlfuncs.h b/sdk/xtdk/rtlfuncs.h index 03660c1..f909a63 100644 --- a/sdk/xtdk/rtlfuncs.h +++ b/sdk/xtdk/rtlfuncs.h @@ -138,6 +138,11 @@ VOID RtlReverseString(IN OUT PCHAR String, IN ULONG Length); +XTAPI +VOID +RtlReverseWideString(IN OUT PWCHAR String, + IN ULONG Length); + XTAPI BOOLEAN RtlSameMemory(IN PCVOID LeftBuffer, @@ -177,14 +182,26 @@ XTCDECL PCHAR RtlTrimLeftString(IN CONST PCHAR String); +XTAPI +PWCHAR +RtlTrimLeftWideString(IN CONST PWCHAR String); + XTCDECL PCHAR RtlTrimRightString(IN CONST PCHAR String); +XTAPI +PWCHAR +RtlTrimRightWideString(IN CONST PWCHAR String); + XTCDECL PCHAR RtlTrimString(IN CONST PCHAR String); +XTAPI +PWCHAR +RtlTrimWideString(IN CONST PWCHAR String); + XTCDECL SIZE_T RtlWideStringLength(IN CONST PWCHAR String, diff --git a/xtoskrnl/rtl/widestr.c b/xtoskrnl/rtl/widestr.c index 6bb77c4..518c93b 100644 --- a/xtoskrnl/rtl/widestr.c +++ b/xtoskrnl/rtl/widestr.c @@ -88,7 +88,7 @@ RtlCompareWideStringInsensitive(IN CONST PWCHAR String1, ULONG Index = 0; /* Iterate through the wide strings */ - while(String1[Index] != '\0' && String2[Index] != '\0') + while(String1[Index] != L'\0' && String2[Index] != L'\0') { /* Check if length limit reached */ if(Index != 0 && Index == Length) @@ -102,15 +102,15 @@ RtlCompareWideStringInsensitive(IN CONST PWCHAR String1, Character2 = String2[Index]; /* Lowercase wide string1 character if needed */ - if(String1[Index] >= 'A' && String1[Index] <= 'Z') + if(String1[Index] >= L'A' && String1[Index] <= L'Z') { - Character1 = String1[Index] - 'A' + 'a'; + Character1 = String1[Index] - L'A' + L'a'; } /* Lowercase wide string2 character if needed */ - if(String2[Index] >= 'A' && String2[Index] <= 'Z') + if(String2[Index] >= L'A' && String2[Index] <= L'Z') { - Character2 = String2[Index] - 'A' + 'a'; + Character2 = String2[Index] - L'A' + L'a'; } /* Compare the characters */ @@ -165,7 +165,7 @@ RtlConcatenateWideString(OUT PWCHAR Destination, do { /* Check if NULL terminated character found */ - if((*Destination = *Source++) == '\0') + if((*Destination = *Source++) == L'\0') { /* Break on '\0' character */ break; @@ -175,7 +175,7 @@ RtlConcatenateWideString(OUT PWCHAR Destination, while(--Count != 0); /* Add NULL termination character to the end of destination wide string */ - *Destination = '\0'; + *Destination = L'\0'; } else { @@ -187,6 +187,37 @@ RtlConcatenateWideString(OUT PWCHAR Destination, return DestString; } +/** + * Reverses a characters order in a wide string. It modifies the original, input variable. + * + * @param String + * Supplies a pointer to the wide string to reverse. + * + * @param Length + * Supplies the length of the wide string to reverse. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTAPI +VOID +RtlReverseWideString(IN OUT PWCHAR String, + IN ULONG Length) +{ + WCHAR TempChar; + ULONG Index; + + /* Iterate through the string */ + for(Index = 0; Index < (Length / 2); Index++) + { + /* Swap characters */ + TempChar = String[Index]; + String[Index] = String[Length - Index - 1]; + String[Length - Index - 1] = TempChar; + } +} + /** * Finds the next token in a null-terminated wide string. * @@ -232,7 +263,7 @@ RtlTokenizeWideString(IN PWCHAR String, for(;;) { Char = *String++; - Span = (WCHAR *)Delimiter; + Span = (PWCHAR)Delimiter; do { if((SpanChar = *Span++) == Char) @@ -257,6 +288,86 @@ RtlTokenizeWideString(IN PWCHAR String, } } +/** + * Removes certain characters from a beginning of the wide string. + * + * @param String + * Pointer to the null-terminated wide string to be trimmed. + * + * @return This routine returns a pointer to the left-trimmed wide string. + * + * @since XT 1.0 + */ +XTAPI +PWCHAR +RtlTrimLeftWideString(IN CONST PWCHAR String) +{ + PWCHAR Start; + + /* Initialize pointer */ + Start = String; + + /* Skip all leading whitespaces */ + while(*Start == L' ' || *Start == L'\n' || *Start == L'\t' || *Start == L'\r' || *Start == L'\v' || *Start == L'\f') + { + /* Advance to the next character */ + Start++; + } + + /* Return left-trimmed string */ + return Start; +} + +/** + * Removes certain characters from the end of the wide string. + * + * @param String + * Pointer to the null-terminated wide string to be trimmed. + * + * @return This routine returns a pointer to the right-trimmed wide string. + * + * @since XT 1.0 + */ +XTAPI +PWCHAR +RtlTrimRightWideString(IN CONST PWCHAR String) +{ + PWCHAR End; + + /* Find end of the string */ + End = String + RtlWideStringLength(String, 0); + + /* Skip all trailing whitespaces */ + while((End != String) && (*End == L' ' || *End == L'\n' || *End == L'\t' || *End == L'\r' || *End == L'\v' || *End == L'\f')) + { + /* Move to the previous character */ + End--; + } + + /* Terminate the string */ + *End = 0; + + /* Return right-trimmed string */ + return String; +} + +/** + * Removes certain characters from the beginning and the end of the wide string. + * + * @param String + * Pointer to the null-terminated wide string to be trimmed. + * + * @return This routine returns a pointer to the trimmed wide string. + * + * @since XT 1.0 + */ +XTAPI +PWCHAR +RtlTrimWideString(IN CONST PWCHAR String) +{ + return RtlTrimLeftWideString(RtlTrimRightWideString(String)); +} + /** * Calculates the length of a given wide string. * diff --git a/xtoskrnl/xtoskrnl.spec b/xtoskrnl/xtoskrnl.spec index 5e10f7e..df4b2cb 100644 --- a/xtoskrnl/xtoskrnl.spec +++ b/xtoskrnl/xtoskrnl.spec @@ -49,6 +49,7 @@ @ stdcall RtlReadRegisterLong(ptr) @ stdcall RtlReadRegisterShort(ptr) @ stdcall RtlReverseString(str long) +@ stdcall RtlReverseWideString(wstr long) @ stdcall RtlSameMemory(ptr ptr long) @ stdcall RtlSetMemory(ptr long long) @ cdecl RtlStringLength(str long) @@ -56,8 +57,11 @@ @ stdcall RtlTokenizeString(str str str) @ cdecl RtlTokenizeWideString(wstr wstr wstr) @ cdecl RtlTrimLeftString(str) +@ stdcall RtlTrimLeftWideString(wstr) @ cdecl RtlTrimRightString(str) +@ stdcall RtlTrimRightWideString(wstr) @ cdecl RtlTrimString(str) +@ stdcall RtlTrimWideString(wstr) @ cdecl RtlWideStringLength(wstr long) @ stdcall RtlWriteRegisterByte(ptr long) @ stdcall RtlWriteRegisterLong(ptr long)