Repository navigation
Expand file tree
/
Copy pathcommon.cpp
More file actions
1092 lines (920 loc) · 40.6 KB
/
Copy pathcommon.cpp
File metadata and controls
1092 lines (920 loc) · 40.6 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
#include "common.h"
#include "aes.h"
#include <cstdlib>
#include <zlib.h>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <atomic>
#include <vector>
#include <fstream>
#include <algorithm>
#include <stdexcept>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <array>
#include <deque>
#include <set>
#include <chrono>
#ifdef __AVX2__
#include <immintrin.h>
#endif
namespace Logger {
bool is_debug_enabled = false;
std::ofstream debug_log;
std::mutex log_mutex;
}
void AESContextDeleter::operator()(AES_ctx* ctx) const {
if (ctx) AES_Context_Destroy(ctx);
}
AES_ctx* AES_Context_Create(const uint8_t* key) {
if (!key) return nullptr;
AES_ctx* ctx = static_cast<AES_ctx*>(calloc(1, sizeof(AES_ctx)));
if (ctx) AES_init_ctx(ctx, key);
return ctx;
}
void AES_Context_Destroy(AES_ctx* ctx) {
if (ctx) free(ctx);
}
void AES_Context_Decrypt(AES_ctx* ctx, uint8_t* buf, uint32_t len) {
if (ctx && buf && len > 0) AES_CBC_decrypt_buffer(ctx, buf, len);
}
void AES_Context_Encrypt(AES_ctx* ctx, uint8_t* buf, uint32_t len) {
if (ctx && buf && len > 0) AES_CBC_encrypt_buffer(ctx, buf, len);
}
OodleLZ_Decompress_t* OodleLZ_Decompress = nullptr;
OodleLZ_Compress_t* OodleLZ_Compress = nullptr;
bool LoadOodle() {
#ifdef _WIN32
HMODULE hModule = LoadLibraryA("oo2core_9_win64.dll");
if (!hModule) hModule = LoadLibraryA("oo2core_8_win64.dll");
if (!hModule) return false;
OodleLZ_Decompress = reinterpret_cast<OodleLZ_Decompress_t*>(GetProcAddress(hModule, "OodleLZ_Decompress"));
OodleLZ_Compress = reinterpret_cast<OodleLZ_Compress_t*>(GetProcAddress(hModule, "OodleLZ_Compress"));
#else
void* hModule = dlopen("./liboo2core.so.9", RTLD_NOW);
if (!hModule) hModule = dlopen("liboo2core.so.9", RTLD_NOW);
if (!hModule) hModule = dlopen("liboo2core.so.8", RTLD_NOW);
if (!hModule) return false;
OodleLZ_Decompress = reinterpret_cast<OodleLZ_Decompress_t*>(dlsym(hModule, "OodleLZ_Decompress"));
OodleLZ_Compress = reinterpret_cast<OodleLZ_Compress_t*>(dlsym(hModule, "OodleLZ_Compress"));
#endif
bool success = (OodleLZ_Decompress != nullptr && OodleLZ_Compress != nullptr);
if (!success && hModule) {
#ifdef _WIN32
FreeLibrary(hModule);
#else
dlclose(hModule);
#endif
OodleLZ_Decompress = nullptr;
OodleLZ_Compress = nullptr;
}
return success;
}
int64_t CompressAndVerify(int32_t method, int32_t level, const uint8_t* src, uint32_t usize,
const uint8_t* expected, uint32_t expected_size, std::vector<uint8_t>& temp_buf,
const OodleLZ_CompressOptions* opts, void* scratchMem, int64_t scratchSize)
{
if (temp_buf.size() < usize * 2 + 4096) temp_buf.resize(usize * 2 + 4096);
int64_t res = OodleLZ_Compress(method, src, usize, temp_buf.data(), level, (void*)opts, nullptr, nullptr, scratchMem, scratchSize);
if (res > 0 && res == static_cast<int64_t>(expected_size)) {
if (std::memcmp(expected, temp_buf.data(), expected_size) == 0) return res;
}
return -1;
}
bool TryMatchBlock(const std::shared_ptr<BlockTask>& task,
const std::vector<int32_t>& all_methods,
const std::vector<int32_t>& all_levels,
std::set<std::pair<int32_t, int32_t>>& cache,
std::shared_mutex& cache_mutex,
int32_t& out_method, int32_t& out_level,
const OodleLZ_CompressOptions* opts)
{
static ReusableBufferPool<uint8_t> g_match_scratch_pool;
static ReusableBufferPool<uint8_t> g_match_comp_pool;
PooledBuffer<uint8_t> scratch_mem(g_match_scratch_pool, 8 * 1024 * 1024);
size_t required_size = std::max(static_cast<size_t>(task->usize) * 2, static_cast<size_t>(256 * 1024));
PooledBuffer<uint8_t> local_comp_buf(g_match_comp_pool, required_size);
{
std::shared_lock<std::shared_mutex> lock(cache_mutex);
for (const auto& pair : cache) {
if (CompressAndVerify(pair.first, pair.second, task->dec_data.data(), task->usize,
task->raw_win_buf.data(), task->csize, local_comp_buf.vec(), opts,
scratch_mem.data(), scratch_mem.size()) > 0) {
out_method = pair.first;
out_level = pair.second;
return true;
}
}
}
for (int32_t method : all_methods) {
for (int32_t level : all_levels) {
if (CompressAndVerify(method, level, task->dec_data.data(), task->usize,
task->raw_win_buf.data(), task->csize, local_comp_buf.vec(), opts,
scratch_mem.data(), scratch_mem.size()) > 0) {
std::unique_lock<std::shared_mutex> lock(cache_mutex);
cache.insert({method, level});
out_method = method;
out_level = level;
return true;
}
}
}
return false;
}
static inline bool IsValidOodleCodec(uint8_t b1) {
uint8_t base_b1 = b1 & 0x7F;
return (base_b1 == 0x06 || base_b1 == 0x0A || base_b1 == 0x0C ||
base_b1 == 0x0D || base_b1 == 0x0B);
}
uint32_t GetOodleBlockSize(const uint8_t* hdr, size_t available_len, uint8_t& codec_out) {
if (available_len < Config::MIN_OODLE_BLOCK_SIZE) return 0;
uint8_t b0 = hdr[0];
if (b0 != 0x8C && b0 != 0xCC && b0 != 0x0C && b0 != 0x4C) return 0;
bool compressed = (b0 == 0x8C || b0 == 0x0C);
uint8_t b1 = hdr[1];
if (compressed) {
if (b1 == 0x06 || b1 == 0x86) codec_out = 8;
else if (b1 == 0x0A || b1 == 0x8A) codec_out = 9;
else if (b1 == 0x0C || b1 == 0x8C) codec_out = 11;
else if (b1 == 0x0D || b1 == 0x8D) codec_out = 12;
else if (b1 == 0x0B || b1 == 0x8B) codec_out = 13;
else return 0;
uint32_t header_size = (b1 & 0x80) ? 9 : 6;
if (available_len < header_size) return 0;
uint32_t csize = (static_cast<uint32_t>(hdr[2]) << 16) |
(static_cast<uint32_t>(hdr[3]) << 8) |
static_cast<uint32_t>(hdr[4]);
csize += header_size;
if (csize < Config::MIN_OODLE_BLOCK_SIZE || csize > 8 * 1024 * 1024) return 0;
return csize;
} else {
if (b1 == 0x06) codec_out = 8;
else if (b1 == 0x0A) codec_out = 9;
else if (b1 == 0x0C) codec_out = 11;
else return 0;
return 262146;
}
}
struct ThreadSafeReader::Impl {
std::string path_;
#ifdef _WIN32
HANDLE hFile = INVALID_HANDLE_VALUE;
#else
int fd_ = -1;
#endif
Impl(const std::string& path) : path_(path) {
#ifdef _WIN32
hFile = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#else
fd_ = open(path.c_str(), O_RDONLY);
#endif
}
~Impl() {
#ifdef _WIN32
if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
#else
if (fd_ != -1) close(fd_);
#endif
}
bool is_open() const {
#ifdef _WIN32
return hFile != INVALID_HANDLE_VALUE;
#else
return fd_ != -1;
#endif
}
uint64_t get_size() const {
#ifdef _WIN32
LARGE_INTEGER size; return GetFileSizeEx(hFile, &size) ? size.QuadPart : 0;
#else
struct stat st; return fstat(fd_, &st) == 0 ? st.st_size : 0;
#endif
}
size_t pread(char* dest, size_t count, uint64_t offset) const {
size_t total_read = 0;
int retry_count = 0;
while (total_read < count) {
#ifdef _WIN32
OVERLAPPED ol = {0}; ol.Offset = static_cast<DWORD>(offset + total_read); ol.OffsetHigh = static_cast<DWORD>((offset + total_read) >> 32);
DWORD bytes_read = 0;
BOOL success = ReadFile(hFile, dest + total_read, static_cast<DWORD>(count - total_read), &bytes_read, &ol);
if (!success) {
DWORD err = GetLastError();
if (err == ERROR_HANDLE_EOF) break;
if ((err == ERROR_LOCK_VIOLATION || err == ERROR_SEM_TIMEOUT) && retry_count++ < 10) {
Sleep(1); continue;
}
break;
}
if (bytes_read == 0) break;
total_read += bytes_read;
#else
ssize_t r = ::pread(fd_, dest + total_read, count - total_read, static_cast<off_t>(offset + total_read));
if (r < 0) { if (errno == EINTR) continue; break; }
if (r == 0) break;
total_read += r;
#endif
}
return total_read;
}
};
ThreadSafeReader::ThreadSafeReader(const std::string& path) : pImpl_(std::make_unique<Impl>(path)) {}
ThreadSafeReader::~ThreadSafeReader() = default;
bool ThreadSafeReader::is_open() const { return pImpl_->is_open(); }
uint64_t ThreadSafeReader::get_size() const { return pImpl_->get_size(); }
size_t ThreadSafeReader::pread(char* dest, size_t count, uint64_t offset) const { return pImpl_->pread(dest, count, offset); }
struct FastStreamWriter::Impl {
std::ofstream file;
std::vector<char> active_buf;
std::vector<char> flush_buf;
std::vector<char> disk_buf;
size_t buffer_size;
std::mutex mtx;
std::mutex io_mtx;
std::condition_variable cv;
std::thread flush_thread;
bool flush_ready;
bool disk_busy;
bool done;
std::atomic<bool> io_error;
std::string last_error;
std::atomic<uint64_t> total_bytes_written{0};
Impl(size_t buf_sz = 32 * 1024 * 1024)
: buffer_size(buf_sz), flush_ready(false), disk_busy(false), done(false), io_error(false) {
active_buf.reserve(buffer_size);
flush_buf.reserve(buffer_size);
disk_buf.reserve(buffer_size);
}
~Impl() { close(); }
bool open(const std::string& path) {
file.open(path, std::ios::binary);
if (!file.is_open()) {
last_error = "Failed to open output file: " + path;
io_error.store(true, std::memory_order_release);
return false;
}
flush_thread = std::thread(&Impl::flush_worker, this);
return true;
}
void check_io() {
if (io_error.load(std::memory_order_acquire))
throw std::runtime_error("Async I/O Error: " + last_error);
}
void write(const char* data, size_t len) {
if (io_error.load(std::memory_order_acquire)) return;
total_bytes_written.fetch_add(len, std::memory_order_relaxed);
std::unique_lock<std::mutex> lock(mtx);
size_t offset = 0;
const size_t FRACTURE_SIZE = 16 * 1024 * 1024;
while (offset < len) {
cv.wait(lock, [this]() {
return !flush_ready || done || io_error.load(std::memory_order_acquire);
});
if (done || io_error.load(std::memory_order_acquire)) {
check_io();
return;
}
size_t space_left = buffer_size - active_buf.size();
size_t chunk_to_write = std::min({len - offset, FRACTURE_SIZE, space_left});
if (chunk_to_write == 0) {
std::swap(flush_buf, active_buf);
flush_ready = true;
cv.notify_all();
continue;
}
active_buf.insert(active_buf.end(), data + offset, data + offset + chunk_to_write);
offset += chunk_to_write;
}
}
void flush() {
std::unique_lock<std::mutex> lock(mtx);
if (io_error.load(std::memory_order_acquire)) return;
cv.wait(lock, [this]() { return !flush_ready || done || io_error.load(std::memory_order_acquire); });
if (io_error.load(std::memory_order_acquire)) return;
if (!active_buf.empty()) {
std::swap(flush_buf, active_buf);
flush_ready = true;
cv.notify_all();
}
cv.wait(lock, [this]() { return !flush_ready || done || io_error.load(std::memory_order_acquire); });
if (io_error.load(std::memory_order_acquire)) return;
}
void close() {
{
std::unique_lock<std::mutex> lock(mtx);
if (done) return;
if (!active_buf.empty() && !io_error.load(std::memory_order_acquire)) {
cv.wait(lock, [this]() { return !flush_ready || done || io_error.load(std::memory_order_acquire); });
if (!io_error.load(std::memory_order_acquire)) {
std::swap(flush_buf, active_buf);
flush_ready = true;
}
}
done = true;
cv.notify_all();
}
if (flush_thread.joinable()) flush_thread.join();
if (file.is_open()) file.close();
}
void flush_worker() {
while (true) {
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this]() { return flush_ready || done; });
if (!flush_ready && done) {
break;
}
if (flush_ready) {
std::swap(disk_buf, flush_buf);
flush_ready = false;
disk_busy = true;
lock.unlock();
cv.notify_all();
if (!disk_buf.empty()) {
std::lock_guard<std::mutex> io_lock(io_mtx);
file.write(disk_buf.data(), disk_buf.size());
if (file.bad()) {
std::lock_guard<std::mutex> err_lock(mtx);
last_error = "Disk write failed. Disk full or hardware fault detected.";
io_error.store(true, std::memory_order_release);
done = true;
disk_busy = false;
cv.notify_all();
return;
}
disk_buf.clear();
}
lock.lock();
disk_busy = false;
cv.notify_all();
}
}
}
};
FastStreamWriter::FastStreamWriter() : pImpl_(std::make_unique<Impl>()) {}
FastStreamWriter::~FastStreamWriter() = default;
bool FastStreamWriter::open(const std::string& path, size_t size) { return pImpl_->open(path); }
void FastStreamWriter::write(const char* data, size_t len) { pImpl_->write(data, len); }
void FastStreamWriter::flush() { pImpl_->flush(); }
void FastStreamWriter::close() { pImpl_->close(); }
bool FastStreamWriter::has_error() const { return pImpl_->io_error.load(std::memory_order_acquire); }
std::string FastStreamWriter::get_last_error() const {
std::lock_guard<std::mutex> lock(pImpl_->mtx);
return pImpl_->last_error;
}
void FastStreamWriter::clear_error() {
pImpl_->io_error.store(false, std::memory_order_release);
std::lock_guard<std::mutex> lock(pImpl_->mtx);
pImpl_->last_error.clear();
}
uint64_t FastStreamWriter::tellp() const {
std::unique_lock<std::mutex> lock(pImpl_->mtx);
pImpl_->cv.wait(lock, [this]() {
return !pImpl_->flush_ready && !pImpl_->disk_busy;
});
std::lock_guard<std::mutex> io_lock(pImpl_->io_mtx);
std::streampos current_pos = pImpl_->file.tellp();
return static_cast<uint64_t>(current_pos);
}
void FastStreamWriter::seekp(uint64_t pos) {
if (pImpl_->io_error.load(std::memory_order_acquire)) return;
pImpl_->check_io();
std::unique_lock<std::mutex> lock(pImpl_->mtx);
if (!pImpl_->active_buf.empty()) {
pImpl_->cv.wait(lock, [this]() {
return !pImpl_->flush_ready || pImpl_->done || pImpl_->io_error.load(std::memory_order_acquire);
});
if (pImpl_->io_error.load(std::memory_order_acquire)) return;
std::swap(pImpl_->flush_buf, pImpl_->active_buf);
pImpl_->flush_ready = true;
pImpl_->cv.notify_all();
}
pImpl_->cv.wait(lock, [this]() {
return (!pImpl_->flush_ready && !pImpl_->disk_busy) || pImpl_->done || pImpl_->io_error.load(std::memory_order_acquire);
});
if (pImpl_->io_error.load(std::memory_order_acquire)) return;
pImpl_->check_io();
static_assert(sizeof(std::streamoff) >= 8,
"CRITICAL ERROR: Compilation target requires 64-bit std::streamoff structures to safely manage archives larger than 2GB.");
if (static_cast<std::streamoff>(pos) < 0) {
throw std::runtime_error("FastStreamWriter::seekp - Target file seek offset causes a negative 32-bit signed overflow.");
}
std::lock_guard<std::mutex> io_lock(pImpl_->io_mtx);
pImpl_->file.clear();
pImpl_->file.seekp(static_cast<std::streamoff>(pos));
if (pImpl_->file.good()) {
pImpl_->total_bytes_written.store(pos, std::memory_order_relaxed);
} else {
pImpl_->last_error = "seekp() failed at offset " + std::to_string(pos);
pImpl_->io_error.store(true, std::memory_order_release);
pImpl_->cv.notify_all();
}
}
BlockScanner::BlockScanner(ThreadSafeReader& reader, uint64_t file_size, bool use_aes, const std::vector<uint8_t>& aes_key, size_t win_size)
: reader_(reader), file_size_(file_size), use_aes_(use_aes), aes_key_(aes_key) {
if (use_aes_) win_size = (win_size + 15) & ~15;
win_size_ = win_size;
win_buf_.reset(new uint8_t[win_size]);
if (use_aes_) {
win_buf_dec_.reset(new uint8_t[win_size]);
aes_ctx_ = AESContextPtr(AES_Context_Create(aes_key_.data()));
}
}
BlockScanner::~BlockScanner() = default;
bool BlockScanner::ensure_window(uint64_t pos, size_t needed) {
if (pos >= file_size_) {
win_len_ = 0;
return false;
}
if (needed > file_size_ - pos) {
needed = static_cast<size_t>(file_size_ - pos);
}
if (pos >= win_start_ && needed <= (win_start_ + win_len_) - pos) {
return win_len_ > 0;
}
uint64_t aligned_pos = pos & ~15ULL;
if (aligned_pos >= file_size_) {
win_len_ = 0;
return false;
}
if (aligned_pos >= win_start_ && aligned_pos < win_start_ + win_len_) {
size_t keep = static_cast<size_t>((win_start_ + win_len_) - aligned_pos);
if (keep > 0 && keep <= win_size_) {
std::memmove(win_buf_.get(), &win_buf_[aligned_pos - win_start_], keep);
if (use_aes_) {
std::memmove(win_buf_dec_.get(), &win_buf_dec_[aligned_pos - win_start_], keep);
}
win_start_ = aligned_pos;
size_t bytes_to_read = win_size_ - keep;
uint64_t read_start = win_start_ + keep;
if (read_start >= file_size_) {
win_len_ = keep;
return win_len_ > 0;
}
size_t max_readable = static_cast<size_t>(file_size_ - read_start);
size_t bytes_to_request = std::min(bytes_to_read, max_readable);
size_t read_bytes = reader_.pread(reinterpret_cast<char*>(&win_buf_[keep]), bytes_to_request, read_start);
if (use_aes_ && read_bytes > 0) {
size_t decrypt_len = read_bytes & ~15ULL;
if (decrypt_len > 0) {
std::memcpy(&win_buf_dec_[keep], &win_buf_[keep], decrypt_len);
AES_Context_Decrypt(aes_ctx_.get(), &win_buf_dec_[keep], static_cast<uint32_t>(decrypt_len));
}
}
win_len_ = keep + read_bytes;
} else {
win_start_ = aligned_pos;
size_t bytes_to_request = std::min(win_size_, static_cast<size_t>(file_size_ - win_start_));
size_t read_bytes = reader_.pread(reinterpret_cast<char*>(win_buf_.get()), bytes_to_request, win_start_);
if (use_aes_ && read_bytes > 0) {
size_t decrypt_len = read_bytes & ~15ULL;
if (decrypt_len > 0) {
std::memcpy(win_buf_dec_.get(), win_buf_.get(), decrypt_len);
AES_Context_Decrypt(aes_ctx_.get(), win_buf_dec_.get(), static_cast<uint32_t>(decrypt_len));
}
}
win_len_ = read_bytes;
}
} else {
win_start_ = aligned_pos;
if (win_start_ >= file_size_) {
win_len_ = 0;
return false;
}
size_t bytes_to_request = std::min(win_size_, static_cast<size_t>(file_size_ - win_start_));
size_t read_bytes = reader_.pread(reinterpret_cast<char*>(win_buf_.get()), bytes_to_request, win_start_);
if (use_aes_ && read_bytes > 0) {
size_t decrypt_len = read_bytes & ~15ULL;
if (decrypt_len > 0) {
std::memcpy(win_buf_dec_.get(), win_buf_.get(), decrypt_len);
AES_Context_Decrypt(aes_ctx_.get(), win_buf_dec_.get(), static_cast<uint32_t>(decrypt_len));
}
}
win_len_ = read_bytes;
}
return win_len_ > 0;
}
bool BlockScanner::find_next_magic(uint64_t& pos, uint64_t limit) {
while (pos < limit && pos < file_size_) {
if (!ensure_window(pos, 16)) return false;
size_t internal_idx = static_cast<size_t>(pos - win_start_);
size_t max_check = std::min(win_len_ - internal_idx, static_cast<size_t>(file_size_ - pos));
if (max_check == 0) { pos++; continue; }
const uint8_t* search_start = use_aes_ ? &win_buf_dec_[internal_idx] : &win_buf_[internal_idx];
size_t found_offset = max_check;
#ifdef __AVX2__
const __m256i magic_8C = _mm256_set1_epi8(0x8C);
const __m256i magic_CC = _mm256_set1_epi8(0xCC);
const __m256i magic_0C = _mm256_set1_epi8(0x0C);
const __m256i magic_4C = _mm256_set1_epi8(0x4C);
size_t i = 0;
for (; i + 32 <= max_check; i += 32) {
__m256i data = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(search_start + i));
__m256i cmp_8C_CC = _mm256_or_si256(_mm256_cmpeq_epi8(data, magic_8C), _mm256_cmpeq_epi8(data, magic_CC));
__m256i cmp_0C_4C = _mm256_or_si256(_mm256_cmpeq_epi8(data, magic_0C), _mm256_cmpeq_epi8(data, magic_4C));
__m256i cmp_final = _mm256_or_si256(cmp_8C_CC, cmp_0C_4C);
int mask = _mm256_movemask_epi8(cmp_final);
if (mask) {
found_offset = i + __builtin_ctz(mask);
break;
}
}
if (found_offset == max_check && i < max_check) {
for (size_t j = i; j < max_check; j++) {
uint8_t b = search_start[j];
if (b == 0x8C || b == 0xCC || b == 0x0C || b == 0x4C) {
found_offset = j;
break;
}
}
}
#else
for (size_t j = 0; j < max_check; j++) {
uint8_t b = search_start[j];
if (b == 0x8C || b == 0xCC || b == 0x0C || b == 0x4C) {
found_offset = j;
break;
}
}
#endif
if (found_offset < max_check) {
pos += found_offset;
return true;
}
pos += max_check;
}
return false;
}
static uint32_t FindCompressedSize(const uint8_t* src, size_t max_len, uint32_t usize, uint8_t* temp_dec_buf) {
uint8_t codec;
uint32_t header_csize = GetOodleBlockSize(src, max_len, codec);
if (header_csize > 0 && header_csize <= max_len) {
if (OodleLZ_Decompress(src, header_csize, temp_dec_buf, usize, 0, 0, 0,
nullptr, 0, nullptr, nullptr, nullptr, 0, 0, 0) == static_cast<int64_t>(usize)) {
return header_csize;
}
}
size_t high = std::min(max_len, static_cast<size_t>(16 * 1024 * 1024));
size_t low = 8;
uint32_t csize = 0;
while (low <= high) {
size_t mid = low + (high - low) / 2;
if (OodleLZ_Decompress(src, static_cast<int>(mid), temp_dec_buf, usize, 0, 0, 0,
nullptr, 0, nullptr, nullptr, nullptr, 0, 0, 0) == static_cast<int64_t>(usize)) {
csize = static_cast<uint32_t>(mid);
high = mid - 1;
} else {
low = mid + 1;
}
}
return csize;
}
static inline uint32_t ReadBE24(const uint8_t* data) {
return (data[0] << 16) | (data[1] << 8) | data[2];
}
uint64_t BlockScanner::WalkOodleChain(uint64_t start_pos, uint8_t& codec_out, bool& is_valid) {
uint64_t offset = start_pos;
uint64_t c_size_total = 0;
bool first = true;
is_valid = false;
codec_out = 0;
uint32_t iteration = 0;
const uint64_t max_allowed = file_size_ - start_pos;
auto read_bytes = [&](uint64_t off, uint8_t* out, size_t count) -> bool {
if (off + count > file_size_) return false;
if (!ensure_window(off, count)) return false;
if (off < win_start_ || off + count > win_start_ + win_len_) return false;
uint8_t* src_buf = use_aes_ ? win_buf_dec_.get() : win_buf_.get();
std::memcpy(out, &src_buf[off - win_start_], count);
return true;
};
while (true) {
if (++iteration > Config::MAX_WALK_ITERATIONS) break;
uint64_t rem_size = file_size_ - offset;
uint8_t hdr[16];
if (!read_bytes(offset, hdr, 16)) break;
if (first) {
diagnostics_.walk_called++;
if (rem_size < Config::MIN_OODLE_BLOCK_SIZE + 4) { diagnostics_.walk_bounds_fail++; return 0; }
uint8_t b0 = hdr[0];
if (b0 != 0x8C && b0 != 0xCC) { diagnostics_.walk_not_a_chain_start++; return 0; }
uint8_t b1 = hdr[1];
if (!IsValidOodleCodec(b1)) { diagnostics_.walk_first_consistency_fail++; return 0; }
if ((b1 & 0x7F) == 0x06) codec_out = 8;
else if ((b1 & 0x7F) == 0x0A) codec_out = 9;
else if ((b1 & 0x7F) == 0x0C) codec_out = 11;
else if ((b1 & 0x7F) == 0x0D) codec_out = 12;
else if ((b1 & 0x7F) == 0x0B) codec_out = 13;
else { diagnostics_.walk_first_consistency_fail++; return 0; }
} else {
if (rem_size < 10) break;
uint8_t b0 = hdr[0];
if (b0 != 0x0C && b0 != 0x4C) break;
if (!IsValidOodleCodec(hdr[1])) break;
}
bool compressed_seg = (hdr[0] == 0x8C || (!first && hdr[0] == 0x0C));
uint8_t b1 = hdr[1];
if (compressed_seg) {
uint32_t i = 0;
uint32_t header_size = (b1 & 0x80) ? 9 : 6;
if (IsValidOodleCodec(b1)) i = ReadBE24(hdr + 2) + header_size;
else break;
if (i > 8 * 1024 * 1024 || i < Config::MIN_OODLE_BLOCK_SIZE) {
diagnostics_.walk_bounds_fail++;
break;
}
if (first && i < Config::MIN_VALID_FIRST_SEGMENT) { diagnostics_.walk_first_too_small++; return 0; }
if (i > max_allowed - c_size_total) {
diagnostics_.walk_bounds_fail++;
break;
}
c_size_total += i;
offset += i;
first = false;
} else {
if (IsValidOodleCodec(b1)) {
const uint32_t BLK_SIZE = 262144;
if (BLK_SIZE + 3 <= max_allowed - c_size_total) {
uint8_t end_bytes[2];
if (read_bytes(offset + BLK_SIZE + 2, end_bytes, 2)) {
if ((end_bytes[0] == 0x0C || end_bytes[0] == 0x4C) && end_bytes[1] == b1) {
c_size_total += BLK_SIZE + 2;
offset += BLK_SIZE + 2;
first = false;
continue;
}
}
}
if (!first && 10 <= max_allowed - c_size_total) {
c_size_total += 10;
break;
}
break;
} else break;
}
}
if (c_size_total > 0) {
is_valid = true;
diagnostics_.walk_succeeded++;
return c_size_total;
}
return 0;
}
std::shared_ptr<BlockTask> BlockScanner::extract_next_block(uint64_t& pos, uint64_t limit, const std::vector<uint32_t>& test_sizes) {
static ReusableBufferPool<uint8_t> g_extract_decomp_pool;
PooledBuffer<uint8_t> dec_buf(g_extract_decomp_pool, 4 * 1024 * 1024);
uint32_t max_usize = 4 * 1024 * 1024;
auto skip_to_next_magic = [&](const uint8_t* current_hdr, size_t pass_sz) {
if (pass_sz <= 1) { pos += 1; return; }
size_t max_safe_jump = 16;
size_t search_limit = std::min(pass_sz - 1, max_safe_jump);
size_t min_dist = search_limit + 1;
for (uint8_t magic : {0x8C, 0xCC, 0x0C, 0x4C}) {
const uint8_t* f = static_cast<const uint8_t*>(std::memchr(current_hdr + 1, magic, search_limit));
if (f) {
size_t dist = static_cast<size_t>(f - current_hdr);
if (dist > 0 && dist < min_dist) min_dist = dist;
}
}
if (min_dist <= search_limit) {
pos += min_dist;
} else {
pos += 1;
}
};
while (pos < limit && pos < file_size_) {
if (!find_next_magic(pos, limit)) break;
size_t needed = std::min(static_cast<uint64_t>(2 * 1024 * 1024), file_size_ - pos);
if (!ensure_window(pos, needed)) break;
size_t available = (win_start_ + win_len_ > pos) ? static_cast<size_t>(win_start_ + win_len_ - pos) : 0;
uint8_t* active_buf = use_aes_ ? win_buf_dec_.get() : win_buf_.get();
uint8_t* hdr = &active_buf[pos - win_start_];
size_t pass_size = std::min(available, static_cast<size_t>(file_size_ - pos));
diagnostics_.magic_candidates_found++;
uint8_t b1 = hdr[1];
if (!IsValidOodleCodec(b1)) {
diagnostics_.rejected_by_fast_rejection++;
if (pass_size > 1) skip_to_next_magic(hdr, pass_size);
else pos += 1;
continue;
}
diagnostics_.passed_fast_rejection++;
uint8_t codec = 0;
uint32_t csize = GetOodleBlockSize(hdr, pass_size, codec);
if (csize > 0 && csize <= pass_size) {
int64_t test_usize = OodleLZ_Decompress(hdr, csize, dec_buf.data(), max_usize, 0, 0, 0, nullptr, 0, nullptr, nullptr, nullptr, 0, 0, 0);
bool continuation_follows = false;
if (test_usize > 0 && test_usize <= max_usize && csize + 2 <= pass_size) {
uint8_t next0 = hdr[csize];
if (next0 == 0x0C || next0 == 0x4C) {
if (IsValidOodleCodec(hdr[csize + 1])) continuation_follows = true;
}
}
if (!continuation_follows && test_usize > 0 && test_usize <= max_usize) {
auto task = std::make_shared<BlockTask>();
task->pos = pos;
task->usize = static_cast<uint32_t>(test_usize);
task->csize = csize;
task->matched_method = codec;
task->is_encrypted = use_aes_ && (win_buf_[pos - win_start_] != active_buf[pos - win_start_]);
task->raw_win_buf.assign(hdr, hdr + csize);
task->dec_data.assign(dec_buf.data(), dec_buf.data() + test_usize);
pos += csize;
diagnostics_.blocks_validated++;
return task;
}
}
uint8_t walk_codec = 0;
bool walk_valid = false;
uint64_t walk_csize = WalkOodleChain(pos, walk_codec, walk_valid);
if (walk_valid && walk_csize >= Config::MIN_OODLE_BLOCK_SIZE && walk_csize <= pass_size) {
if (!ensure_window(pos, static_cast<size_t>(walk_csize))) { pos++; continue; }
active_buf = use_aes_ ? win_buf_dec_.get() : win_buf_.get();
hdr = &active_buf[pos - win_start_];
size_t max_safe_out = Config::MAX_DECOMP_BUF_SIZE;
size_t probe_cap = std::min(max_safe_out, std::max(static_cast<size_t>(max_usize), static_cast<size_t>(walk_csize) * 8));
if (chain_probe_buf_.size() < probe_cap) chain_probe_buf_.resize(probe_cap);
int64_t dec_size = OodleLZ_Decompress(hdr, static_cast<int>(walk_csize), chain_probe_buf_.data(),
static_cast<int>(probe_cap), 0, 0, 0, nullptr, 0, nullptr, nullptr, nullptr, 0, 0, 0);
if (dec_size >= Config::MIN_OODLE_BLOCK_SIZE && dec_size <= static_cast<int64_t>(probe_cap)) {
auto task = std::make_shared<BlockTask>();
task->pos = pos;
task->usize = static_cast<uint32_t>(dec_size);
task->csize = static_cast<uint32_t>(walk_csize);
task->matched_method = walk_codec;
task->is_encrypted = use_aes_ && (win_buf_[pos - win_start_] != active_buf[pos - win_start_]);
task->raw_win_buf.assign(hdr, hdr + walk_csize);
task->dec_data.assign(chain_probe_buf_.data(), chain_probe_buf_.data() + dec_size);
pos += walk_csize;
diagnostics_.blocks_validated++;
return task;
}
}
uint32_t found_usize = 0, found_csize = 0;
for (uint32_t usize : test_sizes) {
if (dec_buf.size() < usize) dec_buf.vec().resize(usize);
int64_t dec_result = OodleLZ_Decompress(hdr, pass_size, dec_buf.data(), usize, 0, 0, 0, nullptr, 0, nullptr, nullptr, nullptr, 0, 0, 0);
if (dec_result == static_cast<int64_t>(usize)) {
uint32_t max_needed = static_cast<uint32_t>(std::min(static_cast<uint64_t>(usize) + 65536, static_cast<uint64_t>(pass_size)));
found_csize = FindCompressedSize(hdr, max_needed, usize, dec_buf.data());
if (found_csize > 0 && found_csize < usize && found_csize >= Config::MIN_OODLE_BLOCK_SIZE) {
found_usize = usize;
break;
}
found_csize = 0;
}
}
if (found_csize > 0) {
diagnostics_.blocks_validated++;
auto task = std::make_shared<BlockTask>();
task->pos = pos;
task->usize = found_usize;
task->csize = found_csize;
task->matched_method = (b1 & 0x7F) == 0x06 ? 8 : (b1 & 0x7F) == 0x0A ? 9 :
(b1 & 0x7F) == 0x0C ? 11 : (b1 & 0x7F) == 0x0D ? 12 : 13;
task->is_encrypted = use_aes_ && (win_buf_[pos - win_start_] != active_buf[pos - win_start_]);
task->raw_win_buf.assign(hdr, hdr + found_csize);
std::vector<uint8_t> final_dec(found_usize);
OodleLZ_Decompress(hdr, found_csize, final_dec.data(), found_usize, 0, 0, 0, nullptr, 0, nullptr, nullptr, nullptr, 0, 0, 0);
task->dec_data = std::move(final_dec);
pos += found_csize;
return task;
}
if (pass_size > 1) skip_to_next_magic(hdr, pass_size);
else pos += 1;
}
return nullptr;
}
ThreadPool::ThreadPool(size_t threads) : stop(false) {
for (size_t i = 0; i < threads; ++i) {
workers.emplace_back([this] {
for (;;) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty()) return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
try { task(); } catch (const std::exception& e) {
std::cerr << "[ThreadPool] Exception: " << e.what() << "\n";
} catch (...) {
std::cerr << "[ThreadPool] Unknown exception.\n";
}
}
});
}
}
void ThreadPool::shutdown() {
{ std::unique_lock<std::mutex> lock(queue_mutex); if (stop) return; stop = true; }
condition.notify_all();
for (std::thread& worker : workers) if (worker.joinable()) worker.join();
}
ThreadPool::~ThreadPool() { shutdown(); }
UI::UI(uint64_t sz, uint32_t blks, bool v) : total_size(sz), total_blocks(blks), verbose(v) { start_time = std::chrono::steady_clock::now(); }
void UI::log(const std::string& message) {
if (!verbose) return;
std::lock_guard<std::mutex> lock(Logger::log_mutex);
std::cout << "[VERBOSE] " << message << "\n";
}
void UI::set_stats(uint32_t m, uint32_t f) {
matches.store(m, std::memory_order_relaxed);
fails.store(f, std::memory_order_relaxed);
}
std::string UI::format_time(double total_seconds) {
int h = static_cast<int>(total_seconds) / 3600; int m = (static_cast<int>(total_seconds) % 3600) / 60; int s = static_cast<int>(total_seconds) % 60;
std::ostringstream oss; oss << std::setfill('0') << std::setw(2) << h << ":" << std::setfill('0') << std::setw(2) << m << ":" << std::setfill('0') << std::setw(2) << s; return oss.str();
}
void UI::update(uint64_t current_pos, uint32_t current_block, const char* label, uint64_t out_size) {
if (verbose) return;
auto now = std::chrono::steady_clock::now();
double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count() / 1000.0;
double progress = total_size > 0 ? static_cast<double>(current_pos) / static_cast<double>(total_size) : 0.0;
if (progress > 1.0) progress = 1.0;
double eta = (progress > 0.0001) ? (elapsed / progress) - elapsed : 0;
double mbps = (current_pos / 1024.0 / 1024.0) / (elapsed + 0.001);
uint32_t m = matches.load(std::memory_order_relaxed);
uint32_t f = fails.load(std::memory_order_relaxed);
std::cout << "\r\033[K" << label << " [" << std::fixed << std::setprecision(2) << progress * 100.0 << "%] "
<< "Blk: " << current_block << (total_blocks > 0 ? "/" + std::to_string(total_blocks) : "")
<< " [E:" << m << " F:" << f << "]"
<< " | " << std::setprecision(2) << mbps << " MB/s"