[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
This commit is contained in:
Mark Jansen 2016-07-10 16:06:39 +00:00
parent 35e5c8b39f
commit 704241af92
6 changed files with 105 additions and 11 deletions

View file

@ -102,7 +102,8 @@ BOOL WINAPIV ShimDbgPrint(SHIM_LOG_LEVEL Level, PCSTR FunctionName, PCSTR Format
{ {
char Buffer[512]; char Buffer[512];
va_list ArgList; va_list ArgList;
char* Current = Buffer, *LevelStr; char* Current = Buffer;
const char* LevelStr;
size_t Length = sizeof(Buffer); size_t Length = sizeof(Buffer);
if (g_ShimDebugLevel == 0xffffffff) if (g_ShimDebugLevel == 0xffffffff)

View file

@ -178,7 +178,7 @@ LPVOID SdbpAlloc(SIZE_T size
return mem; return mem;
} }
LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size LPVOID SdbpReAlloc(LPVOID mem, SIZE_T size, SIZE_T oldSize
#if SDBAPI_DEBUG_ALLOC #if SDBAPI_DEBUG_ALLOC
, int line, const char* file , int line, const char* file
#endif #endif

View file

@ -30,21 +30,21 @@ void SdbpHeapDeinit(void);
#if SDBAPI_DEBUG_ALLOC #if SDBAPI_DEBUG_ALLOC
LPVOID SdbpAlloc(SIZE_T size, int line, const char* file); 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); void SdbpFree(LPVOID mem, int line, const char* file);
#define SdbAlloc(size) SdbpAlloc(size, __LINE__, __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__) #define SdbFree(mem) SdbpFree(mem, __LINE__, __FILE__)
#else #else
LPVOID SdbpAlloc(SIZE_T size); 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); void SdbpFree(LPVOID mem);
#define SdbAlloc(size) SdbpAlloc(size) #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) #define SdbFree(mem) SdbpFree(mem)
#endif #endif
@ -71,6 +71,9 @@ DWORD SdbpStrsize(PCWSTR string);
BOOL WINAPI SdbpCheckTagType(TAG tag, WORD type); BOOL WINAPI SdbpCheckTagType(TAG tag, WORD type);
BOOL WINAPI SdbpCheckTagIDType(PDB db, TAGID tagid, WORD type); BOOL WINAPI SdbpCheckTagIDType(PDB db, TAGID tagid, WORD type);
#ifndef WINAPIV
#define WINAPIV
#endif
typedef enum _SHIM_LOG_LEVEL { typedef enum _SHIM_LOG_LEVEL {
SHIM_ERR = 1, SHIM_ERR = 1,

View file

@ -23,12 +23,21 @@
#include "sdbpapi.h" #include "sdbpapi.h"
#else /* !defined(SDBWRITE_HOSTTOOL) */ #else /* !defined(SDBWRITE_HOSTTOOL) */
#include <typedefs.h> #include <typedefs.h>
#include <guiddef.h>
#include "sdbtypes.h" #include "sdbtypes.h"
#include "sdbpapi.h" #include "sdbpapi.h"
#endif /* !defined(SDBWRITE_HOSTTOOL) */ #endif /* !defined(SDBWRITE_HOSTTOOL) */
#include "sdbstringtable.h" #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 #define DEFAULT_TABLE_SIZE 0x100
typedef struct SdbHashEntry typedef struct SdbHashEntry
@ -98,13 +107,48 @@ static DWORD StringHash(const WCHAR* str)
return hash; 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) static struct SdbHashEntry** TableFindPtr(struct SdbStringHashTable* table, const WCHAR* str)
{ {
DWORD hash = StringHash(str); DWORD hash = StringHash(str);
struct SdbHashEntry** entry = &table->Entries[hash % table->Size]; struct SdbHashEntry** entry = &table->Entries[hash % table->Size];
while (*entry) while (*entry)
{ {
if (!wcscmp((*entry)->Name, str)) if (!Sdbwcscmp((*entry)->Name, str))
return entry; return entry;
entry = &(*entry)->Next; 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) static BOOL HashAddString(struct SdbStringHashTable* table, struct SdbHashEntry** position, const WCHAR* str, TAGID tagid)
{ {
struct SdbHashEntry* entry; struct SdbHashEntry* entry;
SIZE_T size; SIZE_T size, len;
if (!position) if (!position)
position = TableFindPtr(table, str); 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); entry = (*position) = SdbAlloc(size);
if (!entry) if (!entry)
{ {
@ -127,7 +172,7 @@ static BOOL HashAddString(struct SdbStringHashTable* table, struct SdbHashEntry*
return FALSE; return FALSE;
} }
entry->Tagid = tagid; entry->Tagid = tagid;
wcscpy(entry->Name, str); Sdbwcscpy(entry->Name, len, str);
return TRUE; return TRUE;
} }

View file

@ -47,9 +47,10 @@ static void WINAPI SdbpWrite(PDB db, const void* data, DWORD size)
{ {
if (db->write_iter + size > db->size) if (db->write_iter + size > db->size)
{ {
DWORD oldSize = db->size;
/* Round to powers of two to prevent too many reallocations */ /* Round to powers of two to prevent too many reallocations */
while (db->size < db->write_iter + size) db->size <<= 1; 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); memcpy(db->data + db->write_iter, data, size);

View file

@ -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