My tested bacteria
This commit is contained in:
parent
d1de32c0ea
commit
c1dd12c407
1 changed files with 42 additions and 43 deletions
85
bacteria.cpp
85
bacteria.cpp
|
@ -7,55 +7,54 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int file_size(const char *filename){
|
int file_size(const char *filename){
|
||||||
ifstream ifile;
|
ifstream ifile;
|
||||||
ifile.open(filename, ios_base::binary | ios_base::ate);
|
ifile.open(filename, ios_base::binary | ios_base::ate);
|
||||||
int size = ifile.tellg();
|
int size = ifile.tellg();
|
||||||
ifile.close();
|
ifile.close();
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc,char** argv) {
|
int main(int argc,char** argv) {
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
// Read my own program
|
// Read my own program
|
||||||
ifstream ifile;
|
ifstream ifile;
|
||||||
ifile.open(argv[0], ios::binary) ;
|
ifile.open(argv[0], ios::binary) ;
|
||||||
int filesize = file_size(argv[0]);
|
srand( time(NULL) );
|
||||||
srand( time(NULL) );
|
int filesize = file_size(argv[0]);
|
||||||
int rand_byte = rand() % ( filesize + 1 );
|
int rand_byte = rand() % ( filesize + 1 );
|
||||||
|
// Devide
|
||||||
|
ofstream ofile;
|
||||||
|
string filename = tempnam(".", "bact-");
|
||||||
|
cout << "Creating new bacteria called: "<< filename << " with size:" << file_size(argv[0]) << '\n';;
|
||||||
|
ofile.open(filename, ios::binary | ios::out);
|
||||||
|
|
||||||
// Devide
|
if ( ifile.fail() or ofile.fail() ) {
|
||||||
ofstream ofile;
|
return 1; //error
|
||||||
string filename = tempnam(".", "bact-");
|
}
|
||||||
cout << "Creating new bacteria called: "<< filename << " with size:" << file_size(argv[0]) << '\n';;
|
|
||||||
ofile.open(filename, ios::binary | ios::out);
|
|
||||||
|
|
||||||
if ( ifile.fail() or ofile.fail() ) {
|
int byte_counter = 0;
|
||||||
return 1; //error
|
while (!ifile.eof()) {
|
||||||
}
|
unsigned char c;
|
||||||
|
|
||||||
int byte_counter = 0;
|
c = ifile.get();
|
||||||
while (!ifile.eof()) {
|
|
||||||
unsigned char c;
|
|
||||||
|
|
||||||
c = ifile.get();
|
if (ifile.fail()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (byte_counter == rand_byte and i == 1) {
|
||||||
|
int bit = rand() % CHAR_BIT;
|
||||||
|
cout << "Mutating bit: " << bit << " of byte: " << byte_counter << '\n';
|
||||||
|
c ^= (1u << bit);
|
||||||
|
}
|
||||||
|
ofile.put(c);
|
||||||
|
byte_counter++;
|
||||||
|
}
|
||||||
|
|
||||||
if (ifile.fail()) {
|
ofile.close();
|
||||||
break;
|
string executable = "/bin/chmod +x " + filename;
|
||||||
}
|
string run = filename;
|
||||||
if (byte_counter == rand_byte and i == 1) {
|
system(executable.c_str());
|
||||||
int bit = rand() % CHAR_BIT;
|
//system(run.c_str());
|
||||||
cout << "Mutating bit: " << bit << " of byte: " << byte_counter << '\n';
|
}
|
||||||
c ^= (1u << bit);
|
return 0;
|
||||||
}
|
|
||||||
ofile.put(c);
|
|
||||||
byte_counter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
ofile.close();
|
|
||||||
string executable = "/bin/chmod +x " + filename;
|
|
||||||
string run = filename;
|
|
||||||
system(executable.c_str());
|
|
||||||
//system(run.c_str());
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue