/*

RUN A BINRAY FILE OF STATIC CODE...

nasm -f bin hello.asm -o hello.bin

./bytes hello.bin

Author: Matthew W. Coan
Date: Sat Aug 31 14:51:58 EDT 2013

*/
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <cstdlib>

namespace byte_tools {

using namespace std;

class ByteConverter {
   string file_name;

public:
   ByteConverter(const string & file_name) {
      this->file_name = file_name;
   }
   ~ByteConverter() {
   }

   void run() {
      ifstream file(file_name.c_str(), ios::in | ios::binary);
      if(file) {
         list< unsigned char > buffer;
         unsigned char ch;
         cout << "---BEGIN---" << endl;
         cout << "CODE = " << flush;
         ch = file.get();
         while(file) {
            cout << (unsigned int)ch << "," << flush;
            buffer.push_back(ch);
            ch = file.get();
         }
         cout << endl;
         cout << "---END---" << endl;
         file.close();
         unsigned char * code = new unsigned char [ buffer.size() ];
         copy(buffer.begin(), buffer.end(), code);
         buffer.clear();
         //typedef int (*pf_t)();
         //pf_t pf = (pf_t)code;
         //int rc = pf();
         //cout << "rc = " << rc << endl;
         delete [] code;
      }
   }
};

}

using namespace byte_tools;
using namespace std;

int
main(int argc, char ** argv)
{
   if(argc != 2) {
      cerr << "usage: " << argv[0] << " <binfile>" << endl;
      exit(1);
   }
   try {
      ByteConverter bc(argv[1]);
      bc.run();
   }
   catch(...) {
      cerr << "unknown exception..." << endl;
      exit(1);
   }
   return 0;
}
