/* 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 -march=native -mtune=native -flto -o ./debase64 ./debase64.c */ #include #include #include #include #ifdef _WIN32 #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") #ifndef IBUF_SIZE #define IBUF_SIZE (32 * 1024) #endif #ifndef OBUF_SIZE #define OBUF_SIZE (8 * 1024) #endif const char* info = "" "debase64 [options...] [filename...]\n" "\n" "Decode base64-encoded data: these include data-URIs, which start with a\n" "MIME declaration before their base64 payload starts.\n" "\n" "Options\n" "\n" " -h, -help, --h, --help show this help message\n" ""; const char* stdin_name = ""; // bufwriter is, as the name implies, a buffered-writer: when it's aimed at // stdout, it considerably speeds up this app, as intended typedef struct bufwriter { // buf is the buffer proper unsigned char* buf; // len is how many bytes of the buffer are currently being used size_t len; // cap is the capacity of the buffer, or the most bytes it can hold size_t cap; // out is the destination of all that's written into the buffer FILE* out; } bufwriter; void init_bufwriter(bufwriter* w, FILE* out, unsigned char* b, size_t cap) { w->buf = b; w->len = 0; w->cap = cap; w->out = out; } static inline void write_byte(bufwriter* w, unsigned char b) { if (w->len < w->cap) { w->buf[w->len++] = b; return; } fwrite(w->buf, 1, w->cap, w->out); w->buf[0] = b; w->len = 1; } void flush(bufwriter* w) { if (w->len > 0) { fwrite(w->buf, 1, w->len, w->out); } w->len = 0; fflush(w->out); } bool match_lead(unsigned char* buf, size_t n, char* to) { for (; n > 0 && *to != 0; buf++, to++, n--) { if (*buf != *to) { return false; } } return true; } size_t skip_data_uri(unsigned char* buf, size_t n) { for (size_t i = 0; i < n; i++) { if (match_lead(buf + i, n - i, ";base64,")) { return i + (sizeof(";base64,") - 1); } } return 0; } // INVALID signals an input byte isn't allowed in a base64 stream #define INVALID 0xff const unsigned char base64_rev_lookup[256] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; // unsigned char rev_lookup_byte(unsigned char b) { // if ('A' <= b && b <= 'Z') { // return b - 'A'; // } // if ('a' <= b && b <= 'z') { // return (b - 'a') + 26; // } // if ('0' <= b && b <= '9') { // return (b - '0') + 52; // } // // switch (b) { // case '+': // return 62; // case '/': // return 63; // default: // return INVALID; // } // } void show_invalid_byte(unsigned char b, size_t line, size_t pos) { const char* msg = "invalid base64 data"; const char* fmt = ERROR_LINE("%s (byte %d, line: %ld, pos: %ld)"); fprintf(stderr, fmt, msg, b, (long)line, (long)pos); } bool handle_reader(bufwriter* w, FILE* src, const char* path) { unsigned char buf[IBUF_SIZE]; size_t line = 1; size_t pos = 1; size_t payload = 0; size_t padding = 0; unsigned char quad[4]; quad[0] = 0; quad[1] = 0; quad[2] = 0; quad[3] = 0; unsigned char prev = 0; while (!feof(w->out)) { size_t n = fread(&buf, sizeof(buf[0]), sizeof(buf), src); if (n < 1) { // assume input is over when no bytes were read break; } unsigned char* chunk = buf; // skip leading utf-8 byte-order-mark bytes, if present if (payload == 0 && n >= 3 && match_lead(buf, n, "\xef\xbb\xbf")) { chunk += 3; n -= 3; } // skip leading data-URI prelude, if present if (payload == 0 && match_lead(buf, n, "data:")) { const int skip = skip_data_uri(buf, n); chunk += skip; n -= skip; } for (size_t i = 0; i < n; i++) { const unsigned char v = chunk[i]; const unsigned char b = base64_rev_lookup[v]; if (padding > 0 && prev == '=') { if (v != '\r' && v != '\n' && v != '=') { const char* fmt = ERROR_LINE("payload after padding"); fprintf(stderr, fmt); return false; } } prev = v; if (b == INVALID) { // base64 streams can span multiple lines if (v == '\n') { line++; pos = 1; continue; } // ignore carriage-returns to support CRLF-type lines if (v == '\r') { continue; } if (v == '=') { padding++; continue; } show_invalid_byte(v, line, pos); return false; } pos++; const size_t step = payload % 4; quad[step] = b; payload++; if (step == 3) { // 01234567 01234567 01234567 01234567 // 00000000 11111111 22222222 33333333 // xx000000 xx001111 xx111122 xx222222 write_byte(w, (quad[0] << 2) | (quad[1] >> 4)); write_byte(w, (quad[1] << 4) | (quad[2] >> 2)); write_byte(w, (quad[2] << 6) | (quad[3] >> 0)); } } // don't bother with rest of input when padding is clearly wrong if (padding > 2) { break; } } // try to be resilient to missing trailing/padding equals // if (padding == 0 && payload > 0) { // padding = 4 - (payload % 4); // } if (padding > 2 || (padding > 0 && payload == 0)) { const char* fmt = ERROR_LINE("excessive padding"); fprintf(stderr, fmt); return false; } const size_t step = payload % 4; // a single base64 byte on its own is worth only 6 bits if (step == 1) { const char* fmt = ERROR_LINE("missing final bytes"); fprintf(stderr, fmt); return false; } const bool a = (step == 0 && padding != 0); const bool b = (step == 2 && padding != 2); const bool c = (step == 3 && padding != 1); if (a || b || c) { const char* fmt = ERROR_LINE("bad padding"); fprintf(stderr, fmt); return false; } // don't forget unemitted trailing bytes, if any switch (padding) { case 1: write_byte(w, (quad[0] << 2) | (quad[1] >> 4)); write_byte(w, (quad[1] << 4) | (quad[2] >> 2)); break; case 2: write_byte(w, (quad[0] << 2) | (quad[1] >> 4)); break; } flush(w); return true; } // handle_file handles data from the filename given; returns false only when // an error happened bool handle_file(bufwriter* w, const char* path) { // a `-` filename stands for the standard input if (strcmp(path, "-") == 0) { return handle_reader(w, stdin, stdin_name); } FILE* f = fopen(path, "rb"); if (f == NULL) { fprintf(stderr, ERROR_LINE("can't open file named '%s'"), path); return false; } const bool ok = handle_reader(w, f, path); fclose(f); return ok; } // 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 ); } 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 // emit first-step byte-decoding table for base64 symbols; // who needs scripts/interpreters when you have compilers? // for (unsigned int i = 0; i < 256; i++) { // if (i % 8 == 0) { // fprintf(stdout, " "); // } // fprintf(stdout, "0x%02x,", rev_lookup_byte(i)); // fprintf(stdout, (i % 8 == 7 && i > 0) ? "\n" : " "); // } // return 0; if (argc > 1 && is_help_option(argv[1])) { printf("%s", info); return 0; } if (argc > 2) { fprintf(stderr, ERROR_LINE("multiple files not allowed")); return 1; } // enable full/block-buffering for standard output setvbuf(stdout, NULL, _IOFBF, 0); unsigned char outbuf[OBUF_SIZE]; bufwriter bw; init_bufwriter(&bw, stdout, outbuf, sizeof(outbuf)); const char* name = (argc < 2) ? "-" : argv[1]; const int res = handle_file(&bw, name) ? 0 : 1; flush(&bw); return res; }