File: nhex.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 # nhex [options...] [files...]
  27 #
  28 # Nice HEXadecimals is a byte-viewer which shows bytes as base-16 values,
  29 # using various ANSI styles to color-code output.
  30 #
  31 # Output lines end with a panel showing all ASCII sequences detected along.
  32 #
  33 # Options, where leading double-dashes are also allowed:
  34 #
  35 #     -h, -help    show this help message
  36 
  37 
  38 case "$1" in
  39     -h|--h|-help|--help)
  40         awk '/^# +nhex /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  41         exit 0
  42     ;;
  43 esac
  44 
  45 [ "$1" = '--' ] && shift
  46 
  47 dashes=0
  48 for name in "${@:--}"; do
  49     if [ "${name}" = "-" ]; then
  50         dashes="$((dashes + 1))"
  51     fi
  52     if [ "${dashes}" -gt 1 ]; then
  53         printf "can't use dash/stdin multiple times\n" >&2
  54         exit 1
  55     fi
  56 done
  57 
  58 first=1
  59 
  60 for name in "${@:--}"; do
  61     od -v -A none -t u1 --width=16 "${name}" \
  62     | awk '
  63         NR > 1 {
  64             for (i = 1; i <= pnf; i++) printf " %s", p[i]
  65             for (i = 1; i <= pnf; i++) printf " %s", p[i]
  66             for (i = 1; i <= NF; i++) printf " %s", $i
  67             print ""
  68         }
  69 
  70         {
  71             pnf = NF
  72             for (i = 1; i <= NF; i++) p[i] = $i
  73         }
  74 
  75         END {
  76             if (NR > 0) {
  77                 for (i = 1; i <= pnf; i++) printf " %s", p[i]
  78                 for (i = 1; i <= pnf; i++) printf " %s", p[i]
  79                 if (pnf > 0) print ""
  80             }
  81         }' \
  82     | awk -v first="${first}" -v name="${name}" '
  83     BEGIN {
  84         vis1 = " !\"#$%& ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  85         vis2 = "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  86         split(vis1 vis2, visible, "")
  87         l = length(visible)
  88         for (i = 1; i < l; i++) a[i + 32 - 1] = visible[i]
  89         a[39] = "'"'"'"
  90         a[126] = "~"
  91 
  92         spaces = "                                "
  93         spaces = spaces spaces spaces spaces
  94         if (name == "-") name = "<stdin>"
  95         if (!first) printf "\n\n"
  96         printf "• %s\n\n", name
  97     }
  98 
  99     function show_offset(n, s, len, lead, alt) {
 100         s = sprintf("%8d", n)
 101 
 102         while (match(s, /[0-9]{4,}/)) {
 103             len = RLENGTH
 104             lead = len % 3
 105             alt = lead > 0
 106 
 107             printf "%s", substr(s, 1, RSTART + lead - 1)
 108             s = substr(s, RSTART + lead)
 109             len -= lead
 110 
 111             while (len > 0) {
 112                 if (alt == 1) {
 113                     printf "\x1b[38;5;248m%s\x1b[0m", substr(s, 1, 3)
 114                 } else {
 115                     printf "%s", substr(s, 1, 3)
 116                 }
 117 
 118                 alt = 1 - alt
 119                 s = substr(s, 4)
 120                 len -= 3
 121             }
 122         }
 123 
 124         printf "%s", s
 125     }
 126 
 127     function show_byte(n) {
 128         if (n == 0) {
 129             printf "\x1b[38;5;111m%02x ", n
 130             return
 131         }
 132 
 133         if (n == 255) {
 134             printf "\x1b[38;5;209m%x ", n
 135             return
 136         }
 137 
 138         if (32 <= n && n <= 126) {
 139             printf "\x1b[38;5;72m%x\x1b[0m\x1b[48;5;254m%s", n, a[n]
 140             return
 141         }
 142 
 143         printf "\x1b[38;5;246m%02x ", n
 144     }
 145 
 146     NR % 5 == 1 && NR > 1 {
 147         printf "%8s  ", ""
 148         printf "           ·           ·           ·           ·\n"
 149     }
 150 
 151     {
 152         # printf "%8d  \x1b[48;5;254m", offset
 153         show_offset(offset)
 154         printf "  \x1b[48;5;254m"
 155 
 156         for (i = 1; i <= NF; i++) {
 157             if (i > 16) break
 158             show_byte($i)
 159         }
 160 
 161         printf "\x1b[0m"
 162 
 163         runs = 0
 164         asciirun = 0
 165         short = (NF < 2 * 16)
 166         # pad = short ? sprintf("%*s", 3 * (NF - 16) + 2, "") : "  "
 167         padfmt = sprintf("%%%ds", 3 * (NF - 16) + 2, "")
 168         pad = short ? sprintf(padfmt, "") : "  "
 169 
 170         for (i = short ? NF / 2 : 16 + 1; i <= NF; i++) {
 171             n = $i
 172 
 173             if (32 < n && n <= 126) {
 174                 if (!asciirun) {
 175                     asciirun = 1
 176                     runs++
 177                     printf (runs > 1) ? " " : pad
 178                 }
 179 
 180                 printf "%s", a[n]
 181                 continue
 182             }
 183 
 184             asciirun = 0
 185         }
 186 
 187         print ""
 188 
 189         offset += 16
 190     }'
 191 
 192     first=0
 193 done