Repository navigation
rust: Port the Windows branch of util/utf8.c to obs-util - #267
Merged
Merged
Conversation
test_utf8 skips MSVC, so the MultiByteToWideChar/WideCharToMultiByte branch had no test. test_utf8_windows covers it: - round trips through a surrogate pair; no terminator is written, and insize 0 means up to the NUL while a NUL inside insize is converted - a leading UTF-8 BOM is dropped (only from 3 bytes), whatever the flags; a BOM alone leaves no input (0); wide BOMs are kept - invalid UTF-8 and lone surrogates become U+FFFD, also with UTF8_IGNORE_ERROR (the flags are ignored) - a too-small buffer gives 0; a buffer of size 0 asks for the size; a NULL buffer with a size gives 0; empty input gives 0 All cases pass against the current C (MSVC). Co-Authored-By: Claude Opus 5.5 <[email protected]>
- obs-util: ffi::utf8 is ffi/utf8_windows.rs on Windows (16-bit wchar_t) over a new utf8::windows core, todo!() here. The shims handle the two pointer cases the API decides by itself: a null input (0) and a null output with a size (0, ERROR_INVALID_PARAMETER); a buffer of size 0 goes to the API, which then returns the size. - Tier 1 (tests/utf8_windows.rs) mirrors test_utf8_windows.c; Tier 3 (tests/utf8_windows_parity.rs) compares return values and whole output buffers with the oracle (utf8.c's Windows branch), for random UTF-8 (with and without a leading BOM) and UTF-16 (paired and lone surrogates), every output size from 0 to past the need, NUL-terminated input, and the null cases. - libobs leaves util/utf8.c out with ENABLE_RUST_LIBOBS on Windows too; the shims stay hidden (rust-hidden.txt). Co-Authored-By: Claude Opus 5.5 <[email protected]>
obs_util::utf8::windows: utf8_to_wchar and wchar_to_utf8 call MultiByteToWideChar / WideCharToMultiByte with CP_UTF8, no flags and the C's arguments, so invalid input is replaced (U+FFFD) and a too-small buffer fails exactly as in C. utf8_to_wchar drops a leading UTF-8 BOM from 3 bytes up, and the flags are ignored, as in C. kernel32 is declared directly, like pipe::windows, not through windows-sys. Intentional difference: C casts the sizes to int; a length above i32::MAX gives 0 instead of a negative size. Mutation check: keeping the BOM fails bom_dropped and utf8_to_wchar_matches_c_oracle; passing MB_ERR_INVALID_CHARS fails invalid_input_is_replaced, bom_dropped and utf8_to_wchar_matches_c_oracle. Co-Authored-By: Claude Opus 5.5 <[email protected]>
6 of 7 tasks
chriscoveries
pushed a commit
to chriscoveries/obsrust-fork
that referenced
this pull request
Oct 21, 2026
…rsions.c (obs-rust#1610) Behavior-neutral extraction before the Rust port: os_mbs_to_wcs, os_utf8_to_wcs, os_wcs_to_mbs, os_wcs_to_utf8, their _ptr forms, os_utf8_to_mbs_ptr and os_mbs_to_utf8_ptr move verbatim from libobs/util/platform.c.h into libobs/util/platform-conversions.c.h. They were all that was left in platform.c.h after slices 2 to 6, so the file is renamed unchanged (100% similarity). It stays in the libobs.platform unity group and precompiled header, and the oracle compiles it under the new name. No host conditionals, no signature or header change. test_platform_conversions characterizes every function on its own: ASCII, 2-, 3- and 4-byte UTF-8 (a surrogate pair where wchar_t is 16 bits), invalid UTF-8 and lone surrogates, len 0 against an explicit len, the NULL-dst size query, dst_size 0 and 1, an exact and a too-small dst, empty and NULL input, the _ptr allocation and free (bnum_allocs), and the mbs functions in the C locale and a UTF-8 locale, set by the test. Characterized, not endorsed: the mbs functions ignore len; a dst too small for the UTF-8 conversions gives an empty string where the mbs ones cut the text; off Windows the size query accepts an encoded surrogate that the conversion then refuses, and the decoder accepts overlong 3-byte forms, values above U+10FFFF and 5- and 6-byte sequences. Windows replaces invalid input with U+FFFD and drops a leading UTF-8 BOM. Earlier slice 1 work is b7216309-jpg's: obs-rust#263 fixed the mbs conversions' (size_t)-1 and too-small-buffer cases, which the test pins, and obs-rust#267 ported the Windows branch of util/utf8.c that the UTF-8 conversions call. Refs obs-rust#262
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part of slice 1 (conversions) of #262.
os_utf8_to_wcsand friends callutf8_to_wchar/wchar_to_utf8, which only had a Rust port off Windows. With this PR,utf8.cis swapped on every OS.Commits
C characterization:
test/cmocka/test_utf8_windows.c(tests.d, MSVC only, sincetest_utf8skips MSVC). It's built withutf8.c, astest_utf8is, and covers:insize == 0means up to the NUL, while a NUL insideinsizeis converted;UTF8_IGNORE_ERRORincluded (flags are ignored);All of these pass against the current C.
RED:
ffi::utf8isffi/utf8_windows.rson Windows (16-bitwchar_t), over autf8::windowscore that istodo!()in this commit.tests/utf8_windows.rs) and Tier 3 (tests/utf8_windows_parity.rs) are added.util/utf8.cout withENABLE_RUST_LIBOBSon Windows too.rust-hidden.txt), like the C original.GREEN: the core.
Design
The core calls
MultiByteToWideChar/WideCharToMultiBytewithCP_UTF8, no flags and the C's arguments, rather than reimplementing UTF-8 decoding. So invalid input is replaced (U+FFFD) and a too-small buffer fails (0, possibly with part of it written) exactly as in C.kernel32is declared directly, likepipe::windowssince #261, to keepwindows-sys's import records out ofobs.dll.The shim handles the two pointer cases the API decides by itself, both probed on Windows 11:
ERROR_INVALID_PARAMETER).A real buffer of size 0 is passed through, and the API returns the size.
Intentional difference: C casts the sizes to
int. A length abovei32::MAXgives 0 instead of reaching the API as a negative size.Testing (Windows 11, MSVC)
test_utf8_windows.c: 5/5 againstutf8.c, and 5/5 linked againstlibobs_rust.libwith noutf8.c.bom_droppedandutf8_to_wchar_matches_c_oracle. PassingMB_ERR_INVALID_CHARSfailsinvalid_input_is_replaced,bom_droppedandutf8_to_wchar_matches_c_oracle./WHOLEARCHIVE:libobs_rust.lib(asobs.dllis) links with noLNK2005.cargo test --workspaceand clippy-D warningspass on Windows. Clippy is also clean for x86_64 Linux and aarch64 macOS, and the full workspace tests and clippy pass on Ubuntu 26.04.🤖 Generated with Claude Code