/* 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 -O3 -march=native -mtune=native -flto -o ./vulgarize ./vulgarize.c */ #include #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") const char* info = "" "vulgarize [filenames...]\n" "\n" "Turn files with ISO-8859-1 text into UTF8 text. The name `-` stands for the\n" "standard input. When no names are given, the standard input is used by\n" "default.\n" ""; // slice is a growable region of bytes in memory typedef struct slice { // ptr is the starting place of the region unsigned char* ptr; // len is how many bytes are currently being used size_t len; // cap is how many bytes the memory region has available size_t cap; } slice; /* tbp = 'range(0, 256)' | iconv -f iso-8859-1 -t utf8 | tbp '(f"{str(e)}, " if e >= 128 else f"{str(e)}, 0, " for e in d)' */ const unsigned char iso2utf8[512] = { 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16, 0, 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, 0, 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0, 31, 0, 32, 0, 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 40, 0, 41, 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, 0, 47, 0, 48, 0, 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 55, 0, 56, 0, 57, 0, 58, 0, 59, 0, 60, 0, 61, 0, 62, 0, 63, 0, 64, 0, 65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0, 73, 0, 74, 0, 75, 0, 76, 0, 77, 0, 78, 0, 79, 0, 80, 0, 81, 0, 82, 0, 83, 0, 84, 0, 85, 0, 86, 0, 87, 0, 88, 0, 89, 0, 90, 0, 91, 0, 92, 0, 93, 0, 94, 0, 95, 0, 96, 0, 97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102, 0, 103, 0, 104, 0, 105, 0, 106, 0, 107, 0, 108, 0, 109, 0, 110, 0, 111, 0, 112, 0, 113, 0, 114, 0, 115, 0, 116, 0, 117, 0, 118, 0, 119, 0, 120, 0, 121, 0, 122, 0, 123, 0, 124, 0, 125, 0, 126, 0, 127, 0, 194, 128, 194, 129, 194, 130, 194, 131, 194, 132, 194, 133, 194, 134, 194, 135, 194, 136, 194, 137, 194, 138, 194, 139, 194, 140, 194, 141, 194, 142, 194, 143, 194, 144, 194, 145, 194, 146, 194, 147, 194, 148, 194, 149, 194, 150, 194, 151, 194, 152, 194, 153, 194, 154, 194, 155, 194, 156, 194, 157, 194, 158, 194, 159, 194, 160, 194, 161, 194, 162, 194, 163, 194, 164, 194, 165, 194, 166, 194, 167, 194, 168, 194, 169, 194, 170, 194, 171, 194, 172, 194, 173, 194, 174, 194, 175, 194, 176, 194, 177, 194, 178, 194, 179, 194, 180, 194, 181, 194, 182, 194, 183, 194, 184, 194, 185, 194, 186, 194, 187, 194, 188, 194, 189, 194, 190, 194, 191, 195, 128, 195, 129, 195, 130, 195, 131, 195, 132, 195, 133, 195, 134, 195, 135, 195, 136, 195, 137, 195, 138, 195, 139, 195, 140, 195, 141, 195, 142, 195, 143, 195, 144, 195, 145, 195, 146, 195, 147, 195, 148, 195, 149, 195, 150, 195, 151, 195, 152, 195, 153, 195, 154, 195, 155, 195, 156, 195, 157, 195, 158, 195, 159, 195, 160, 195, 161, 195, 162, 195, 163, 195, 164, 195, 165, 195, 166, 195, 167, 195, 168, 195, 169, 195, 170, 195, 171, 195, 172, 195, 173, 195, 174, 195, 175, 195, 176, 195, 177, 195, 178, 195, 179, 195, 180, 195, 181, 195, 182, 195, 183, 195, 184, 195, 185, 195, 186, 195, 187, 195, 188, 195, 189, 195, 190, 195, 191, }; // handle_reader skips leading UTF-8 BOMs (byte-order marks), and turns all // CR-LF pairs into single LF bytes bool handle_reader(FILE* w, FILE* r, slice* line, slice* out) { while (!feof(w)) { ssize_t len = getline((char**)&line->ptr, &line->cap, r); if (line->ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); return false; } if (len < 0) { break; } // ensure there's room for up to twice the current line's byte-count if (out->cap < 2 * line->cap) { size_t new_cap = 2 * line->cap; void* new_ptr = realloc(out->ptr, new_cap); if (new_ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); return false; } out->ptr = new_ptr; out->cap = new_cap; } if (out->ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); return false; } size_t olen = 0; unsigned char* dest = out->ptr; for (size_t i = 0; i < len; i++) { const unsigned char b = line->ptr[i]; const size_t j = 2 * b; dest[olen + 0] = iso2utf8[j + 0]; dest[olen + 1] = iso2utf8[j + 1]; olen += 1 + (b >> 7); } fwrite(out->ptr, olen, 1, w); fflush(w); } return true; } // 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, slice* line, slice* out) { 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, line, out); fclose(f); return ok; } // run returns the number of errors int run(int argc, char** argv, FILE* w) { size_t dashes = 0; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == 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; } slice line; line.len = 0; line.cap = 32 * 1024; line.ptr = malloc(line.cap); if (line.ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); return 1; } // out is the destination for decoded bytes from lines: since all bytes // except trailing line-feeds could expand each into 2 bytes, twice the // space is needed just in case slice out; out.len = 0; out.cap = 2 * line.cap; out.ptr = malloc(out.cap); if (out.ptr == NULL) { free(line.ptr); fprintf(stderr, ERROR_LINE("out of memory")); return 1; } size_t errors = 0; for (int i = 1; i < argc && !feof(w) && line.ptr != NULL; i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { if (!handle_reader(w, stdin, &line, &out)) { errors++; } continue; } if (!handle_file(w, argv[i], &line, &out)) { errors++; } } // use stdin when not given any filepaths if (argc <= 1) { if (!handle_reader(w, stdin, &line, &out)) { errors++; } } free(line.ptr); free(out.ptr); 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) { 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; } } return run(argc, argv, stdout) == 0 ? 0 : 1; }