/* 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 ./utfate ./utfate.c */ #include #include #include #include #include #ifdef _WIN32 #include #include #endif // info is the multi-line help message const char* info = "" "utfate [options...] [filenames...]\n" "\n" "This app turns ASCII/UTF text into UTF-8. ASCII/UTF-8 inputs stay the same,\n" "leading UTF-8 BOMs (byte-order marks) are ignored, UTF-16 and UTF-32 (both\n" "in either kind of endianess) are turned into UTF-8.\n" "\n" "\n" "Options\n" "\n" " -h, --h show this help message\n" " -help, --help aliases for option -h\n" ""; typedef struct bufreader { // buf is the buffer, (re)filled periodically as needed unsigned char* buf; // len is how many buffer bytes are being used, out of its max capacity size_t len; // cap is the buffer's capacity, or the most bytes it can hold at once size_t cap; // pos is the current position, up to the current buffer length size_t pos; // src is the data source used to fill the buffer FILE* src; } bufreader; // init_bufreader is the constructor for type bufreader void init_bufreader(bufreader* r) { r->buf = NULL; r->len = 0; r->cap = 0; r->pos = 0; r->src = NULL; } void restart_bufreader(bufreader* r, FILE* src) { r->src = src; // allow peeking at the first few input bytes, which are needed to detect // which specific utf input-format is being used ssize_t len = getline((char**)&r->buf, &r->cap, r->src); r->len = (len > 0) ? len : 0; } bool check_bufreader(const bufreader* r) { return r->buf != NULL; } // read_byte does as it says: check its return for the value EOF, before // using it as the next byte int read_byte(bufreader* r) { if (r->pos < r->len) { // inside current chunk const unsigned char b = r->buf[r->pos]; r->pos++; return b; } // need to read the next block r->pos = 0; ssize_t len = getline((char**)&r->buf, &r->cap, r->src); if (len > 0) { r->len = len; return r->buf[r->pos++]; } // reached the end of data r->len = 0; return EOF; } int64_t discard_bytes(bufreader* r, size_t n) { if (r->pos + n < r->len) { r->pos += n; return n; } int64_t discarded = 0; for (; n > 0; n--, discarded++) { if (read_byte(r) == EOF) { break; } } return discarded; } // https://lemire.me/blog/2018/05/09/how-quickly-can-you-check-that-a-string-is-valid-unicode-utf-8/ bool check_2_byte_rune(int a, int b) { return (0xc2 <= a && a <= 0xdf) && (0x80 <= b && b <= 0xbf); } bool check_3_byte_rune(int a, int b, int c) { return ( (a == 0xe0) && (0xa0 <= b && b <= 0xbf) && (0x80 <= c && c <= 0xbf) ) || ( (0xe1 <= a && a <= 0xec) && (0x80 <= b && b <= 0xbf) && (0x80 <= c && c <= 0xbf) ) || ( (a == 0xed) && (0x80 <= b && b <= 0x9f) && (0x80 <= c && c <= 0xbf) ) || ( (a == 0xee || a == 0xef) && (0x80 <= b && b <= 0xbf) && (0x80 <= c && c <= 0xbf) ); } bool check_4_byte_rune(int a, int b, int c, int d) { return ( (a == 0xf0) && (0x90 <= b && b <= 0xbf) && (0x80 <= c && c <= 0xbf) && (0x80 <= d && d <= 0xbf) ) || ( (a == 0xf1 || a == 0xf3) && (0x80 <= b && b <= 0xbf) && (0x80 <= c && c <= 0xbf) && (0x80 <= d && d <= 0xbf) ) || ( (a == 0xf4) && (0x80 <= b && b <= 0xbf) && (0x80 <= c && c <= 0x8f) && (0x80 <= d && d <= 0xbf) ); } // write_replacement_char is the recommended action to handle invalid bytes void write_replacement_char(FILE* w) { putc(0xef, w); putc(0xbf, w); putc(0xbd, w); } void copy_utf8_rune(FILE* w, bufreader* r) { const int a = read_byte(r); if (a == EOF) { return; } // handle 1-byte runes if (a < 128) { putc(a, w); return; } const int b = read_byte(r); if (b == EOF) { write_replacement_char(w); return; } // handle 2-byte runes if (check_2_byte_rune(a, b)) { putc(a, w); putc(b, w); return; } const int c = read_byte(r); if (c == EOF) { write_replacement_char(w); return; } // handle 3-byte runes if (check_3_byte_rune(a, b, c)) { putc(a, w); putc(b, w); putc(c, w); return; } const int d = read_byte(r); if (d == EOF) { write_replacement_char(w); return; } // handle 4-byte runes if (check_4_byte_rune(a, b, c, d)) { putc(a, w); putc(b, w); putc(c, w); putc(d, w); return; } write_replacement_char(w); } // write_rune is following the table at https://en.wikipedia.org/wiki/UTF-8 // void write_rune(FILE* w, uint32_t rune) { // if (rune < (1 << 7)) { // putc(rune, w); // return; // } // // if (rune < (1 << (5 + 6))) { // putc(0b11000000 | (rune >> 6), w); // putc(0b10000000 | (rune & 0b00111111), w); // return; // } // // if (rune < (1 << (4 + 6 + 6))) { // putc(0b11100000 | (rune >> 12), w); // putc(0b10000000 | ((rune >> 6) & 0b00111111), w); // putc(0b10000000 | (rune & 0b00111111), w); // return; // } // // if (rune < (1 << (3 + 6 + 6 + 6))) { // putc(0b11110000 | (rune >> 18), w); // putc(0b10000000 | ((rune >> 12) & 0b00111111), w); // putc(0b10000000 | ((rune >> 6) & 0b00111111), w); // putc(0b10000000 | (rune & 0b00111111), w); // return; // } // // // handle invalid runes with a utf-8 replacement character // write_replacement_char(w); // } // write_rune is following the table at https://en.wikipedia.org/wiki/UTF-8 void write_rune(FILE* w, uint32_t rune) { if (rune < (1 << 7)) { putc(rune, w); return; } if (rune < (1 << (5 + 6))) { const int a = 0b11000000 | (rune >> 6); const int b = 0b10000000 | (rune & 0b00111111); if (check_2_byte_rune(a, b)) { putc(a, w); putc(b, w); } else { write_replacement_char(w); } return; } if (rune < (1 << (4 + 6 + 6))) { const int a = 0b11100000 | (rune >> 12); const int b = 0b10000000 | ((rune >> 6) & 0b00111111); const int c = 0b10000000 | (rune & 0b00111111); if (check_3_byte_rune(a, b, c)) { putc(a, w); putc(b, w); putc(c, w); } else { write_replacement_char(w); } return; } if (rune < (1 << (3 + 6 + 6 + 6))) { const int a = 0b11110000 | (rune >> 18); const int b = 0b10000000 | ((rune >> 12) & 0b00111111); const int c = 0b10000000 | ((rune >> 6) & 0b00111111); const int d = 0b10000000 | (rune & 0b00111111); if (check_4_byte_rune(a, b, c, d)) { putc(a, w); putc(b, w); putc(c, w); putc(d, w); } else { write_replacement_char(w); } return; } // handle invalid runes with a utf-8 replacement character write_replacement_char(w); } void show_error(FILE* w, const char* msg) { putc('\n', w); fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); } typedef enum detected_bom { NO_BOM = 0, UTF8_BOM = 1, UTF16_BE_BOM = 2, UTF16_LE_BOM = 3, UTF32_BE_BOM = 4, UTF32_LE_BOM = 5, } detected_bom; detected_bom detect_bom(const bufreader* r) { const unsigned char* p = r->buf; const ssize_t len = r->len; if (len >= 4) { if (p[0] == 0x00 && p[1] == 0x00 && p[2] == 0xfe && p[3] == 0xff) { return UTF32_BE_BOM; } if (p[0] == 0xff && p[1] == 0xfe && p[2] == 0x00 && p[3] == 0x00) { return UTF32_LE_BOM; } } if (len >= 3 && p[0] == 0xef && p[1] == 0xbb && p[2] == 0xbf) { return UTF8_BOM; } if (len >= 2) { if (p[0] == 0xfe && p[1] == 0xff) { return UTF16_BE_BOM; } if (p[0] == 0xff && p[1] == 0xfe) { return UTF16_LE_BOM; } } return NO_BOM; } // desurrogate assumes the utf16 pair given to it is a valid surrogate uint32_t desurrogate(uint16_t high, uint16_t low) { return 0x400 * (high - 0xd800) + (low - 0xdc00) + 0x10000; } bool handle_utf8(FILE* w, bufreader* r) { for (uint64_t i = 0; r->len > 0; i++) { copy_utf8_rune(w, r); if ((i % 1024 == 0) && feof(w)) { break; } } return check_bufreader(r); } bool handle_utf16be(FILE* w, bufreader* r) { for (uint64_t i = 0; r->len > 0; i++) { if ((i % 1024 == 0) && feof(w)) { break; } const int a = read_byte(r); if (a == EOF) { break; } const int b = read_byte(r); if (b == EOF) { write_replacement_char(w); break; } const uint32_t code = (a << 8) + b; // handle non-surrogate runes if ((code <= 0xd7ff) || (code >= 0xe000)) { write_rune(w, code); continue; } const int c = read_byte(r); if (c == EOF) { write_replacement_char(w); break; } const int d = read_byte(r); if (d == EOF) { write_replacement_char(w); break; } // https://en.wikipedia.org/wiki/UTF-16 const uint16_t high = code; const uint16_t low = (c << 8) + d; // handle valid surrogate runes if (0xdc00 <= low && low <= 0xdfff) { write_rune(w, desurrogate(high, low)); continue; } write_replacement_char(w); } return check_bufreader(r); } bool handle_utf16le(FILE* w, bufreader* r) { for (uint64_t i = 0; r->len > 0; i++) { if ((i % 1024 == 0) && feof(w)) { break; } const int a = read_byte(r); if (a == EOF) { break; } const int b = read_byte(r); if (b == EOF) { write_replacement_char(w); break; } const uint32_t code = (b << 8) + a; // handle non-surrogate runes if ((code <= 0xd7ff) || (code >= 0xe000)) { write_rune(w, code); continue; } const int c = read_byte(r); if (c == EOF) { write_replacement_char(w); break; } const int d = read_byte(r); if (d == EOF) { write_replacement_char(w); break; } // https://en.wikipedia.org/wiki/UTF-16 const uint16_t high = code; const uint16_t low = (d << 8) + c; // handle valid surrogate runes if (0xdc00 <= low && low <= 0xdfff) { write_rune(w, desurrogate(high, low)); continue; } write_replacement_char(w); } return check_bufreader(r); } bool handle_utf32be(FILE* w, bufreader* r) { for (uint64_t i = 0; r->len > 0; i++) { if ((i % 1024 == 0) && feof(w)) { break; } const int a = read_byte(r); if (a == EOF) { break; } const int b = read_byte(r); if (b == EOF) { write_replacement_char(w); break; } const int c = read_byte(r); if (c == EOF) { write_replacement_char(w); break; } const int d = read_byte(r); if (d == EOF) { write_replacement_char(w); break; } write_rune(w, (a << 24) + (b << 16) + (c << 8) + d); } return check_bufreader(r); } bool handle_utf32le(FILE* w, bufreader* r) { for (uint64_t i = 0; r->len > 0; i++) { if ((i % 1024 == 0) && feof(w)) { break; } const int a = read_byte(r); if (a == EOF) { break; } const int b = read_byte(r); if (b == EOF) { write_replacement_char(w); break; } const int c = read_byte(r); if (c == EOF) { write_replacement_char(w); break; } const int d = read_byte(r); if (d == EOF) { write_replacement_char(w); break; } write_rune(w, (d << 24) + (c << 16) + (b << 8) + a); } return check_bufreader(r); } bool dispatch_reader(FILE* w, FILE* src, bufreader* r) { switch (detect_bom(r)) { case NO_BOM: return handle_utf8(w, r); case UTF8_BOM: discard_bytes(r, 3); return handle_utf8(w, r); case UTF16_BE_BOM: discard_bytes(r, 2); return handle_utf16be(w, r); case UTF16_LE_BOM: discard_bytes(r, 2); return handle_utf16le(w, r); case UTF32_BE_BOM: discard_bytes(r, 4); return handle_utf32be(w, r); case UTF32_LE_BOM: discard_bytes(r, 4); return handle_utf32le(w, r); default: return handle_utf8(w, r); } } bool handle_reader(FILE* w, FILE* src, bufreader* r) { restart_bufreader(r, src); const bool ok = dispatch_reader(w, src, r); if (r->buf == NULL) { show_error(w, "can't get memory to read text lines"); } return ok; } // 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, bufreader* r) { FILE* f = fopen(path, "rb"); if (f == NULL) { putc('\n', w); fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", path); return false; } const bool ok = handle_reader(w, f, r); 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 ); } // run returns the number of errors int run(int argc, char** argv, FILE* w) { if (argc > 1 && is_help_option(argv[1])) { // help option quits the app right away fprintf(stderr, "%s", info); return 0; } bufreader r; init_bufreader(&r); size_t errors = 0; // handle all filenames/options given for (size_t i = 1; i < argc && !feof(w); i++) { // a `-` filename stands for the standard input if (argv[i][0] == '-' && argv[i][1] == 0) { if (!handle_reader(w, stdin, &r)) { errors++; } continue; } if (!handle_file(w, argv[i], &r)) { errors++; } } // no filenames means use stdin as the only input if (argc < 2) { if (!handle_reader(w, stdin, &r)) { errors++; } } free(r.buf); 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 return run(argc, argv, stdout) == 0 ? 0 : 1; }