Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 35 additions & 12 deletions src/IOViewer/Exporters/BinaryBlobBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Buffers;
using System.Buffers.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json;

Expand Down Expand Up @@ -33,15 +34,15 @@ public BinaryBlobBuilder()
}
}

public BlobView Add(ReadOnlySpan<float> values) => Add(MemoryMarshal.AsBytes(values), values.Length, "f32");
public BlobView Add(ReadOnlySpan<float> values) => Add(values, "f32");

public BlobView Add(ReadOnlySpan<ushort> values) => Add(MemoryMarshal.AsBytes(values), values.Length, "u16");
public BlobView Add(ReadOnlySpan<ushort> values) => Add(values, "u16");

public BlobView Add(ReadOnlySpan<short> values) => Add(MemoryMarshal.AsBytes(values), values.Length, "i16");
public BlobView Add(ReadOnlySpan<short> values) => Add(values, "i16");

public BlobView Add(ReadOnlySpan<uint> values) => Add(MemoryMarshal.AsBytes(values), values.Length, "u32");
public BlobView Add(ReadOnlySpan<uint> values) => Add(values, "u32");

public BlobView Add(ReadOnlySpan<int> values) => Add(MemoryMarshal.AsBytes(values), values.Length, "i32");
public BlobView Add(ReadOnlySpan<int> values) => Add(values, "i32");

/// <summary>Returns the bytes of a previously added array.</summary>
public ReadOnlySpan<byte> Bytes(BlobView view)
Expand Down Expand Up @@ -72,13 +73,35 @@ public void WriteBase64(Stream output)
}
}

private BlobView Add(ReadOnlySpan<byte> bytes, int count, string type)
private BlobView Add<T>(ReadOnlySpan<T> 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<T>());
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;
}
}
11 changes: 9 additions & 2 deletions src/IOViewer/Exporters/ViewerPayloadWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -414,8 +415,14 @@ private static bool SameBytes<T>(T[]? a, T[]? b) where T : struct =>
private bool Same<T>(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<byte> Bytes<T>(T[]? values) where T : struct =>
values is null ? default : MemoryMarshal.AsBytes(values.AsSpan());
private static ReadOnlySpan<byte> Bytes<T>(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<T>(), 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.
Expand Down
49 changes: 49 additions & 0 deletions tests/IOViewer.Tests/BinaryBlobBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -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<InvalidDataException>(() => 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<InvalidDataException>(() => 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<InvalidDataException>(() => 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<ushort>.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())));
}
}
Loading