Sometimes there’s a need to embed some little bit of binary data in the executable. The reasons may vary – perhaps it is for a resource you want to exist even if data files are missing, or it could be even something you want to “hide” from some curious wannabe-hackers.
In the past years my typical use case was as follows. I used to make “64 KB intros” (such as this one), and for those I needed to embed a few data files to the executable as object data, so that I could exploit compression of the executable packer without writing a proper compression tool myself (such as UPX).
First I ended up making a simple tool which just reads in a file, byte-by-byte, and outputs something like this:
static unsigned char data[]={13,37,74,47, ... };
That worked just fine and easy – sizeof(data)
can be used to get the length of the data. Writing such a tool took probably something like 5-10 minutes. I think I have actually written that same little utility multiple times in the last decade, sometimes just forgetting where the previously created version were and then just re-creating it.
However, at some point I ended up with a solution where music of the production was an XM module with generated samples. The actual playable module file was huge in size – several megabytes – but the trick was that all the samples were generated at initialization time of the application. My quick hack for this was to make a tool which read in the XM module, replaced the sample data with blank data and stored the tiny bits of metadata for sample generation. And then just embed the huge binary file, so that executable compressor would crunch it down a lot (because it’s mostly empty blank data anyway).
Everything was fine, except for the fact that the compiler&linker I used didn’t like that idea at all, declaring a compile-time handled char array of several megabytes. To workaround this, I took the previously mentioned little utility and added simple RLE compression, which was just enough of a workaround and was trivial to decode as well. Problem solved. (Note that this way of adding music to an intro is now considered ancient, and more modern way is something like this).
Recently I found a version of these two little utilities and decided maybe I can save a few minutes of somebody’s time — or at least my own time if I ever consider writing the same thing over again and happen to remember I have added those to my web page.
I called the first one “binso” for reason I can’t remember anymore. The RLE-version is simply “binsorle”. There’s also testrle.c code for testing and decompressing the data made by binsorle. Note that the code has been written no later than 2001 (possibly even earlier than that), and hasn’t been really “stress tested” to be bug-free. Any bug reports are still welcome, of course.
Download source-code and pre-built win32 executable files:
binso.zip