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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ API between minor releases.

## [Unreleased]

### Fixed
- Packed 16-bit and 32-bit integers in their full-width form are read little-endian. They were read with their bytes
reversed, so entity records that store a colour, extended data size or reactor count in that form were decoded from
the wrong bit position and failed or gave NaN or absurd coordinates (#7).

## [0.4.0] - 2026-11-11

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/DwgIO/Internal/Container/BitReaderMsb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public uint ReadPackedInt()
uint tag = ReadTwoBits();
return tag switch
{
0 => (uint)ReadBits(32),
0 => ReadRawUInt32(),
1 => (uint)ReadBits(8),
_ => 0,
};
Expand All @@ -89,7 +89,7 @@ public uint ReadPackedShort()
uint tag = ReadTwoBits();
return tag switch
{
0 => (uint)ReadBits(16),
0 => ReadRawByte() | (uint)ReadRawByte() << 8, // operands run left to right: low byte first
1 => (uint)ReadBits(8),
2 => 0,
_ => 256,
Expand Down
46 changes: 46 additions & 0 deletions tests/DwgIO.Tests/BitReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@ public void ReadRawUInt32_LittleEndian()
Assert.Equal(0x12345678u, reader.ReadRawUInt32());
}

[Fact]
public void ReadPackedShort_FullWidth_IsLittleEndian()
{
// 6 padding bits, then tag 00 and the bytes 0x34 0x12.
var reader = new BitReaderMsb([0x00, 0x34, 0x12]);
reader.ReadBits(6);
Assert.Equal(0x1234u, reader.ReadPackedShort());
}

[Fact]
public void ReadPackedInt_FullWidth_IsLittleEndian()
{
var reader = new BitReaderMsb([0x00, 0x78, 0x56, 0x34, 0x12]);
reader.ReadBits(6);
Assert.Equal(0x12345678u, reader.ReadPackedInt());
}

[Fact]
public void ReadPackedShort_FullWidth_Unaligned()
{
var reader = new BitReaderMsb([0x0D, 0x04, 0x80]);
Assert.Equal(0x1234u, reader.ReadPackedShort());
}

[Fact]
public void ReadPackedShortAndInt_ShortForms_AreUnchanged()
{
// Tags 01 (one byte 0xAB), 10 and 11.
var shorts = new BitReaderMsb([0x6A, 0xEC]);
Assert.Equal(0xABu, shorts.ReadPackedShort());
Assert.Equal(0u, shorts.ReadPackedShort());
Assert.Equal(256u, shorts.ReadPackedShort());

var ints = new BitReaderMsb([0x6A, 0xEC]);
Assert.Equal(0xABu, ints.ReadPackedInt());
Assert.Equal(0u, ints.ReadPackedInt());
Assert.Equal(0u, ints.ReadPackedInt());
}

[Fact]
public void ReadObjectType_BeforeR2010_FullWidthIsLittleEndian()
{
var reader = new BitReaderMsb([0x3D, 0x00, 0x40]);
Assert.Equal(0x1F4, reader.ReadObjectType(r2010Plus: false));
}

[Fact]
public void ReadVarUInt_MultiByteContinuation()
{
Expand Down
Loading