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