Skip to content

rust: Port the Windows branch of util/utf8.c to obs-util - #267

Merged
zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:port/utf8-windows
Oct 11, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:port/utf8-windows

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

Part of slice 1 (conversions) of #262. os_utf8_to_wcs and friends call utf8_to_wchar/wchar_to_utf8, which only had a Rust port off Windows. With this PR, utf8.c is swapped on every OS.

Commits

  1. C characterization: test/cmocka/test_utf8_windows.c (tests.d, MSVC only, since test_utf8 skips MSVC). It's built with utf8.c, as test_utf8 is, and covers:

    • round trips through a surrogate pair;
    • no terminator written; insize == 0 means up to the NUL, while a NUL inside insize is converted;
    • a leading UTF-8 BOM dropped (from 3 bytes up) whatever the flags; a lone BOM gives 0; wide BOMs kept;
    • invalid UTF-8 and lone surrogates becoming U+FFFD, UTF8_IGNORE_ERROR included (flags are ignored);
    • buffer sizes: a too-small buffer gives 0, size 0 asks for the size, NULL with a size gives 0, empty input gives 0.

    All of these pass against the current C.

  2. RED:

    • ffi::utf8 is ffi/utf8_windows.rs on Windows (16-bit wchar_t), over a utf8::windows core that is todo!() in this commit.
    • Tier 1 (tests/utf8_windows.rs) and Tier 3 (tests/utf8_windows_parity.rs) are added.
    • libobs leaves util/utf8.c out with ENABLE_RUST_LIBOBS on Windows too.
    • The shims stay hidden (rust-hidden.txt), like the C original.
  3. GREEN: the core.

Design

The core calls MultiByteToWideChar/WideCharToMultiByte with CP_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. kernel32 is declared directly, like pipe::windows since #261, to keep windows-sys's import records out of obs.dll.

The shim handles the two pointer cases the API decides by itself, both probed on Windows 11:

  • a null input gives 0;
  • a null output with a size gives 0 (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 above i32::MAX gives 0 instead of reaching the API as a negative size.

Testing (Windows 11, MSVC)

  • test_utf8_windows.c: 5/5 against utf8.c, and 5/5 linked against libobs_rust.lib with no utf8.c.
  • Rust tests: Tier 1 passes 5/5. Tier 3 passes 3/3, which covers 2,048 random cases per direction comparing return values and whole output buffers with sentinels, against the oracle (the real Windows branch).
  • 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.
  • Link check: a DLL linked with /WHOLEARCHIVE:libobs_rust.lib (as obs.dll is) links with no LNK2005.
  • Workspace and clippy: cargo test --workspace and clippy -D warnings pass 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

b7216309-jpg and others added 3 commits October 11, 2026 22:50
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]>
@b7216309-jpg
b7216309-jpg requested a review from zackees as a code owner October 11, 2026 20:54
@zackees
zackees merged commit 7ced9d1 into obs-rust:master Oct 11, 2026
22 of 23 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants