/* 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 ./utfate ./utfate.c */ #include #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") #ifndef IBUF_SIZE #define IBUF_SIZE (32 * 1024) #endif 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, FILE* src, unsigned char* buf, size_t cap) { r->buf = buf; r->len = 0; r->cap = cap; r->pos = 0; r->src = src; } void restart_bufreader(bufreader* r, FILE* src) { r->src = src; ssize_t len = fread(r->buf, sizeof(unsigned char), r->cap, r->src); r->len = (len > 0) ? len : 0; } // 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 = fread(r->buf, sizeof(unsigned char), 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/ static inline 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) { fputc(0xef, w); fputc(0xbf, w); fputc(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) { fputc(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)) { fputc(a, w); fputc(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)) { fputc(a, w); fputc(b, w); fputc(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)) { fputc(a, w); fputc(b, w); fputc(c, w); fputc(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)) { // fputc(rune, w); // return; // } // // if (rune < (1 << (5 + 6))) { // fputc(0b11000000 | (rune >> 6), w); // fputc(0b10000000 | (rune & 0b00111111), w); // return; // } // // if (rune < (1 << (4 + 6 + 6))) { // fputc(0b11100000 | (rune >> 12), w); // fputc(0b10000000 | ((rune >> 6) & 0b00111111), w); // fputc(0b10000000 | (rune & 0b00111111), w); // return; // } // // if (rune < (1 << (3 + 6 + 6 + 6))) { // fputc(0b11110000 | (rune >> 18), w); // fputc(0b10000000 | ((rune >> 12) & 0b00111111), w); // fputc(0b10000000 | ((rune >> 6) & 0b00111111), w); // fputc(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)) { fputc(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)) { fputc(a, w); fputc(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)) { fputc(a, w); fputc(b, w); fputc(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)) { fputc(a, w); fputc(b, w); fputc(c, w); fputc(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) { fputc('\n', w); fprintf(stderr, ERROR_LINE("%s"), msg); } // desurrogate assumes the utf16 pair given to it is a valid surrogate static inline uint32_t desurrogate(uint16_t high, uint16_t low) { return 0x400 * (high - 0xd800) + (low - 0xdc00) + 0x10000; } void 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; } } } void handle_utf8_bom(FILE* w, bufreader* r) { discard_bytes(r, 3); handle_utf8(w, r); } void handle_utf16be(FILE* w, bufreader* r) { discard_bytes(r, 2); 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); } } void handle_utf16le(FILE* w, bufreader* r) { discard_bytes(r, 2); 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); } } void handle_utf32be(FILE* w, bufreader* r) { discard_bytes(r, 4); 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); } } void handle_utf32le(FILE* w, bufreader* r) { discard_bytes(r, 4); 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); } } void (*detect_bom(const bufreader* r))(FILE*, bufreader*) { 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 handle_utf32be; } if (p[0] == 0xff && p[1] == 0xfe && p[2] == 0x00 && p[3] == 0x00) { return handle_utf32le; } } if (len >= 3 && p[0] == 0xef && p[1] == 0xbb && p[2] == 0xbf) { return handle_utf8_bom; } if (len >= 2) { if (p[0] == 0xfe && p[1] == 0xff) { return handle_utf16be; } if (p[0] == 0xff && p[1] == 0xfe) { return handle_utf16le; } } return handle_utf8; } void handle_reader(FILE* w, FILE* src, bufreader* r) { restart_bufreader(r, src); detect_bom(r)(w, r); 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* path, bufreader* r) { FILE* f = fopen(path, "rb"); if (f == NULL) { fputc('\n', w); fprintf(stderr, ERROR_LINE("can't open file named '%s'"), path); return false; } handle_reader(w, f, r); fclose(f); return true; } // 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) { unsigned char buf[IBUF_SIZE]; bufreader r; init_bufreader(&r, stdin, buf, sizeof(buf)); 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 (strcmp(argv[i], "-") == 0) { handle_reader(w, stdin, &r); continue; } if (!handle_file(w, argv[i], &r)) { errors++; } } // no filenames means use stdin as the only input if (argc < 2) { handle_reader(w, stdin, &r); } 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 && is_help_option(argv[1])) { fprintf(stderr, "%s", info); return 0; } return run(argc, argv, stdout) == 0 ? 0 : 1; }