/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running cc -Wall -s -O2 -o ./bitdump ./bitdump.c */ #include #include #include #include #include #ifdef _WIN32 #include #endif // info is the multi-line help message const char* info = "" "bitdump [options...] [filenames...]\n" "\n" "Show all bits for all input bytes, starting each output line with the\n" "leading byte's offset.\n" "\n" "\n" "Options\n" "\n" " -h, --h show this help message\n" " -help, --help aliases for option -h\n" ""; // EMIT_CONST abstracts emitting string constants without their final null byte #define EMIT_CONST(w, x) fwrite(x, sizeof(x) - 1, 1, w) inline void write_bytes(FILE* w, const unsigned char* src, size_t len) { fwrite(src, len, 1, w); } /* tlp = '(f"{bin(v)[2:]:>08}" for v in range(256))' | lineup 8 | gsub '\t' | tlp 'f" \"{l}\""' */ const unsigned char lookup[256 * 8] = "" "0000000000000001000000100000001100000100000001010000011000000111" "0000100000001001000010100000101100001100000011010000111000001111" "0001000000010001000100100001001100010100000101010001011000010111" "0001100000011001000110100001101100011100000111010001111000011111" "0010000000100001001000100010001100100100001001010010011000100111" "0010100000101001001010100010101100101100001011010010111000101111" "0011000000110001001100100011001100110100001101010011011000110111" "0011100000111001001110100011101100111100001111010011111000111111" "0100000001000001010000100100001101000100010001010100011001000111" "0100100001001001010010100100101101001100010011010100111001001111" "0101000001010001010100100101001101010100010101010101011001010111" "0101100001011001010110100101101101011100010111010101111001011111" "0110000001100001011000100110001101100100011001010110011001100111" "0110100001101001011010100110101101101100011011010110111001101111" "0111000001110001011100100111001101110100011101010111011001110111" "0111100001111001011110100111101101111100011111010111111001111111" "1000000010000001100000101000001110000100100001011000011010000111" "1000100010001001100010101000101110001100100011011000111010001111" "1001000010010001100100101001001110010100100101011001011010010111" "1001100010011001100110101001101110011100100111011001111010011111" "1010000010100001101000101010001110100100101001011010011010100111" "1010100010101001101010101010101110101100101011011010111010101111" "1011000010110001101100101011001110110100101101011011011010110111" "1011100010111001101110101011101110111100101111011011111010111111" "1100000011000001110000101100001111000100110001011100011011000111" "1100100011001001110010101100101111001100110011011100111011001111" "1101000011010001110100101101001111010100110101011101011011010111" "1101100011011001110110101101101111011100110111011101111011011111" "1110000011100001111000101110001111100100111001011110011011100111" "1110100011101001111010101110101111101100111011011110111011101111" "1111000011110001111100101111001111110100111101011111011011110111" "1111100011111001111110101111101111111100111111011111111011111111" ""; // write_bin is faster than calling fprintf(w, "%08b", b): this matters // because it's called for every input byte inline void write_bin(FILE* w, unsigned char b) { const void* ptr = &lookup[8 * b]; fwrite(ptr, 8, 1, w); } void write_decimal_uint(FILE* w, size_t n) { if (n < 1) { EMIT_CONST(w, "00000000"); return; } size_t digits; // 20 is the most digits unsigned 64-bit ints can ever need unsigned char buf[24]; for (digits = 0; n > 0; digits++, n /= 10) { buf[sizeof(buf) - 1 - digits] = (n % 10) + '0'; } // left-pad the coming digits up to 8 chars if (digits < 8) { write_bytes(w, (unsigned char*)"00000000", 8 - digits); } // emit all digits unsigned char* start = buf + sizeof(buf) - digits; write_bytes(w, start, digits); } void write_hex_uint(FILE* w, size_t n) { if (n < 1) { EMIT_CONST(w, "00000000"); return; } size_t digits; // 20 is the most digits unsigned 64-bit ints can ever need unsigned char buf[24]; for (digits = 0; n > 0; digits += 2, n /= 256) { unsigned char b = n % 256; const char* hex_digits = "0123456789abcdef"; buf[sizeof(buf) - 1 - digits - 1] = hex_digits[b >> 4]; buf[sizeof(buf) - 1 - digits - 0] = hex_digits[b & 0x0f]; } // left-pad the coming digits up to 8 chars if (digits < 8) { write_bytes(w, (unsigned char*)"00000000", 8 - digits); } // emit all digits unsigned char* start = buf + sizeof(buf) - digits; write_bytes(w, start, digits); } void decimal_offset(FILE* w, size_t offset) { write_decimal_uint(w, offset); putc(' ', w); } void hexadecimal_offset(FILE* w, size_t offset) { write_hex_uint(w, offset); putc(' ', w); } void no_offset(FILE*, size_t) { } // handle_reader shows all bytes read from the source given as colored hex // values, showing offsets and ASCII symbols on the sides of each output line void handle_reader(FILE* w, FILE* src, void (*start_row)(FILE*, size_t)) { const size_t bufcap = 32 * 1024; unsigned char buf[bufcap]; size_t offset = 0; while (!feof(w)) { const size_t len = fread(&buf, sizeof(buf[0]), sizeof(buf), src); if (len < 1) { // assume input is over when no bytes were read if (offset > 0) { putc('\n', w); } break; } for (size_t i = 0; i < len; i++, offset++) { const size_t rem = offset % 8; if (rem == 0) { if (offset > 0) { putc('\n', w); } start_row(w, offset); } else { putc(' ', w); } write_bin(w, buf[i]); } } } // handle_file handles data from the filename given; returns false only when // the file can't be opened bool handle_file(FILE* w, const char* path, void (*start_row)(FILE*, size_t)) { FILE* f = fopen(path, "rb"); if (f == NULL) { // ensure currently-buffered/deferred output shows up right now: not // doing so may scramble results in the common case where stdout and // stderr are the same, thus confusing users putc('\n', w); fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", path); return false; } handle_reader(w, f, start_row); fclose(f); return true; } // is_help_option simplifies control-flow for func run bool is_help_option(char* s) { return (s[0] == '-') && ( strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 || strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0 ); } // run returns the number of errors int run(int argc, char** argv, FILE* w) { size_t files = 0; size_t errors = 0; const void (*start_row)() = decimal_offset; // handle all filenames/options given for (size_t i = 1; i < argc && !feof(w); i++) { // a `-` filename stands for the standard input if (argv[i][0] == '-' && argv[i][1] == 0) { handle_reader(w, stdin, start_row); continue; } if (is_help_option(argv[i])) { // help option quits the app right away fprintf(stderr, "%s", info); return 0; } if (files > 0) { // put an empty line between adjacent outputs putc('\n', w); } if (!handle_file(w, argv[i], start_row)) { errors++; } files++; } // no filenames means use stdin as the only input if (files == 0) { handle_reader(w, stdin, start_row); } return errors; } int main(int argc, char** argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); // ensure output lines end in LF instead of CRLF on windows setmode(fileno(stdout), O_BINARY); setmode(fileno(stderr), O_BINARY); #endif return run(argc, argv, stdout) == 0 ? 0 : 1; }