Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # if your distro does not provide the bash-masters xtr utility you may download it here:
- # http://pastebin.com/67H6Us9V
- # 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;"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement