-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorruptor.cpp
346 lines (321 loc) · 12.8 KB
/
Corruptor.cpp
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
#include "Corruptor.h"
#include <QTime>
#include <QFileInfo>
#include <QMessageBox>
#include <iostream>
#include <assert.h>
Corruptor::Corruptor(QWidget *parent, Settings *settings, const QString &inFileLocation, const QString &outFileLocation) {
assert(settings);
this->parent = parent;
qsrand(static_cast<unsigned int>(QTime::currentTime().msecsSinceStartOfDay()));
this->settings = settings;
this->inFileLocation = inFileLocation;
this->outFileLocation = outFileLocation;
this->fileSize = 0;
this->isNESROM = false;
}
Corruptor::~Corruptor() { }
int Corruptor::Run() {
//Open the input file for reading
QFile inFile(this->inFileLocation);
if (!inFile.exists()) return 1; //input file does not exist
this->fileSize = inFile.size();
if (this->fileSize > MAX_FILE_SIZE) return 2; //input file is too large
if (!inFile.open(QFile::ReadOnly)) return 3; //unable to read the input file
//Open the output file for writing
QFile outFile(this->outFileLocation);
if (outFile.exists()) {
if (!outFile.remove()) {
return 4; //unable to write the output file
}
}
if (!outFile.open(QFile::ReadWrite)) {
return 4; //unable to write the output file
}
this->isNESROM = this->Use_NES_CPU_Jam_Protection(&inFile);
if (!inFile.seek(0)) return 3; //unable to read the input file
//Read the file in sections of 32MB if it is a large file
if (this->fileSize > MAX_BUFFER_SIZE) {
qint64 remainingBytes = this->fileSize;
while (remainingBytes > 0) {
int bufferSize = 0;
if (remainingBytes > MAX_BUFFER_SIZE) bufferSize = MAX_BUFFER_SIZE;
else bufferSize = static_cast<int>(remainingBytes);
QByteArray buffer(bufferSize, ' ');
qint64 startingPosition = inFile.pos();
if (inFile.read(buffer.data(), bufferSize) != bufferSize) return 3; //unable to read the input file
qint64 endingPosition = inFile.pos();
assert(startingPosition <= endingPosition);
if ((this->settings->startingOffset >= startingPosition && this->settings->startingOffset <= endingPosition)
|| (this->settings->endingOffset >= startingPosition && this->settings->endingOffset <= endingPosition)) {
qint64 readBytes = this->fileSize-remainingBytes;
qint64 startingCorruptionPos = this->settings->startingOffset-readBytes;
if (startingCorruptionPos < 0) startingCorruptionPos = 0;
qint64 endingCorruptionPos = 0;
if (this->settings->endingOffset > (readBytes+MAX_BUFFER_SIZE)) endingCorruptionPos = MAX_BUFFER_SIZE;
else endingCorruptionPos = this->settings->endingOffset-readBytes;
if (endingCorruptionPos < 0) endingCorruptionPos = 0;
assert(startingCorruptionPos <= endingCorruptionPos);
assert(endingCorruptionPos <= this->fileSize);
assert(this->Corrupt_Buffer(&buffer, startingCorruptionPos, endingCorruptionPos));
}
if (outFile.write(buffer, bufferSize) != bufferSize) return 4; //unable to write the output file
remainingBytes -= bufferSize;
}
inFile.close();
} else { //read the entire file into one buffer
QByteArray buffer = inFile.readAll();
inFile.close();
assert(this->Corrupt_Buffer(&buffer, this->settings->startingOffset, this->settings->endingOffset));
if (outFile.write(buffer, this->fileSize) != this->fileSize) return 4; //unable to write the output file
}
outFile.close();
this->Handle_Increment();
return 0;
}
bool Corruptor::Show_Message(int errorCode) {
QFileInfo inFile(this->inFileLocation);
QFileInfo outFile(this->outFileLocation);
if (this->parent != nullptr) {
switch (errorCode) {
case 0: //success
QMessageBox::information(this->parent, "ROM Poison",
"Corruption generated successfully!", "OK");
return true;
case 1: //input file does not exist
QMessageBox::critical(this->parent, "ROM Poison",
inFile.fileName() + " does not exist!", "OK");
return true;
case 2: //input file is too large
QMessageBox::critical(this->parent, "ROM Poison",
inFile.fileName() + " is larger than the maximum supported filesize of 1GB!", "OK");
return true;
case 3: //unable to read the input file
QMessageBox::critical(this->parent, "ROM Poison",
"ROM Poison does not have proper permissions to read " + inFile.fileName() + "!", "OK");
return true;
case 4: //unable to write to the output file
QMessageBox::critical(this->parent, "ROM Poison",
"ROM Poison does not have proper permissions to write " + outFile.fileName() + "!", "OK");
return true;
default:
return false;
}
} else {
switch (errorCode) {
case 0: //success
std::cout << "Corruption generated successfully!" << std::endl;
return true;
case 1: //input file does not exist
std::cerr << inFile.fileName().toStdString() + " does not exist!" << std::endl;
return true;
case 2: //input file is too large
std::cerr << inFile.fileName().toStdString() + " is larger than the maximum supported filesize of 1GB!" << std::endl;
return true;
case 3: //unable to read the input file
std::cerr << "ROM Poison does not have proper permissions to read " + inFile.fileName().toStdString() + "!" << std::endl;
return true;
case 4: //unable to write to the output file
std::cerr << "ROM Poison does not have proper permissions to write " + outFile.fileName().toStdString() + "!" << std::endl;
return true;
default:
return false;
}
}
}
bool Corruptor::Use_NES_CPU_Jam_Protection(QFile *file) {
assert(file);
if (this->fileSize > MAX_BUFFER_SIZE) return false; //way too big to be an NES ROM
if (!file->seek(0)) return false;
QByteArray buffer(4, ' ');
if (file->read(buffer.data(), 4) != 4) return false;
if (buffer.data() == nullptr || buffer.size() != 4) return false;
if (this->Is_NES_ROM(&buffer)) return true;
if (this->Is_Famicom_ROM(&buffer)) return true;
return false;
}
bool Corruptor::Is_NES_ROM(QByteArray *buffer) {
assert(buffer);
assert(buffer->size() == 4);
//Make sure the first 4 bytes of the header are valid: "NES"
if (static_cast<unsigned char>(buffer->at(0)) != 0x4E) return false;
if (static_cast<unsigned char>(buffer->at(1)) != 0x45) return false;
if (static_cast<unsigned char>(buffer->at(2)) != 0x53) return false;
return static_cast<unsigned char>(buffer->at(3)) == 0x1A;
}
bool Corruptor::Is_Famicom_ROM(QByteArray *buffer) {
assert(buffer);
assert(buffer->size() == 4);
//Make sure the first 4 bytes of the header are valid: "FDS"
if (static_cast<unsigned char>(buffer->at(0)) != 0x46) return false;
if (static_cast<unsigned char>(buffer->at(1)) != 0x44) return false;
if (static_cast<unsigned char>(buffer->at(2)) != 0x53) return false;
return static_cast<unsigned char>(buffer->at(3)) == 0x1A;
}
void Corruptor::Fix_Byte(unsigned char &byte) {
if (!this->isNESROM) return;
//These bytes should be avoided
switch (byte) {
default: return;
case 0x00:
case 0x02:
case 0x08:
case 0x12:
case 0x22:
case 0x28:
case 0x32:
case 0x42:
case 0x48:
case 0x52:
case 0x62:
case 0x68:
case 0x72:
case 0x78:
case 0x92:
case 0xB2:
case 0xD2:
case 0xF2:
++byte;
}
}
bool Corruptor::Is_Byte_Protected(QByteArray *buffer, qint64 pos) {
assert(buffer);
if (!this->isNESROM) return false;
if (pos == 0) {
return this->Is_Third_Byte_Protected(static_cast<unsigned char>(buffer->data()[pos]));
} else if (pos == 1) {
if (!this->Is_Third_Byte_Protected(static_cast<unsigned char>(buffer->data()[pos]))) return false;
return this->Is_Second_Byte_Protected(static_cast<unsigned char>(buffer->data()[pos-1]));
} else { //pos >= 2
if (!this->Is_Third_Byte_Protected(static_cast<unsigned char>(buffer->data()[pos]))) return false;
if (!this->Is_Second_Byte_Protected(static_cast<unsigned char>(buffer->data()[pos-1]))) return false;
return this->Is_First_Byte_Protected(static_cast<unsigned char>(buffer->data()[pos-2]));
}
}
bool Corruptor::Is_First_Byte_Protected(unsigned char byte) {
switch (byte) {
case 0x20:
case 0x4C:
case 0x6C:
return true;
default:
return false;
}
}
bool Corruptor::Is_Second_Byte_Protected(unsigned char byte) {
switch (byte) {
case 0x10:
case 0x20:
case 0x30:
case 0x4C:
case 0x50:
case 0x6C:
case 0x70:
case 0x90:
case 0xB0:
case 0xD0:
case 0xF0:
return true;
default:
return false;
}
}
bool Corruptor::Is_Third_Byte_Protected(unsigned char byte) {
switch (byte) {
case 0x00:
case 0x08:
case 0x10:
case 0x20:
case 0x28:
case 0x30:
case 0x40:
case 0x48:
case 0x4C:
case 0x50:
case 0x60:
case 0x68:
case 0x6C:
case 0x70:
case 0x78:
case 0x90:
case 0xB0:
case 0xD0:
case 0xF0:
return true;
default:
return false;
}
}
bool Corruptor::Corrupt_Buffer(QByteArray *buffer, qint64 startingPos, qint64 endingPos) {
assert(buffer);
assert(endingPos <= buffer->size());
//Perform the Corruption
for (qint64 i = startingPos; i < endingPos; i += ((qrand()%((this->settings->incrementMaxNum-this->settings->incrementMinNum)+1))+this->settings->incrementMinNum)) {
if (!Is_Byte_Protected(buffer, i)) {
unsigned char byte = static_cast<unsigned char>(buffer->data()[i]);
if (this->settings->random) {
this->Randomize_Byte(byte);
} else {
bool replaced = false;
if (this->settings->replace) {
replaced = this->Replace_Byte(byte);
}
if (!replaced) {
if (this->settings->add) this->Add_To_Byte(byte);
if (this->settings->shiftLeft) this->Shift_Byte(byte);
}
}
this->Fix_Byte(byte);
buffer->data()[i] = static_cast<char>(byte);
}
}
return true;
}
void Corruptor::Randomize_Byte(unsigned char &byte) {
byte = static_cast<unsigned char>(qrand()%0x100);
}
void Corruptor::Add_To_Byte(unsigned char &byte) {
if (this->settings->addNum < 0) { //decrement
int decrementAmount = 0-this->settings->addNum;
if (byte < decrementAmount) {
while (byte != 0x00) {
--byte;
--decrementAmount;
}
assert(decrementAmount >= 0);
byte = static_cast<unsigned char>(0xFF-decrementAmount);
} else {
byte -= decrementAmount;
}
} else { //increment
byte = static_cast<unsigned char>((byte+this->settings->addNum)%0x100);
}
}
void Corruptor::Shift_Byte(unsigned char &byte) {
if (this->settings->shiftLeftNum < 0) { //shift right
int shiftAmount = 0-this->settings->shiftLeftNum;
byte = byte>>shiftAmount;
} else { //shift left
byte = static_cast<unsigned char>(byte<<this->settings->shiftLeftNum);
}
}
bool Corruptor::Replace_Byte(unsigned char &byte) {
if (byte == this->settings->replaceOldNum) {
byte = static_cast<unsigned char>(this->settings->replaceNewNum);
return true;
}
return false;
}
void Corruptor::Handle_Increment() {
if (this->settings->increment && this->settings->incrementMinNum < this->settings->endingOffset) {
++this->settings->incrementMinNum;
++this->settings->incrementMaxNum;
if (this->settings->incrementMinNum > this->settings->endingOffset) {
this->settings->incrementMinNum = this->settings->endingOffset;
}
if (this->settings->incrementMaxNum > this->settings->endingOffset) {
this->settings->incrementMaxNum = this->settings->endingOffset;
}
assert(this->settings->incrementMinNum <= this->settings->incrementMaxNum);
}
}