-
Notifications
You must be signed in to change notification settings - Fork 14
/
BDOFiles.cpp
748 lines (602 loc) · 20.3 KB
/
BDOFiles.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#if _MSC_VER >= 1910
#include "BDOFiles-exp.h"
namespace fs = std::experimental::filesystem;
#else
#include "BDOFiles-boost.h"
namespace fs = boost::filesystem;
#endif
using namespace BDO;
/// ***** FileEntry Structure ***** ///
FileEntry::FileEntry() :
uiFileHash(0),
uiFolderNum(0),
uiFileNum(0),
uiPazNum(0),
uiOffset(0),
uiCompressedSize(0),
uiOriginalSize(0),
sFileName(""),
sFilePath("")
{};
/// ***** BDOFile Class ***** ///
///constructor
BDOFile::BDOFile() :
IceKey(0),
bQuiet(false),
bNoFolders(false),
bYesToAll(false),
bRenameFiles(false),
bOverwriteFiles(false),
bCreatePath(false),
bMobile(false),
vFilesTable(),
mPazNames(),
ArchivePath()
{
const unsigned char decrypction_key[8] = { 0x51, 0xF3, 0x0F, 0x11, 0x04, 0x24, 0x6A, 0x00 }; /// The Black Desert ICE decryption key
this->set(decrypction_key);
}
///Public functions
void BDOFile::ExtractFile(fs::path FilePath, fs::path PazName, uint32_t uiOffset, uint32_t uiCompressedSize, uint32_t uiOriginalSize)
{
this->internalExtractFile(FilePath, PazName, uiOffset, uiCompressedSize, uiOriginalSize);
}
uint32_t BDOFile::ExtractFileMask(std::string sFileMask, fs::path OutputPath)
{
std::vector<FileEntry>::iterator it;
uint32_t counter = 0;
bool bProgress = false;
bool bIsQuiet = bQuiet;
this->bOverwriteFiles = this->GetYesToAll();
this->bCreatePath = this->GetYesToAll();
if (sFileMask.empty()) { ///turn off progressbar when listing all files
this->SetQuiet(true);
}
///make sure that output folder exists
if (!fs::exists(OutputPath)) {
if (this->bCreatePath) {
fs::create_directories(OutputPath);
}
else {
std::string sUserInput;
char c;
bool bInput = true;
std::cerr << "\nPath \"" << OutputPath.string() << "\" doesn't exist.\n>Create path? (Y)es / (n)o" << std::endl;
std::cin.clear();
while (bInput) {
std::getline(std::cin, sUserInput);
if (sUserInput.empty()) {
c = 'y';
}
else {
c = tolower(sUserInput[0]);
}
switch (c) {
case 'n':
case 'e':
exit(0);
case 'y':
fs::create_directories(OutputPath);
bInput = false;
}
}
}
}
///set working path to OutputPath because I want shorter path in user prompt if directory doesn't exist
fs::path currentPath = fs::current_path();
fs::current_path(OutputPath);
for (it = this->vFilesTable.begin(); it != this->vFilesTable.end(); ++it) {
if (WildMatch(sFileMask, it->sFilePath)) {
fs::path FilePath = OutputPath;
if (this->GetNoFolders()) {
FilePath /= it->sFileName;
} else {
FilePath /= it->sFilePath;
}
this->internalExtractFile(FilePath, this->GetPazName(it->uiPazNum), it->uiOffset, it->uiCompressedSize, it->uiOriginalSize);
if (bProgress) {
printProgress(); ///delete current line (progress bar)
bProgress = false;
}
if (!bIsQuiet) {
std::cout << "> " << it->sFilePath << " (size: " << it->uiOriginalSize << ")\n";
}
counter++;
}
if (!this->GetQuiet()) {
bProgress = printProgress(it - this->vFilesTable.begin() + 1, this->vFilesTable.size(), "Searching: ") || bProgress;
}
}
///restore original working path
fs::current_path(currentPath);
this->SetQuiet(bIsQuiet);
if (!this->GetQuiet()) {
if (bProgress) { ///delete current line (progress bar)
printProgress(); ///delete current line (progress bar)
}
std::cout << "\nExtracted files: " << counter << ", total files: " << this->vFilesTable.size() << std::endl;
}
return counter;
}
uint32_t BDOFile::List()
{
return this->ListFileMask("");
}
uint32_t BDOFile::ListFileMask(std::string sFileMask)
{
std::vector<FileEntry>::iterator it;
uint32_t counter = 0;
bool bProgress = false;
bool bIsQuiet = bQuiet;
if (sFileMask.empty()) { ///turn off progressbar when listing all files
this->SetQuiet(true);
}
for (it = this->vFilesTable.begin(); it != this->vFilesTable.end(); ++it) {
if (WildMatch(sFileMask, it->sFilePath)) {
if (bProgress) {
printProgress(); ///delete current line (progress bar)
bProgress = false;
}
std::cout << "[" << this->GetPazName(it->uiPazNum).filename().string() << "] " << it->sFilePath << " (size: " << it->uiOriginalSize << ")\n";
counter++;
}
if (!this->GetQuiet()) {
bProgress = printProgress(it - this->vFilesTable.begin() + 1, this->vFilesTable.size(), "Searching: ") || bProgress;
}
}
this->SetQuiet(bIsQuiet);
if (!this->GetQuiet()) {
if (bProgress) {
printProgress(); ///delete current line (progress bar)
}
std::cout << "\nListed files: " << counter << ", total files: " << vFilesTable.size() << std::endl;
}
return counter;
}
bool BDOFile::GetQuiet()
{
return this->bQuiet;
}
void BDOFile::SetQuiet(bool bQuiet)
{
this->bQuiet = bQuiet;
}
bool BDOFile::GetNoFolders()
{
return this->bNoFolders;
}
void BDOFile::SetNoFolders(bool bNoFolders)
{
this->bNoFolders = bNoFolders;
}
bool BDOFile::GetYesToAll()
{
return this->bYesToAll;
}
void BDOFile::SetYesToAll(bool bYesToAll)
{
this->bYesToAll = bYesToAll;
}
fs::path BDOFile::GetArchivePath()
{
return this->ArchivePath;
}
void BDOFile::SetArchivePath(fs::path ArchivePath)
{
this->ArchivePath = ArchivePath;
this->mPazNames.clear();
}
bool BDOFile::GetMobile()
{
return this->bMobile;
}
void BDOFile::SetMobile(bool bMobile)
{
this->bMobile = bMobile;
}
///protected functions
void BDOFile::exitError(int errCode, std::string sDetail) {
switch (errCode) {
case -2:
std::cerr << "ERROR: Can't open file " << sDetail << std::endl;
case -3:
std::cerr << "ERROR: Insufficient memory." << std::endl;
case -4:
std::cerr << "ERROR: Invalid compressed size." << std::endl;
default:
std::cerr << "ERROR: " << sDetail << " (code: " << errCode << ")." << std::endl;
}
exit(errCode);
}
void BDOFile::exitError(int errCode)
{
this->exitError(errCode, "");
}
void BDOFile::ICEdecrypt(uint8_t *encrypted, uint8_t *decrypted, uint32_t dataSize)
{
uint8_t uBlockSize = this->blockSize();
if (dataSize % uBlockSize != 0)
this->exitError(-4);
uint32_t blocks = dataSize / uBlockSize;
while (blocks--) {
this->decrypt(encrypted, decrypted);
encrypted += uBlockSize;
decrypted += uBlockSize;
}
encrypted -= dataSize; ///reset the pointers back to the beginning.
decrypted -= dataSize;
}
void BDOFile::ICEencrypt(uint8_t *decrypted, uint8_t *encrypted, uint32_t dataSize)
{
uint8_t uBlockSize = this->blockSize();
if (dataSize % uBlockSize != 0)
exitError(-4);
uint32_t blocks = dataSize / uBlockSize;
while (blocks--) {
this->encrypt(decrypted, encrypted);
encrypted += uBlockSize;
decrypted += uBlockSize;
}
encrypted -= dataSize; ///reset the pointers back to the beginning.
decrypted -= dataSize;
}
///returns path for uiPazNum, paths are stored in map and created on demand
fs::path BDOFile::GetPazName(uint32_t uiPazNum)
{
std::map<uint32_t, fs::path>::iterator it;
fs::path PazName;
it = this->mPazNames.lower_bound(uiPazNum);
if (it != mPazNames.end()) {
if (it->first == uiPazNum) {
return it->second;
}
else {
it++; ///map.insert -> C++11 The function optimizes its insertion time if position points to the element that will follow the inserted element (or to the end, if it would be the last).
}
}
std::string sPazName;
std::stringstream ss(sPazName);
ss << "pad" << std::setw(5) << std::setfill('0') << uiPazNum << ".paz";
PazName = this->GetArchivePath() / ss.str();
this->mPazNames.insert(it, std::pair<uint32_t, fs::path>(uiPazNum, PazName));
return PazName;
}
///Private functions
void BDOFile::internalExtractFile(fs::path FilePath, fs::path PazName, uint32_t uiOffset, uint32_t uiCompressedSize, uint32_t uiOriginalSize)
{
if (!this->GetMobile()) {
if (uiCompressedSize % 8 != 0)
this->exitError(-4);
}
///make sure that output folder exists
if (FilePath.has_parent_path() && !fs::exists(FilePath.parent_path())) {
if (this->bCreatePath) {
fs::create_directories(FilePath.parent_path());
}
else {
switch (createPathPrompt(FilePath)) { ///(Y)es / (a)lways / (s)kip file / (e)xit
case 'e':
exit(0);
case 'a':
this->bCreatePath = true;
}
}
}
if (uiCompressedSize == 0) { //just create empty file
std::ofstream ofsFile(FilePath.string(), std::ios::binary);
ofsFile.close();
}
else {
std::ifstream ifsPazFile(PazName.string(), std::ios::binary);
if (!ifsPazFile.is_open()) {
this->exitError(-2, PazName.string());
}
///check if output file exists, ask user if he wants to overwrite it
if (!this->bOverwriteFiles) {
if (fs::exists(FilePath)) {
if (this->bRenameFiles) {
autoRenameFile(FilePath);
}
else {
switch (autoRenameFilePrompt(FilePath)) { ///options: (o)verwrite / (R)ename / overwrite (a)ll / re(n)ame all / (e)xit
case 'e':
exit(0);
case 'a':
this->bOverwriteFiles = true;
break;
case 'n':
this->bRenameFiles = true;
}
}
}
}
std::ofstream ofsFile(FilePath.string(), std::ios::binary);
if (!ofsFile.is_open()) {
this->exitError(-2, FilePath.string());
}
///decrypt data
uint8_t *decrypted = new uint8_t[uiCompressedSize];
if (decrypted == 0) exitError(-3);
ifsPazFile.seekg(uiOffset);
if (!this->GetMobile()) {
uint8_t *encrypted = new uint8_t[uiCompressedSize];
if (encrypted == 0) exitError(-3);
ifsPazFile.read(reinterpret_cast<char *>(encrypted), uiCompressedSize);
this->ICEdecrypt(encrypted, decrypted, uiCompressedSize);
delete[] encrypted;
///check if data have header, valid header is 9 bytes long and contains:
///- ID (unit8_t) = 0x6E for uncompressed data or 0x6F for compressed data
///- data size (uint32_t)
///- original file size (unit32_t)
if ((decrypted[0] == 0x6F || decrypted[0] == 0x6E) && uiCompressedSize > 9) {
uint32_t uiSize = 0;
memcpy(&uiSize, decrypted + 1 + 4, 4); ///copy original file size from decrypted data
if (uiSize == uiOriginalSize) { ///We can consider data header as valid. Size in data header is the same as size in .meta/.paz file.
uint8_t *decompressed = new uint8_t[uiOriginalSize];
if (decompressed == 0) exitError(-3);
BDO::decompress(decrypted, decompressed);
delete[] decrypted;
decrypted = decompressed;
}
}
} else {
ifsPazFile.read(reinterpret_cast<char *>(decrypted), uiCompressedSize);
if (uiOriginalSize != uiCompressedSize) {
uint8_t *decompressed = new uint8_t[uiOriginalSize];
if (decompressed == 0) exitError(-3);
int result = uncompress(decompressed, reinterpret_cast<uLongf *>(&uiOriginalSize), decrypted, uiCompressedSize);
if (result == Z_OK) {
delete[] decrypted;
decrypted = decompressed;
} else if (result == Z_MEM_ERROR) {
exitError(-5, "zlib - Not enough memory.");
} else if (result == Z_BUF_ERROR) {
exitError(-5, "zlib - Output buffer is too small.");
} else if (result == Z_DATA_ERROR) {
exitError(-5, "zlib - Input data are corrupted or incomplete.");
}
}
}
ofsFile.write(reinterpret_cast<char *>(decrypted), uiOriginalSize);
ofsFile.close();
delete[] decrypted;
}
}
/// ***** MetaFile class ***** ///
///constructor
MetaFile::MetaFile() :
uiClientVersion(0),
uiFilesCount(0),
uiFileNamesCount(0),
uiFoldersCount(0)
{
}
MetaFile::MetaFile(fs::path FileName, bool bQuiet) :
uiClientVersion(0),
uiFilesCount(0),
uiFileNamesCount(0),
uiFoldersCount(0)
{
this->SetQuiet(bQuiet);
this->ReadSource(FileName);
};
///public functions
uint32_t MetaFile::GetClientVersion()
{
return uiClientVersion;
}
uint32_t MetaFile::GetFilesCount()
{
return uiFilesCount;
}
uint32_t MetaFile::GetFileNamesCount()
{
return uiFileNamesCount;
}
uint32_t MetaFile::GetFoldersCount()
{
return uiFoldersCount;
}
///private functions
void MetaFile::ReadSource(fs::path FileName)
{
std::ifstream ifsMetaFile;
uint32_t uiFolderNamesLength;
uint32_t uiFileNamesLength;
uint32_t uiPazCount;
uint8_t readBuffer[32];
uint8_t *pFilesInfo;
uint8_t *pFolderNamesEncrypted, *pFolderNamesDecrypted;
uint8_t *pFileNamesEncrypted, *pFileNamesDecrypted;
uint8_t *ptr, *pEnd;
std::vector<std::string> vFolderNames;
std::vector<std::string> vFileNames;
ifsMetaFile.open(FileName.string(), std::ios::binary);
if (!ifsMetaFile.is_open()) this->exitError(-2, FileName.string());
this->SetArchivePath(FileName.parent_path());
ifsMetaFile.read(reinterpret_cast<char *>(readBuffer), 8);
memcpy(&this->uiClientVersion, readBuffer, 4);
memcpy(&uiPazCount, readBuffer + 4, 4);
if (!this->GetQuiet()) {
std::cout << "\nClient version: " << uiClientVersion << std::endl;
}
///Game client doesn't use all .paz files, so uiPazCount is less then actual number of .paz files in BDO folder.
///For example NA/EU client v478 doesn't use 43 .paz numbers: 5504-5525, 5535-5552, 5591, 5601 and 5602.
///as there is no real use of PazInfo, just skip it...
ifsMetaFile.seekg(uiPazCount * 12, std::ios::cur);
ifsMetaFile.read(reinterpret_cast<char *>(readBuffer), 4);
memcpy(&this->uiFilesCount, readBuffer, 4);
if (!this->GetQuiet()) {
std::cout << "Number of stored files: " << this->uiFilesCount << std::endl;
}
pFilesInfo = new uint8_t[this->uiFilesCount * 28];
if (pFilesInfo == 0) this->exitError(-3);
ifsMetaFile.read(reinterpret_cast<char *>(pFilesInfo), this->uiFilesCount * 28);
//folder names
ifsMetaFile.read(reinterpret_cast<char *>(readBuffer), 4);
memcpy(&uiFolderNamesLength, readBuffer, 4);
pFolderNamesEncrypted = new uint8_t[uiFolderNamesLength];
if (pFolderNamesEncrypted == 0) this->exitError(-3);
pFolderNamesDecrypted = new uint8_t[uiFolderNamesLength];
if (pFolderNamesDecrypted == 0) this->exitError(-3);
ifsMetaFile.read(reinterpret_cast<char *>(pFolderNamesEncrypted), uiFolderNamesLength);
///test if meta file is from BDO mobile (names are not encrypted and all folder names starts with "res/")
if (pFolderNamesEncrypted[8] == 'r' && pFolderNamesEncrypted[9] == 'e' && pFolderNamesEncrypted[10] == 's') {
this->SetMobile(true);
memcpy(pFolderNamesDecrypted, pFolderNamesEncrypted, uiFolderNamesLength);
} else {
this->ICEdecrypt(pFolderNamesEncrypted, pFolderNamesDecrypted, uiFolderNamesLength);
}
delete[] pFolderNamesEncrypted;
ptr = pFolderNamesDecrypted;
pEnd = ptr + uiFolderNamesLength - 8;
while (ptr < pEnd) {
ptr += 8; ///skip 2x int
vFolderNames.push_back(reinterpret_cast<char *>(ptr));
ptr += vFolderNames.back().length() + 1; ///length + zero terminator
}
delete[] pFolderNamesDecrypted;
this->uiFoldersCount = vFolderNames.size();
if (!this->GetQuiet()) {
std::cout << "Number of stored folder names: " << uiFoldersCount << std::endl;
}
///file names
ifsMetaFile.read(reinterpret_cast<char *>(readBuffer), 4);
memcpy(&uiFileNamesLength, readBuffer, 4);
pFileNamesDecrypted = new uint8_t[uiFileNamesLength];
if (pFileNamesDecrypted == 0) this->exitError(-3);
if (!this->GetMobile()) {
pFileNamesEncrypted = new uint8_t[uiFileNamesLength];
if (pFileNamesEncrypted == 0) this->exitError(-3);
ifsMetaFile.read(reinterpret_cast<char *>(pFileNamesEncrypted), uiFileNamesLength);
ICEdecrypt(pFileNamesEncrypted, pFileNamesDecrypted, uiFileNamesLength);
delete[] pFileNamesEncrypted;
} else {
ifsMetaFile.read(reinterpret_cast<char *>(pFileNamesDecrypted), uiFileNamesLength);
}
ptr = pFileNamesDecrypted;
pEnd = ptr + uiFileNamesLength;
while (ptr < pEnd) {
vFileNames.push_back(reinterpret_cast<char *>(ptr));
ptr += vFileNames.back().length() + 1;
}
delete[] pFileNamesDecrypted;
this->uiFileNamesCount = vFileNames.size();
if (!this->GetQuiet()) {
std::cout << "Number of stored file names: " << this->uiFileNamesCount << "\n" << std::endl;
}
///fill vFilesTable
for (uint32_t i = 0; i < this->uiFilesCount; ++i) {
FileEntry fileEntry;
memcpy(&fileEntry.uiFileHash, pFilesInfo + i * 28 + 0, 4);
memcpy(&fileEntry.uiFolderNum, pFilesInfo + i * 28 + 4, 4);
memcpy(&fileEntry.uiFileNum, pFilesInfo + i * 28 + 8, 4);
memcpy(&fileEntry.uiPazNum, pFilesInfo + i * 28 + 12, 4);
memcpy(&fileEntry.uiOffset, pFilesInfo + i * 28 + 16, 4);
memcpy(&fileEntry.uiCompressedSize, pFilesInfo + i * 28 + 20, 4);
memcpy(&fileEntry.uiOriginalSize, pFilesInfo + i * 28 + 24, 4);
fileEntry.sFileName = vFileNames.at(fileEntry.uiFileNum);
fileEntry.sFilePath = vFolderNames.at(fileEntry.uiFolderNum) + fileEntry.sFileName;
if (!this->GetQuiet()) {
printProgress(i + 1, this->uiFilesCount, "Building files info: ");
}
this->vFilesTable.push_back(fileEntry);
}
if (!this->GetQuiet()) {
printProgress(); //delete current line (progress bar)
}
delete[] pFilesInfo;
}
/// ***** PazFile class ***** ///
///constructors
PazFile::PazFile() :
uiPazHash(0),
uiFilesCount(0)
{
}
PazFile::PazFile(fs::path FileName, bool bQuiet) :
uiPazHash(0),
uiFilesCount(0)
{
this->SetQuiet(bQuiet);
this->ReadSource(FileName);
};
void PazFile::ReadSource(fs::path FileName)
{
std::ifstream ifsPazFile;
uint32_t uiPazNum;
uint32_t uiNamesLength;
uint8_t readBuffer[32];
uint8_t *pFilesInfo;
uint8_t *pNamesEncrypted, *pNamesDecrypted;
uint8_t *ptr, *pEnd;
std::vector<std::string> vNames;
ifsPazFile.open(FileName.string(), std::ios::binary);
if (!ifsPazFile.is_open()) this->exitError(-2, FileName.string());
this->SetArchivePath(FileName.parent_path());
std::istringstream iss(FileName.stem().string()); ///stem (file name without extension) should be "pad012345"
iss.seekg(3, std::istringstream::beg); ///move to the first digit
iss >> uiPazNum;
ifsPazFile.read(reinterpret_cast<char *>(readBuffer), 12);
memcpy(&this->uiPazHash, readBuffer, 4);
memcpy(&this->uiFilesCount, readBuffer + 4, 4);
memcpy(&uiNamesLength, readBuffer + 8, 4);
if (!this->GetQuiet()) {
std::cout << "\nNumber of stored files: " << this->uiFilesCount << "\n" << std::endl;
}
///read files info
pFilesInfo = new uint8_t[this->uiFilesCount * 24];
if (pFilesInfo == 0) this->exitError(-3);
ifsPazFile.read(reinterpret_cast<char *>(pFilesInfo), this->uiFilesCount * 24);
///read & decrypt file names
pNamesEncrypted = new uint8_t[uiNamesLength];
if (pNamesEncrypted == 0) exitError(-3);
pNamesDecrypted = new uint8_t[uiNamesLength];
if (pNamesDecrypted == 0) exitError(-3);
ifsPazFile.read(reinterpret_cast<char *>(pNamesEncrypted), uiNamesLength);
///test if meta file is from BDO mobile (names are not encrypted and always starts with folder name "res/")
if (pNamesEncrypted[0] == 'r' && pNamesEncrypted[1] == 'e' && pNamesEncrypted[2] == 's') {
this->SetMobile(true);
memcpy(pNamesDecrypted, pNamesEncrypted, uiNamesLength);
} else {
this->ICEdecrypt(pNamesEncrypted, pNamesDecrypted, uiNamesLength);
}
delete[] pNamesEncrypted;
ptr = pNamesDecrypted;
pEnd = ptr + uiNamesLength;
while (ptr < pEnd) {
vNames.push_back(reinterpret_cast<char *>(ptr));
ptr += vNames.back().length() + 1; ///length + zero terminator
}
delete[] pNamesDecrypted;
///fill vFilesTable
for (uint32_t i = 0; i < uiFilesCount; ++i) {
FileEntry fileEntry;
memcpy(&fileEntry.uiFileHash, pFilesInfo + i * 24 + 0, 4);
memcpy(&fileEntry.uiFolderNum, pFilesInfo + i * 24 + 4, 4);
memcpy(&fileEntry.uiFileNum, pFilesInfo + i * 24 + 8, 4);
memcpy(&fileEntry.uiOffset, pFilesInfo + i * 24 + 12, 4);
memcpy(&fileEntry.uiCompressedSize, pFilesInfo + i * 24 + 16, 4);
memcpy(&fileEntry.uiOriginalSize, pFilesInfo + i * 24 + 20, 4);
fileEntry.uiPazNum = uiPazNum;
fileEntry.sFileName = vNames.at(fileEntry.uiFileNum);
fileEntry.sFilePath = vNames.at(fileEntry.uiFolderNum) + fileEntry.sFileName;
if (!this->GetQuiet()) {
printProgress(i + 1, uiFilesCount, "Building files info: ");
}
this->vFilesTable.push_back(fileEntry);
}
if (!this->GetQuiet()) {
printProgress(); ///delete current line (progress bar)
}
delete[] pFilesInfo;
}
///public functions
uint32_t PazFile::GetPazHash()
{
return this->uiPazHash;
}
uint32_t PazFile::GetFilesCount()
{
return this->uiFilesCount;
}