File: bytedump.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # bytedump [options...] [files...] 27 # 28 # Show bytes in hexadecimal notation, with an ASCII panel on the side which 29 # shows 2 lines worth of visible-ASCII symbols, which allows reliable matching 30 # for strings up to 16 symbols long. 31 # 32 # The options are, available both in single and double-dash versions 33 # 34 # -h, -help show this help message 35 36 37 case "$1" in 38 -h|--h|-help|--help) 39 awk '/^# +bytedump /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 40 exit 0 41 ;; 42 43 -) ;; 44 45 --) shift ;; 46 47 -*) 48 printf "unsupported option '%s'\n" "$1" >&2 49 exit 1 50 ;; 51 esac 52 53 # show all non-existing files given 54 failed=0 55 for arg in "$@"; do 56 [ "${arg}" = "-" ] && continue 57 [ -e "${arg}" ] && continue 58 printf "no file named \"%s\"\n" "${arg}" >&2 59 failed=1 60 done 61 62 [ "${failed}" -gt 0 ] && exit 2 63 64 flush=0 65 if [ -p /dev/stdout ] || [ -t 1 ]; then 66 flush=1 67 fi 68 69 od -v -t x1 "${@:-/dev/stdin}" | awk -v flush="${flush}" ' 70 BEGIN { 71 s = s " " 72 s = s " !\"#$%&'"'"'()*+,-./0123456789:;<=>?" 73 s = s "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" 74 s = s "`abcdefghijklmnopqrstuvwxyz{|}~ " 75 s = s " " 76 s = s " " 77 s = s " " 78 s = s " " 79 split(s, v, "") 80 for (i = 0; i < 256; i++) vascii[sprintf("%02x", i)] = v[i + 1] 81 } 82 83 NR > 1 { 84 printf "%s ", offset 85 86 for (i = 1; i <= n; i++) printf " %s", cur[i] 87 # printf "%*s", 3 * (maxw - n), "" # not working with busybox 88 for (i = n + 1; i <= maxw; i++) printf " " 89 90 printf " " 91 for (i = 1; i <= n; i++) printf "%s", vascii[cur[i]] 92 for (i = 2; i <= NF; i++) printf "%s", vascii[$i] 93 print "" 94 if (flush) fflush() 95 } 96 97 { 98 n = 0 99 offset = $1 100 for (i = 2; i <= NF; i++) cur[++n] = $i 101 if (maxw < n) maxw = n 102 } 103 '