/* 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 sudo apt install libcurl4-openssl-dev cc -Wall -s -O2 -o ./get ./get.c -l curl */ #include #include #include #include #include #ifdef _WIN32 #include #include #endif #include const char* info = "" "get [options...] [filenames/data-URIs/URIs...]\n" "\n" "\n" "Load bytes from all the named sources given, be them files, base64-encoded\n" "URIs, or HTTP/HTTPS URIs. The name `-` stands for the standard input. When\n" "no names are given, the standard input is used by default.\n" "\n" "The help option is `-h`, `--h`, `-help`, or `--help`." ""; // handle_reader skips leading UTF-8 BOMs (byte-order marks), and turns all // CR-LF pairs into single LF bytes void handle_reader(FILE* w, FILE* r) { const int bufsize = 16 * 1024; unsigned char buf[bufsize]; while (!feof(w)) { size_t len = fread(&buf, sizeof(buf[0]), sizeof(buf), r); if (len < 1) { break; } fwrite(&buf, len, 1, w); } fflush(w); } // 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* fname) { FILE* f = fopen(fname, "rb"); if (f == NULL) { fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", fname); return false; } handle_reader(w, f); fclose(f); return true; } bool fetch(FILE* w, CURL* curl, const char* uri) { curl_easy_setopt(curl, CURLOPT_URL, uri); CURLcode code = curl_easy_perform(curl); if (code != CURLE_OK) { putc('\n', w); const char* msg = curl_easy_strerror(code); fprintf(stderr, "\x1b[31m%s: %s\x1b[0m\n", uri, msg); return false; } fflush(w); return true; } // 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 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; } bool handle_data_uri(FILE* w, char* uri) { 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; size_t n = strlen(uri); unsigned char* chunk = (unsigned char*)uri; // skip leading utf-8 byte-order-mark bytes, if present if (n >= 3 && match_lead(chunk, n, "\xef\xbb\xbf")) { chunk += 3; n -= 3; } // skip leading data-URI prelude, if present if (match_lead(chunk, n, "data:")) { const int skip = skip_data_uri(chunk, 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); fflush(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; } bool starts_with(const char* s, const char* prefix) { for (size_t i = 0; prefix[i] != 0; i++) { if (s[i] == 0) { return prefix[i] == 0; } if (s[i] != prefix[i]) { return false; } } return true; } bool seems_curlable(const char* s) { return false || starts_with(s, "https://") || starts_with(s, "http://") || starts_with(s, "ftp://") || starts_with(s, "ftps://") || starts_with(s, "gopher://") || starts_with(s, "gophers://") || starts_with(s, "rtmp://") || starts_with(s, "rtsp://") || starts_with(s, "scp://") || starts_with(s, "sftp://") || starts_with(s, "smb://") || starts_with(s, "smbs://") || starts_with(s, "telnet://") || starts_with(s, "tftp://") || false; } // run returns the number of errors int run(int argc, char** argv, FILE* w) { size_t dashes = 0; CURL* curl = NULL; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { if (dashes > 1) { break; } dashes++; } if (curl != NULL || seems_curlable(argv[i])) { curl = curl_easy_init(); if (curl == NULL) { fprintf(stderr, "\x1b[31mcan't initialize libcurl\x1b[0m\n"); return 1; } curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEDATA, w); } } if (dashes > 1) { const char* msg = "can't use the standard input (dash) more than once"; fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); return 1; } // use stdin when not given any filepaths if (argc <= 1) { handle_reader(w, stdin); return 0; } for (int i = 1; i < argc && !feof(stdout); i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { handle_reader(w, stdin); continue; } if (starts_with(argv[i], "data:")) { if (!handle_data_uri(w, argv[i])) { return 1; } continue; } if (starts_with(argv[i], "file://")) { if (!handle_file(w, argv[i] + sizeof("file://") - 1)) { return 1; } continue; } if (seems_curlable(argv[i])) { if (!fetch(w, curl, argv[i])) { return 1; } continue; } if (!handle_file(w, argv[i])) { return 1; } } if (curl != NULL) { curl_easy_cleanup(curl); } return 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 if (argc > 1) { if ( strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--h") == 0 || strcmp(argv[1], "--help") == 0 ) { fprintf(stdout, "%s", info); return 0; } } setvbuf(stdout, NULL, _IOFBF, 0); return run(argc, argv, stdout) == 0 ? 0 : 1; }