Advertisement
bash-masters

bin2inc

Dec 9th, 2012
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.44 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # if your distro does not provide the bash-masters xtr utility you may download it here:
  4. # http://pastebin.com/67H6Us9V
  5.  
  6. # This is a script we call bin2inc. The first parameter is the name of the resulting char[]
  7. # variable. The second parameter is the name of the file. The output is a C include file with
  8. # the target file content encoded (in lowercase hex) as the variable name given.
  9.  
  10. # The char array is zero terminated, and the length of the data is stored in $1_length as
  11. # an unsigned long integer.
  12.  
  13. hash sed fold xtr || {
  14.     printf "%s\n" "bin2inc: error: missing program dependencies";
  15.     exit 1; # if you don't have xtr or fold, get them.
  16. } >&2;
  17.  
  18. fileSize () { [ -e "$1" ]  && { set -- `ls -l "$1"`; echo $5; } }
  19. fullPath() { echo "`readlink -f "$(dirname "$1")"`/`basename "$1"`"; }
  20.  
  21. [[ -e "$2" ]] || {
  22.     printf "%s\n" "bin2inc: error: argument 2 is not a valid file";
  23.     exit 2; # c'mon man file must exist
  24. } >&2;
  25.  
  26. fileSize=`fileSize "$2"`;
  27.  
  28. (( fileSize )) || {
  29.     printf "%s\n" "bin2inc: error: \`$2' has no content to encode";
  30.     exit 3; # no data
  31. } >&2;
  32.  
  33. echo "/* auto-generated binary include: \"$2\" encoded by bin2inc (sh)
  34.  
  35. resource : file://$(fullPath "$2")
  36. developer: $USER
  37. machine  : $HOSTNAME
  38. content-length: $fileSize
  39.  
  40. */
  41.  
  42. unsigned char $1[] = {
  43.  
  44. $( { ./xtr -fhex -p 0x -s ', ' < "$2"; echo -n 0x00; } | fold -sw 87 | sed  's/^/    /')
  45.  
  46. };
  47.  
  48. unsigned long int ${1}_length = $fileSize;"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement