3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Implement platform agnostic read / write / allocation functions, parse commandline
5 * COPYRIGHT: Copyright 2016,2017 Mark Jansen (mark.jansen@reactos.org)
10 #include "sdbstringtable.h"
17 ULONG g_ShimDebugLevel
= SHIM_WARN
;
19 LPVOID WINAPI
SdbpAlloc(SIZE_T size
)
21 return ::calloc(1, size
);
24 LPVOID WINAPI
SdbpReAlloc(LPVOID mem
, SIZE_T size
, SIZE_T oldSize
)
26 LPVOID newMem
= ::realloc(mem
, size
);
27 if (newMem
&& size
> oldSize
)
29 memset((BYTE
*)newMem
+ oldSize
, 0, size
- oldSize
);
34 void WINAPI
SdbpFree(LPVOID mem
)
39 DWORD
SdbpStrlen(PCWSTR string
)
47 DWORD WINAPI
SdbpStrsize(PCWSTR string
)
49 return (SdbpStrlen(string
) + 1) * sizeof(WCHAR
);
52 PDB WINAPI
SdbpCreate(LPCWSTR path
, PATH_TYPE type
, BOOL write
)
56 std::string
pathA(path
, path
+ SdbpStrlen(path
));
58 f
= fopen(pathA
.c_str(), write
? "wb" : "rb");
62 pdb
= (PDB
)SdbAlloc(sizeof(DB
));
64 pdb
->for_write
= write
;
69 void WINAPI
SdbpFlush(PDB pdb
)
71 ASSERT(pdb
->for_write
);
73 fwrite(pdb
->data
, pdb
->write_iter
, 1, (FILE*)pdb
->file
);
76 void WINAPI
SdbCloseDatabase(PDB pdb
)
82 fclose((FILE*)pdb
->file
);
83 if (pdb
->string_buffer
)
84 SdbCloseDatabase(pdb
->string_buffer
);
85 if (pdb
->string_lookup
)
86 SdbpTableDestroy(&pdb
->string_lookup
);
91 BOOL WINAPI
SdbpCheckTagType(TAG tag
, WORD type
)
93 if ((tag
& TAG_TYPE_MASK
) != type
)
98 BOOL WINAPI
SdbpReadData(PDB pdb
, PVOID dest
, DWORD offset
, DWORD num
)
100 DWORD size
= offset
+ num
;
102 /* Either overflow or no data to read */
107 if (pdb
->size
< size
)
110 memcpy(dest
, pdb
->data
+ offset
, num
);
114 TAG WINAPI
SdbGetTagFromTagID(PDB pdb
, TAGID tagid
)
117 if (!SdbpReadData(pdb
, &data
, tagid
, sizeof(data
)))
122 BOOL WINAPI
SdbpCheckTagIDType(PDB pdb
, TAGID tagid
, WORD type
)
124 TAG tag
= SdbGetTagFromTagID(pdb
, tagid
);
127 return SdbpCheckTagType(tag
, type
);
130 BOOL WINAPIV
ShimDbgPrint(SHIM_LOG_LEVEL Level
, PCSTR FunctionName
, PCSTR Format
, ...)
133 const char* LevelStr
;
135 if ((ULONG
)Level
> g_ShimDebugLevel
)
153 printf("[%s][%-20s] ", LevelStr
, FunctionName
);
154 va_start(ArgList
, Format
);
155 vprintf(Format
, ArgList
);
161 #define TICKSPERSEC 10000000
162 #if defined(__GNUC__)
163 #define TICKSTO1970 0x019db1ded53e8000LL
165 #define TICKSTO1970 0x019db1ded53e8000i64
167 VOID NTAPI
RtlSecondsSince1970ToTime(IN ULONG SecondsSince1970
,
168 OUT PLARGE_INTEGER Time
)
170 Time
->QuadPart
= ((LONGLONG
)SecondsSince1970
* TICKSPERSEC
) + TICKSTO1970
;
177 bool xml_2_db(const char* xml
, const WCHAR
* sdb
);
179 static bool run_one(std::string
& input
, std::string
& output
)
181 sdbstring
outputW(output
.begin(), output
.end());
182 if (!xml_2_db(input
.c_str(), outputW
.c_str()))
188 static std::string
get_strarg(int argc
, char* argv
[], int& i
)
191 return std::string(argv
[i
] + 2);
194 if (i
>= argc
|| !argv
[i
])
195 return std::string();
199 static void update_loglevel(int argc
, char* argv
[], int& i
)
201 std::string value
= get_strarg(argc
, argv
, i
);
202 g_ShimDebugLevel
= strtoul(value
.c_str(), NULL
, 10);
205 int main(int argc
, char * argv
[])
207 std::string input
, output
;
210 for (int i
= 1; i
< argc
; ++i
)
212 if (argv
[i
][0] != '/' && argv
[i
][0] != '-')
218 input
= get_strarg(argc
, argv
, i
);
221 output
= get_strarg(argc
, argv
, i
);
224 update_loglevel(argc
, argv
, i
);
227 if (input
.empty() || output
.empty())
230 if (!run_one(input
, output
))
232 printf("Failed converting '%s' to '%s'\n", input
.c_str(), output
.c_str());