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