Repository navigation
Expand file tree
/
Copy pathreconstruct.cpp
More file actions
301 lines (255 loc) · 12.5 KB
/
Copy pathreconstruct.cpp
File metadata and controls
301 lines (255 loc) · 12.5 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
#include "common.h"
#include <iomanip>
#include <future>
Result<int> RunReconstruct(const std::string& input_path, const std::string& output_path, bool verbose, int num_threads,
std::vector<uint8_t>& aesKey, bool& useAES, uint32_t tradeoff_bytes, bool quantum_crc) {
ThreadSafeReader reader(input_path);
if (!reader.is_open()) {
return Result<int>(ErrorCode::ERR_FILE_NOT_FOUND, "Failed to open target archive file: " + input_path);
}
uint64_t fsz = reader.get_size();
if (fsz < sizeof(PreHeader)) {
return Result<int>(ErrorCode::ERR_INVALID_MAGIC, "File is too small to be a valid archive.");
}
PreHeader hdr;
reader.pread(reinterpret_cast<char*>(&hdr), sizeof(hdr), 0);
if (hdr.magic != 0x50524546 || hdr.version != 34) {
return Result<int>(ErrorCode::ERR_INVALID_MAGIC, "Invalid archive magic or version mismatch.");
}
ResolveAESKey(aesKey, useAES, hdr);
tradeoff_bytes = hdr.space_speed_tradeoff_bytes;
quantum_crc = hdr.quantum_crc != 0;
if (hdr.block_count > Config::MAX_BLOCKS) {
return Result<int>(ErrorCode::ERR_INVALID_MAGIC,
"Archive claims too many blocks (" + std::to_string(hdr.block_count) + " > " + std::to_string(Config::MAX_BLOCKS) + ")");
}
std::vector<PreBlock> blocks(hdr.block_count);
uint64_t blocks_meta_size = hdr.block_count * sizeof(PreBlock);
if (fsz < sizeof(PreHeader) + blocks_meta_size) {
return Result<int>(ErrorCode::ERR_INVALID_MAGIC, "Archive metadata exceeds file size.");
}
reader.pread(reinterpret_cast<char*>(blocks.data()), blocks_meta_size, fsz - blocks_meta_size);
std::cout << "[REC] Reconstructing " << hdr.original_size / 1024 / 1024 << " MB (" << hdr.block_count << " blocks) using " << num_threads << " threads." << std::endl;
FastStreamWriter fast_out;
if (!fast_out.open(output_path)) {
return Result<int>(ErrorCode::ERR_FILE_NOT_FOUND, "Failed to open rebuild file.");
}
AESContextPtr aesCtxE;
if (useAES) {
if (aesKey.size() != 32) {
return Result<int>(ErrorCode::ERR_INVALID_ARGUMENT, "AES key must be 32 bytes.");
}
aesCtxE = AESContextPtr(AES_Context_Create(aesKey.data()));
if (!aesCtxE) {
return Result<int>(ErrorCode::ERR_UNKNOWN, "Failed to create AES encryption context.");
}
}
ThreadPool pool(num_threads);
ObjectPool<char> gap_pool(num_threads * 2, Config::GAP_POOL_CHUNK);
UI ui(hdr.original_size, hdr.block_count, verbose);
std::atomic<uint32_t> dec_matches{0}, dec_fails{0};
std::queue<std::future<std::shared_ptr<DecTask>>> dec_queue;
uint64_t archive_offset = sizeof(PreHeader);
uint64_t original_offset = 0;
std::atomic<bool> fatal_abort{false};
auto last_ui_time = std::chrono::steady_clock::now();
uint32_t processed_blocks = 0;
OodleLZ_CompressOptions opts = {};
opts.version = 232;
opts.spaceSpeedTradeoffBytes = tradeoff_bytes;
opts.sendQuantumCRCs = quantum_crc ? 1 : 0;
auto process_and_write = [&](std::shared_ptr<DecTask> task) -> bool {
if (task->fatal_error) {
std::cerr << "\n[FATAL] " << task->error_msg << std::endl;
return false;
}
try {
if (task->gap_len > 0) {
if (!write_gap(reader, fast_out, gap_pool, task->gap_archive_offset, task->gap_len)) {
std::cerr << "[FATAL] Failed to write gap data.\n";
return false;
}
if (fast_out.has_error()) {
std::cerr << "[FATAL] Gap write failed: " << fast_out.get_last_error() << "\n";
return false;
}
}
uint8_t* data_ptr = task->b.exact_match ? task->compressed_out.data() : task->block_data_in.data();
size_t data_len = task->b.exact_match ? task->compressed_out.size() : task->b.stored_size;
if (useAES && task->b.was_encrypted && task->b.exact_match) {
size_t aes_len = (data_len + 15) & ~15;
if (task->compressed_out.size() < aes_len) {
task->compressed_out.resize(aes_len, 0);
data_ptr = task->compressed_out.data();
}
AES_Context_Encrypt(aesCtxE.get(), data_ptr, static_cast<uint32_t>(aes_len));
data_len = aes_len;
}
fast_out.write(reinterpret_cast<const char*>(data_ptr), data_len);
if (fast_out.has_error()) {
std::cerr << "[FATAL] Write failed: " << fast_out.get_last_error() << "\n";
return false;
}
} catch (const std::exception& e) {
std::cerr << "\n[FATAL] File I/O Error: " << e.what() << std::endl;
return false;
}
return true;
};
for (uint32_t i = 0; i < hdr.block_count; ++i) {
if (fatal_abort) break;
const auto& b = blocks[i];
if (b.original_offset < original_offset) {
std::cerr << "\n[FATAL] Malformed archive: block offsets overlap or are out of order." << std::endl;
fatal_abort = true;
break;
}
uint64_t gap = b.original_offset - original_offset;
auto task = std::make_shared<DecTask>();
task->b = b;
task->gap_len = gap;
task->gap_archive_offset = archive_offset;
archive_offset += gap;
task->o_pos_start = original_offset;
if (b.exact_match) {
if (b.stored_size < sizeof(BlockHeader) + b.decompressed_size) {
task->fatal_error = true;
task->error_msg = "Invalid stored_size for exact-match block: " + std::to_string(b.stored_size);
fatal_abort = true;
break;
}
}
task->block_data_in.resize(b.stored_size);
if (reader.pread(reinterpret_cast<char*>(task->block_data_in.data()), b.stored_size, archive_offset) != b.stored_size) {
task->fatal_error = true;
task->error_msg = "Failed to read block data at offset " + std::to_string(archive_offset);
fatal_abort = true;
break;
}
if (b.exact_match) {
if (b.decompressed_size > Config::MAX_SAFE_COMPRESSED_SIZE) {
task->fatal_error = true;
task->error_msg = "Decompressed size too large: " + std::to_string(b.decompressed_size);
fatal_abort = true;
break;
}
dec_queue.push(pool.enqueue([task, &dec_matches, &opts]() {
if (task->b.original_compressed_size > Config::MAX_SAFE_COMPRESSED_SIZE) {
task->fatal_error = true;
task->error_msg = "Unreasonable compressed size detected.";
return task;
}
uint32_t crc_stored = CalculateCRC32(task->block_data_in.data(), task->b.stored_size);
if (crc_stored != task->b.crc32) {
task->fatal_error = true;
task->error_msg = "CRC32 Integrity Check Failed for exact-match block data.";
return task;
}
static ReusableBufferPool<uint8_t> g_rec_scratch_pool;
static ReusableBufferPool<uint8_t> g_rec_temp_pool;
PooledBuffer<uint8_t> scratch_mem(g_rec_scratch_pool, 8 * 1024 * 1024);
size_t safe_bound = SafeCompressBound(task->b.decompressed_size);
PooledBuffer<uint8_t> safe_temp(g_rec_temp_pool, safe_bound);
int32_t rec_method = task->b.compressor & 0xFF;
int32_t rec_level = task->b.compressor >> 8;
int64_t comp_res = OodleLZ_Compress(
rec_method, task->block_data_in.data() + sizeof(BlockHeader),
task->b.decompressed_size, safe_temp.data(), rec_level,
&opts, nullptr, nullptr, scratch_mem.data(), scratch_mem.size()
);
if (comp_res <= 0 || static_cast<size_t>(comp_res) > safe_temp.size()) {
task->fatal_error = true;
task->error_msg = "Oodle Re-compression failed or output overflow.";
return task;
}
if (comp_res != static_cast<int64_t>(task->b.original_compressed_size)) {
task->fatal_error = true;
task->error_msg = "Oodle Re-compression size mismatch. Stream is non-deterministic or corrupted.";
return task;
}
task->compressed_out.assign(safe_temp.data(), safe_temp.data() + comp_res);
dec_matches++;
return task;
}));
} else {
dec_queue.push(pool.enqueue([task, &dec_fails]() {
uint32_t crc_stored = CalculateCRC32(task->block_data_in.data(), task->b.stored_size);
if (crc_stored != task->b.crc32) {
task->fatal_error = true;
task->error_msg = "CRC32 Integrity Check Failed for raw block.";
return task;
}
dec_fails++;
return task;
}));
}
archive_offset += b.stored_size;
original_offset = b.original_offset + b.original_compressed_size;
// [ARCHITECTURAL GUARANTEE: Strict FIFO Sequencer]
// We MUST call .get() on dec_queue.front(). This acts as a synchronization barrier.
// Even if block N+1 finishes decompression before block N, the main thread will block
// here until block N is ready. This guarantees that process_and_write() is called
// in strict sequential order (0, 1, 2, ...), preventing any out-of-order disk writes.
while (dec_queue.size() >= static_cast<size_t>(num_threads * 2)) {
std::shared_ptr<DecTask> completed;
try {
completed = dec_queue.front().get();
} catch (const std::exception& e) {
std::cerr << "\n[FATAL] Task exception: " << e.what() << std::endl;
fatal_abort = true;
break;
}
dec_queue.pop();
if (!process_and_write(completed)) {
fatal_abort = true;
break;
}
processed_blocks++;
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last_ui_time).count() >= 500) {
ui.set_stats(dec_matches.load(), dec_fails.load());
ui.update(completed->o_pos_start, processed_blocks, "REC", fast_out.tellp());
last_ui_time = now;
}
}
if (fatal_abort) break;
}
while (!dec_queue.empty() && !fatal_abort) {
std::shared_ptr<DecTask> completed;
try {
completed = dec_queue.front().get();
} catch (const std::exception& e) {
std::cerr << "\n[FATAL] Task exception: " << e.what() << std::endl;
fatal_abort = true;
break;
}
dec_queue.pop();
if (!process_and_write(completed)) {
fatal_abort = true;
}
}
if (fatal_abort) {
return Result<int>(ErrorCode::ERR_UNKNOWN, "Reconstruction aborted due to fatal error.");
}
try {
if (hdr.original_size > original_offset) {
if (!write_gap(reader, fast_out, gap_pool, archive_offset, hdr.original_size - original_offset)) {
return Result<int>(ErrorCode::ERR_UNKNOWN, "Failed to write final gap.");
}
if (fast_out.has_error()) {
return Result<int>(ErrorCode::ERR_UNKNOWN, "Final gap write failed: " + fast_out.get_last_error());
}
}
fast_out.flush();
if (fast_out.has_error()) {
return Result<int>(ErrorCode::ERR_UNKNOWN, "Final flush failed: " + fast_out.get_last_error());
}
} catch (const std::exception& e) {
return Result<int>(ErrorCode::ERR_UNKNOWN, std::string("Final file write/flush failed: ") + e.what());
}
std::cout << std::endl << "--- Reconstruction Performance ---" << std::endl
<< "Restored Architecture Size: " << hdr.original_size / 1024.0 / 1024.0 << " MB" << std::endl
<< "Exact Matches: " << dec_matches.load() << " | Full Copies: " << dec_fails.load() << std::endl
<< "Total Duration: " << ui.format_time(ui.get_elapsed()) << std::endl;
return Result<int>(0);
}