Hello, im a noob when it comes to coding.
I am trying to write a file corruptor that edit bytes of a file by applying math to the bytes . I want it to have these modes:
1. Corrupt every x amount of bytes of a file. And be able to spcify start and stop of rom. then start the rom.
For example. Start at byte 30000 and end at end of file. Corrupt every 3000th byte by adding(or any other operator) 20 to it.
2. Real time corruption: corrupt a random number of bytes at a random byte location. and launch the rom to play in an emulator
Example
While (program is running)
{
Corrupt x number of bytes of the rom in memory at a random location by using a operator on it.
}
so far i have figured out how to part the arguments from the cli.
The way i think it would work that that i make a copy of the file and then edit copy by moving the cursor with seekp() and editing the byte.
would this be the best? i'm worried that might not be the fastest, its fine for small nes roms but if i plan on doing this for ps1 games it can be close to a gig of data.
I am stuck at the part where i need to get the byte at the cursor and apply some math. Then put it back where the cursor is.
Also i am worried about being able to put the byte back if it is more than a byte. For example if the byte at the cursor was 2 and and i added 10 to it then if i tried to put the number in would it work?
my code so far
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
|
#include <iostream>
#include <unistd.h>
//#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <fstream>
#include <string>
using namespace std;
void copy_file( string&, string&);
//using namespace boost::filesystem
// -s start byte
// -e end byte
// -i input file
// -o output file
// -n corruption is done every nth byte
// -a amount of corruption
// -m corruption method
// -l launch
//
//
//
int main(int argc, char *argv[])
{
int start = 0;
int end = 0;
int nthByte = 0;
int corruptionAmount = 0;
//int c = 0;
string input= "";
string output= "";
char byte = ' ';
string method = "";
string launch = "";
//cout << argv[2] << endl;
for (int i = 1; (i +1) < argc; i++)
{
if (strcmp(argv[i], "-s") == 0)
{
start = atoi(argv[i + 1]);
}
else if (strcmp(argv[i], "-e") == 0)
{
end = atoi(argv[i + 1]);
}
else if (strcmp(argv[i], "-i") == 0)
{
input = argv[i + 1];
}
else if (strcmp(argv[i], "-o") == 0)
{
output = argv[i + 1];
}
else if (strcmp(argv[i], "-n") == 0)
{
nthByte = atoi(argv[i + 1]);
}
else if (strcmp(argv[i], "-a") == 0)
{
corruptionAmount = atoi(argv[i + 1]);
}
else if (strcmp(argv[i], "-m") == 0)
{
method = argv[i + 1];
}
else if (strcmp(argv[i], "-l") == 0)
{
launch = argv[i + 1];
}
copy_file(input, output);
}
//cout << start << endl << end << endl << nthByte << endl << corruptionAmount << endl << input << endl << output << endl << method << endl << launch;
copy_file(input, output);
ofstream out;
out.open (output);
out.seekp (nthByte);
}
void copy_file( string& srce_file, string& dest_file )
{
std::ifstream srce( srce_file, std::ios::binary ) ;
std::ofstream dest( dest_file, std::ios::binary ) ;
dest << srce.rdbuf() ;
}
|
Any help or suggestions are very welcome, thank you for your time.