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 only option available is to show this help message, using any of 33 # `-h`, `--h`, `-help`, or `--help`, without the quotes. 34 35 36 case "$1" in 37 -h|--h|-help|--help) 38 awk '/^# +bytedump /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 39 exit 0 40 ;; 41 esac 42 43 [ "$1" = '--' ] && shift 44 45 command='awk' 46 if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then 47 command='stdbuf -oL awk' 48 fi 49 50 # show all non-existing files given 51 failed=0 52 for arg in "$@"; do 53 if [ "${arg}" = "-" ]; then 54 continue 55 fi 56 if [ ! -e "${arg}" ]; then 57 printf "no file named \"%s\"\n" "${arg}" > /dev/stderr 58 failed=1 59 fi 60 done 61 62 if [ "${failed}" -gt 0 ]; then 63 exit 2 64 fi 65 66 od -v -t x1 "${@:-/dev/stdin}" | ${command} ' 67 BEGIN { 68 s = s " " 69 s = s " !\"#$%&'"'"'()*+,-./0123456789:;<=>?" 70 s = s "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" 71 s = s "`abcdefghijklmnopqrstuvwxyz{|}~ " 72 s = s " " 73 s = s " " 74 s = s " " 75 s = s " " 76 split(s, v, "") 77 for (i = 0; i < 256; i++) vascii[sprintf("%02x", i)] = v[i + 1] 78 } 79 80 NR > 1 { 81 printf "%s ", offset 82 83 for (i = 1; i <= n; i++) printf " %s", cur[i] 84 # printf "%*s", 3 * (maxw - n), "" # not working with busybox 85 for (i = n + 1; i <= maxw; i++) printf " " 86 87 printf " " 88 for (i = 1; i <= n; i++) printf "%s", vascii[cur[i]] 89 for (i = 2; i <= NF; i++) printf "%s", vascii[$i] 90 print "" 91 } 92 93 { 94 n = 0 95 offset = $1 96 for (i = 2; i <= NF; i++) cur[++n] = $i 97 if (maxw < n) maxw = n 98 } 99 '