Guest User

Untitled

a guest
Jun 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #!/bin/sh -e
  2. # shellcheck disable=SC2039,SC2155
  3. # Copyright 2018 Oliver Smith
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5.  
  6. script="$(basename "$0")"
  7. label="$1"
  8. input="$2"
  9. output="$3"
  10. temp=""
  11.  
  12.  
  13. # $1: argument count
  14. check_arguments() {
  15. if [ "$1" -ne 3 ]; then
  16. echo "usage: $script LABEL INPUT OUTPUT"
  17. exit 1
  18. fi
  19. if ! [ -e "$input" ]; then
  20. echo "File not found: $input"
  21. exit 1
  22. fi
  23. if [ -e "$output" ]; then
  24. echo "Output file already exists: $output"
  25. exit 1
  26. fi
  27. }
  28.  
  29.  
  30. write_identifier() {
  31. printf '\x88\x16\x88\x58' >> "$temp"
  32. }
  33.  
  34.  
  35. # $1: new file size
  36. # $2: padding character code (377 for 0xFF, 000 for 0x00)
  37. write_padding() {
  38. # Missing byte count
  39. local old="$(stat -c%s "$temp")"
  40. local count="$(($1 - old))"
  41.  
  42. # Write to file
  43. # This uses printf to generate $count white spaces, then replaces them with
  44. # the desired character. shellcheck disable=SC2059
  45. printf "%0${count}s" "" | tr " " "\\$2" >> "$temp"
  46. }
  47.  
  48.  
  49. write_size() {
  50. # File size as hexadecimal number
  51. local int="$(stat -c%s "$input")"
  52. local hex="$(printf '%x' "$int")"
  53.  
  54. # Format string with reversed byte order
  55. local split="$(echo "$hex" | sed 's/.\{2\}/& /g')"
  56. local formatstring=""
  57. for byte in $split; do
  58. formatstring="\\x$byte$formatstring"
  59. done
  60.  
  61. # shellcheck disable=SC2059
  62. printf "$formatstring" >> "$temp"
  63. write_padding "8" "000"
  64. }
  65.  
  66.  
  67. write_label() {
  68. printf "%s" "$label" >> "$temp"
  69. write_padding "40" "000"
  70. }
  71.  
  72.  
  73. write_header() {
  74. write_identifier
  75. write_size
  76. write_label
  77. write_padding "512" "377"
  78. }
  79.  
  80.  
  81. # $1: argument count
  82. main() {
  83. check_arguments "$1"
  84.  
  85. temp="$(mktemp -t mtkheaderXXXXXX)"
  86. write_header
  87.  
  88. cat "$temp" "$input" > "$output"
  89. rm "$temp"
  90. }
  91.  
  92.  
  93. main "$#"
Add Comment
Please, Sign In to add comment