Repository navigation
Expand file tree
/
Copy pathscan.cpp
More file actions
197 lines (162 loc) · 7.9 KB
/
Copy pathscan.cpp
File metadata and controls
197 lines (162 loc) · 7.9 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
#include "common.h"
Result<int> RunScan(const std::string& input_path, bool verbose, int num_threads,
const std::vector<int32_t>& m_ids, const std::vector<int32_t>& opt_levels,
bool opt_auto, bool opt_force, const std::vector<uint8_t>& aesKey,
bool useAES, double scan_percent, bool debug_mode,
uint32_t tradeoff_bytes, bool quantum_crc)
{
if (scan_percent <= 0.0 || scan_percent > 100.0) {
return Result<int>(ErrorCode::ERR_INVALID_ARGUMENT, std::string("Scan percentage must be between 0.0 and 100.0"));
}
Logger::Init(debug_mode);
ThreadSafeReader reader(input_path);
if (!reader.is_open()) {
return Result<int>(ErrorCode::ERR_FILE_NOT_FOUND, "Failed to open input file: " + input_path);
}
uint64_t fSize = reader.get_size();
uint64_t scan_limit = static_cast<uint64_t>(fSize * (scan_percent / 100.0));
if (scan_percent >= 99.9) scan_limit = fSize;
std::cout << "[BUILD MARKER: STABLE_V2_" << __DATE__ << "_" << __TIME__ << "]" << std::endl;
std::cout << "[SCAN] Scanning " << scan_percent << "% of file ("
<< scan_limit / 1024 / 1024 << " / " << fSize / 1024 / 1024
<< " MB) using " << num_threads << " threads." << std::endl;
OodleLZ_CompressOptions opts = {};
opts.version = 232;
opts.spaceSpeedTradeoffBytes = tradeoff_bytes;
opts.sendQuantumCRCs = quantum_crc ? 1 : 0;
UI ui(scan_limit, 0, verbose);
size_t actual_win = Config::WIN_SIZE_LARGE;
BlockScanner scanner(reader, fSize, useAES, aesKey, actual_win);
ScanStats stats;
std::set<std::pair<int32_t, int32_t>> identified_cache;
std::shared_mutex scan_mutex;
std::queue<std::future<std::shared_ptr<BlockTask>>> scan_queue;
std::array<std::atomic<uint32_t>, 16> method_counts_atomic{};
std::array<std::atomic<uint32_t>, 16> level_counts_atomic{};
uint64_t pos = 0;
uint64_t last_block_end = 0;
std::vector<std::pair<uint64_t, uint64_t>> gaps;
auto last_ui_time = std::chrono::steady_clock::now();
std::vector<uint32_t> usizes_vec(std::begin(Config::TEST_USIZES), std::end(Config::TEST_USIZES));
std::vector<int32_t> methods_vec = m_ids.empty()
? std::vector<int32_t>(std::begin(Config::ALL_METHODS), std::end(Config::ALL_METHODS))
: m_ids;
std::vector<int32_t> levels_vec;
if (!opt_levels.empty()) {
levels_vec = opt_levels;
} else if (opt_auto) {
levels_vec = {4, 6, 7};
} else if (opt_force) {
levels_vec = {3, 4, 5, 6, 7, 8};
} else {
levels_vec = {4, 5, 6, 7};
}
ThreadPool pool(num_threads);
while (pos < scan_limit && pos < fSize) {
auto task = scanner.extract_next_block(pos, scan_limit, usizes_vec);
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last_ui_time).count() >= 1000) {
uint32_t m = stats.matches_identified.load(std::memory_order_relaxed);
uint32_t f = stats.fails_unidentified.load(std::memory_order_relaxed);
uint32_t bf = stats.blocks_found.load(std::memory_order_relaxed);
ui.set_stats(m, f);
ui.update(pos, bf, "SCAN", pos);
last_ui_time = now;
}
if (!task) {
pos += 64;
continue;
}
if (task->pos > last_block_end) {
gaps.push_back({last_block_end, task->pos - last_block_end});
}
last_block_end = task->pos + task->csize;
stats.blocks_found.fetch_add(1, std::memory_order_relaxed);
scan_queue.push(pool.enqueue([task, methods_vec, levels_vec, &scan_mutex, &identified_cache, &stats, &opts, &method_counts_atomic, &level_counts_atomic]() {
int32_t m = -1, l = -1;
bool match = TryMatchBlock(task, methods_vec, levels_vec, identified_cache, scan_mutex, m, l, &opts);
if (match) {
stats.matches_identified.fetch_add(1, std::memory_order_relaxed);
if (m >= 0 && m < 16) method_counts_atomic[m].fetch_add(1, std::memory_order_relaxed);
if (l >= 0 && l < 16) level_counts_atomic[l].fetch_add(1, std::memory_order_relaxed);
} else {
stats.fails_unidentified.fetch_add(1, std::memory_order_relaxed);
}
return task;
}));
while (scan_queue.size() >= static_cast<size_t>(num_threads) * 2) {
try {
auto completed = scan_queue.front().get();
} catch (const std::exception& e) {
std::cerr << "[SCAN] Task exception: " << e.what() << "\n";
}
scan_queue.pop();
}
}
while (!scan_queue.empty()) {
try {
auto completed = scan_queue.front().get();
} catch (const std::exception& e) {
std::cerr << "[SCAN] Task exception: " << e.what() << "\n";
}
scan_queue.pop();
}
std::map<int32_t, uint32_t> final_method_counts;
std::map<int32_t, uint32_t> final_level_counts;
for (int i = 0; i < 16; ++i) {
uint32_t m = method_counts_atomic[i].load(std::memory_order_relaxed);
uint32_t l = level_counts_atomic[i].load(std::memory_order_relaxed);
if (m > 0) final_method_counts[i] = m;
if (l > 0) final_level_counts[l] = l;
}
std::cout << std::endl << "--- Scan Report ---" << std::endl
<< "Blocks Found: " << stats.blocks_found.load()
<< " | Matches: " << stats.matches_identified.load()
<< " | Fails: " << stats.fails_unidentified.load() << std::endl;
if (stats.blocks_found.load() > 0) {
std::cout << "Success Rate: " << std::fixed << std::setprecision(2)
<< (static_cast<double>(stats.matches_identified.load()) / stats.blocks_found.load() * 100.0) << "%" << std::endl;
}
if (!final_method_counts.empty()) {
std::cout << "Methods Identified: ";
for (auto const& [method_id, count] : final_method_counts) {
std::cout << "Mth:" << method_id << " (" << count << ") ";
}
std::cout << std::endl;
}
if (!final_level_counts.empty()) {
std::cout << "Levels Identified: ";
for (auto const& [level_id, count] : final_level_counts) {
std::cout << "Lvl:" << level_id << " (" << count << ") ";
}
std::cout << std::endl;
}
std::cout << "Time: " << ui.format_time(ui.get_elapsed()) << std::endl;
{
const auto& d = scanner.diagnostics_;
std::cout << std::endl << "--- Scan Diagnostics ---" << std::endl
<< "Magic byte candidates found: " << d.magic_candidates_found << std::endl
<< "Passed fast-rejection filter: " << d.passed_fast_rejection << std::endl
<< "Rejected by fast-rejection: " << d.rejected_by_fast_rejection << std::endl
<< "--- WalkOodleChain breakdown ---" << std::endl
<< "Calls: " << d.walk_called << std::endl
<< " Succeeded: " << d.walk_succeeded << std::endl;
}
if (scan_limit > last_block_end) {
gaps.push_back({last_block_end, scan_limit - last_block_end});
}
{
uint64_t total_gap_bytes = 0;
for (const auto& g : gaps) total_gap_bytes += g.second;
double gap_percentage = (fSize > 0) ? (total_gap_bytes / static_cast<double>(fSize) * 100.0) : 0.0;
std::cout << std::endl << "--- Gap Report ---" << std::endl
<< "Gap count: " << gaps.size() << " | Total gap bytes: " << total_gap_bytes << std::endl
<< "Gap percentage: " << gap_percentage << "%" << std::endl;
if (gap_percentage > 15.0) {
std::cout << "\n⚠️ HIGH GAP PERCENTAGE DETECTED!" << std::endl;
} else {
std::cout << "\n✓ Gap percentage is within normal range" << std::endl;
}
}
return Result<int>(0);
}