/* The MIT License (MIT) Copyright (c) 2026 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 -flto -o ./bitdump ./bitdump.c Building with TSV defined makes `bitdump` emit lines of tab-separated values, instead of space-separated values. You can do that by running cc -Wall -s -O2 -flto -D TSV -o ./bitdump ./bitdump.c */ #include #include #include #ifdef _WIN32 #include #include #endif #ifdef RED_ERRORS #define ERROR_STYLE "\x1b[38;2;204;0;0m" #ifdef __APPLE__ #define ERROR_STYLE "\x1b[31m" #endif #define RESET_STYLE "\x1b[0m" #else #define ERROR_STYLE #define RESET_STYLE #endif #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n") // ITEM_SEP is the item separator inserted between all values in output lines #ifdef TSV #define ITEM_SEP '\t' #else #define ITEM_SEP ' ' #endif // EMIT_CONST emits string constants without their final null byte #define EMIT_CONST(w, x) fwrite(x, 1, sizeof(x) - 1, w) 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" "\n" " -no-offset, --no-offset don't start lines with current byte offsets\n" " -no-offsets, --no-offsets aliases for option -no-offset\n" ""; /* 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 static inline void write_bin(FILE* w, unsigned char b) { const void* ptr = &lookup[8 * b]; fwrite(ptr, 1, 8, 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) { fwrite((unsigned char*)"00000000", 1, 8 - digits, w); } // emit all digits const unsigned char* start = buf + sizeof(buf) - digits; fwrite(start, 1, digits, w); } 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) { fwrite((unsigned char*)"00000000", 1, 8 - digits, w); } // emit all digits const unsigned char* start = buf + sizeof(buf) - digits; fwrite(start, 1, digits, w); } void decimal_offset(FILE* w, size_t offset) { write_decimal_uint(w, offset); fputc(ITEM_SEP, w); } void hexadecimal_offset(FILE* w, size_t offset) { write_hex_uint(w, offset); fputc(ITEM_SEP, w); } void no_offset(FILE*, size_t) { // do nothing on purpose } // 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) { fputc('\n', w); } break; } for (size_t i = 0; i < len; i++, offset++) { const size_t rem = offset % 8; if (rem == 0) { if (offset > 0) { fputc('\n', w); } start_row(w, offset); } else { fputc(ITEM_SEP, 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) { fputc('\n', w); fprintf(stderr, ERROR_LINE("can't open file named '%s'"), 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(const 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 dashes = 0; for (size_t i = 1; i < argc && !feof(w); i++) { // a `-` filename stands for the standard input if (strcmp(argv[i], "-") == 0) { dashes++; } } if (dashes > 1) { const char* m = "can't use the standard input (dash) more than once"; fprintf(stderr, ERROR_LINE("%s"), m); return 1; } size_t files = 0; size_t errors = 0; const void (*start_row)() = decimal_offset; bool options = true; // handle all filenames/options given for (size_t i = 1; i < argc && !feof(w); i++) { // a `-` filename stands for the standard input if (strcmp(argv[i], "-") == 0) { handle_reader(w, stdin, start_row); continue; } if (options) { if ( strcmp(argv[i], "-no-offset") == 0 || strcmp(argv[i], "-no-offsets") == 0 || strcmp(argv[i], "--no-offset") == 0 || strcmp(argv[i], "--no-offsets") == 0 ) { start_row = no_offset; continue; } } if (strcmp(argv[i], "--") == 0) { options = false; continue; } if (files > 0) { // put an empty line between adjacent outputs fputc('\n', w); } if (!handle_file(w, argv[i], start_row)) { errors++; } files++; } // no filenames means use stdin as the only input if (files == 0 && !feof(w)) { 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 if (argc > 1 && is_help_option(argv[1])) { fprintf(stderr, "%s", info); return 0; } return run(argc, argv, stdout) == 0 ? 0 : 1; }