From 8d9b815544902d860e9c489378799d71a00d1ff2 Mon Sep 17 00:00:00 2001 From: Colin Finck Date: Sun, 22 Jun 2008 20:58:56 +0000 Subject: [PATCH] Add a library "host_wcsfuncs" with implementations for UTF-16 string functions needed for some host tools Instead of copying those functions into every host tool, which needs it (as we did previously), we can now implement them all in this library and link the host tools to it. If USE_HOST_WCSFUNCS is not defined, the "wcsfuncs.h" file will define them to the CRT functions (so this library does not create overhead, when the code is built for the target platform) See issue #3285 for more details. svn path=/trunk/; revision=34050 --- reactos/include/host/wcsfuncs.h | 24 ++++++++++++ reactos/lib/cmlib/cminit.c | 2 +- reactos/lib/cmlib/cmlib.h | 32 ++++++++------- reactos/lib/host/directory.rbuild | 7 ++++ reactos/lib/host/wcsfuncs/wcsfuncs.c | 48 +++++++++++++++++++++++ reactos/lib/host/wcsfuncs/wcsfuncs.rbuild | 6 +++ reactos/lib/lib.rbuild | 3 ++ reactos/tools/mkhive/mkhive.h | 10 ++--- reactos/tools/mkhive/mkhive.rbuild | 1 + reactos/tools/mkhive/registry.c | 8 ++-- reactos/tools/mkhive/rtl.c | 37 +---------------- 11 files changed, 116 insertions(+), 62 deletions(-) create mode 100644 reactos/include/host/wcsfuncs.h create mode 100644 reactos/lib/host/directory.rbuild create mode 100644 reactos/lib/host/wcsfuncs/wcsfuncs.c create mode 100644 reactos/lib/host/wcsfuncs/wcsfuncs.rbuild diff --git a/reactos/include/host/wcsfuncs.h b/reactos/include/host/wcsfuncs.h new file mode 100644 index 00000000000..bf2c12771f2 --- /dev/null +++ b/reactos/include/host/wcsfuncs.h @@ -0,0 +1,24 @@ +/* + PROJECT: ReactOS + LICENSE: GPL v2 or any later version + FILE: include/host/wcsfuncs.h + PURPOSE: Header for the "host_wcsfuncs" static library + COPYRIGHT: Copyright 2008 Colin Finck +*/ + +#ifndef _HOST_WCSFUNCS_H +#define _HOST_WCSFUNCS_H + +#ifdef USE_HOST_WCSFUNCS + /* Function prototypes */ + SIZE_T utf16_wcslen(PCWSTR str); + PWSTR utf16_wcschr(PWSTR str, WCHAR c); + INT utf16_wcsncmp(PCWSTR string1, PCWSTR string2, size_t count); +#else + /* Define the utf16_ functions to the CRT functions */ + #define utf16_wcslen wcslen + #define utf16_wcschr wcschr + #define utf16_wcsncmp wcsncmp +#endif + +#endif diff --git a/reactos/lib/cmlib/cminit.c b/reactos/lib/cmlib/cminit.c index 816d4d2e03d..7c3f7ddf838 100644 --- a/reactos/lib/cmlib/cminit.c +++ b/reactos/lib/cmlib/cminit.c @@ -19,7 +19,7 @@ CmCreateRootNode( SIZE_T NameSize; /* Allocate the cell */ - NameSize = wcslen(Name) * sizeof(WCHAR); + NameSize = utf16_wcslen(Name) * sizeof(WCHAR); RootCellIndex = HvAllocateCell(Hive, FIELD_OFFSET(CM_KEY_NODE, Name) + NameSize, Stable, diff --git a/reactos/lib/cmlib/cmlib.h b/reactos/lib/cmlib/cmlib.h index ebcf2ec6066..66a5ffa44c6 100644 --- a/reactos/lib/cmlib/cmlib.h +++ b/reactos/lib/cmlib/cmlib.h @@ -9,22 +9,26 @@ #define CMLIB_H #ifdef CMLIB_HOST -#include -#include -#include - -// Definitions copied from -// We only want to include host headers, so we define them manually -#define STATUS_SUCCESS ((NTSTATUS)0x00000000) -#define STATUS_NOT_IMPLEMENTED ((NTSTATUS)0xC0000002) -#define STATUS_NO_MEMORY ((NTSTATUS)0xC0000017) -#define STATUS_INSUFFICIENT_RESOURCES ((NTSTATUS)0xC000009A) -#define STATUS_REGISTRY_CORRUPT ((NTSTATUS)0xC000014C) -#define STATUS_NOT_REGISTRY_FILE ((NTSTATUS)0xC000015C) -#define STATUS_REGISTRY_RECOVERED ((NTSTATUS)0x40000009) - + #include + #include + #include + + // Definitions copied from + // We only want to include host headers, so we define them manually + #define STATUS_SUCCESS ((NTSTATUS)0x00000000) + #define STATUS_NOT_IMPLEMENTED ((NTSTATUS)0xC0000002) + #define STATUS_NO_MEMORY ((NTSTATUS)0xC0000017) + #define STATUS_INSUFFICIENT_RESOURCES ((NTSTATUS)0xC000009A) + #define STATUS_REGISTRY_CORRUPT ((NTSTATUS)0xC000014C) + #define STATUS_NOT_REGISTRY_FILE ((NTSTATUS)0xC000015C) + #define STATUS_REGISTRY_RECOVERED ((NTSTATUS)0x40000009) + + /* For */ + #define USE_HOST_WCSFUNCS #endif +#include + // // Debug support switch // diff --git a/reactos/lib/host/directory.rbuild b/reactos/lib/host/directory.rbuild new file mode 100644 index 00000000000..d39ecfa981f --- /dev/null +++ b/reactos/lib/host/directory.rbuild @@ -0,0 +1,7 @@ + + + + + + + diff --git a/reactos/lib/host/wcsfuncs/wcsfuncs.c b/reactos/lib/host/wcsfuncs/wcsfuncs.c new file mode 100644 index 00000000000..fc40b7408c6 --- /dev/null +++ b/reactos/lib/host/wcsfuncs/wcsfuncs.c @@ -0,0 +1,48 @@ +/* + PROJECT: ReactOS + LICENSE: GPL v2 or any later version + FILE: lib/host/wcsfuncs/wcsfuncs.c + PURPOSE: Reimplemented wide-character string functions for host tools (to be independent of the host wchar_t size) + COPYRIGHT: Copyright 2008 Colin Finck +*/ + +#include + +/* Function implementations */ +SIZE_T utf16_wcslen(PCWSTR str) +{ + SIZE_T i; + + for(i = 0; str[i]; i++); + + return i; +} + +PWSTR utf16_wcschr(PWSTR str, WCHAR c) +{ + SIZE_T i; + + for(i = 0; str[i] && str[i] != c; i++); + + if(str[i]) + return &str[i]; + else + return NULL; +} + +INT utf16_wcsncmp(PCWSTR string1, PCWSTR string2, size_t count) +{ + while(count--) + { + if(*string1 != *string2) + return 1; + + if(*string1 == 0) + return 0; + + string1++; + string2++; + } + + return 0; +} diff --git a/reactos/lib/host/wcsfuncs/wcsfuncs.rbuild b/reactos/lib/host/wcsfuncs/wcsfuncs.rbuild new file mode 100644 index 00000000000..d955a305b8f --- /dev/null +++ b/reactos/lib/host/wcsfuncs/wcsfuncs.rbuild @@ -0,0 +1,6 @@ + + + + include + wcsfuncs.c + diff --git a/reactos/lib/lib.rbuild b/reactos/lib/lib.rbuild index 9048bc45b40..fdc8fc585fa 100644 --- a/reactos/lib/lib.rbuild +++ b/reactos/lib/lib.rbuild @@ -22,6 +22,9 @@ + + + diff --git a/reactos/tools/mkhive/mkhive.h b/reactos/tools/mkhive/mkhive.h index e683ee9117c..237bfa4ba49 100644 --- a/reactos/tools/mkhive/mkhive.h +++ b/reactos/tools/mkhive/mkhive.h @@ -32,6 +32,9 @@ #include +#define USE_HOST_WCSFUNCS +#include + // Definitions copied from // We only want to include host headers, so we define them manually #define STATUS_SUCCESS ((NTSTATUS)0x00000000) @@ -96,13 +99,6 @@ extern LIST_ENTRY CmiHiveListHead; #define GCC_PACKED __attribute__((packed)) #endif//_MSC_VER -/* rtl.c */ -PWSTR -xwcschr( - PWSTR String, - WCHAR Char -); - #endif /* __MKHIVE_H__ */ /* EOF */ diff --git a/reactos/tools/mkhive/mkhive.rbuild b/reactos/tools/mkhive/mkhive.rbuild index e3d750654f6..0eed6bfa1f4 100644 --- a/reactos/tools/mkhive/mkhive.rbuild +++ b/reactos/tools/mkhive/mkhive.rbuild @@ -11,6 +11,7 @@ -fshort-wchar inflibhost cmlibhost + host_wcsfuncs binhive.c cmi.c mkhive.c diff --git a/reactos/tools/mkhive/registry.c b/reactos/tools/mkhive/registry.c index 4b1492c9763..1a70bdf7931 100644 --- a/reactos/tools/mkhive/registry.c +++ b/reactos/tools/mkhive/registry.c @@ -127,7 +127,7 @@ RegpOpenOrCreateKey( LocalKeyName = (PWSTR)KeyName; for (;;) { - End = (PWSTR) xwcschr(LocalKeyName, '\\'); + End = (PWSTR) utf16_wcschr(LocalKeyName, '\\'); if (End) { KeyString.Buffer = LocalKeyName; @@ -138,9 +138,9 @@ RegpOpenOrCreateKey( RtlInitUnicodeString(&KeyString, LocalKeyName); /* Redirect from 'CurrentControlSet' to 'ControlSet001' */ - if (!xwcsncmp(LocalKeyName, L"CurrentControlSet", 17) && - ParentKey->NameSize == 12 && - !memcmp(ParentKey->Name, L"SYSTEM", 12)) + if (!utf16_wcsncmp(LocalKeyName, L"CurrentControlSet", 17) && + ParentKey->NameSize == 12 && + !memcmp(ParentKey->Name, L"SYSTEM", 12)) RtlInitUnicodeString(&KeyString, L"ControlSet001"); /* Check subkey in memory structure */ diff --git a/reactos/tools/mkhive/rtl.c b/reactos/tools/mkhive/rtl.c index 1ccb8ac4e0a..eec2fc3872e 100644 --- a/reactos/tools/mkhive/rtl.c +++ b/reactos/tools/mkhive/rtl.c @@ -10,41 +10,6 @@ #include "mkhive.h" #include -SIZE_T xwcslen( PCWSTR String ) { - SIZE_T i; - - for( i = 0; String[i]; i++ ); - - return i; -} - -PWSTR xwcschr( PWSTR String, WCHAR Char ) -{ - SIZE_T i; - - for( i = 0; String[i] && String[i] != Char; i++ ); - - if( String[i] ) return &String[i]; - else return NULL; -} - -int xwcsncmp(PCWSTR s1, PCWSTR s2, size_t n) -{ - while(n--) - { - if(*s1 != *s2) - return 1; - - if(*s1 == 0) - return 0; - - s1++; - s2++; - } - - return 0; -} - /* * @implemented * @@ -88,7 +53,7 @@ RtlInitUnicodeString( if(SourceString) { - DestSize = xwcslen(SourceString) * sizeof(WCHAR); + DestSize = utf16_wcslen(SourceString) * sizeof(WCHAR); DestinationString->Length = (USHORT)DestSize; DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR); } -- 2.17.1