Here is a script called bin2inc. It converts binary files to a text representation for inclusion
into C programs as a zero terminated byte array of known length.
$1 is the name of the C variable (char[]), $2 is the name of the source file (binary data). Output is a C include file of the input file encoded as a C char[], with a trailing NULL.
Array size (file length) is stored in $1_length as an unsigned integer.
#!/bin/bash # if your distro does not provide the bash-masters xtr utility you may download it here: # https://royalhack.xyz/upload/showthread.php/112799-C-XTR-C?p=254612#post254612 # This is a script we call bin2inc. The first parameter is the name of the resulting char[] # variable. The second parameter is the name of the file. The output is a C include file with # the target file content encoded (in lowercase hex) as the variable name given. # The char array is zero terminated, and the length of the data is stored in $1_length as # an unsigned long integer. hash sed fold xtr || { printf "%s\n" "bin2inc: error: missing program dependencies"; exit 1; # if you don't have xtr or fold, get them. } >&2; fileSize () { [ -e "$1" ] && { set -- `ls -l "$1"`; echo $5; } } fullPath() { echo "`readlink -f "$(dirname "$1")"`/`basename "$1"`"; } [[ -e "$2" ]] || { printf "%s\n" "bin2inc: error: argument 2 is not a valid file"; exit 2; # c'mon man file must exist } >&2; fileSize=`fileSize "$2"`; (( fileSize )) || { printf "%s\n" "bin2inc: error: \`$2' has no content to encode"; exit 3; # no data } >&2; echo "/* auto-generated binary include: \"$2\" encoded by bin2inc (sh) resource : file://$(fullPath "$2") developer: $USER machine : $HOSTNAME content-length: $fileSize */ unsigned char $1[] = { $( { ./xtr -fhex -p 0x -s ', ' < "$2"; echo -n 0x00; } | fold -sw 87 | sed 's/^/ /') }; unsigned long int ${1}_length = $fileSize;"
Once you have installed bin2inc, and xtr, you can wrap some data into a binary.
./bin2inc bin2inc ./bin2inc > ./bin2inc.inc; # file test.c cat > "test.c" <<EOF #include "bin2inc.inc"; int main() { puts(bin2inc); } EOF
cc test.c -o test
./test
So how do we run the script? With bash of course.
Lets execute the script and pass it some arguments. Bootstrapping a bootstrapped bootstrapper.
bash <(./test) testData ./test
/* auto-generated binary include: "bin2inc" encoded by bin2inc (sh) resource : file:///home/triston/projects/bash-masters/application/xtr-rc1/bin2inc developer: triston machine : AOD257 content-length: 1430 */ unsigned char bin2inc[] = { 0x23, 0x21, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x62, 0x61, 0x73, 0x68, 0x0a, 0x0a, 0x23, 0x20, 0x69, 0x66, 0x20, 0x79, 0x6f, 0x75, 0x72, 0x20, 0x64, 0x69, 0x73, 0x74, 0x72, 0x6f, 0x20, 0x64, 0x6f, 0x65, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x70, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x61, 0x73, 0x68, 0x2d, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x73, 0x20, 0x78, 0x74, 0x72, 0x20, 0x75, 0x74, 0x69, 0x6c, 0x69, 0x74, 0x79, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x64, 0x6f, 0x77, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x20, 0x69, 0x74, 0x20, 0x68, 0x65, 0x72, 0x65, 0x3a, 0x0a, 0x23, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x70, 0x61, 0x73, 0x74, 0x65, 0x62, 0x69, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x36, 0x37, 0x48, 0x36, 0x55, 0x73, 0x39, 0x56, 0x0a, 0x0a, 0x23, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x20, 0x77, 0x65, 0x20, 0x63, 0x61, 0x6c, 0x6c, 0x20, 0x62, 0x69, 0x6e, 0x32, 0x69, 0x6e, 0x63, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x63, 0x68, 0x61, 0x72, 0x5b, 0x5d, 0x0a, 0x23, 0x20, 0x76, 0x61, 0x72, 0x69, 0x61, 0x62, 0x6c, 0x65, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2e, 0x20, 0x54, 0x68, 0x65, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x43, 0x20, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x77, 0x69, 0x74, 0x68, 0x0a, 0x23, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x20, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x64, 0x20, 0x28, 0x69, 0x6e, 0x20, 0x6c, 0x6f, 0x77, 0x65, 0x72, 0x63, 0x61, 0x73, 0x65, 0x20, 0x68, 0x65, 0x78, 0x29, 0x20, 0x61, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x76, 0x61, 0x72, 0x69, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x67, 0x69, 0x76, 0x65, 0x6e, 0x2e, 0x20, 0x0a, 0x0a, 0x23, 0x20, 0x54, 0x68, 0x65, 0x20, 0x63, 0x68, 0x61, 0x72, 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x69, 0x73, 0x20, 0x7a, 0x65, 0x72, 0x6f, 0x20, 0x74, 0x65, 0x72, 0x6d, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x64, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0x20, 0x69, 0x73, 0x20, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x64, 0x20, 0x69, 0x6e, 0x20, 0x24, 0x31, 0x5f, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x20, 0x61, 0x73, 0x0a, 0x23, 0x20, 0x61, 0x6e, 0x20, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x2e, 0x0a, 0x0a, 0x68, 0x61, 0x73, 0x68, 0x20, 0x73, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x6c, 0x64, 0x20, 0x78, 0x74, 0x72, 0x20, 0x7c, 0x7c, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x25, 0x73, 0x5c, 0x6e, 0x22, 0x20, 0x22, 0x62, 0x69, 0x6e, 0x32, 0x69, 0x6e, 0x63, 0x3a, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x3a, 0x20, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x64, 0x65, 0x70, 0x65, 0x6e, 0x64, 0x65, 0x6e, 0x63, 0x69, 0x65, 0x73, 0x22, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x65, 0x78, 0x69, 0x74, 0x20, 0x31, 0x3b, 0x20, 0x23, 0x20, 0x69, 0x66, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x64, 0x6f, 0x6e, 0x27, 0x74, 0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x78, 0x74, 0x72, 0x20, 0x6f, 0x72, 0x20, 0x66, 0x6f, 0x6c, 0x64, 0x2c, 0x20, 0x67, 0x65, 0x74, 0x20, 0x74, 0x68, 0x65, 0x6d, 0x2e, 0x0a, 0x7d, 0x20, 0x3e, 0x26, 0x32, 0x3b, 0x0a, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x20, 0x28, 0x29, 0x20, 0x7b, 0x20, 0x5b, 0x20, 0x2d, 0x65, 0x20, 0x22, 0x24, 0x31, 0x22, 0x20, 0x5d, 0x20, 0x20, 0x26, 0x26, 0x20, 0x7b, 0x20, 0x73, 0x65, 0x74, 0x20, 0x2d, 0x2d, 0x20, 0x60, 0x6c, 0x73, 0x20, 0x2d, 0x6c, 0x20, 0x22, 0x24, 0x31, 0x22, 0x60, 0x3b, 0x20, 0x65, 0x63, 0x68, 0x6f, 0x20, 0x24, 0x35, 0x3b, 0x20, 0x7d, 0x20, 0x7d, 0x0a, 0x66, 0x75, 0x6c, 0x6c, 0x50, 0x61, 0x74, 0x68, 0x28, 0x29, 0x20, 0x7b, 0x20, 0x65, 0x63, 0x68, 0x6f, 0x20, 0x22, 0x60, 0x72, 0x65, 0x61, 0x64, 0x6c, 0x69, 0x6e, 0x6b, 0x20, 0x2d, 0x66, 0x20, 0x22, 0x24, 0x28, 0x64, 0x69, 0x72, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x22, 0x24, 0x31, 0x22, 0x29, 0x22, 0x60, 0x2f, 0x60, 0x62, 0x61, 0x73, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x22, 0x24, 0x31, 0x22, 0x60, 0x22, 0x3b, 0x20, 0x7d, 0x0a, 0x0a, 0x5b, 0x5b, 0x20, 0x2d, 0x65, 0x20, 0x22, 0x24, 0x32, 0x22, 0x20, 0x5d, 0x5d, 0x20, 0x7c, 0x7c, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x25, 0x73, 0x5c, 0x6e, 0x22, 0x20, 0x22, 0x62, 0x69, 0x6e, 0x32, 0x69, 0x6e, 0x63, 0x3a, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x3a, 0x20, 0x61, 0x72, 0x67, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x32, 0x20, 0x69, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x61, 0x20, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x22, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x65, 0x78, 0x69, 0x74, 0x20, 0x32, 0x3b, 0x20, 0x23, 0x20, 0x63, 0x27, 0x6d, 0x6f, 0x6e, 0x20, 0x6d, 0x61, 0x6e, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x20, 0x65, 0x78, 0x69, 0x73, 0x74, 0x0a, 0x7d, 0x20, 0x3e, 0x26, 0x32, 0x3b, 0x0a, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x3d, 0x60, 0x66, 0x69, 0x6c, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x20, 0x22, 0x24, 0x32, 0x22, 0x60, 0x3b, 0x0a, 0x0a, 0x28, 0x28, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x20, 0x29, 0x29, 0x20, 0x7c, 0x7c, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x25, 0x73, 0x5c, 0x6e, 0x22, 0x20, 0x22, 0x62, 0x69, 0x6e, 0x32, 0x69, 0x6e, 0x63, 0x3a, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x3a, 0x20, 0x5c, 0x60, 0x24, 0x32, 0x27, 0x20, 0x68, 0x61, 0x73, 0x20, 0x6e, 0x6f, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x6f, 0x20, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x22, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x65, 0x78, 0x69, 0x74, 0x20, 0x33, 0x3b, 0x20, 0x23, 0x20, 0x6e, 0x6f, 0x20, 0x64, 0x61, 0x74, 0x61, 0x0a, 0x7d, 0x20, 0x3e, 0x26, 0x32, 0x3b, 0x0a, 0x0a, 0x65, 0x63, 0x68, 0x6f, 0x20, 0x22, 0x2f, 0x2a, 0x20, 0x61, 0x75, 0x74, 0x6f, 0x2d, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x3a, 0x20, 0x5c, 0x22, 0x24, 0x32, 0x5c, 0x22, 0x20, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x62, 0x69, 0x6e, 0x32, 0x69, 0x6e, 0x63, 0x20, 0x28, 0x73, 0x68, 0x29, 0x0a, 0x0a, 0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x20, 0x3a, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x3a, 0x2f, 0x2f, 0x24, 0x28, 0x66, 0x75, 0x6c, 0x6c, 0x50, 0x61, 0x74, 0x68, 0x20, 0x22, 0x24, 0x32, 0x22, 0x29, 0x0a, 0x64, 0x65, 0x76, 0x65, 0x6c, 0x6f, 0x70, 0x65, 0x72, 0x3a, 0x20, 0x24, 0x55, 0x53, 0x45, 0x52, 0x0a, 0x6d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x20, 0x20, 0x3a, 0x20, 0x24, 0x48, 0x4f, 0x53, 0x54, 0x4e, 0x41, 0x4d, 0x45, 0x0a, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x24, 0x66, 0x69, 0x6c, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x0a, 0x0a, 0x2a, 0x2f, 0x0a, 0x0a, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x63, 0x68, 0x61, 0x72, 0x20, 0x24, 0x31, 0x5b, 0x5d, 0x20, 0x3d, 0x20, 0x7b, 0x0a, 0x0a, 0x24, 0x28, 0x20, 0x7b, 0x20, 0x2e, 0x2f, 0x78, 0x74, 0x72, 0x20, 0x2d, 0x66, 0x68, 0x65, 0x78, 0x20, 0x2d, 0x70, 0x20, 0x30, 0x78, 0x20, 0x2d, 0x73, 0x20, 0x27, 0x2c, 0x20, 0x27, 0x20, 0x3c, 0x20, 0x22, 0x24, 0x32, 0x22, 0x3b, 0x20, 0x65, 0x63, 0x68, 0x6f, 0x20, 0x2d, 0x6e, 0x20, 0x30, 0x78, 0x30, 0x30, 0x3b, 0x20, 0x7d, 0x20, 0x7c, 0x20, 0x66, 0x6f, 0x6c, 0x64, 0x20, 0x2d, 0x73, 0x77, 0x20, 0x38, 0x37, 0x20, 0x7c, 0x20, 0x73, 0x65, 0x64, 0x20, 0x20, 0x27, 0x73, 0x2f, 0x5e, 0x2f, 0x20, 0x20, 0x20, 0x20, 0x2f, 0x27, 0x29, 0x0a, 0x0a, 0x7d, 0x3b, 0x0a, 0x0a, 0x75, 0x6e, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x24, 0x7b, 0x31, 0x7d, 0x5f, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x20, 0x3d, 0x20, 0x24, 0x66, 0x69, 0x6c, 0x65, 0x53, 0x69, 0x7a, 0x65, 0x3b, 0x22, 0x0a, 0x00 }; unsigned long int bin2inc_length = 1430;
bash <(./test) testData ./test