From 704241af92cccc5bee918885e3680e05c24f1616 Mon Sep 17 00:00:00 2001 From: Mark Jansen Date: Sun, 10 Jul 2016 16:06:39 +0000 Subject: [PATCH] [APPHELP] Prepare sdbwrite related api for a new hosttool. CORE-11302 - Adding an extra argument to SdbReAlloc - Do not rely on platform wcs* functions. svn path=/trunk/; revision=71885 --- reactos/dll/appcompat/apphelp/apphelp.c | 3 +- reactos/dll/appcompat/apphelp/sdbapi.c | 2 +- reactos/dll/appcompat/apphelp/sdbpapi.h | 11 ++-- .../dll/appcompat/apphelp/sdbstringtable.c | 53 +++++++++++++++++-- reactos/dll/appcompat/apphelp/sdbwrite.c | 3 +- reactos/dll/appcompat/apphelp/sdbwrite.h | 44 +++++++++++++++ 6 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 reactos/dll/appcompat/apphelp/sdbwrite.h diff --git a/reactos/dll/appcompat/apphelp/apphelp.c b/reactos/dll/appcompat/apphelp/apphelp.c index 4c97581a99d..6d217696978 100644 --- a/reactos/dll/appcompat/apphelp/apphelp.c +++ b/reactos/dll/appcompat/apphelp/apphelp.c @@ -102,7 +102,8 @@ BOOL WINAPIV ShimDbgPrint(SHIM_LOG_LEVEL Level, PCSTR FunctionName, PCSTR Format { char Buffer[512]; va_list ArgList; - char* Current = Buffer, *LevelStr; + char* Current = Buffer; + const char* LevelStr; size_t Length = sizeof(Buffer); if (g_ShimDebugLevel == 0xffffffff) diff --git a/reactos/dll/appcompat/apphelp/sdbapi.c b/reactos/dll/appcompat/apphelp/sdbapi.c index d898f95076b..4d5b647b67a 100644 --- a/reactos/dll/appcompat/apphelp/sdbapi.c +++ b/reactos/dll/appcompat/apphelp/sdbapi.c @@ -178,7 +178,7 @@ LPVOID SdbpAlloc(SIZE_T size return mem; } -LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size +LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size, SIZE_T oldSize #if SDBAPI_DEBUG_ALLOC , int line, const char* file #endif diff --git a/reactos/dll/appcompat/apphelp/sdbpapi.h b/reactos/dll/appcompat/apphelp/sdbpapi.h index b2106337a84..a19b006f789 100644 --- a/reactos/dll/appcompat/apphelp/sdbpapi.h +++ b/reactos/dll/appcompat/apphelp/sdbpapi.h @@ -30,21 +30,21 @@ void SdbpHeapDeinit(void); #if SDBAPI_DEBUG_ALLOC LPVOID SdbpAlloc(SIZE_T size, int line, const char* file); -LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size, int line, const char* file); +LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size, SIZE_T oldSize, int line, const char* file); void SdbpFree(LPVOID mem, int line, const char* file); #define SdbAlloc(size) SdbpAlloc(size, __LINE__, __FILE__) -#define SdbReAlloc(mem, size) SdbpReAlloc(mem, size, __LINE__, __FILE__) +#define SdbReAlloc(mem, size, oldSize) SdbpReAlloc(mem, size, oldSize, __LINE__, __FILE__) #define SdbFree(mem) SdbpFree(mem, __LINE__, __FILE__) #else LPVOID SdbpAlloc(SIZE_T size); -LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size); +LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size, SIZE_T oldSize); void SdbpFree(LPVOID mem); #define SdbAlloc(size) SdbpAlloc(size) -#define SdbReAlloc(mem, size) SdbpReAlloc(mem, size) +#define SdbReAlloc(mem, size, oldSize) SdbpReAlloc(mem, size, oldSize) #define SdbFree(mem) SdbpFree(mem) #endif @@ -71,6 +71,9 @@ DWORD SdbpStrsize(PCWSTR string); BOOL WINAPI SdbpCheckTagType(TAG tag, WORD type); BOOL WINAPI SdbpCheckTagIDType(PDB db, TAGID tagid, WORD type); +#ifndef WINAPIV +#define WINAPIV +#endif typedef enum _SHIM_LOG_LEVEL { SHIM_ERR = 1, diff --git a/reactos/dll/appcompat/apphelp/sdbstringtable.c b/reactos/dll/appcompat/apphelp/sdbstringtable.c index 429934611a2..02b2124a310 100644 --- a/reactos/dll/appcompat/apphelp/sdbstringtable.c +++ b/reactos/dll/appcompat/apphelp/sdbstringtable.c @@ -23,12 +23,21 @@ #include "sdbpapi.h" #else /* !defined(SDBWRITE_HOSTTOOL) */ #include +#include #include "sdbtypes.h" #include "sdbpapi.h" #endif /* !defined(SDBWRITE_HOSTTOOL) */ #include "sdbstringtable.h" +#if !defined(offsetof) +#if defined(__GNUC__) +#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER) +#else +#define offsetof(TYPE, MEMBER) ((size_t)&(((TYPE *)0)->MEMBER)) +#endif +#endif // !defined(offsetof) + #define DEFAULT_TABLE_SIZE 0x100 typedef struct SdbHashEntry @@ -98,13 +107,48 @@ static DWORD StringHash(const WCHAR* str) return hash; } +int Sdbwcscmp(const WCHAR* s1, const WCHAR* s2) +{ + while (*s1 == *s2) + { + if (*s1 == 0) + return 0; + s1++; + s2++; + } + return *s1 - *s2; +} + + +// implementation taken from reactos/sdk/lib/crt/string/wcs.c +INT Sdbwcscpy(WCHAR* wcDest, size_t numElement, const WCHAR *wcSrc) +{ + size_t size = 0; + if(!wcDest || !numElement) + return 22; /* EINVAL */ + + wcDest[0] = 0; + + if(!wcSrc) + return 22; /* EINVAL */ + + size = SdbpStrlen(wcSrc) + 1; + + if(size > numElement) + return 34; /* ERANGE */ + + memcpy(wcDest, wcSrc, size * sizeof(WCHAR)); + + return 0; +} + static struct SdbHashEntry** TableFindPtr(struct SdbStringHashTable* table, const WCHAR* str) { DWORD hash = StringHash(str); struct SdbHashEntry** entry = &table->Entries[hash % table->Size]; while (*entry) { - if (!wcscmp((*entry)->Name, str)) + if (!Sdbwcscmp((*entry)->Name, str)) return entry; entry = &(*entry)->Next; } @@ -114,12 +158,13 @@ static struct SdbHashEntry** TableFindPtr(struct SdbStringHashTable* table, cons static BOOL HashAddString(struct SdbStringHashTable* table, struct SdbHashEntry** position, const WCHAR* str, TAGID tagid) { struct SdbHashEntry* entry; - SIZE_T size; + SIZE_T size, len; if (!position) position = TableFindPtr(table, str); - size = offsetof(struct SdbHashEntry, Name[SdbpStrlen(str) + 2]); + len = SdbpStrlen(str) + 1; + size = offsetof(struct SdbHashEntry, Name[len]); entry = (*position) = SdbAlloc(size); if (!entry) { @@ -127,7 +172,7 @@ static BOOL HashAddString(struct SdbStringHashTable* table, struct SdbHashEntry* return FALSE; } entry->Tagid = tagid; - wcscpy(entry->Name, str); + Sdbwcscpy(entry->Name, len, str); return TRUE; } diff --git a/reactos/dll/appcompat/apphelp/sdbwrite.c b/reactos/dll/appcompat/apphelp/sdbwrite.c index dc36a91a8bd..de41bec8f3e 100644 --- a/reactos/dll/appcompat/apphelp/sdbwrite.c +++ b/reactos/dll/appcompat/apphelp/sdbwrite.c @@ -47,9 +47,10 @@ static void WINAPI SdbpWrite(PDB db, const void* data, DWORD size) { if (db->write_iter + size > db->size) { + DWORD oldSize = db->size; /* Round to powers of two to prevent too many reallocations */ while (db->size < db->write_iter + size) db->size <<= 1; - db->data = SdbReAlloc(db->data, db->size); + db->data = SdbReAlloc(db->data, db->size, oldSize); } memcpy(db->data + db->write_iter, data, size); diff --git a/reactos/dll/appcompat/apphelp/sdbwrite.h b/reactos/dll/appcompat/apphelp/sdbwrite.h new file mode 100644 index 00000000000..2a28282d4a6 --- /dev/null +++ b/reactos/dll/appcompat/apphelp/sdbwrite.h @@ -0,0 +1,44 @@ +/* + * Copyright 2013 Mislav Blažević + * Copyright 2015,2016 Mark Jansen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef SDBWRITE_H +#define SDBWRITE_H + +#ifdef __cplusplus +extern "C" { +#endif + +PDB WINAPI SdbCreateDatabase(LPCWSTR path, PATH_TYPE type); +void WINAPI SdbCloseDatabaseWrite(PDB db); +BOOL WINAPI SdbWriteNULLTag(PDB db, TAG tag); +BOOL WINAPI SdbWriteWORDTag(PDB db, TAG tag, WORD data); +BOOL WINAPI SdbWriteDWORDTag(PDB db, TAG tag, DWORD data); +BOOL WINAPI SdbWriteQWORDTag(PDB db, TAG tag, QWORD data); +BOOL WINAPI SdbWriteStringTag(PDB db, TAG tag, LPCWSTR string); +BOOL WINAPI SdbWriteStringRefTag(PDB db, TAG tag, TAGID tagid); +BOOL WINAPI SdbWriteBinaryTag(PDB db, TAG tag, BYTE* data, DWORD size); +BOOL WINAPI SdbWriteBinaryTagFromFile(PDB db, TAG tag, LPCWSTR path); +TAGID WINAPI SdbBeginWriteListTag(PDB db, TAG tag); +BOOL WINAPI SdbEndWriteListTag(PDB db, TAGID tagid); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // SDBWRITE_H