Now it sleeps for 3 seconds between each new bacteria and the mutations are more random
This commit is contained in:
parent
c1dd12c407
commit
ce2b090db0
1 changed files with 36 additions and 13 deletions
43
bacteria.cpp
43
bacteria.cpp
|
@ -1,8 +1,12 @@
|
||||||
#include <fstream>
|
#include <bitset>
|
||||||
#include <cstdlib>
|
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
#include <chrono> // std::chrono::seconds
|
||||||
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread> // std::this_thread::sleep_for
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -19,20 +23,22 @@ int main(int argc,char** argv) {
|
||||||
// Read my own program
|
// Read my own program
|
||||||
ifstream ifile;
|
ifstream ifile;
|
||||||
ifile.open(argv[0], ios::binary) ;
|
ifile.open(argv[0], ios::binary) ;
|
||||||
|
|
||||||
|
//Get some randomness
|
||||||
srand( time(NULL) );
|
srand( time(NULL) );
|
||||||
int filesize = file_size(argv[0]);
|
int filesize = file_size(argv[0]);
|
||||||
int rand_byte = rand() % ( filesize + 1 );
|
int rand_byte = rand() % ( filesize + 1 );
|
||||||
|
|
||||||
// Devide
|
// Devide
|
||||||
ofstream ofile;
|
ofstream ofile;
|
||||||
string filename = tempnam(".", "bact-");
|
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);
|
ofile.open(filename, ios::binary | ios::out);
|
||||||
|
|
||||||
if ( ifile.fail() or ofile.fail() ) {
|
// We count our bytes
|
||||||
return 1; //error
|
|
||||||
}
|
|
||||||
|
|
||||||
int byte_counter = 0;
|
int byte_counter = 0;
|
||||||
|
|
||||||
|
// Loop through binary file byte by byte
|
||||||
while (!ifile.eof()) {
|
while (!ifile.eof()) {
|
||||||
unsigned char c;
|
unsigned char c;
|
||||||
|
|
||||||
|
@ -41,12 +47,28 @@ int main(int argc,char** argv) {
|
||||||
if (ifile.fail()) {
|
if (ifile.fail()) {
|
||||||
break;
|
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;
|
int bit = rand() % CHAR_BIT;
|
||||||
cout << "Mutating bit: " << bit << " of byte: " << byte_counter << '\n';
|
cout << "Mutating bit: " << bit << " of byte: " << byte_counter << '\n';
|
||||||
c ^= (1u << bit);
|
c ^= (1u << bit);
|
||||||
}
|
|
||||||
ofile.put(c);
|
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++;
|
byte_counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +76,8 @@ int main(int argc,char** argv) {
|
||||||
string executable = "/bin/chmod +x " + filename;
|
string executable = "/bin/chmod +x " + filename;
|
||||||
string run = filename;
|
string run = filename;
|
||||||
system(executable.c_str());
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue