#!/bin/sh # The MIT License (MIT) # # Copyright (c) 2026 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # bytedump [options...] [files...] # # Show bytes in hexadecimal notation, with an ASCII panel on the side which # shows 2 lines worth of visible-ASCII symbols, which allows reliable matching # for strings up to 16 symbols long. # # The only option available is to show this help message, using any of # `-h`, `--h`, `-help`, or `--help`, without the quotes. case "$1" in -h|--h|-help|--help) awk '/^# +bytedump /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac [ "$1" = '--' ] && shift command='awk' if { [ -p /dev/stdout ] || [ -t 1 ]; } && [ -e /usr/bin/stdbuf ]; then command='stdbuf -oL awk' fi # show all non-existing files given failed=0 for arg in "$@"; do if [ "${arg}" = "-" ]; then continue fi if [ ! -e "${arg}" ]; then printf "no file named \"%s\"\n" "${arg}" > /dev/stderr failed=1 fi done if [ "${failed}" -gt 0 ]; then exit 2 fi od -v -t x1 "${@:-/dev/stdin}" | ${command} ' BEGIN { s = s " " s = s " !\"#$%&'"'"'()*+,-./0123456789:;<=>?" s = s "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" s = s "`abcdefghijklmnopqrstuvwxyz{|}~ " s = s " " s = s " " s = s " " s = s " " split(s, v, "") for (i = 0; i < 256; i++) vascii[sprintf("%02x", i)] = v[i + 1] } NR > 1 { printf "%s ", offset for (i = 1; i <= n; i++) printf " %s", cur[i] # printf "%*s", 3 * (maxw - n), "" # not working with busybox for (i = n + 1; i <= maxw; i++) printf " " printf " " for (i = 1; i <= n; i++) printf "%s", vascii[cur[i]] for (i = 2; i <= NF; i++) printf "%s", vascii[$i] print "" } { n = 0 offset = $1 for (i = 2; i <= NF; i++) cur[++n] = $i if (maxw < n) maxw = n } '