Now it sleeps for 3 seconds between each new bacteria and the mutations are more random

master
mickenordin 9 years ago
parent c1dd12c407
commit ce2b090db0

@ -1,8 +1,12 @@
#include <fstream>
#include <cstdlib>
#include <bitset>
#include <climits>
#include <chrono> // std::chrono::seconds
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <thread> // std::this_thread::sleep_for
using namespace std;
@ -19,20 +23,22 @@ int main(int argc,char** argv) {
// Read my own program
ifstream ifile;
ifile.open(argv[0], ios::binary) ;
//Get some randomness
srand( time(NULL) );
int filesize = file_size(argv[0]);
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';;
cout << "Creating new bacteria called: "<< filename << " with size:" << filesize << '\n';;
ofile.open(filename, ios::binary | ios::out);
if ( ifile.fail() or ofile.fail() ) {
return 1; //error
}
// We count our bytes
int byte_counter = 0;
// Loop through binary file byte by byte
while (!ifile.eof()) {
unsigned char c;
@ -41,12 +47,28 @@ int main(int argc,char** argv) {
if (ifile.fail()) {
break;
}
if (byte_counter == rand_byte and i == 1) {
if (byte_counter == rand_byte ) {
int insert = rand() % 6;
if (insert == 0) {
char byte = (char) rand() % 256;
bitset<8> binbyte(byte);
cout << "Inserting byte with value: " << binbyte << " after byte: " << byte_counter << '\n';
ofile.put(c);
ofile.put(byte);
} else if (insert == 1) {
int bit = rand() % CHAR_BIT;
cout << "Mutating bit: " << bit << " of byte: " << byte_counter << '\n';
c ^= (1u << bit);
ofile.put(c);
} else if (insert == 2){
cout << "Witholding byte: " << byte_counter << '\n';
} else {
cout << "Just letting this one pass."<< '\n';
ofile.put(c);
}
} else {
ofile.put(c);
}
byte_counter++;
}
@ -54,7 +76,8 @@ int main(int argc,char** argv) {
string executable = "/bin/chmod +x " + filename;
string run = filename;
system(executable.c_str());
//system(run.c_str());
this_thread::sleep_for (chrono::seconds(3));
system(run.c_str()); // This is the dangerous part ^^
}
return 0;
}

Loading…
Cancel
Save