From 5e6611766e614f1c388ed0dacb7a9c0cc8e770b7 Mon Sep 17 00:00:00 2001 From: mahach666 <79989386+mahach666@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:28:03 +0300 Subject: [PATCH] Report the HTML binary payload size limit --- CHANGELOG.md | 1 + src/IOViewer/Exporters/BinaryBlobBuilder.cs | 47 +++++++++++++----- src/IOViewer/Exporters/ViewerPayloadWriter.cs | 11 ++++- .../IOViewer.Tests/BinaryBlobBuilderTests.cs | 49 +++++++++++++++++++ 4 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 tests/IOViewer.Tests/BinaryBlobBuilderTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0992959..0f0ba22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ API between minor releases. ### Fixed - Preserve all 64 bits of element IDs in HTML properties, Copy Id, clash results and 4D tasks. +- Report the required byte count and the limit when an HTML export exceeds its binary buffer capacity. ## [0.3.1] - 2026-11-13 diff --git a/src/IOViewer/Exporters/BinaryBlobBuilder.cs b/src/IOViewer/Exporters/BinaryBlobBuilder.cs index 82aabaa..c88c4b0 100644 --- a/src/IOViewer/Exporters/BinaryBlobBuilder.cs +++ b/src/IOViewer/Exporters/BinaryBlobBuilder.cs @@ -1,5 +1,6 @@ using System.Buffers; using System.Buffers.Text; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text.Json; @@ -33,15 +34,15 @@ public BinaryBlobBuilder() } } - public BlobView Add(ReadOnlySpan values) => Add(MemoryMarshal.AsBytes(values), values.Length, "f32"); + public BlobView Add(ReadOnlySpan values) => Add(values, "f32"); - public BlobView Add(ReadOnlySpan values) => Add(MemoryMarshal.AsBytes(values), values.Length, "u16"); + public BlobView Add(ReadOnlySpan values) => Add(values, "u16"); - public BlobView Add(ReadOnlySpan values) => Add(MemoryMarshal.AsBytes(values), values.Length, "i16"); + public BlobView Add(ReadOnlySpan values) => Add(values, "i16"); - public BlobView Add(ReadOnlySpan values) => Add(MemoryMarshal.AsBytes(values), values.Length, "u32"); + public BlobView Add(ReadOnlySpan values) => Add(values, "u32"); - public BlobView Add(ReadOnlySpan values) => Add(MemoryMarshal.AsBytes(values), values.Length, "i32"); + public BlobView Add(ReadOnlySpan values) => Add(values, "i32"); /// Returns the bytes of a previously added array. public ReadOnlySpan Bytes(BlobView view) @@ -72,13 +73,35 @@ public void WriteBase64(Stream output) } } - private BlobView Add(ReadOnlySpan bytes, int count, string type) + private BlobView Add(ReadOnlySpan values, string type) where T : unmanaged { - int padding = (4 - _buffer.WrittenCount % 4) % 4; - _buffer.GetSpan(padding)[..padding].Clear(); - _buffer.Advance(padding); - var view = new BlobView(_buffer.WrittenCount, count, type); - _buffer.Write(bytes); - return view; + int written = _buffer.WrittenCount; + var (offset, end) = CheckAppend(written, values.Length, Unsafe.SizeOf()); + int added = end - written; + if (added > 0) + { + int padding = offset - written; + var target = _buffer.GetSpan(added); + target[..padding].Clear(); + MemoryMarshal.AsBytes(values).CopyTo(target[padding..]); + _buffer.Advance(added); + } + return new BlobView(offset, values.Length, type); + } + + internal static (int Offset, int End) CheckAppend(int written, int count, int elementSize) + { + long offset = (written + 3L) & ~3L; + int end = CheckByteCount(offset + (long)count * elementSize, Array.MaxLength); + return ((int)offset, end); + } + + internal static int CheckByteCount(long required, int limit) + { + if (required > limit) + { + throw new InvalidDataException($"The HTML viewer binary buffer requires {required} bytes, exceeding the limit of {limit} bytes."); + } + return (int)required; } } diff --git a/src/IOViewer/Exporters/ViewerPayloadWriter.cs b/src/IOViewer/Exporters/ViewerPayloadWriter.cs index 31fbc43..e9cacb0 100644 --- a/src/IOViewer/Exporters/ViewerPayloadWriter.cs +++ b/src/IOViewer/Exporters/ViewerPayloadWriter.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; @@ -414,8 +415,14 @@ private static bool SameBytes(T[]? a, T[]? b) where T : struct => private bool Same(T[]? values, BlobView? view) where T : struct => values is null ? view is null : view is { } v && Bytes(values).SequenceEqual(_blob.Bytes(v)); - private static ReadOnlySpan Bytes(T[]? values) where T : struct => - values is null ? default : MemoryMarshal.AsBytes(values.AsSpan()); + private static ReadOnlySpan Bytes(T[]? values) where T : struct + { + if (values is null) return default; + + // Hashing needs a byte span before the geometry can be deduplicated and appended. + BinaryBlobBuilder.CheckByteCount((long)values.Length * Unsafe.SizeOf(), int.MaxValue); + return MemoryMarshal.AsBytes(values.AsSpan()); + } // The instance colour overrides the material colour; the alpha of the colour drawn is the only opacity, and the // texture is the material's. diff --git a/tests/IOViewer.Tests/BinaryBlobBuilderTests.cs b/tests/IOViewer.Tests/BinaryBlobBuilderTests.cs new file mode 100644 index 0000000..f3dc619 --- /dev/null +++ b/tests/IOViewer.Tests/BinaryBlobBuilderTests.cs @@ -0,0 +1,49 @@ +using System.Text; +using IOViewer.Exporters; +using Xunit; + +namespace IOViewer.Tests; + +public class BinaryBlobBuilderTests +{ + [Fact] + public void CheckAppend_IncludesAlignmentAtTheArrayLimit() + { + int lastAligned = Array.MaxLength & ~3; + Assert.Equal((lastAligned - 4, lastAligned), BinaryBlobBuilder.CheckAppend(lastAligned - 6, 1, sizeof(float))); + + var error = Assert.Throws(() => BinaryBlobBuilder.CheckAppend(lastAligned - 2, 1, sizeof(float))); + Assert.Equal($"The HTML viewer binary buffer requires {(long)lastAligned + 4} bytes, exceeding the limit of {Array.MaxLength} bytes.", error.Message); + } + + [Fact] + public void CheckAppend_ReportsTheSizeOfAnArrayThatExceedsInt32Bytes() + { + const long required = (long)int.MaxValue * sizeof(float); + var error = Assert.Throws(() => BinaryBlobBuilder.CheckAppend(0, int.MaxValue, sizeof(float))); + Assert.Equal($"The HTML viewer binary buffer requires {required} bytes, exceeding the limit of {Array.MaxLength} bytes.", error.Message); + } + + [Fact] + public void CheckByteCount_RejectsAByteSpanOverInt32MaxValue() + { + const long required = (long)int.MaxValue + 1; + Assert.Equal(int.MaxValue, BinaryBlobBuilder.CheckByteCount(int.MaxValue, int.MaxValue)); + var error = Assert.Throws(() => BinaryBlobBuilder.CheckByteCount(required, int.MaxValue)); + Assert.Equal($"The HTML viewer binary buffer requires {required} bytes, exceeding the limit of {int.MaxValue} bytes.", error.Message); + } + + [Fact] + public void Add_PreservesTypedOffsetsAndZeroPadding() + { + var blob = new BinaryBlobBuilder(); + Assert.Equal(new BlobView(0, 1, "i16"), blob.Add(new short[] { -1 })); + Assert.Equal(new BlobView(4, 0, "u16"), blob.Add(ReadOnlySpan.Empty)); + Assert.Equal(new BlobView(4, 1, "f32"), blob.Add(new float[] { 1 })); + + using var output = new MemoryStream(); + blob.WriteBase64(output); + byte[] expected = [255, 255, 0, 0, 0, 0, 128, 63]; + Assert.Equal(expected, Convert.FromBase64String(Encoding.ASCII.GetString(output.ToArray()))); + } +}