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 flush=0
  48 if [ -p /dev/stdout ] || [ -t 1 ]; then
  49     flush=1
  50 fi
  51 
  52 dashes=0
  53 for name in "${@:--}"; do
  54     if [ "${name}" = "-" ]; then
  55         dashes="$((dashes + 1))"
  56     fi
  57     if [ "${dashes}" -gt 1 ]; then
  58         printf "can't use dash/stdin multiple times\n" >&2
  59         exit 1
  60     fi
  61 done
  62 
  63 first=1
  64 
  65 for name in "${@:--}"; do
  66     od -v -A none -t u1 --width=16 "${name}" \
  67     | awk -v flush="${flush}" '
  68         NR > 1 {
  69             for (i = 1; i <= pnf; i++) printf " %s", p[i]
  70             for (i = 1; i <= pnf; i++) printf " %s", p[i]
  71             for (i = 1; i <= NF; i++) printf " %s", $i
  72             print ""
  73             if (flush) fflush()
  74         }
  75 
  76         {
  77             pnf = NF
  78             for (i = 1; i <= NF; i++) p[i] = $i
  79         }
  80 
  81         END {
  82             if (NR > 0) {
  83                 for (i = 1; i <= pnf; i++) printf " %s", p[i]
  84                 for (i = 1; i <= pnf; i++) printf " %s", p[i]
  85                 if (pnf > 0) print ""
  86             }
  87         }' \
  88     | awk -v flush="${flush}" -v first="${first}" -v name="${name}" '
  89     BEGIN {
  90         vis1 = " !\"#$%& ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  91         vis2 = "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  92         split(vis1 vis2, visible, "")
  93         l = length(visible)
  94         for (i = 1; i < l; i++) a[i + 32 - 1] = visible[i]
  95         a[39] = "'"'"'"
  96         a[126] = "~"
  97 
  98         if (name == "-") name = "<stdin>"
  99         if (!first) printf "\n\n"
 100         printf "• %s\n\n", name; fflush()
 101     }
 102 
 103     function show_offset(n, s, len, lead, alt) {
 104         s = sprintf("%8d", n)
 105 
 106         while (match(s, /[0-9]{4,}/)) {
 107             len = RLENGTH
 108             lead = len % 3
 109             alt = lead > 0
 110 
 111             printf "%s", substr(s, 1, RSTART + lead - 1)
 112             s = substr(s, RSTART + lead)
 113             len -= lead
 114 
 115             while (len > 0) {
 116                 if (alt == 1) {
 117                     printf "\x1b[38;5;248m%s\x1b[0m", substr(s, 1, 3)
 118                 } else {
 119                     printf "%s", substr(s, 1, 3)
 120                 }
 121 
 122                 alt = 1 - alt
 123                 s = substr(s, 4)
 124                 len -= 3
 125             }
 126         }
 127 
 128         printf "%s", s
 129     }
 130 
 131     function show_byte(n) {
 132         if (n == 0) {
 133             printf "\x1b[38;5;111m%02x ", n
 134             return
 135         }
 136 
 137         if (n == 255) {
 138             printf "\x1b[38;5;209m%x ", n
 139             return
 140         }
 141 
 142         if (32 <= n && n <= 126) {
 143             printf "\x1b[38;5;72m%x\x1b[0m\x1b[48;5;254m%s", n, a[n]
 144             return
 145         }
 146 
 147         printf "\x1b[38;5;246m%02x ", n
 148     }
 149 
 150     NR % 5 == 1 && NR > 1 {
 151         printf "%8s  ", ""
 152         printf "           ·           ·           ·           ·\n"
 153     }
 154 
 155     {
 156         # printf "%8d  \x1b[48;5;254m", offset
 157         show_offset(offset)
 158         printf "  \x1b[48;5;254m"
 159 
 160         for (i = 1; i <= NF; i++) {
 161             if (i > 16) break
 162             show_byte($i)
 163         }
 164 
 165         printf "\x1b[0m"
 166 
 167         runs = 0
 168         asciirun = 0
 169         short = (NF < 2 * 16)
 170         pad = short ? sprintf("%*s", 3 * (NF - 16) + 2, "") : "  "
 171 
 172         for (i = short ? NF / 2 : 16 + 1; i <= NF; i++) {
 173             n = $i
 174 
 175             if (32 < n && n <= 126) {
 176                 if (!asciirun) {
 177                     asciirun = 1
 178                     runs++
 179                     printf (runs > 1) ? " " : pad
 180                 }
 181 
 182                 printf "%s", a[n]
 183                 continue
 184             }
 185 
 186             asciirun = 0
 187         }
 188 
 189         print ""
 190         if (flush) fflush()
 191 
 192         offset += 16
 193     }'
 194 
 195     first=0
 196 done