-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogos.cpp
More file actions
645 lines (565 loc) · 21.7 KB
/
logos.cpp
File metadata and controls
645 lines (565 loc) · 21.7 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
/*
* LOGOS: Semantic Compression Engine (C++ Port)
* Compile: g++ -O3 logos.cpp -o logos -lz
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>
#include <algorithm>
#include <zlib.h>
#include <iomanip>
#include <stdexcept>
#include <ctime>
#include <cstdint>
// Use 500MB blocks to avoid RAM exhaustion
#define BLOCK_SIZE (500 * 1024 * 1024)
using namespace std;
// ==========================================
// UTILS: Varint & ZigZag Encoding
// ==========================================
// ZigZag: Maps signed integers to unsigned (0=-0, -1=1, 1=2, -2=3...)
// This allows small negative numbers (deltas) to use few bits.
uint64_t zigzag_encode(int64_t n) {
return (n << 1) ^ (n >> 63);
}
int64_t zigzag_decode(uint64_t n) {
return (n >> 1) ^ -(n & 1);
}
// Write a number into a buffer using variable length encoding (LEB128 style)
void write_varint(vector<uint8_t>& buf, uint64_t val) {
while (val >= 128) {
buf.push_back((val & 0x7F) | 0x80);
val >>= 7;
}
buf.push_back(val);
}
// Read varint with bounds checking
uint64_t read_varint(const uint8_t*& ptr, const uint8_t* end) {
uint64_t val = 0;
int shift = 0;
while (ptr < end) {
uint8_t b = *ptr++;
val |= (uint64_t)(b & 0x7F) << shift;
if (!(b & 0x80)) return val;
shift += 7;
if (shift > 63) {
throw runtime_error("Varint overflow");
}
}
throw runtime_error("Unexpected end of buffer reading varint");
}
// ==========================================
// COMPRESSION WRAPPERS (ZLIB)
// ==========================================
vector<uint8_t> zlib_compress(const vector<uint8_t>& data) {
if (data.empty()) {
return vector<uint8_t>();
}
uLong destLen = compressBound(data.size());
vector<uint8_t> dest(destLen);
if (compress(dest.data(), &destLen, data.data(), data.size()) != Z_OK) {
throw runtime_error("Zlib compression failed");
}
dest.resize(destLen);
return dest;
}
vector<uint8_t> zlib_decompress(const uint8_t* data, size_t size, size_t original_size) {
if (size == 0 || original_size == 0) {
return vector<uint8_t>();
}
vector<uint8_t> dest(original_size);
uLong destLen = original_size;
if (uncompress(dest.data(), &destLen, data, size) != Z_OK) {
throw runtime_error("Zlib decompression failed");
}
return dest;
}
// ==========================================
// PARSERS
// ==========================================
struct Column {
vector<string> raw;
bool is_int = true;
void add(const string& s) {
raw.push_back(s);
if (is_int) {
// Quick check if integer
if (s.empty()) { is_int = false; return; }
size_t start = (s[0] == '-' || s[0] == '+') ? 1 : 0;
if (start >= s.size()) { is_int = false; return; }
for (size_t i = start; i < s.size(); i++) {
if (!isdigit(s[i])) { is_int = false; break; }
}
// Prevent leading zero corruption (Zip codes)
if (s.size() > 1 && s[0] == '0') is_int = false;
}
}
};
// Helper to escape a CSV value properly
string escape_csv_value(const string& val) {
bool needs_quote = false;
for (char c : val) {
if (c == ',' || c == '"' || c == '\n' || c == '\r') {
needs_quote = true;
break;
}
}
if (!needs_quote) {
return val;
}
string result = "\"";
for (char c : val) {
if (c == '"') {
result += "\"\""; // Escape quotes by doubling
} else {
result += c;
}
}
result += "\"";
return result;
}
// Helper to escape a SQL string value properly
string escape_sql_value(const string& val) {
string result;
for (char c : val) {
if (c == '\'') {
result += "''"; // Escape quotes by doubling
} else {
result += c;
}
}
return result;
}
class LogosEncoder {
public:
static vector<uint8_t> pack_csv(const string& text) {
// 1. Parse CSV
vector<Column> columns;
size_t row_count = 0;
size_t pos = 0;
size_t len = text.size();
bool in_quote = false;
string current_val;
size_t col_idx = 0;
while (pos < len) {
char c = text[pos];
if (c == '"') {
// Handle escaped quotes ("") inside quoted fields
if (in_quote && pos + 1 < len && text[pos + 1] == '"') {
current_val += '"';
pos++; // Skip the second quote
} else {
in_quote = !in_quote;
}
} else if (c == ',' && !in_quote) {
// Dynamically grow columns if needed
while (columns.size() <= col_idx) {
Column new_col;
// Pad with empty values for previous rows
for (size_t r = 0; r < row_count; r++) {
new_col.add("");
}
columns.push_back(new_col);
}
columns[col_idx].add(current_val);
current_val.clear();
col_idx++;
} else if ((c == '\n' || c == '\r') && !in_quote) {
// Handle CRLF
if (c == '\r' && pos + 1 < len && text[pos+1] == '\n') pos++;
// Dynamically grow columns if needed
while (columns.size() <= col_idx) {
Column new_col;
for (size_t r = 0; r < row_count; r++) {
new_col.add("");
}
columns.push_back(new_col);
}
columns[col_idx].add(current_val);
current_val.clear();
// Pad any short rows (columns that didn't appear in this row)
for (size_t c = col_idx + 1; c < columns.size(); c++) {
columns[c].add("");
}
col_idx = 0;
row_count++;
} else {
current_val += c;
}
pos++;
}
// Handle trailing value (always add, even if empty, to preserve column alignment)
if (col_idx > 0 || !current_val.empty() || (len > 0 && text[len-1] == ',')) {
while (columns.size() <= col_idx) {
Column new_col;
for (size_t r = 0; r < row_count; r++) {
new_col.add("");
}
columns.push_back(new_col);
}
columns[col_idx].add(current_val);
// Pad remaining columns
for (size_t c = col_idx + 1; c < columns.size(); c++) {
columns[c].add("");
}
}
return encode_columns(columns);
}
static vector<uint8_t> pack_sql(const string& text) {
// 1. Find VALUES
// Simple search for bulk inserts: (...)
vector<Column> columns;
size_t num_cols_detected = 0;
// Skip until VALUES (case insensitive simplified)
size_t val_pos = text.find("VALUES");
if (val_pos == string::npos) val_pos = text.find("values");
if (val_pos == string::npos) {
// Try lowercase
string lower_text = text;
transform(lower_text.begin(), lower_text.end(), lower_text.begin(), ::tolower);
val_pos = lower_text.find("values");
}
if (val_pos == string::npos) throw runtime_error("No VALUES clause found");
size_t cursor = val_pos;
bool inside_tuple = false;
string current_val;
size_t col_idx = 0;
bool in_quote = false;
while (cursor < text.size()) {
char c = text[cursor];
if (c == '\'') {
// Handle escaped quotes ('')
if (in_quote && cursor + 1 < text.size() && text[cursor + 1] == '\'') {
current_val += '\'';
cursor++; // Skip the second quote
} else {
in_quote = !in_quote;
}
} else if (!in_quote) {
if (c == '(') {
inside_tuple = true;
col_idx = 0;
current_val.clear();
} else if (c == ')') {
if (inside_tuple) {
// Push last value
if (columns.size() <= col_idx) columns.resize(col_idx + 1);
columns[col_idx].add(clean_sql_val(current_val));
current_val.clear();
// Track expected column count from first tuple
if (num_cols_detected == 0) {
num_cols_detected = col_idx + 1;
}
inside_tuple = false;
}
} else if (c == ',' && inside_tuple) {
if (columns.size() <= col_idx) columns.resize(col_idx + 1);
columns[col_idx].add(clean_sql_val(current_val));
current_val.clear();
col_idx++;
} else if (inside_tuple) {
current_val += c;
}
} else {
// Inside quoted string
if (inside_tuple) current_val += c;
}
cursor++;
}
return encode_columns(columns);
}
private:
static string clean_sql_val(string s) {
// Trim whitespace and quotes
size_t first = s.find_first_not_of(" \t\r\n");
if (string::npos == first) return "";
size_t last = s.find_last_not_of(" \t\r\n");
string t = s.substr(first, (last - first + 1));
if (t.size() >= 2 && t.front() == '\'' && t.back() == '\'') {
return t.substr(1, t.size() - 2);
}
// Handle NULL values
if (t == "NULL" || t == "null") {
return "";
}
return t;
}
static vector<uint8_t> encode_columns(const vector<Column>& cols) {
vector<uint8_t> final_blob;
// Header: Num Cols
uint32_t num_cols = cols.size();
final_blob.resize(4);
memcpy(final_blob.data(), &num_cols, 4);
for (const auto& col : cols) {
vector<uint8_t> col_buffer;
uint8_t type_tag = 0; // 0=STR, 1=INT_DELTA
if (col.is_int && !col.raw.empty()) {
type_tag = 1;
// INT DELTA
vector<uint8_t> delta_buf;
int64_t prev = 0;
for (const auto& s : col.raw) {
try {
int64_t curr = stoll(s);
int64_t delta = curr - prev;
write_varint(delta_buf, zigzag_encode(delta));
prev = curr;
} catch (...) {
type_tag = 0; break; // Fallback
}
}
if (type_tag == 1) col_buffer = delta_buf;
}
if (type_tag == 0) {
// STRING (Null separated)
for (const auto& s : col.raw) {
col_buffer.insert(col_buffer.end(), s.begin(), s.end());
col_buffer.push_back(0);
}
}
// Compress
vector<uint8_t> compressed = zlib_compress(col_buffer);
// Format: [Type 1b] [Orig Size 4b] [Comp Size 4b] [Data...]
uint32_t orig_sz = col_buffer.size();
uint32_t comp_sz = compressed.size();
final_blob.push_back(type_tag);
size_t old_sz = final_blob.size();
final_blob.resize(old_sz + 8);
memcpy(final_blob.data() + old_sz, &orig_sz, 4);
memcpy(final_blob.data() + old_sz + 4, &comp_sz, 4);
final_blob.insert(final_blob.end(), compressed.begin(), compressed.end());
}
return final_blob;
}
};
class LogosDecoder {
public:
static void unpack_csv(const uint8_t* data, size_t len, const string& outfile) {
ofstream out(outfile, ios::binary);
if (!out) {
throw runtime_error("Failed to open output file: " + outfile);
}
vector<vector<string>> columns = decode_columns(data, len);
if (columns.empty()) return;
size_t num_rows = columns[0].size();
size_t num_cols = columns.size();
for (size_t i = 0; i < num_rows; i++) {
for (size_t c = 0; c < num_cols; c++) {
if (i < columns[c].size()) {
out << escape_csv_value(columns[c][i]);
}
if (c < num_cols - 1) out << ",";
}
out << "\n";
}
}
static void unpack_sql(const uint8_t* data, size_t len, const string& outfile) {
ofstream out(outfile, ios::binary);
if (!out) {
throw runtime_error("Failed to open output file: " + outfile);
}
vector<vector<string>> columns = decode_columns(data, len);
if (columns.empty()) return;
size_t num_rows = columns[0].size();
size_t num_cols = columns.size();
out << "INSERT INTO restored_table VALUES \n";
for (size_t i = 0; i < num_rows; i++) {
out << "(";
for (size_t c = 0; c < num_cols; c++) {
if (i < columns[c].size()) {
string val = columns[c][i];
// Heuristic: if it looks like int/float, no quotes. Else quotes.
bool numeric = !val.empty();
bool has_dot = false;
for (size_t k = 0; k < val.size(); k++) {
char ch = val[k];
if (ch == '-' && k == 0) continue; // Allow leading minus
if (ch == '.' && !has_dot) { has_dot = true; continue; }
if (!isdigit(ch)) { numeric = false; break; }
}
if (numeric && !val.empty()) {
out << val;
} else {
out << "'" << escape_sql_value(val) << "'";
}
} else {
out << "NULL";
}
if (c < num_cols - 1) out << ",";
}
out << ")";
if (i < num_rows - 1) out << ",\n";
else out << ";\n";
}
}
private:
static vector<vector<string>> decode_columns(const uint8_t* ptr, size_t total_len) {
vector<vector<string>> columns;
const uint8_t* end = ptr + total_len;
if (total_len < 4) {
throw runtime_error("Invalid data: too short for header");
}
// Read Num Cols
uint32_t num_cols;
memcpy(&num_cols, ptr, 4);
ptr += 4;
// Sanity check
if (num_cols > 10000) {
throw runtime_error("Invalid data: too many columns");
}
for (uint32_t i = 0; i < num_cols; i++) {
if (ptr + 9 > end) {
throw runtime_error("Unexpected end of data reading column header");
}
uint8_t type_tag = *ptr++;
uint32_t orig_sz, comp_sz;
memcpy(&orig_sz, ptr, 4); ptr += 4;
memcpy(&comp_sz, ptr, 4); ptr += 4;
if (ptr + comp_sz > end) {
throw runtime_error("Unexpected end of data reading column data");
}
vector<uint8_t> decomp = zlib_decompress(ptr, comp_sz, orig_sz);
ptr += comp_sz;
vector<string> col_vals;
if (type_tag == 1) { // INT DELTA
const uint8_t* dptr = decomp.data();
const uint8_t* dend = dptr + decomp.size();
int64_t val = 0;
while (dptr < dend) {
int64_t delta = zigzag_decode(read_varint(dptr, dend));
val += delta;
col_vals.push_back(to_string(val));
}
} else { // STRING
size_t start = 0;
for (size_t k = 0; k < decomp.size(); k++) {
if (decomp[k] == 0) {
col_vals.push_back(string(decomp.begin() + start, decomp.begin() + k));
start = k + 1;
}
}
// Handle case where data doesn't end with null terminator
if (start < decomp.size()) {
col_vals.push_back(string(decomp.begin() + start, decomp.end()));
}
}
columns.push_back(col_vals);
}
return columns;
}
};
// ==========================================
// MAIN
// ==========================================
string get_extension(const string& path) {
size_t dot_pos = path.find_last_of(".");
if (dot_pos == string::npos || dot_pos == path.size() - 1) {
return "";
}
return path.substr(dot_pos + 1);
}
int main(int argc, char* argv[]) {
if (argc < 3) {
cerr << "Logos C++ v1.0\nUsage: ./logos [pack|unpack] [file]\n";
return 1;
}
string mode = argv[1];
string path = argv[2];
try {
if (mode == "pack") {
ifstream in(path, ios::binary);
if (!in) {
cerr << "[!] Failed to open input file: " << path << endl;
return 1;
}
string str((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
vector<uint8_t> out;
uint8_t flag = 0; // 1=SQL, 2=CSV
// Detection
string ext = get_extension(path);
cout << "[*] Processing " << path << " (" << str.size()/1024 << " KB)..." << endl;
clock_t start = clock();
if (ext == "sql" || str.find("INSERT INTO") != string::npos || str.find("insert into") != string::npos) {
out = LogosEncoder::pack_sql(str);
flag = 1;
} else if (ext == "csv") {
out = LogosEncoder::pack_csv(str);
flag = 2;
} else {
cerr << "[!] Unknown texture. Fallback to raw zlib." << endl;
// Fallback: just compress the raw data
vector<uint8_t> raw_data(str.begin(), str.end());
out = zlib_compress(raw_data);
flag = 0;
}
string out_path = path + ".logos";
ofstream f_out(out_path, ios::binary);
if (!f_out) {
cerr << "[!] Failed to open output file: " << out_path << endl;
return 1;
}
f_out.put(flag);
f_out.write((char*)out.data(), out.size());
f_out.close();
double elapsed = (double)(clock() - start) / CLOCKS_PER_SEC;
cout << "[+] Packed to " << out.size()/1024 << " KB in " << elapsed << "s" << endl;
cout << "[+] Ratio: " << (1.0 - (double)out.size()/str.size()) * 100.0 << "%" << endl;
} else if (mode == "unpack") {
ifstream in(path, ios::binary);
if (!in) {
cerr << "[!] Failed to open input file: " << path << endl;
return 1;
}
uint8_t flag = in.get();
// Read remaining
vector<uint8_t> data((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
string out_path = path + ".restored";
if (path.find(".logos") != string::npos) {
out_path = path.substr(0, path.find(".logos")) + ".restored";
}
cout << "[*] Unpacking..." << endl;
if (flag == 1) {
LogosDecoder::unpack_sql(data.data(), data.size(), out_path);
} else if (flag == 2) {
LogosDecoder::unpack_csv(data.data(), data.size(), out_path);
} else if (flag == 0) {
// Raw zlib fallback
cout << "[*] Detected raw zlib data, decompressing..." << endl;
// We don't know original size, so we need to try progressively larger buffers
size_t est_size = data.size() * 4;
vector<uint8_t> dest;
int result;
do {
dest.resize(est_size);
uLong destLen = est_size;
result = uncompress(dest.data(), &destLen, data.data(), data.size());
if (result == Z_OK) {
dest.resize(destLen);
break;
}
est_size *= 2;
} while (result == Z_BUF_ERROR && est_size < 1024 * 1024 * 1024);
if (result != Z_OK) {
cerr << "[!] Decompression failed" << endl;
return 1;
}
ofstream f_out(out_path, ios::binary);
f_out.write((char*)dest.data(), dest.size());
} else {
cerr << "[!] Unknown format flag: " << (int)flag << endl;
return 1;
}
cout << "[+] Restored to " << out_path << endl;
} else {
cerr << "[!] Unknown mode: " << mode << ". Use 'pack' or 'unpack'." << endl;
return 1;
}
} catch (const exception& e) {
cerr << "[!] Error: " << e.what() << endl;
return 1;
}
return 0;
}