-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (65 loc) · 2.2 KB
/
main.cpp
File metadata and controls
78 lines (65 loc) · 2.2 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
#include <array>
#include <cstdint>
#include <fstream>
#include <iostream>
constexpr size_t SECTOR_SIZE = 2352;
static constexpr auto ECMA_130 = []() {
uint16_t sr = 1;
std::array<uint8_t, SECTOR_SIZE> arr{};
for (uint16_t i = 12; i < SECTOR_SIZE; ++i) {
arr[i] = static_cast<uint8_t>(sr);
for (uint8_t b = 0; b < 8; ++b) {
uint16_t c = sr & 1 ^ sr >> 1 & 1;
sr = (c << 15 | sr) >> 1;
}
}
return arr;
}();
int main(int argc, char *argv[]) {
// Print help text if no inputs given
if (argc < 2) {
std::cout << "ECMA-130 Scrambler/Descrambler (Deterous, 2024)"<< std::endl;
std::cout << "Usage: scramble <file_path> [scrambler_offset = 0]" << std::endl;
return 1;
}
// Parse inputs
const char* path = argv[1];
int offset = 0;
if (argc >= 3) {
offset = std::atoi(argv[2]);
if (offset < 0 || offset >= SECTOR_SIZE) {
std::cout << "Error: Invalid scrambler offset. It must be between 0 and " << 2352 - 1 << std::endl;
return 1;
}
}
// Open file for reading and writing
std::fstream file(path, std::ios::in | std::ios::out | std::ios::binary);
if (!file) {
std::cout << "Error: Could not open file " << path << std::endl;
return 1;
}
// Scramble file one sector at a time
char sector[SECTOR_SIZE];
std::streampos pos = 0;
while (!file.eof()) {
file.seekg(pos);
file.read(sector, SECTOR_SIZE);
std::streamsize num_bytes = file.gcount();
if (num_bytes == 0)
break;
while (num_bytes < SECTOR_SIZE && !file.eof()) {
file.read(sector + num_bytes, SECTOR_SIZE - num_bytes);
num_bytes += file.gcount();
}
for (int i = 0; i < SECTOR_SIZE; ++i)
sector[i] ^= ECMA_130[offset + i];
if (file.eof())
file.clear();
file.seekp(pos);
file.write(sector, num_bytes);
pos += num_bytes;
}
file.close();
std::cout << "Scrambled " << pos << " bytes with an offset of " << offset << std::endl;
return 0;
}