forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 230
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
Open
bobtista
wants to merge
20
commits into
TheSuperHackers:main
Choose a base branch
from
bobtista:bobtista/feat/utf8-string-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
40393b8
feat(utf8): add UTF-8 string conversion and validation functions
bobtista abb71f0
refactor(utf8): Return size_t from conversions, use consistent len na…
bobtista 0c9074d
refactor(utf8): Update callers to use new conversion API
bobtista 149a07f
refactor(utf8): rename to Utf16Le_To_Utf8 and return required size on…
bobtista 6097799
refactor(utf8): add writeDirect mode, use _Len helpers, const locals,…
bobtista 9078de5
refactor(utf8): simplify conversion API and reject UTF-16 surrogates
bobtista 327bb4b
style(utf8): assert after write, add braces, const locals
bobtista 4d5d2dc
style(utf8): Use >= 0 in length return ternaries
bobtista f582ac8
refactor(utf8): remove unused validators and simplify conversion fail…
bobtista 40683de
style(string): add braces to translate conversion check
bobtista 44f8fca
fix(string): return empty string when ThreadUtils conversion fails
bobtista fc3add5
refactor(string): drop unrelated ensureUniqueBufferOfSize null-termin…
bobtista ce8a2f4
refactor(utf8): replace Win32 wrappers with portable RFC 3629 transcoder
bobtista d4a7e73
fix(string): fall back to 1:1 byte cast for non-UTF-8 data in Unicode…
bobtista e0a425e
fix(utf8): Substitute U+FFFD for wide values with no UTF-8 representa…
bobtista 159da01
refactor(utf8): Return UTF8_INVALID instead of 0 for malformed UTF-8
bobtista 51a0460
fix(gamespy): Fall back to byte cast for non-UTF-8 text in MultiByteT…
bobtista 1a58d95
refactor(utf8): Rename conversions to Wide_To_Utf8 and Utf8_To_Wide
bobtista 81a47bf
refactor(utf8): Remove unused Utf8_Validate
bobtista d98492c
build: Use qualified include path for WWLib utf8.h
bobtista File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| ** 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; | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
|
||
| i += bytes; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| size_t Get_Utf8_Size(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 Get_Unicode_Size(const char* src, size_t srcLen) | ||
| { | ||
| int wchars = MultiByteToWideChar(CP_UTF8, 0, src, (int)srcLen, nullptr, 0); | ||
|
bobtista marked this conversation as resolved.
Outdated
|
||
| return (wchars > 0) ? (size_t)wchars : 0; | ||
|
bobtista marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| bool Unicode_To_Utf8(char* dest, const wchar_t* src, size_t srcLen, size_t destSize) | ||
| { | ||
| if (destSize == 0) | ||
| return false; | ||
| int result = WideCharToMultiByte(CP_UTF8, 0, src, (int)srcLen, dest, (int)destSize, nullptr, nullptr); | ||
| if (result == 0) | ||
| dest[0] = '\0'; | ||
| return result != 0; | ||
| } | ||
|
|
||
| bool Utf8_To_Unicode(wchar_t* dest, const char* src, size_t srcLen, size_t destSize) | ||
| { | ||
| if (destSize == 0) | ||
| return false; | ||
| int result = MultiByteToWideChar(CP_UTF8, 0, src, (int)srcLen, dest, (int)destSize); | ||
| if (result == 0) | ||
| dest[0] = L'\0'; | ||
| return result != 0; | ||
| } | ||
|
|
||
| #else | ||
| #error "Not implemented" | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| ** 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/>. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <stddef.h> | ||
| #include <wchar.h> | ||
|
|
||
| // Returns the number of bytes in a UTF-8 character based on its lead byte. | ||
| // Returns 0 if the lead byte is invalid. | ||
| size_t Utf8_Num_Bytes(char lead); | ||
|
|
||
| // Returns the number of invalid bytes at the end of the string due to an | ||
| // incomplete multi-byte sequence. Returns 0 if the string ends on a complete sequence. | ||
| size_t Utf8_Trailing_Invalid_Bytes(const char* str, size_t length); | ||
|
|
||
| // Returns true if the null-terminated string is valid UTF-8, false otherwise. | ||
| bool Utf8_Validate(const char* str); | ||
| bool Utf8_Validate(const char* str, size_t length); | ||
|
|
||
| // Returns the number of bytes in the UTF-8 representation of srcLen wide characters | ||
| // from src. Returns 0 on failure or if srcLen is 0. | ||
| size_t Get_Utf8_Size(const wchar_t* src, size_t srcLen); | ||
|
|
||
| // Returns the number of wchar_t elements in the wide character representation of | ||
| // srcLen bytes from the UTF-8 string src. Returns 0 on failure or if srcLen is 0. | ||
| size_t Get_Unicode_Size(const char* src, size_t srcLen); | ||
|
|
||
| // Converts srcLen wide characters from src to UTF-8. destSize is in bytes. | ||
| // Does not write a null terminator. Caller must allocate destSize + 1 and | ||
| // write the terminator if one is needed. Returns true on success, false on failure. | ||
| // On failure, dest[0] is set to '\0'. | ||
| bool Unicode_To_Utf8(char* dest, const wchar_t* src, size_t srcLen, size_t destSize); | ||
|
|
||
| // Converts srcLen bytes from the UTF-8 string src to wide characters. destSize is in wchar_t elements. | ||
| // Does not write a null terminator. Caller must allocate destSize + 1 and | ||
| // write the terminator if one is needed. Returns true on success, false on failure. | ||
| // On failure, dest[0] is set to L'\0'. | ||
| bool Utf8_To_Unicode(wchar_t* dest, const char* src, size_t srcLen, size_t destSize); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
So is this translating UTF16LE from windows into UTF8 that is then stored within AsciiString?
If so this may help with the paths issue with usernames and paths not using Latin characters, but file handling functions will need updating to use unicode variants instead of Ascii.
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.
Yes. It does not fix path handling on its own though, the file APIs still take the narrow string. Should that be a separate change?