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