Embedding data in source code in Linux
Most non-linux programming environments can embed proprietary data into a resource file. Linux has two ways:
- make a hexdump
- use objcopy
Make a hexdump
This is easy, and plenty of utilities exist for it. One of them is xxd utility from vim editor. You can easily create a header file with the construct:
xxd -i input_file output_file
If you're using gtk+ toolkit, you can embed images using:
gdk-pixbuf-csource --raw image.png > image.h
objcopy from binutils
This closely mimics the idea of resource file. It converts a binary file into an object which you just link into your program...
objcopy --input binary --output elf32-i386 \
--binary-architecture i386 foo.txt foo.o
For x64 substitute "--output elf32-i386" with "--output elf64-x86-64"
Inside the object file, you'll have two symbols defined: _binary_foo_txt_start and _binary_foo_txt_end. _foo_txt_ in this case is the name of the file included.
You then use it in your C code in the following manner:
#include <stdio.h>
#include <stdlib.h>
extern char _binary_foo_txt_start;
extern char _binary_foo_txt_end;
main( int argc, char *argv[] ) {
char* p = &_binary_foo_txt_start;
while ( p != &_binary_foo_txt_end ) putchar(*p++);
return EXIT_SUCCESS;
}