-
Notifications
You must be signed in to change notification settings - Fork 180
feat(string): add UTF-8 string conversion and validation functions #2528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
40393b8
abb71f0
0c9074d
149a07f
6097799
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,8 @@ set(WWLIB_SRC | |
| trim.cpp | ||
| trim.h | ||
| uarray.h | ||
| utf8.cpp | ||
| utf8.h | ||
| vector.cpp | ||
| Vector.h | ||
| visualc.h | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,174 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** Command & Conquer Generals Zero Hour(tm) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** Copyright 2026 TheSuperHackers | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** the Free Software Foundation, either version 3 of the License, or | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** (at your option) any later version. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ** along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "always.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "utf8.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <string.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef _WIN32 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <windows.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| static bool Is_Trail_Byte(char c) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (c & 0xC0) == 0x80; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t Utf8_Num_Bytes(char lead) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((lead & 0x80) == 0x00) return 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((lead & 0xE0) == 0xC0) return 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((lead & 0xF0) == 0xE0) return 3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((lead & 0xF8) == 0xF0) return 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t Utf8_Trailing_Invalid_Bytes(const char* str, size_t length) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (length == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t i = length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (i > 0 && Is_Trail_Byte(str[i - 1])) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| --i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (i == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t claimed = Utf8_Num_Bytes(str[i - 1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t actual = length - (i - 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (claimed == 0 || claimed != actual) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return actual; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool Utf8_Validate(const char* str) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Utf8_Validate(str, strlen(str)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool Utf8_Validate(const char* str, size_t length) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const unsigned char* s = (const unsigned char*)str; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t i = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (i < length) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t bytes = Utf8_Num_Bytes(str[i]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bytes == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (i + bytes > length) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t j = 1; j < bytes; ++j) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!Is_Trail_Byte(str[i + j])) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Reject overlong encodings per RFC 3629 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bytes == 2 && s[i] < 0xC2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bytes == 3 && s[i] == 0xE0 && s[i + 1] < 0xA0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bytes == 4 && s[i] == 0xF0 && s[i + 1] < 0x90) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Reject codepoints above U+10FFFF | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bytes == 4 && s[i] > 0xF4) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (bytes == 4 && s[i] == 0xF4 && s[i + 1] > 0x8F) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+84
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Add the missing guard alongside the existing 3-byte check:
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: Core/Libraries/Source/WWVegas/WWLib/utf8.cpp
Line: 84-94
Comment:
**Missing surrogate rejection breaks RFC 3629 compliance**
`Utf8_Validate` accepts UTF-16 surrogate codepoints (U+D800–U+DFFF) encoded as `\xED\xA0\x80`–`\xED\xBF\xBF`. The `\xED` lead byte only triggers the 3-byte overlong check for `s[i] == 0xE0`, not for `0xED`, so every surrogate sequence passes. RFC 3629 §4 defines the valid range for 3-byte sequences as `%xED %x80-9F UTF8-tail` — second byte `0xA0–0xBF` is explicitly excluded. Since `Utf8_Validate` is called to gate player-name input, malformed surrogate sequences will slip through.
Add the missing guard alongside the existing 3-byte check:
```suggestion
// Reject overlong encodings per RFC 3629
if (bytes == 2 && s[i] < 0xC2)
return false;
if (bytes == 3 && s[i] == 0xE0 && s[i + 1] < 0xA0)
return false;
if (bytes == 3 && s[i] == 0xED && s[i + 1] > 0x9F)
return false;
if (bytes == 4 && s[i] == 0xF0 && s[i + 1] < 0x90)
return false;
// Reject codepoints above U+10FFFF
if (bytes == 4 && s[i] > 0xF4)
return false;
if (bytes == 4 && s[i] == 0xF4 && s[i + 1] > 0x8F)
return false;
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| i += bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
bobtista marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t Utf16Le_To_Utf8_Len(const wchar_t* src, size_t srcLen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| int bytes = WideCharToMultiByte(CP_UTF8, 0, src, (int)srcLen, nullptr, 0, nullptr, nullptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (bytes > 0) ? (size_t)bytes : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t Utf8_To_Utf16Le_Len(const char* src, size_t srcLen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| int wchars = MultiByteToWideChar(CP_UTF8, 0, src, (int)srcLen, nullptr, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (wchars > 0) ? (size_t)wchars : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t Utf16Le_To_Utf8(char* dest, size_t destLen, const wchar_t* src, size_t srcLen, bool writeDirect) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!writeDirect) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const size_t required = Utf16Le_To_Utf8_Len(src, srcLen); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (required == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (destLen > 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dest[0] = '\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (required > destLen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (destLen > 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dest[0] = '\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return required; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const int written = WideCharToMultiByte(CP_UTF8, 0, src, (int)srcLen, dest, (int)destLen, nullptr, nullptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (written <= 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (destLen > 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dest[0] = '\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((size_t)written < destLen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dest[written] = '\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (size_t)written; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t Utf8_To_Utf16Le(wchar_t* dest, size_t destLen, const char* src, size_t srcLen, bool writeDirect) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!writeDirect) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const size_t required = Utf8_To_Utf16Le_Len(src, srcLen); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (required == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (destLen > 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dest[0] = L'\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (required > destLen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (destLen > 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dest[0] = L'\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return required; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const int written = MultiByteToWideChar(CP_UTF8, 0, src, (int)srcLen, dest, (int)destLen); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (written <= 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (destLen > 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dest[0] = L'\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ((size_t)written < destLen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| dest[written] = L'\0'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (size_t)written; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| #error "Not implemented" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utf8.cppunconditionally listed outside theif(WIN32)blockutf8.cpp's entire implementation is wrapped in#ifdef _WIN32 … #else #error "Not implemented" #endif, and the callers (AsciiString.cpp,UnicodeString.cpp,ThreadUtils.cpp) now unconditionally includeutf8.hand call its functions. Any non-Windows build will hit the#errorimmediately. Other Win32-specific sources (registry.cpp,verchk.cpp, etc.) are correctly placed inside theif(WIN32)block below —utf8.cppshould follow the same pattern.A proper cross-platform implementation (or
#ifdefguards in the callers) would be needed before removing the#error.Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to change this - cross-platform implementation is a todo and the #error is intentional as a placeholder