|
| 1 | +/* |
| 2 | +** Command & Conquer Generals Zero Hour(tm) |
| 3 | +** Copyright 2026 TheSuperHackers |
| 4 | +** |
| 5 | +** This program is free software: you can redistribute it and/or modify |
| 6 | +** it under the terms of the GNU General Public License as published by |
| 7 | +** the Free Software Foundation, either version 3 of the License, or |
| 8 | +** (at your option) any later version. |
| 9 | +** |
| 10 | +** This program is distributed in the hope that it will be useful, |
| 11 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +** GNU General Public License for more details. |
| 14 | +** |
| 15 | +** You should have received a copy of the GNU General Public License |
| 16 | +** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include "always.h" |
| 20 | +#include "utf8.h" |
| 21 | + |
| 22 | +#include <string.h> |
| 23 | + |
| 24 | +#ifdef _WIN32 |
| 25 | +#include <windows.h> |
| 26 | + |
| 27 | +static bool Is_Trail_Byte(char c) |
| 28 | +{ |
| 29 | + return (c & 0xC0) == 0x80; |
| 30 | +} |
| 31 | + |
| 32 | +size_t Utf8_Num_Bytes(char lead) |
| 33 | +{ |
| 34 | + if ((lead & 0x80) == 0x00) return 1; |
| 35 | + if ((lead & 0xE0) == 0xC0) return 2; |
| 36 | + if ((lead & 0xF0) == 0xE0) return 3; |
| 37 | + if ((lead & 0xF8) == 0xF0) return 4; |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +size_t Utf8_Trailing_Invalid_Bytes(const char* str, size_t length) |
| 42 | +{ |
| 43 | + if (length == 0) |
| 44 | + return 0; |
| 45 | + |
| 46 | + size_t i = length; |
| 47 | + while (i > 0 && Is_Trail_Byte(str[i - 1])) |
| 48 | + --i; |
| 49 | + |
| 50 | + if (i == 0) |
| 51 | + return length; |
| 52 | + |
| 53 | + size_t claimed = Utf8_Num_Bytes(str[i - 1]); |
| 54 | + size_t actual = length - (i - 1); |
| 55 | + |
| 56 | + if (claimed == 0 || claimed != actual) |
| 57 | + return actual; |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +bool Utf8_Validate(const char* str) |
| 63 | +{ |
| 64 | + return Utf8_Validate(str, strlen(str)); |
| 65 | +} |
| 66 | + |
| 67 | +bool Utf8_Validate(const char* str, size_t length) |
| 68 | +{ |
| 69 | + const unsigned char* s = (const unsigned char*)str; |
| 70 | + size_t i = 0; |
| 71 | + while (i < length) |
| 72 | + { |
| 73 | + size_t bytes = Utf8_Num_Bytes(str[i]); |
| 74 | + if (bytes == 0) |
| 75 | + return false; |
| 76 | + if (i + bytes > length) |
| 77 | + return false; |
| 78 | + for (size_t j = 1; j < bytes; ++j) |
| 79 | + { |
| 80 | + if (!Is_Trail_Byte(str[i + j])) |
| 81 | + return false; |
| 82 | + } |
| 83 | + // Reject overlong encodings per RFC 3629 |
| 84 | + if (bytes == 2 && s[i] < 0xC2) |
| 85 | + return false; |
| 86 | + if (bytes == 3 && s[i] == 0xE0 && s[i + 1] < 0xA0) |
| 87 | + return false; |
| 88 | + if (bytes == 4 && s[i] == 0xF0 && s[i + 1] < 0x90) |
| 89 | + return false; |
| 90 | + // Reject codepoints above U+10FFFF |
| 91 | + if (bytes == 4 && s[i] > 0xF4) |
| 92 | + return false; |
| 93 | + if (bytes == 4 && s[i] == 0xF4 && s[i + 1] > 0x8F) |
| 94 | + return false; |
| 95 | + i += bytes; |
| 96 | + } |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +size_t Get_Utf8_Size(const wchar_t* src, size_t srcLen) |
| 101 | +{ |
| 102 | + int bytes = WideCharToMultiByte(CP_UTF8, 0, src, (int)srcLen, nullptr, 0, nullptr, nullptr); |
| 103 | + return (bytes > 0) ? (size_t)bytes : 0; |
| 104 | +} |
| 105 | + |
| 106 | +size_t Get_Unicode_Size(const char* src, size_t srcLen) |
| 107 | +{ |
| 108 | + int wchars = MultiByteToWideChar(CP_UTF8, 0, src, (int)srcLen, nullptr, 0); |
| 109 | + return (wchars > 0) ? (size_t)wchars : 0; |
| 110 | +} |
| 111 | + |
| 112 | +bool Unicode_To_Utf8(char* dest, const wchar_t* src, size_t srcLen, size_t destSize) |
| 113 | +{ |
| 114 | + if (destSize == 0) |
| 115 | + return false; |
| 116 | + int result = WideCharToMultiByte(CP_UTF8, 0, src, (int)srcLen, dest, (int)destSize, nullptr, nullptr); |
| 117 | + if (result == 0) |
| 118 | + dest[0] = '\0'; |
| 119 | + return result != 0; |
| 120 | +} |
| 121 | + |
| 122 | +bool Utf8_To_Unicode(wchar_t* dest, const char* src, size_t srcLen, size_t destSize) |
| 123 | +{ |
| 124 | + if (destSize == 0) |
| 125 | + return false; |
| 126 | + int result = MultiByteToWideChar(CP_UTF8, 0, src, (int)srcLen, dest, (int)destSize); |
| 127 | + if (result == 0) |
| 128 | + dest[0] = L'\0'; |
| 129 | + return result != 0; |
| 130 | +} |
| 131 | + |
| 132 | +#else |
| 133 | +#error "Not implemented" |
| 134 | +#endif |
0 commit comments