From b1c08e4a9f68f20b8f7983543dbc249af6b689bb Mon Sep 17 00:00:00 2001 From: belliash Date: Tue, 25 Oct 2022 23:45:12 +0200 Subject: [PATCH] Implement RtlStringLength() and RtlStringToWideString() routines --- sdk/xtdk/rtlfuncs.h | 11 +++++ xtoskrnl/CMakeLists.txt | 1 + xtoskrnl/rtl/string.c | 101 ++++++++++++++++++++++++++++++++++++++++ xtoskrnl/xtoskrnl.spec | 2 + 4 files changed, 115 insertions(+) create mode 100644 xtoskrnl/rtl/string.c diff --git a/sdk/xtdk/rtlfuncs.h b/sdk/xtdk/rtlfuncs.h index 3da0b38..9bfe9f8 100644 --- a/sdk/xtdk/rtlfuncs.h +++ b/sdk/xtdk/rtlfuncs.h @@ -67,6 +67,17 @@ RtlSetMemory(OUT PVOID Destination, IN UCHAR Byte, IN SIZE_T Length); +XTCDECL +SIZE_T +RtlStringLength(IN CONST PUCHAR String, + IN SIZE_T MaxLength); + +XTCDECL +INT +RtlStringToWideString(PWCHAR Destination, + CONST PUCHAR *Source, + SIZE_T Length); + XTCDECL INT RtlWideStringCompare(IN CONST PWCHAR String1, diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index 2ddaf6b..11bf37c 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -12,6 +12,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/ke/krnlinit.c ${XTOSKRNL_SOURCE_DIR}/rtl/memory.c ${XTOSKRNL_SOURCE_DIR}/rtl/plist.c + ${XTOSKRNL_SOURCE_DIR}/rtl/string.c ${XTOSKRNL_SOURCE_DIR}/rtl/widestr.c) # Set module definition SPEC file diff --git a/xtoskrnl/rtl/string.c b/xtoskrnl/rtl/string.c new file mode 100644 index 0000000..cc4940b --- /dev/null +++ b/xtoskrnl/rtl/string.c @@ -0,0 +1,101 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/rtl/string.c + * DESCRIPTION: String support + * DEVELOPERS: Rafal Kupiec + */ + +#include "xtkmapi.h" + + +/** + * Calculates the length of a given string. + * + * @param String + * Pointer to the null-terminated string to be examined. + * + * @param MaxLength + * Maximum number of characters to examine. If no limit set, it examines whole string. + * + * @return The length of the null-terminated string. + * + * @since: XT 1.0 + */ +XTCDECL +SIZE_T +RtlStringLength(IN CONST PUCHAR String, + IN SIZE_T MaxLength) +{ + SIZE_T Length; + + /* Check if NULL pointer passed */ + if(String == NULL) + { + return 0; + } + + /* Iterate through the wide string */ + for(Length = 0; ; Length++) + { + + /* Check if NULL found or max length limit reached */ + if((Length != 0 && Length == MaxLength) || !String[Length]) + { + /* Finish examination */ + break; + } + } + + /* Return wide string length */ + return Length; +} + +/** + * Converts a multibyte character string to its wide character representation. + * + * @param Destination + * Pointer to wide character array where the wide string will be stored + * + * @param Source + * Pointer to the first element of a multibyte string to convert. + * + * @param Length + * Number of characters in the source string. + * + * @return Returns the number of wide characters written to the destination array on success, or -1 on error. + * + * @since XT 1.0 + */ +XTCDECL +INT +RtlStringToWideString(PWCHAR Destination, + CONST PUCHAR *Source, + SIZE_T Length) +{ + PUCHAR LocalSource = *Source; + SIZE_T Count = Length; + + if(Destination == NULL) + { + return 0; + } + + while(Count) + { + if((*Destination = (UCHAR)*LocalSource) == 0) + { + LocalSource = NULL; + break; + } + if(*Destination >= 0x80) + { + return -1; + } + LocalSource++; + Destination++; + Count--; + } + + return Length - Count; +} diff --git a/xtoskrnl/xtoskrnl.spec b/xtoskrnl/xtoskrnl.spec index 987c3dd..ff19369 100644 --- a/xtoskrnl/xtoskrnl.spec +++ b/xtoskrnl/xtoskrnl.spec @@ -6,6 +6,8 @@ @ stdcall RtlMoveMemory(ptr ptr long) @ stdcall RtlSameMemory(ptr ptr long) @ stdcall RtlSetMemory(ptr long long) +@ cdecl RtlStringLength(str long) +@ cdecl RtlStringToWideString(wstr str long) @ cdecl RtlWideStringCompare(wstr wstr long) @ cdecl RtlWideStringConcatenate(wstr wstr long) @ cdecl RtlWideStringLength(wstr long)