-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathextract.cpp
More file actions
1422 lines (1155 loc) · 40.8 KB
/
Copy pathextract.cpp
File metadata and controls
1422 lines (1155 loc) · 40.8 KB
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011-2020 Daniel Scharrer
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the author(s) be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "cli/extract.hpp"
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <limits>
#include <boost/foreach.hpp>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/unordered_map.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/range/size.hpp>
#include <boost/version.hpp>
#if BOOST_VERSION >= 104800
#include <boost/container/flat_map.hpp>
#endif
#include "cli/debug.hpp"
#include "cli/gog.hpp"
#include "cli/goggalaxy.hpp"
#include "crypto/checksum.hpp"
#include "crypto/hasher.hpp"
#include "loader/offsets.hpp"
#include "setup/data.hpp"
#include "setup/directory.hpp"
#include "setup/expression.hpp"
#include "setup/file.hpp"
#include "setup/info.hpp"
#include "setup/language.hpp"
#include "stream/chunk.hpp"
#include "stream/file.hpp"
#include "stream/slice.hpp"
#include "util/boostfs_compat.hpp"
#include "util/console.hpp"
#include "util/encoding.hpp"
#include "util/fstream.hpp"
#include "util/load.hpp"
#include "util/log.hpp"
#include "util/output.hpp"
#include "util/time.hpp"
namespace fs = boost::filesystem;
namespace {
template <typename Entry>
class processed_item {
std::string path_;
const Entry * entry_;
public:
processed_item(const std::string & path, const Entry * entry)
: path_(path), entry_(entry) { }
bool has_entry() const { return entry_ != NULL; }
const Entry & entry() const { return *entry_; }
const std::string & path() const { return path_; }
void set_entry(const Entry * entry) { entry_ = entry; }
void set_path(const std::string & path) { path_ = path; }
};
class processed_file : public processed_item<setup::file_entry> {
public:
processed_file(const setup::file_entry * entry, const std::string & path)
: processed_item<setup::file_entry>(path, entry) { }
bool is_multipart() const { return !entry().additional_locations.empty(); }
};
class processed_directory : public processed_item<setup::directory_entry> {
bool implied_;
public:
explicit processed_directory(const std::string & path)
: processed_item<setup::directory_entry>(path, NULL), implied_(false) { }
bool implied() const { return implied_; }
void set_implied(bool implied) { implied_ = implied; }
};
class file_output : private boost::noncopyable {
fs::path path_;
const processed_file * file_;
util::fstream stream_;
crypto::hasher checksum_;
boost::uint64_t checksum_position_;
boost::uint64_t position_;
boost::uint64_t total_written_;
bool write_;
public:
explicit file_output(const fs::path & dir, const processed_file * f, bool write)
: path_(dir / f->path())
, file_(f)
, checksum_(f->entry().checksum.type)
, checksum_position_(f->entry().checksum.type == crypto::None ? boost::uint64_t(-1) : 0)
, position_(0)
, total_written_(0)
, write_(write)
{
if(write_) {
try {
std::ios_base::openmode flags = std::ios_base::out | std::ios_base::binary | std::ios_base::trunc;
if(file_->is_multipart()) {
flags |= std::ios_base::in;
}
stream_.open(path_, flags);
if(!stream_.is_open()) {
throw std::exception();
}
} catch(...) {
throw std::runtime_error("Could not open output file \"" + path_.string() + '"');
}
}
}
bool write(const char * data, size_t n) {
if(write_) {
stream_.write(data, std::streamsize(n));
}
if(checksum_position_ == position_) {
checksum_.update(data, n);
checksum_position_ += n;
}
position_ += n;
total_written_ += n;
return !write_ || !stream_.fail();
}
void seek(boost::uint64_t new_position) {
if(new_position == position_) {
return;
}
debug("seeking output from " << print_hex(position_) << " to " << print_hex(new_position));
if(!write_) {
position_ = new_position;
return;
}
const boost::uint64_t max = boost::uint64_t(std::numeric_limits<util::fstream::off_type>::max() / 4);
if(new_position <= max) {
stream_.seekp(util::fstream::off_type(new_position), std::ios_base::beg);
} else {
util::fstream::off_type sign = (new_position > position_) ? 1 : -1;
boost::uint64_t diff = (new_position > position_) ? new_position - position_ : position_ - new_position;
while(diff > 0) {
stream_.seekp(sign * util::fstream::off_type(std::min(diff, max)), std::ios_base::cur);
diff -= std::min(diff, max);
}
}
position_ = new_position;
}
void close() {
if(write_) {
stream_.close();
}
}
const fs::path & path() const { return path_; }
const processed_file * file() const { return file_; }
bool is_complete() const {
return total_written_ == file_->entry().size;
}
bool has_checksum() const {
return checksum_position_ == file_->entry().size;
}
bool calculate_checksum() {
if(has_checksum()) {
return true;
}
if(!write_) {
return false;
}
debug("calculating output checksum for " << path_);
const boost::uint64_t max = boost::uint64_t(std::numeric_limits<util::fstream::off_type>::max() / 4);
boost::uint64_t diff = checksum_position_;
stream_.seekg(util::fstream::off_type(std::min(diff, max)), std::ios_base::beg);
diff -= std::min(diff, max);
while(diff > 0) {
stream_.seekg(util::fstream::off_type(std::min(diff, max)), std::ios_base::cur);
diff -= std::min(diff, max);
}
while(!stream_.eof()) {
char buffer[8192];
std::streamsize n = stream_.read(buffer, sizeof(buffer)).gcount();
checksum_.update(buffer, size_t(n));
checksum_position_ += boost::uint64_t(n);
}
if(!has_checksum()) {
log_warning << "Could not read back " << path_ << " to calculate output checksum for multi-part file";
return false;
}
return true;
}
crypto::checksum checksum() {
return checksum_.finalize();
}
};
class path_filter {
typedef std::pair<bool, std::string> Filter;
std::vector<Filter> includes;
public:
explicit path_filter(const extract_options & o) {
BOOST_FOREACH(const std::string & include, o.include) {
if(!include.empty() && include[0] == setup::path_sep) {
includes.push_back(Filter(true, boost::to_lower_copy(include) + setup::path_sep));
} else {
includes.push_back(Filter(false, setup::path_sep + boost::to_lower_copy(include)
+ setup::path_sep));
}
}
}
bool match(const std::string & path) const {
if(includes.empty()) {
return true;
}
BOOST_FOREACH(const Filter & i, includes) {
if(i.first) {
if(!i.second.compare(1, i.second.size() - 1,
path + setup::path_sep, 0, i.second.size() - 1)) {
return true;
}
} else {
if((setup::path_sep + path + setup::path_sep).find(i.second) != std::string::npos) {
return true;
}
}
}
return false;
}
};
void print_filter_info(const setup::item & item, bool temp) {
bool first = true;
if(!item.languages.empty()) {
std::cout << " [";
first = false;
std::cout << color::green << item.languages << color::reset;
}
if(temp) {
std::cout << (first ? " [" : ", ");
first = false;
std::cout << color::cyan << "temp" << color::reset;
}
if(!first) {
std::cout << "]";
}
}
void print_filter_info(const setup::file_entry & file) {
bool is_temp = !!(file.options & setup::file_entry::DeleteAfterInstall);
print_filter_info(file, is_temp);
}
void print_filter_info(const setup::directory_entry & dir) {
bool is_temp = !!(dir.options & setup::directory_entry::DeleteAfterInstall);
print_filter_info(dir, is_temp);
}
void print_size_info(const stream::file & file, boost::uint64_t size) {
if(logger::debug) {
std::cout << " @ " << print_hex(file.offset);
}
std::cout << " (" << color::dim_cyan << print_bytes(size ? size : file.size) << color::reset << ")";
}
void print_checksum_info(const stream::file & file, const crypto::checksum * checksum) {
if(!checksum || checksum->type == crypto::None) {
checksum = &file.checksum;
}
std::cout << color::dim_magenta << *checksum << color::reset;
}
void print_file_details(const extract_options & o, const stream::file & file, const stream::chunk & chunk,
boost::uint64_t size, const crypto::checksum * checksum, const std::string & key) {
if(o.list_sizes) {
print_size_info(file, size);
}
if(o.list_checksums) {
std::cout << ' ';
print_checksum_info(file, checksum);
}
if(chunk.encryption != stream::Plaintext && key.empty()) {
std::cout << " - encrypted";
}
std::cout << '\n';
}
bool prompt_overwrite() {
return true; // TODO the user always overwrites
}
const char * handle_collision(const setup::file_entry & oldfile, const setup::data_entry & olddata,
const setup::file_entry & newfile, const setup::data_entry & newdata) {
bool allow_timestamp = true;
if(!(newfile.options & setup::file_entry::IgnoreVersion)) {
bool version_info_valid = !!(newdata.options & setup::data_entry::VersionInfoValid);
if(olddata.options & setup::data_entry::VersionInfoValid) {
allow_timestamp = false;
if(!version_info_valid || olddata.file_version > newdata.file_version) {
if(!(newfile.options & setup::file_entry::PromptIfOlder) || !prompt_overwrite()) {
return "old version";
}
} else if(newdata.file_version == olddata.file_version
&& !(newfile.options & setup::file_entry::OverwriteSameVersion)) {
if((newfile.options & setup::file_entry::ReplaceSameVersionIfContentsDiffer)
&& olddata.file.checksum == newdata.file.checksum) {
return "duplicate (checksum)";
}
if(!(newfile.options & setup::file_entry::CompareTimeStamp)) {
return "duplicate (version)";
}
allow_timestamp = true;
}
} else if(version_info_valid) {
allow_timestamp = false;
}
}
if(allow_timestamp && (newfile.options & setup::file_entry::CompareTimeStamp)) {
if(newdata.timestamp == olddata.timestamp
&& newdata.timestamp_nsec == olddata.timestamp_nsec) {
return "duplicate (modification time)";
}
if(newdata.timestamp < olddata.timestamp
|| (newdata.timestamp == olddata.timestamp
&& newdata.timestamp_nsec < olddata.timestamp_nsec)) {
if(!(newfile.options & setup::file_entry::PromptIfOlder) || !prompt_overwrite()) {
return "old version (modification time)";
}
}
}
if((newfile.options & setup::file_entry::ConfirmOverwrite) && !prompt_overwrite()) {
return "user chose not to overwrite";
}
if(oldfile.attributes != boost::uint32_t(-1)
&& (oldfile.attributes & setup::file_entry::ReadOnly) != 0) {
if(!(newfile.options & setup::file_entry::OverwriteReadOnly) && !prompt_overwrite()) {
return "user chose not to overwrite read-only file";
}
}
return NULL; // overwrite old file
}
typedef boost::unordered_map<std::string, processed_file> FilesMap;
#if BOOST_VERSION >= 104800
typedef boost::container::flat_map<std::string, processed_directory> DirectoriesMap;
#else
typedef std::map<std::string, processed_directory> DirectoriesMap;
#endif
typedef boost::unordered_map<std::string, std::vector<processed_file> > CollisionMap;
std::string parent_dir(const std::string & path) {
size_t pos = path.find_last_of(setup::path_sep);
if(pos == std::string::npos) {
return std::string();
}
return path.substr(0, pos);
}
bool insert_dirs(DirectoriesMap & processed_directories, const path_filter & includes,
const std::string & internal_path, std::string & path, bool implied) {
std::string dir = parent_dir(path);
std::string internal_dir = parent_dir(internal_path);
if(internal_dir.empty()) {
return false;
}
if(implied || includes.match(internal_dir)) {
std::pair<DirectoriesMap::iterator, bool> existing = processed_directories.insert(
std::make_pair(internal_dir, processed_directory(dir))
);
if(implied) {
existing.first->second.set_implied(true);
}
if(!existing.second) {
if(existing.first->second.path() != dir) {
// Existing dir case differs, fix path
if(existing.first->second.path().length() == dir.length()) {
path.replace(0, dir.length(), existing.first->second.path());
} else {
path = existing.first->second.path() + path.substr(dir.length());
}
return true;
} else {
return false;
}
}
implied = true;
}
size_t oldlength = dir.length();
if(insert_dirs(processed_directories, includes, internal_dir, dir, implied)) {
// Existing dir case differs, fix path
if(dir.length() == oldlength) {
path.replace(0, dir.length(), dir);
} else {
path = dir + path.substr(oldlength);
}
// Also fix previously inserted directory
DirectoriesMap::iterator inserted = processed_directories.find(internal_dir);
if(inserted != processed_directories.end()) {
inserted->second.set_path(dir);
}
return true;
}
return false;
}
bool rename_collision(const extract_options & o, FilesMap & processed_files, const std::string & path,
const processed_file & other, bool common_component, bool common_language,
bool common_arch, bool first) {
const setup::file_entry & file = other.entry();
bool require_number_suffix = !first || (o.collisions == RenameAllCollisions);
std::ostringstream oss;
const setup::file_entry::flags arch_flags = setup::file_entry::Bits32 | setup::file_entry::Bits64;
if(!common_component && !file.components.empty()) {
if(setup::is_simple_expression(file.components)) {
require_number_suffix = false;
oss << '#' << file.components;
}
}
if(!common_language && !file.languages.empty()) {
if(setup::is_simple_expression(file.languages)) {
require_number_suffix = false;
if(file.languages != o.default_language) {
oss << '@' << file.languages;
}
}
}
if(!common_arch && (file.options & arch_flags) == setup::file_entry::Bits32) {
require_number_suffix = false;
oss << "@32bit";
} else if(!common_arch && (file.options & arch_flags) == setup::file_entry::Bits64) {
require_number_suffix = false;
oss << "@64bit";
}
size_t i = 0;
std::string suffix = oss.str();
if(require_number_suffix) {
oss << '$' << i++;
}
for(;;) {
std::pair<FilesMap::iterator, bool> insertion = processed_files.insert(std::make_pair(
path + oss.str(), processed_file(&file, other.path() + oss.str())
));
if(insertion.second) {
// Found an available name and inserted
return true;
}
if(&insertion.first->second.entry() == &file) {
// File already has the desired name, abort
return false;
}
oss.str(suffix);
oss << '$' << i++;
}
}
void rename_collisions(const extract_options & o, FilesMap & processed_files,
const CollisionMap & collisions) {
BOOST_FOREACH(const CollisionMap::value_type & collision, collisions) {
const std::string & path = collision.first;
const processed_file & base = processed_files.find(path)->second;
const setup::file_entry & file = base.entry();
const setup::file_entry::flags arch_flags = setup::file_entry::Bits32 | setup::file_entry::Bits64;
bool common_component = true;
bool common_language = true;
bool common_arch = true;
BOOST_FOREACH(const processed_file & other, collision.second) {
common_component = common_component && other.entry().components == file.components;
common_language = common_language && other.entry().languages == file.languages;
common_arch = common_arch && (other.entry().options & arch_flags) == (file.options & arch_flags);
}
bool ignore_component = common_component || o.collisions != RenameAllCollisions;
if(rename_collision(o, processed_files, path, base,
ignore_component, common_language, common_arch, true)) {
processed_files.erase(path);
}
BOOST_FOREACH(const processed_file & other, collision.second) {
rename_collision(o, processed_files, path, other,
common_component, common_language, common_arch, false);
}
}
}
bool print_file_info(const extract_options & o, const setup::info & info) {
if(!o.quiet) {
const std::string & name = info.header.app_versioned_name.empty()
? info.header.app_name : info.header.app_versioned_name;
const char * verb = "Inspecting";
if(o.extract) {
verb = "Extracting";
} else if(o.test) {
verb = "Testing";
} else if(o.list) {
verb = "Listing";
}
std::cout << verb << " \"" << color::green << name << color::reset
<< "\" - setup data version " << color::white << info.version << color::reset
<< std::endl;
}
#ifdef DEBUG
if(logger::debug) {
std::cout << '\n';
print_info(info);
std::cout << '\n';
}
#endif
bool multiple_sections = (o.list_languages + o.gog_game_id + o.list + o.show_password > 1);
if(!o.quiet && multiple_sections) {
std::cout << '\n';
}
if(o.list_languages) {
if(o.silent) {
BOOST_FOREACH(const setup::language_entry & language, info.languages) {
std::cout << language.name <<' ' << language.language_name << '\n';
}
} else {
if(multiple_sections) {
std::cout << "Languages:\n";
}
BOOST_FOREACH(const setup::language_entry & language, info.languages) {
std::cout << " - " << color::green << language.name << color::reset;
if(!language.language_name.empty()) {
std::cout << ": " << color::white << language.language_name << color::reset;
}
std::cout << '\n';
}
if(info.languages.empty()) {
std::cout << " (none)\n";
}
}
if((o.silent || !o.quiet) && multiple_sections) {
std::cout << '\n';
}
}
if(o.gog_game_id) {
std::string id = gog::get_game_id(info);
if(id.empty()) {
if(!o.quiet) {
std::cout << "No GOG.com game ID found!\n";
}
} else if(!o.silent) {
std::cout << "GOG.com game ID is " << color::cyan << id << color::reset << '\n';
} else {
std::cout << id << '\n';
}
if((o.silent || !o.quiet) && multiple_sections) {
std::cout << '\n';
}
}
if(o.show_password) {
if(info.header.options & setup::header::Password) {
if(o.silent) {
std::cout << info.header.password << '\n';
} else {
std::cout << "Password hash: " << color::yellow << info.header.password << color::reset << '\n';
}
if(o.silent) {
std::cout << print_hex(info.header.password_salt) << '\n';
} else if(!info.header.password_salt.empty()) {
std::cout << "Password salt: " << color::yellow
<< print_hex(info.header.password_salt) << color::reset;
if(!o.quiet) {
if(info.header.password.type == crypto::PBKDF2_SHA256_XChaCha20) {
std::cout << " (PBKDF2 salt, iteration count and XChaCha base nonce)";
} else {
std::cout << " (hex bytes, prepended to password)";
}
}
std::cout << '\n';
}
if(o.silent) {
std::cout << util::encoding_name(info.codepage) << '\n';
} else {
std::cout << "Password encoding: " << color::yellow
<< util::encoding_name(info.codepage) << color::reset << '\n';
}
} else if(!o.quiet) {
std::cout << "Setup is not passworded!\n";
}
if((o.silent || !o.quiet) && multiple_sections) {
std::cout << '\n';
}
}
return multiple_sections;
}
struct processed_entries {
FilesMap files;
DirectoriesMap directories;
};
processed_entries filter_entries(const extract_options & o, const setup::info & info) {
processed_entries processed;
#if BOOST_VERSION >= 105000
processed.files.reserve(info.files.size());
#endif
#if BOOST_VERSION >= 104800
processed.directories.reserve(info.directories.size()
+ size_t(std::log(double(info.files.size()))));
#endif
CollisionMap collisions;
path_filter includes(o);
// Filter the directories to be created
BOOST_FOREACH(const setup::directory_entry & directory, info.directories) {
if(!o.extract_temp && (directory.options & setup::directory_entry::DeleteAfterInstall)) {
continue; // Ignore temporary dirs
}
if(!directory.languages.empty()) {
if(!o.language.empty() && !setup::expression_match(o.language, directory.languages)) {
continue; // Ignore other languages
}
} else if(o.language_only) {
continue; // Ignore language-agnostic dirs
}
std::string path = o.filenames.convert(directory.name);
if(path.empty()) {
continue; // Don't know what to do with this
}
std::string internal_path = boost::algorithm::to_lower_copy(path);
bool path_included = includes.match(internal_path);
insert_dirs(processed.directories, includes, internal_path, path, path_included);
DirectoriesMap::iterator it;
if(path_included) {
std::pair<DirectoriesMap::iterator, bool> existing = processed.directories.insert(
std::make_pair(internal_path, processed_directory(path))
);
it = existing.first;
} else {
it = processed.directories.find(internal_path);
if(it == processed.directories.end()) {
continue;
}
}
it->second.set_entry(&directory);
}
// Filter the files to be extracted
BOOST_FOREACH(const setup::file_entry & file, info.files) {
if(file.location >= info.data_entries.size()) {
continue; // Ignore external files (copy commands)
}
if(!o.extract_temp && (file.options & setup::file_entry::DeleteAfterInstall)) {
continue; // Ignore temporary files
}
if(!file.languages.empty()) {
if(!o.language.empty() && !setup::expression_match(o.language, file.languages)) {
continue; // Ignore other languages
}
} else if(o.language_only) {
continue; // Ignore language-agnostic files
}
std::string path = o.filenames.convert(file.destination);
if(path.empty()) {
continue; // Internal file, not extracted
}
std::string internal_path = boost::algorithm::to_lower_copy(path);
bool path_included = includes.match(internal_path);
insert_dirs(processed.directories, includes, internal_path, path, path_included);
if(!path_included) {
continue; // Ignore excluded file
}
std::pair<FilesMap::iterator, bool> insertion = processed.files.insert(std::make_pair(
internal_path, processed_file(&file, path)
));
if(!insertion.second) {
// Collision!
processed_file & existing = insertion.first->second;
if(o.collisions == ErrorOnCollisions) {
throw std::runtime_error("Collision: " + path);
} else if(o.collisions == RenameAllCollisions) {
collisions[internal_path].push_back(processed_file(&file, path));
} else {
const setup::data_entry & newdata = info.data_entries[file.location];
const setup::data_entry & olddata = info.data_entries[existing.entry().location];
const char * skip = handle_collision(existing.entry(), olddata, file, newdata);
if(!o.default_language.empty()) {
bool oldlang = setup::expression_match(o.default_language, file.languages);
bool newlang = setup::expression_match(o.default_language, existing.entry().languages);
if(oldlang && !newlang) {
skip = NULL;
} else if(!oldlang && newlang) {
skip = "overwritten";
}
}
if(o.collisions == RenameCollisions) {
const setup::file_entry & clobberedfile = skip ? file : existing.entry();
const std::string & clobberedpath = skip ? path : existing.path();
collisions[internal_path].push_back(processed_file(&clobberedfile, clobberedpath));
} else if(!o.silent) {
std::cout << " - ";
const std::string & clobberedpath = skip ? path : existing.path();
std::cout << '"' << color::dim_yellow << clobberedpath << color::reset << '"';
print_filter_info(skip ? file : existing.entry());
if(o.list_sizes) {
print_size_info(skip ? newdata.file : olddata.file, skip ? file.size : existing.entry().size);
}
if(o.list_checksums) {
std::cout << ' ';
print_checksum_info(skip ? newdata.file : olddata.file,
skip ? &file.checksum : &existing.entry().checksum);
}
std::cout << " - " << (skip ? skip : "overwritten") << '\n';
}
if(!skip) {
existing.set_entry(&file);
if(file.type != setup::file_entry::UninstExe) {
// Old file is "deleted" first → use case from new file
existing.set_path(path);
}
}
}
}
}
if(o.collisions == RenameCollisions || o.collisions == RenameAllCollisions) {
rename_collisions(o, processed.files, collisions);
}
return processed;
}
void create_output_directory(const extract_options & o) {
try {
if(!o.output_dir.empty() && !fs::exists(o.output_dir)) {
fs::create_directory(o.output_dir);
}
} catch(...) {
throw std::runtime_error("Could not create output directory \"" + o.output_dir.string() + '"');
}
}
} // anonymous namespace
void process_file(const fs::path & installer, const extract_options & o) {
bool is_directory;
try {
is_directory = fs::is_directory(installer);
} catch(...) {
throw std::runtime_error("Could not open file \"" + installer.string()
+ "\": access denied");
}
if(is_directory) {
throw std::runtime_error("Input file \"" + installer.string() + "\" is a directory!");
}
util::ifstream ifs;
try {
ifs.open(installer, std::ios_base::in | std::ios_base::binary);
if(!ifs.is_open()) {
throw std::exception();
}
} catch(...) {
throw std::runtime_error("Could not open file \"" + installer.string() + '"');
}
loader::offsets offsets;
offsets.load(ifs);
#ifdef DEBUG
if(logger::debug) {
print_offsets(offsets);
std::cout << '\n';
}
#endif
if(o.data_version) {
setup::version version;
ifs.seekg(offsets.header_offset);
version.load(ifs);
if(o.silent) {
std::cout << version << '\n';
} else {
std::cout << color::white << version << color::reset << '\n';
}
return;
}
#ifdef DEBUG
if(o.dump_headers) {
create_output_directory(o);
dump_headers(ifs, offsets, o);
return;
}
#endif
setup::info::entry_types entries = 0;
if(o.list || o.test || o.extract || (o.gog_galaxy && o.list_languages)) {
entries |= setup::info::Files;
entries |= setup::info::Directories;
entries |= setup::info::DataEntries;
}
if(o.list_languages) {
entries |= setup::info::Languages;
}
if(o.gog_game_id || o.gog) {
entries |= setup::info::RegistryEntries;
}
if(!o.extract_unknown) {
entries |= setup::info::NoUnknownVersion;
}
#ifdef DEBUG
if(logger::debug) {
entries = setup::info::entry_types::all() & ~setup::info::NoUnknownVersion;
}
#endif
ifs.seekg(offsets.header_offset);
setup::info info;
try {
info.load(ifs, entries, o.codepage);
} catch(const setup::version_error &) {
fs::path headerfile = installer;
headerfile.replace_extension(".0");
if(offsets.header_offset == 0 && headerfile != installer && fs::exists(headerfile)) {
log_info << "Opening \"" << color::cyan << headerfile.string() << color::reset << '"';
process_file(headerfile, o);
return;
}
if(offsets.found_magic) {