/* 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 ./debase64 ./debase64.c */ #include #include #include #include #ifdef _WIN32 #include #endif const char* info = "" "debase64 [options...] [filename...]\n" "\n" "\n" "Decode base64-encoded data: these include data-URIs, which start with a\n" "MIME declaration before their base64 payload starts.\n" "\n" "\n" "Options\n" "\n" " -h, -help, --h, --help show this help message\n" ""; const char* stdin_name = ""; 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; // } // } unsigned char rev_lookup_byte(unsigned char b) { return base64_rev_lookup[b]; } void show_invalid_byte(unsigned char b, size_t line, size_t pos) { const char* msg = "invalid base64 data"; const char* fmt = "\x1b[31m%s (byte %d, line: %ld, pos: %ld)\x1b[0m\n"; fprintf(stderr, fmt, msg, b, (long)line, (long)pos); } bool handle_reader(FILE* w, FILE* src, const char* path) { unsigned char buf[32 * 1024]; uint64_t line = 1; uint64_t pos = 1; uint64_t payload = 0; uint64_t padding = 0; unsigned char quad[4]; quad[0] = 0; quad[1] = 0; quad[2] = 0; quad[3] = 0; while (!feof(w)) { 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]; // ignore carriage-returns to support CRLF lines if (v == '\r') { continue; } // base64 streams can span multiple lines if (v == '\n') { line++; pos = 1; continue; } pos++; if (v == '=') { padding++; continue; } if (padding > 0 && v != '=') { putc('\n', w); const char* msg = "equal signs are only valid at the end"; const char* fmt = "\x1b[31m%s (line %ld, pos %ld)\x1b[0m\n"; fprintf(stderr, fmt, msg, (long)line, (long)pos); return false; } unsigned char b = rev_lookup_byte(v); if (b == INVALID) { show_invalid_byte(v, line, pos); return false; } 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 putc((quad[0] << 2) | (quad[1] >> 4), w); putc((quad[1] << 4) | (quad[2] >> 2), w); putc((quad[2] << 6) | (quad[3] >> 0), w); } } } // try to be resilient to missing trailing/padding equals // if (padding == 0 && payload > 0) { // padding = 4 - (payload % 4); // } // don't forget unemitted trailing bytes, if any switch (padding) { case 1: putc((quad[0] << 2) | (quad[1] >> 4), w); putc((quad[1] << 4) | (quad[2] >> 2), w); break; case 2: putc((quad[0] << 2) | (quad[1] >> 4), w); break; } fflush(w); return true; } // handle_file handles data from the filename given; returns false only when // an error happened bool handle_file(FILE* w, const char* path) { // a `-` filename stands for the standard input if (path[0] == '-' && path[1] == 0) { return handle_reader(w, stdin, stdin_name); } FILE* f = fopen(path, "rb"); if (f == NULL) { fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", 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; // handle special cmd-line options for (size_t i = 1; i < argc; i++) { if (is_help_option(argv[i])) { puts(info); return 0; } } if (argc > 2) { fprintf(stderr, "\x1b[31mmultiple files not allowed\x1b[0m\n"); return 1; } const char* name = (argc < 2) ? "-" : argv[1]; return handle_file(stdout, name) ? 0 : 1; }