File: bitdump.c
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2020-2025 pacman64
   5 
   6 Permission is hereby granted, free of charge, to any person obtaining a copy of
   7 this software and associated documentation files (the “Software”), to deal
   8 in the Software without restriction, including without limitation the rights to
   9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10 of the Software, and to permit persons to whom the Software is furnished to do
  11 so, subject to the following conditions:
  12 
  13 The above copyright notice and this permission notice shall be included in all
  14 copies or substantial portions of the Software.
  15 
  16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22 SOFTWARE.
  23 */
  24 
  25 /*
  26 You can build this command-line app by running
  27 
  28 cc -Wall -s -O2 -o ./bitdump ./bitdump.c
  29 */
  30 
  31 #include <fcntl.h>
  32 #include <stdbool.h>
  33 #include <stdio.h>
  34 #include <string.h>
  35 #include <sys/stat.h>
  36 
  37 #ifdef _WIN32
  38 #include <windows.h>
  39 #endif
  40 
  41 // info is the multi-line help message
  42 const char* info = ""
  43 "bitdump [options...] [filenames...]\n"
  44 "\n"
  45 "Show all bits for all input bytes, starting each output line with the\n"
  46 "leading byte's offset.\n"
  47 "\n"
  48 "\n"
  49 "Options\n"
  50 "\n"
  51 "    -h, --h            show this help message\n"
  52 "    -help, --help      aliases for option -h\n"
  53 "";
  54 
  55 // EMIT_CONST abstracts emitting string constants without their final null byte
  56 #define EMIT_CONST(w, x) fwrite(x, sizeof(x) - 1, 1, w)
  57 
  58 inline void write_bytes(FILE* w, const unsigned char* src, size_t len) {
  59     fwrite(src, len, 1, w);
  60 }
  61 
  62 /*
  63 tlp = '(f"{bin(v)[2:]:>08}" for v in range(256))' | lineup 8 | gsub '\t' |
  64     tlp 'f"    \"{l}\""'
  65 */
  66 const unsigned char lookup[256 * 8] = ""
  67     "0000000000000001000000100000001100000100000001010000011000000111"
  68     "0000100000001001000010100000101100001100000011010000111000001111"
  69     "0001000000010001000100100001001100010100000101010001011000010111"
  70     "0001100000011001000110100001101100011100000111010001111000011111"
  71     "0010000000100001001000100010001100100100001001010010011000100111"
  72     "0010100000101001001010100010101100101100001011010010111000101111"
  73     "0011000000110001001100100011001100110100001101010011011000110111"
  74     "0011100000111001001110100011101100111100001111010011111000111111"
  75     "0100000001000001010000100100001101000100010001010100011001000111"
  76     "0100100001001001010010100100101101001100010011010100111001001111"
  77     "0101000001010001010100100101001101010100010101010101011001010111"
  78     "0101100001011001010110100101101101011100010111010101111001011111"
  79     "0110000001100001011000100110001101100100011001010110011001100111"
  80     "0110100001101001011010100110101101101100011011010110111001101111"
  81     "0111000001110001011100100111001101110100011101010111011001110111"
  82     "0111100001111001011110100111101101111100011111010111111001111111"
  83     "1000000010000001100000101000001110000100100001011000011010000111"
  84     "1000100010001001100010101000101110001100100011011000111010001111"
  85     "1001000010010001100100101001001110010100100101011001011010010111"
  86     "1001100010011001100110101001101110011100100111011001111010011111"
  87     "1010000010100001101000101010001110100100101001011010011010100111"
  88     "1010100010101001101010101010101110101100101011011010111010101111"
  89     "1011000010110001101100101011001110110100101101011011011010110111"
  90     "1011100010111001101110101011101110111100101111011011111010111111"
  91     "1100000011000001110000101100001111000100110001011100011011000111"
  92     "1100100011001001110010101100101111001100110011011100111011001111"
  93     "1101000011010001110100101101001111010100110101011101011011010111"
  94     "1101100011011001110110101101101111011100110111011101111011011111"
  95     "1110000011100001111000101110001111100100111001011110011011100111"
  96     "1110100011101001111010101110101111101100111011011110111011101111"
  97     "1111000011110001111100101111001111110100111101011111011011110111"
  98     "1111100011111001111110101111101111111100111111011111111011111111"
  99     "";
 100 
 101 // write_bin is faster than calling fprintf(w, "%08b", b): this matters
 102 // because it's called for every input byte
 103 inline void write_bin(FILE* w, unsigned char b) {
 104     const void* ptr = &lookup[8 * b];
 105     fwrite(ptr, 8, 1, w);
 106 }
 107 
 108 void write_decimal_uint(FILE* w, size_t n) {
 109     if (n < 1) {
 110         EMIT_CONST(w, "00000000");
 111         return;
 112     }
 113 
 114     size_t digits;
 115     // 20 is the most digits unsigned 64-bit ints can ever need
 116     unsigned char buf[24];
 117     for (digits = 0; n > 0; digits++, n /= 10) {
 118         buf[sizeof(buf) - 1 - digits] = (n % 10) + '0';
 119     }
 120 
 121     // left-pad the coming digits up to 8 chars
 122     if (digits < 8) {
 123         write_bytes(w, (unsigned char*)"00000000", 8 - digits);
 124     }
 125 
 126     // emit all digits
 127     unsigned char* start = buf + sizeof(buf) - digits;
 128     write_bytes(w, start, digits);
 129 }
 130 
 131 void write_hex_uint(FILE* w, size_t n) {
 132     if (n < 1) {
 133         EMIT_CONST(w, "00000000");
 134         return;
 135     }
 136 
 137     size_t digits;
 138     // 20 is the most digits unsigned 64-bit ints can ever need
 139     unsigned char buf[24];
 140     for (digits = 0; n > 0; digits += 2, n /= 256) {
 141         unsigned char b = n % 256;
 142         const char* hex_digits = "0123456789abcdef";
 143         buf[sizeof(buf) - 1 - digits - 1] = hex_digits[b >> 4];
 144         buf[sizeof(buf) - 1 - digits - 0] = hex_digits[b & 0x0f];
 145     }
 146 
 147     // left-pad the coming digits up to 8 chars
 148     if (digits < 8) {
 149         write_bytes(w, (unsigned char*)"00000000", 8 - digits);
 150     }
 151 
 152     // emit all digits
 153     unsigned char* start = buf + sizeof(buf) - digits;
 154     write_bytes(w, start, digits);
 155 }
 156 
 157 void decimal_offset(FILE* w, size_t offset) {
 158     write_decimal_uint(w, offset);
 159     putc(' ', w);
 160 }
 161 
 162 void hexadecimal_offset(FILE* w, size_t offset) {
 163     write_hex_uint(w, offset);
 164     putc(' ', w);
 165 }
 166 
 167 void no_offset(FILE*, size_t) {
 168 }
 169 
 170 // handle_reader shows all bytes read from the source given as colored hex
 171 // values, showing offsets and ASCII symbols on the sides of each output line
 172 void handle_reader(FILE* w, FILE* src, void (*start_row)(FILE*, size_t)) {
 173     const size_t bufcap = 32 * 1024;
 174     unsigned char buf[bufcap];
 175     size_t offset = 0;
 176 
 177     while (!feof(w)) {
 178         const size_t len = fread(&buf, sizeof(buf[0]), sizeof(buf), src);
 179         if (len < 1) {
 180             // assume input is over when no bytes were read
 181             if (offset > 0) {
 182                 putc('\n', w);
 183             }
 184             break;
 185         }
 186 
 187         for (size_t i = 0; i < len; i++, offset++) {
 188             const size_t rem = offset % 8;
 189             if (rem == 0) {
 190                 if (offset > 0) {
 191                     putc('\n', w);
 192                 }
 193                 start_row(w, offset);
 194             } else {
 195                 putc(' ', w);
 196             }
 197             write_bin(w, buf[i]);
 198         }
 199     }
 200 }
 201 
 202 // handle_file handles data from the filename given; returns false only when
 203 // the file can't be opened
 204 bool handle_file(FILE* w, const char* path, void (*start_row)(FILE*, size_t)) {
 205     FILE* f = fopen(path, "rb");
 206     if (f == NULL) {
 207         // ensure currently-buffered/deferred output shows up right now: not
 208         // doing so may scramble results in the common case where stdout and
 209         // stderr are the same, thus confusing users
 210         putc('\n', w);
 211 
 212         fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", path);
 213         return false;
 214     }
 215 
 216     handle_reader(w, f, start_row);
 217 
 218     fclose(f);
 219     return true;
 220 }
 221 
 222 // is_help_option simplifies control-flow for func run
 223 bool is_help_option(char* s) {
 224     return (s[0] == '-') && (
 225         strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 ||
 226         strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0
 227     );
 228 }
 229 
 230 // run returns the number of errors
 231 int run(int argc, char** argv, FILE* w) {
 232     size_t files = 0;
 233     size_t errors = 0;
 234     const void (*start_row)() = decimal_offset;
 235 
 236     // handle all filenames/options given
 237     for (size_t i = 1; i < argc && !feof(w); i++) {
 238         // a `-` filename stands for the standard input
 239         if (argv[i][0] == '-' && argv[i][1] == 0) {
 240             handle_reader(w, stdin, start_row);
 241             continue;
 242         }
 243 
 244         if (is_help_option(argv[i])) {
 245             // help option quits the app right away
 246             fprintf(stderr, "%s", info);
 247             return 0;
 248         }
 249 
 250         if (files > 0) {
 251             // put an empty line between adjacent outputs
 252             putc('\n', w);
 253         }
 254 
 255         if (!handle_file(w, argv[i], start_row)) {
 256             errors++;
 257         }
 258         files++;
 259     }
 260 
 261     // no filenames means use stdin as the only input
 262     if (files == 0) {
 263         handle_reader(w, stdin, start_row);
 264     }
 265 
 266     return errors;
 267 }
 268 
 269 int main(int argc, char** argv) {
 270 #ifdef _WIN32
 271     setmode(fileno(stdin), O_BINARY);
 272     // ensure output lines end in LF instead of CRLF on windows
 273     setmode(fileno(stdout), O_BINARY);
 274     setmode(fileno(stderr), O_BINARY);
 275 #endif
 276 
 277     return run(argc, argv, stdout) == 0 ? 0 : 1;
 278 }