#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>

using namespace std;

int
main(int argc, char ** argv, char ** envp)
{
   if(argc != 3) {
      cerr << "usage: " << argv[0] << " <exe> <hash>" << endl;
      exit(1); 
   }
   ifstream fin(argv[2]);
   string hash;
   if(fin) {
      fin >> hash;
      fin.close();
   }
   fstream bin(argv[1], ios::binary | ios::in | ios::out);
   if(bin) {
      bin.seekg(0L, ios::end);
      size_t size = (size_t)bin.tellg();
      char * file = new char [size];
      bin.seekg(0L, ios::beg);
      bin.read(file, size); 
      size_t i = 0; 
      for(char * ptr = file; i < size; i++, ptr++) {
         if(memcmp(ptr, "$start$", 7) == 0) {
            ptr += 7;
            for(size_t j = 0; j < hash.size(); j++) {
               ptr[j] = hash[j];
            }
            break;
         }
      }
      bin.seekg(0L, ios::beg);
      bin.write(file, size);
      bin.close();
      delete [] file;    
   }
   return 0;
}
