ReactOS 0.4.15-dev-8339-g4028de8
sdbwrite.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Application compatibility module
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Shim database manipulation functions
5 * COPYRIGHT: Copyright 2011 André Hentschel
6 * Copyright 2013 Mislav Blažević
7 * Copyright 2015-2019 Mark Jansen (mark.jansen@reactos.org)
8 */
9
10#if !defined(SDBWRITE_HOSTTOOL)
11#define WIN32_NO_STATUS
12#include "windef.h"
13#include "ntndk.h"
14#include <appcompat/sdbtypes.h>
15#include <appcompat/sdbtagid.h>
16#else
17#include <typedefs.h>
18#include <guiddef.h>
19#include <sdbtypes.h>
20#include <sdbtagid.h>
21#endif
22
23
24#include "sdbpapi.h"
25#include "sdbstringtable.h"
26
27
28/* Local functions */
33
34/* sdbapi.c */
36
37/* Copy data to the allocated database */
38static void WINAPI SdbpWrite(PDB pdb, const void* data, DWORD size)
39{
40 ASSERT(pdb->for_write);
41 if (pdb->write_iter + size > pdb->size)
42 {
43 DWORD oldSize = pdb->size;
44 /* Round to powers of two to prevent too many reallocations */
45 while (pdb->size < pdb->write_iter + size) pdb->size <<= 1;
46 pdb->data = SdbReAlloc(pdb->data, pdb->size, oldSize);
47 }
48
49 memcpy(pdb->data + pdb->write_iter, data, size);
50 pdb->write_iter += size;
51}
52
53/* Add a string to the string table (creating it when it did not exist yet),
54 returning if it succeeded or not */
56{
57 PDB buf = pdb->string_buffer;
58 ASSERT(pdb->for_write);
59
60 if (pdb->string_buffer == NULL)
61 {
62 pdb->string_buffer = buf = SdbAlloc(sizeof(DB));
63 if (buf == NULL)
64 return FALSE;
65 buf->size = 128;
66 buf->data = SdbAlloc(buf->size);
67 buf->for_write = TRUE;
68 if (buf->data == NULL)
69 return FALSE;
70 }
71
72 *tagid = buf->write_iter + sizeof(TAG) + sizeof(DWORD);
73 if (SdbpAddStringToTable(&pdb->string_lookup, string, tagid))
75
76 return pdb->string_lookup != NULL;
77}
78
79/* Write the in-memory stringtable to the specified db */
81{
83 PDB buf = pdb->string_buffer;
84 if (buf == NULL || pdb->string_lookup == NULL)
85 return FALSE;
86
88 SdbpWrite(pdb, buf->data, buf->write_iter);
90}
91
105{
106 static const DWORD version_major = 2, version_minor = 1;
107 static const char* magic = "sdbf";
108 PDB pdb;
109
111 if (!pdb)
112 return NULL;
113
114 pdb->size = sizeof(DWORD) + sizeof(DWORD) + (DWORD)strlen(magic);
115 pdb->data = SdbAlloc(pdb->size);
116
117 SdbpWrite(pdb, &version_major, sizeof(DWORD));
118 SdbpWrite(pdb, &version_minor, sizeof(DWORD));
120
121 return pdb;
122}
123
130{
131 ASSERT(pdb->for_write);
133 SdbpFlush(pdb);
135}
136
146{
148 return FALSE;
149
150 SdbpWrite(pdb, &tag, sizeof(TAG));
151 return TRUE;
152}
153
164{
166 return FALSE;
167
168 SdbpWrite(pdb, &tag, sizeof(TAG));
169 SdbpWrite(pdb, &data, sizeof(data));
170 return TRUE;
171}
172
183{
185 return FALSE;
186
187 SdbpWrite(pdb, &tag, sizeof(TAG));
188 SdbpWrite(pdb, &data, sizeof(data));
189 return TRUE;
190}
191
202{
204 return FALSE;
205
206 SdbpWrite(pdb, &tag, sizeof(TAG));
207 SdbpWrite(pdb, &data, sizeof(data));
208 return TRUE;
209}
210
221{
222 DWORD size;
223
225 {
226 TAGID tagid = 0;
227 if (!SdbpGetOrAddStringRef(pdb, string, &tagid))
228 return FALSE;
229
230 return SdbWriteStringRefTag(pdb, tag, tagid);
231 }
232
234 return FALSE;
235
236 size = SdbpStrsize(string);
237 SdbpWrite(pdb, &tag, sizeof(TAG));
238 SdbpWrite(pdb, &size, sizeof(size));
239 SdbpWrite(pdb, string, size);
240 return TRUE;
241}
242
254{
256 return FALSE;
257
258 SdbpWrite(pdb, &tag, sizeof(TAG));
259 SdbpWrite(pdb, &tagid, sizeof(tagid));
260 return TRUE;
261}
262
274{
276 return FALSE;
277
278 SdbpWrite(pdb, &tag, sizeof(TAG));
279 SdbpWrite(pdb, &size, sizeof(size));
281 return TRUE;
282}
283
284#if !defined(SDBWRITE_HOSTTOOL)
295{
296 MEMMAPPED mapped;
297
299 return FALSE;
300
301 if (!SdbpOpenMemMappedFile(path, &mapped))
302 return FALSE;
303
304 SdbWriteBinaryTag(pdb, tag, mapped.view, mapped.size);
305 SdbpCloseMemMappedFile(&mapped);
306 return TRUE;
307}
308#endif
309
322{
323 TAGID list_id;
324 DWORD dum = 0;
325
327 return TAGID_NULL;
328
329 list_id = pdb->write_iter;
330 SdbpWrite(pdb, &tag, sizeof(TAG));
331 SdbpWrite(pdb, &dum, sizeof(dum)); /* reserve some memory for storing list size */
332 return list_id;
333}
334
344{
345 ASSERT(pdb->for_write);
346
348 return FALSE;
349
350 /* Write size of list to list tag header */
351 *(DWORD*)&pdb->data[tagid + sizeof(TAG)] = pdb->write_iter - tagid - sizeof(TAG) - sizeof(TAGID);
352 return TRUE;
353}
354
CDumExe dum
Definition: CComObject.cpp:71
#define TAG(x)
Definition: ObTypes.c:145
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
DWORD SdbpStrsize(PCWSTR string)
Definition: sdbapi.c:157
DWORD TAGID
INT PATH_TYPE
static PDB pdb
Definition: db.cpp:172
#define TAGID_NULL
Definition: db.cpp:38
#define TAG_STRINGTABLE
Definition: db.cpp:94
#define TAG_STRINGTABLE_ITEM
Definition: db.cpp:96
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLsizeiptr size
Definition: glext.h:5919
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define ASSERT(a)
Definition: mode.c:44
#define TAG_TYPE_WORD
Definition: apphelp.c:38
#define TAG_TYPE_BINARY
Definition: apphelp.c:44
#define TAG_TYPE_NULL
Definition: apphelp.c:36
#define TAG_TYPE_LIST
Definition: apphelp.c:42
#define TAG_TYPE_STRING
Definition: apphelp.c:43
u32_t magic(void)
#define DWORD
Definition: nt_native.h:44
BOOL WINAPI SdbpCheckTagType(TAG tag, WORD type)
Definition: sdbapi.c:254
void WINAPI SdbpCloseMemMappedFile(PMEMMAPPED mapping)
Definition: sdbapi.c:244
void WINAPI SdbpFlush(PDB pdb)
Definition: sdbapi.c:140
BOOL WINAPI SdbpCheckTagIDType(PDB pdb, TAGID tagid, WORD type)
Definition: sdbapi.c:261
BOOL WINAPI SdbpOpenMemMappedFile(LPCWSTR path, PMEMMAPPED mapping)
Definition: sdbapi.c:170
PDB WINAPI SdbpCreate(LPCWSTR path, PATH_TYPE type, BOOL write)
Definition: sdbapi.c:93
#define SdbAlloc(size)
Definition: sdbpapi.h:35
#define SdbReAlloc(mem, size, oldSize)
Definition: sdbpapi.h:36
BOOL SdbpAddStringToTable(struct SdbStringHashTable **table, const WCHAR *str, TAGID *tagid)
BOOL WINAPI SdbWriteNULLTag(PDB pdb, TAG tag)
Definition: sdbwrite.c:145
static void WINAPI SdbpWrite(PDB pdb, const void *data, DWORD size)
Definition: sdbwrite.c:38
TAGID WINAPI SdbBeginWriteListTag(PDB pdb, TAG tag)
Definition: sdbwrite.c:321
BOOL WINAPI SdbWriteQWORDTag(PDB pdb, TAG tag, QWORD data)
Definition: sdbwrite.c:201
BOOL WINAPI SdbWriteStringTag(PDB pdb, TAG tag, LPCWSTR string)
Definition: sdbwrite.c:220
BOOL WINAPI SdbWriteStringRefTag(PDB pdb, TAG tag, TAGID tagid)
Definition: sdbwrite.c:253
static BOOL WINAPI SdbpWriteStringtable(PDB pdb)
Definition: sdbwrite.c:80
BOOL WINAPI SdbWriteBinaryTagFromFile(PDB pdb, TAG tag, LPCWSTR path)
Definition: sdbwrite.c:294
BOOL WINAPI SdbWriteDWORDTag(PDB pdb, TAG tag, DWORD data)
Definition: sdbwrite.c:182
void WINAPI SdbCloseDatabase(PDB)
Definition: sdbapi.c:373
static BOOL WINAPI SdbpGetOrAddStringRef(PDB pdb, LPCWSTR string, TAGID *tagid)
Definition: sdbwrite.c:55
BOOL WINAPI SdbEndWriteListTag(PDB pdb, TAGID tagid)
Definition: sdbwrite.c:343
BOOL WINAPI SdbWriteBinaryTag(PDB pdb, TAG tag, const BYTE *data, DWORD size)
Definition: sdbwrite.c:273
PDB WINAPI SdbCreateDatabase(LPCWSTR path, PATH_TYPE type)
Definition: sdbwrite.c:104
void WINAPI SdbCloseDatabaseWrite(PDB pdb)
Definition: sdbwrite.c:129
BOOL WINAPI SdbWriteWORDTag(PDB pdb, TAG tag, WORD data)
Definition: sdbwrite.c:163
#define TAG_TYPE_DWORD
Definition: shimdbg.c:107
#define TAG_TYPE_QWORD
Definition: shimdbg.c:108
#define TAG_TYPE_STRINGREF
Definition: shimdbg.c:109
UINT64 QWORD
Definition: shimdbg.c:104
Definition: sdbtypes.h:24
Definition: fs_rec.h:143
SIZE_T size
Definition: sdbpapi.h:46
PBYTE view
Definition: sdbpapi.h:45
Definition: ecma_167.h:138
#define WINAPI
Definition: msvc.h:6
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
unsigned char BYTE
Definition: xxhash.c:193