forked from shawwwn/H2OExtractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchive.cpp
348 lines (310 loc) · 10 KB
/
Archive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* Archive.cpp */
#include "Archive.h"
Archive::Archive()
{
init();
}
Archive::Archive(char* szFilePath)
{
init();
open(szFilePath);
}
Archive::~Archive()
{
close();
SAFE_DELETE_ARRAY(m_arrDirectoryParents);
SAFE_DELETE_ARRAY(m_pCompressedFileNameChunk);
SAFE_DELETE_ARRAY(m_pCompressedDirectoryNameChunk);
Util::Gen::clearVectorOfArray(m_DirectoryNameList);
Util::Gen::clearVectorOfArray(m_FileNameList);
}
uint64_t Archive::readComment(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
// Read Header
m_hH2O.read(m_Comment.szHeader, 8);
// Read VersionInfo
m_hH2O.read((char*)&m_Comment.VersionInfo, 4);
// Read Comments
uint64_t beg = m_hH2O.tellg();
char curByte = 0x0;
while (!m_hH2O.eof() && curByte!=H2O_COMMENT_TERMINATOR)
{
m_hH2O.read(&curByte, 1);
}
uint64_t end = m_hH2O.tellg();
m_hH2O.seekg(beg);
uint64_t length = end-beg;
m_Comment.szComments = new char[length];
memset(m_Comment.szComments, 0, length);
m_hH2O.read(m_Comment.szComments, length-1);
return end;
}
uint64_t Archive::readHeader(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
m_hH2O.read((char*)&m_Header, sizeof(ArchiveHeader));
return m_hH2O.tellg();
}
uint64_t Archive::readFileList(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
ArchiveFile curFile;
for (int i=0; i<m_Header.FileCount; i++)
{
m_hH2O.read((char*)&curFile, sizeof(ArchiveFile));
if (curFile.RawSize!=0 && curFile.CompressedSize!=0)
m_FileList.push_back(curFile); // file is valid
#if defined(H2O_DEBUG_LOG) && defined (H2O_PRINT_FILE_DESC)
DB::debugLog("- CompressionTag: ", curFile.CompressionTag, NULL);
DB::debugLog("- FolderNameIndex: ", curFile.FolderNameIndex, NULL);
DB::debugLog("- FileNameIndex: ", curFile.FileNameIndex, NULL);
DB::debugLog("- FileId: ", curFile.FileId, NULL);
DB::debugLog("- RawSize: ", curFile.RawSize, NULL);
DB::debugLog("- CompressedSize: ", curFile.CompressedSize, NULL);
DB::debugLog("- Offest: ", curFile.Offest, NULL);
DB::debugLog("- CRC32[hex]: ", curFile.CRC32, NULL, true);
DB::debugLog("- UnknownConstant: ", curFile.UnknownConstant, NULL);
DB::debugLog("-------------------", "", NULL);
#endif
}
return m_hH2O.tellg();
}
uint64_t Archive::readFileNameDesc(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
m_hH2O.read((char*)&m_FileNameDesc, sizeof(ArchiveFileNameDesc));
return m_hH2O.tellg();
}
uint64_t Archive::readFileNameChunk(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
m_pCompressedFileNameChunk = new char[m_FileNameDesc.CompressedSize];
m_hH2O.read(m_pCompressedFileNameChunk, m_FileNameDesc.CompressedSize);
// TODO: Decompress the file name chunk
return m_hH2O.tellg();
}
uint64_t Archive::readDirectoryNameDesc(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
m_hH2O.read((char*)&m_DirectoryNameDesc, sizeof(ArchiveDirectoryNameDesc));
return m_hH2O.tellg();
}
uint64_t Archive::readDirectoryNameChunk(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
m_pCompressedDirectoryNameChunk = new char[m_DirectoryNameDesc.CompressedSize];
m_hH2O.read(m_pCompressedDirectoryNameChunk, m_DirectoryNameDesc.CompressedSize);
// TODO: Decompress the file name chunk
return m_hH2O.tellg();
}
uint64_t Archive::readDirectoryParents(uint64_t streamPos)
{
m_hH2O.seekg(streamPos, std::ios::beg);
m_hH2O.read((char*)&m_DirectoryCount, sizeof(m_DirectoryCount));
m_arrDirectoryParents = new int32_t[m_DirectoryCount];
m_hH2O.read((char*)m_arrDirectoryParents, sizeof(*m_arrDirectoryParents)*m_DirectoryCount);
#ifdef H2O_PRINT_DIR_INHEIRTANCY
for (int i=0; i<m_DirectoryCount; i++)
{
DB::debugLog("-> ", *(m_arrDirectoryParents), "");
}
#endif
return m_hH2O.tellg();
}
bool Archive::parseStringChunk(ArchiveCompressedStringDesc& desc, ArchiveCompressedStringChunk& chunk,
char*& pCompressedBuffer, std::vector<wchar_t*>& outputVector)
{
// Decompress
char* pDecompressedBuffer = new char[desc.RawSize];
bool result = runblast_mem2mem(pCompressedBuffer, desc.RawSize, pDecompressedBuffer, *(uint32_t*)(desc.CRC32));
// Serialize the header of the chunk
memcpy(&chunk, pDecompressedBuffer, sizeof(chunk));
// Serialize the file names
//outputVector.clear();
uint32_t length = 0;
uint32_t offest = sizeof(chunk);
while (offest < desc.RawSize)
{
wchar_t* wszStartingPos = (wchar_t*)&pDecompressedBuffer[offest];
length = wcslen(wszStartingPos);
if (length==0)
break;
wchar_t* wszString = new wchar_t[length+1];
wcscpy(wszString, wszStartingPos);
outputVector.push_back(wszString);
offest += length*sizeof(wchar_t) + 2; // plus the length of null teminator
}
delete[] pDecompressedBuffer;
return result;
}
void Archive::open(char* szFilePath)
{
m_hH2O.open(szFilePath, std::ifstream::binary);
uint64_t curPos = 0;
curPos = readComment(curPos);
curPos = readHeader(curPos);
curPos = readFileList(curPos);
curPos = readDirectoryNameDesc(curPos);
curPos = readDirectoryNameChunk(curPos);
curPos = readFileNameDesc(curPos);
curPos = readFileNameChunk(curPos);
curPos = readDirectoryParents(curPos);
bool success;
// Parse compressed file names
success = parseStringChunk(m_FileNameDesc, m_FileNameChunk, m_pCompressedFileNameChunk, m_FileNameList);
#ifdef H2O_DEBUG_LOG
if (success)
DB::debugLog("\nFileNameChunk - decompress: ", "Success!", "");
else
DB::debugLog("\nFileNameChunk - decompress: ", "Fail!", "");
#ifdef H2O_PRINT_FILE_LIST
DB::debugLog("FileNames: ", "", "");
for (int i=0; i<m_FileNameList.size(); i++)
{
std::wcout << m_FileNameList[i] << std::endl;
}
DB::debugLog("---------------", "", "");
#endif
#endif
// Parse compressed directory names
success = parseStringChunk(m_DirectoryNameDesc, m_DirectoryNameChunk, m_pCompressedDirectoryNameChunk, m_DirectoryNameList);
#ifdef H2O_DEBUG_LOG
if (success)
DB::debugLog("\nDirectoryNameChunk - decompress: ", "Success!", "");
else
DB::debugLog("\nDirectoryNameChunk - decompress: ", "Fail!", "");
#ifdef H2O_PRINT_FILE_LIST
DB::debugLog("DirectoryNames: ", "", "");
for (int j=0; j<m_DirectoryNameList.size(); j++)
{
std::wcout << m_DirectoryNameList[j] << std::endl;
}
DB::debugLog("---------------", "", "");
#endif
#endif
}
// TODO: Refactor this function.
void Archive::extractByIndex(uint32_t index)
{
//
// Generate File Name
//
// TODO: Add folder structure
char fileName[128];
memset(fileName, 0, 128);
if (m_FileList[index].FileNameIndex==-1)
{
getFileName(index, "", fileName);
}
else
{
wtoc(m_FileNameList[index], fileName);
}
char fullPath[256];
getFullPath("output/", fileName, fullPath);
printf("%s\n", fileName);
//
// Extract File
//
ArchiveFile& rFile = m_FileList[index];
char* pFileBuffer;
m_hH2O.seekg(rFile.Offest, std::ios::beg);
// Decompress buffer if compressed
if (rFile.CompressionTag!=0)
{
ArchiveCompressedFileData tFileDesc;
m_hH2O.read((char*)&tFileDesc, sizeof(ArchiveCompressedFileData));
#ifdef H2O_PRINT_WHEN_EXTRACT
DB::debugLog("- CompressedSize: ", tFileDesc.CompressedSize, "");
DB::debugLog("- RawSize: ", tFileDesc.RawSize, "");
DB::debugLog("- CRC32[hex]: ", tFileDesc.CRC32, NULL, true);
#endif
pFileBuffer = new char[rFile.CompressedSize];
m_hH2O.read(pFileBuffer, rFile.CompressedSize);
char* pDecompressedBuffer = new char[tFileDesc.RawSize];
bool result = runblast_mem2mem(pFileBuffer, tFileDesc.RawSize, pDecompressedBuffer, *(uint32_t*)(tFileDesc.CRC32));
if (result)
{
#ifdef H2O_PRINT_WHEN_EXTRACT
DB::debugLog("- Decompress: ", "Success!", " - output decompressed data");
#endif
delete[] pFileBuffer; // delete the compressed buffer since we don't need it anymore
pFileBuffer = pDecompressedBuffer;
}
else
{
#ifdef H2O_PRINT_WHEN_EXTRACT
DB::debugLog("- Decompress: ", "Fail!", " - output compressed data");
#endif
delete[] pDecompressedBuffer;
}
pDecompressedBuffer = NULL;
}
else
{
pFileBuffer = new char[rFile.CompressedSize];
m_hH2O.read(pFileBuffer, rFile.CompressedSize);
}
// write file
std::ofstream destFile;
destFile.open(fullPath, std::ofstream::binary);
destFile.write(pFileBuffer, rFile.CompressedSize);
destFile.close();
delete[] pFileBuffer;
pFileBuffer = NULL;
}
void Archive::extractAll()
{
const char* dirPath = "output/";
if (Util::File::checkPathType(dirPath)!=Util::File::TYPE_DIR)
mkdir(dirPath);
for (int i=0; i<m_FileList.size(); i++) //m_Header.FileCount
{
extractByIndex(i);
}
}
void Archive::close()
{
m_hH2O.close();
}
void Archive::displayInfo()
{
printf("Header:\n");
DB::debugLog("- ArchiveHeader: ", m_Comment.szHeader, NULL);
DB::debugLog("- VersionInfo: ", m_Comment.VersionInfo, NULL);
DB::debugLog("- Comments: ", m_Comment.szComments, NULL);
DB::debugLog("- Version: ", m_Header.Version, NULL);
DB::debugLog("- FileCount: ", m_Header.FileCount, NULL);
DB::debugLog("- CompressedSize: ", m_Header.CompressedSize, NULL);
DB::debugLog("- RawSize: ", m_Header.RawSize, NULL);
printf("============\n");
printf("File:\n");
DB::debugLog("- CompressedSize: ", m_FileNameDesc.CompressedSize, NULL);
DB::debugLog("- RawSize: ", m_FileNameDesc.RawSize, NULL);
DB::debugLog("- CRC32[hex]: ", m_FileNameDesc.CRC32, NULL, true);
DB::debugLog("- FileNameCount: ", m_FileNameChunk.StringsCount, "");
DB::debugLog("- ChunkSize: ", m_FileNameChunk.ChunkSize, "");
printf("============\n");
printf("Directory:\n");
DB::debugLog("- CompressedSize: ", m_DirectoryNameDesc.CompressedSize, NULL);
DB::debugLog("- RawSize: ", m_DirectoryNameDesc.RawSize, NULL);
DB::debugLog("- CRC32[hex]: ", m_DirectoryNameDesc.CRC32, NULL, true);
DB::debugLog("- DirectoryNameCount: ", m_DirectoryNameChunk.StringsCount, "");
DB::debugLog("- ChunkSize: ", m_DirectoryNameChunk.ChunkSize, "");
DB::debugLog("- Directory Count: ", m_DirectoryCount, NULL);
printf("============\n");
}
//
// Private
//
void Archive::init()
{
memset(&m_Comment, 0, sizeof(m_Comment));
memset(&m_Header, 0, sizeof(m_Header));
memset(&m_Header, 0, sizeof(m_FileNameDesc));
memset(&m_Header, 0, sizeof(m_DirectoryNameDesc));
m_pCompressedDirectoryNameChunk = NULL;
m_pCompressedFileNameChunk = NULL;
m_arrDirectoryParents = NULL;
}