Jump to content


Photo

Embedding Binary Data


  • Please log in to reply
5 replies to this topic

#1 2DLuis

2DLuis

    Graphic Designer

  • GMC Member
  • 2493 posts
  • Version:GM8

Posted 30 July 2012 - 11:16 PM

*
I am pretty sure this topic is allowed in this forum, if not mods/admins please close it
*

Currently, I am programming a C++ runner for learning purposes and would like to use my own language with it. I know how to write the Abstract Syntax Tree, Lexer and syntax analyzer.

However, my question is when you compile a VS Studio .exe, does it compile in binary?
Assuming it does, I append my code to the end of the .exe with fstreams, and the runner still executes.

However, when I attempt to read the appended data from within the runner, I sometimes get a gargled mess.
Is this the best way to code a runner based language? Aka an interpreted language?

I will post my attempted code shortly

Thanks.
  • 0

#2 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 30 July 2012 - 11:39 PM

You likely miscalculated where your data begins... Or your math is correct but the variable type(s) used to do the math to find the beginning of the data fails. Say you wrote it in c++ and used short... the function will work until the file size is too large to be held in the short.

and to append you data to the exe...

console
copy program.exe+data.dat

should result in proram.exe to have data.dat appended to the end of it.
  • 0

#3 Experimenator

Experimenator

    Java Developer

  • GMC Member
  • 376 posts
  • Version:GM8

Posted 30 July 2012 - 11:43 PM

Uhm, I'm not sure if I understand (anything).
C++ is a compiled not an interpreted language afaik,
and it compiles as binary I suppose (...?)

I append my code to the end of the .exe with fstreams, and the runner still executes.

?

Edited by Experimenator, 30 July 2012 - 11:43 PM.

  • 0

#4 2DLuis

2DLuis

    Graphic Designer

  • GMC Member
  • 2493 posts
  • Version:GM8

Posted 31 July 2012 - 12:36 AM

You likely miscalculated where your data begins... Or your math is correct but the variable type(s) used to do the math to find the beginning of the data fails. Say you wrote it in c++ and used short... the function will work until the file size is too large to be held in the short.

and to append you data to the exe...

console
copy program.exe+data.dat

should result in proram.exe to have data.dat appended to the end of it.


Right.

When I append data, I do the following:
fstream outFile("program.exe", ios::binary | ios::app | ios::out);
char* data = "sample string";
outFile << data;
outFile << '\0';
outFile.close();

Note: I could use the .write() function to allocate memory, but see note below:
I use a null character to null terminate the string, and I believe I use the appropriate flags to open the binary file.

As you can see, I directly append to the exe file here, not creating a new one concatenating both data types, however it should yield the same result..

Now I know I should be writing a size in bytes of the amount of data to expect so I can read the appended data appropriately, however first I'd like to get the simple environment up and running.

Now, when I read data, I do the following:
ifstream ifs;
ifs.open ("program.exe", ios::binary | ios::in);
char buff[13];
ifs.seekg (0, std::ios::beg);

while(!ifs.eof()) {
	ifs.read(buff, 13);
}

std::cout << buff << std::endl;

system("PAUSE");

Note I have defined the buffer char array as a 13, because I know the data to expect for now.
Is there any fundamental errors in that logic?

Thanks again.

Uhm, I'm not sure if I understand (anything).
C++ is a compiled not an interpreted language afaik,
and it compiles as binary I suppose (...?)

I append my code to the end of the .exe with fstreams, and the runner still executes.

?

Yes, C++ is a compiled language, however I am using it to create a runner to interpret a homemade language upon execution which is appended through binary functions to the original runner .exe

---
I am well aware that this isn't a C++ forum however wanted to show what I have attempted in C++ to further add to the discussion

Edited by 2DLuis, 31 July 2012 - 12:41 AM.

  • 0

#5 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14446 posts
  • Version:GM:Studio

Posted 31 July 2012 - 01:18 AM

Well, you are reading 13 bytes to the end. it would be pure luck the last buff read would be the exact 13 characters of your string...
from
"ample string\0" //dead on on last read, you should have said 14, not 13
to
"ple string\0"
to
"\0"

Now you can seek to the end of the file first, then seek from current position -13... actually -14 to include that last null, then read in buff, size 14, again, you need to read the \0 in your buff for std::cout << buff to work else if the \0 is missing, << will stop at the first \0 is finds in the memory right after where buff begins... "sample string%$^%$%^$^"

This is how I would do it

get the file size of the exe file
get the file size of the data file
open the exe for append binary
open the data file for reading binary
append the data file to the exe (while not eof data, read byte, append the exe)
append the exe file size to the exe (in a DWORD format (unsigned int))
append the data file size to the exe (in a DWORD format (unsigned int))
close data file
close exe file

to read
open the exe (self)
seek eof
seek relative -sizeof(DWORD)*2
exesize = read dword
datasize = read dword

seek to beginning of file
seek to exesize (where the data starts)

or read absolute exesize I think can replace those 2 calls

repeat(datasize) data+=read byte
close exe file
  • 0

#6 2DLuis

2DLuis

    Graphic Designer

  • GMC Member
  • 2493 posts
  • Version:GM8

Posted 31 July 2012 - 01:41 AM

Yes,

Currently I'm appending a custom struct (footer) which houses the original file sizes and the appended data filesize.
Footer is written last and then I seek() it to see where to start reading null terminated strings.

Thank you very much for your help, will edit this post once I finish the code
Also, just found this page: http://edn.embarcade...m/article/27979

It's for Pascal however it is a great tutorial for this and states something very similar to what you mentioned in your post.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users