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
2 changes: 1 addition & 1 deletion docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ drop points that fail a plausibility rule (see the README), so a scan can yield
| COPC | The header count (the extended 1.4 count when it states one). It is not checked against the chunk data until read. |
| EPT | The point count declared in the manifest, or the sum of points across discovered octree nodes. |
| Leica PTG | Total valid returns across all gridded scans, determined by decoding the column validity bitmasks. |
| CC BIN | The point count declared for the entity. Points with non-finite coordinates are skipped. Packed point normals are decoded in file versions 41, 42 and 50. A stored zero direction is returned as `PclVector3.Zero`. |
| CC BIN | The point count declared for the entity. The reader rejects files with a truncated XYZ block or a point count that exceeds the available XYZ data. Points with non-finite coordinates are skipped. Packed point normals are decoded in file versions 41, 42 and 50. A stored zero direction is returned as `PclVector3.Zero`. |

`HasColors`, `HasIntensities` and `HasNormals` follow what the file contains, except for the scans of a ReCap project, where all three
are true when the `.rcs` file was found and false otherwise, without the file being opened. A ReCap scan reads the channels
Expand Down
9 changes: 6 additions & 3 deletions src/PclIO/Formats/CcBin/CcBinDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ private bool TryParsePointCloud(

// 43 bytes: flags (8), shift (24), scale (8), marker (3)
byte[] ccHead = new byte[43];
if (fs.Read(ccHead) < 43) return false;
// Confirm the XYZ array before treating an incomplete point block as a damaged scan.
if (fs.Read(ccHead) < ccHead.Length || ccHead[^1] != 3) return false;

double sx = BinaryPrimitives.ReadDoubleLittleEndian(ccHead.AsSpan(8, 8));
double sy = BinaryPrimitives.ReadDoubleLittleEndian(ccHead.AsSpan(16, 8));
Expand All @@ -213,13 +214,15 @@ private bool TryParsePointCloud(
throw FormatError.Corrupted("CC BIN has an invalid global shift or scale.");

byte[] countBuf = new byte[4];
if (fs.Read(countBuf) < 4) return false;
if (fs.Read(countBuf) < countBuf.Length)
throw FormatError.Truncated($"CC BIN point count for scan {scanIndex}");
uint count = BinaryPrimitives.ReadUInt32LittleEndian(countBuf);
if (count == 0) return false;

long ptStart = fs.Position;
long ptBytes = (long)count * 12;
if (ptStart + ptBytes > fileLength) return false;
if (ptBytes > fileLength - ptStart)
throw FormatError.Truncated($"CC BIN XYZ coordinates for scan {scanIndex} ({count} declared points)");

pointsEndOffset = ptStart + ptBytes;

Expand Down
22 changes: 22 additions & 0 deletions tests/PclIO.Tests/FormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6519,6 +6519,28 @@ public void Cc_Bin_Opens_Adjacent_Multi_Cloud_Entities()
Assert.Equal(2, doc.Scans[1].PointCount);
}

[Fact]
public void Cc_Bin_Rejects_Truncated_Coordinates_In_A_Later_Cloud()
{
using var dir = new TempDirectory();
byte[] bytes = SyntheticFixtures.CreateSyntheticCcBinMultiCloud(
[[new(1, 2, 3)], [new(4, 5, 6), new(7, 8, 9)]], truncateLastCloudPoints: true);

var error = Assert.Throws<InvalidDataException>(() => PclFile.Open(dir.Write("truncated_xyz.bin", bytes)));
Assert.Contains("XYZ coordinates", error.Message);
}

[Fact]
public void Cc_Bin_Rejects_An_Oversized_Point_Count_In_A_Later_Cloud()
{
using var dir = new TempDirectory();
byte[] bytes = SyntheticFixtures.CreateSyntheticCcBinMultiCloud(
[[new(1, 2, 3)], [new(4, 5, 6)]], declaredLastCloudPointCount: uint.MaxValue);

var error = Assert.Throws<InvalidDataException>(() => PclFile.Open(dir.Write("oversized_count.bin", bytes)));
Assert.Contains("XYZ coordinates", error.Message);
}

[Fact]
public void Cc_Bin_Reads_Intensity_Within_Declared_Count_Only()
{
Expand Down
14 changes: 10 additions & 4 deletions tests/PclIO.Tests/SyntheticFixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3000,7 +3000,9 @@ public static byte[] CreateSyntheticCcBin(
/// <summary>Creates a synthetic CC BIN file containing multiple adjacent cloud entities.</summary>
public static byte[] CreateSyntheticCcBinMultiCloud(
IReadOnlyList<IReadOnlyList<CcBinSamplePoint>> clouds,
uint version = 47)
uint version = 47,
bool truncateLastCloudPoints = false,
uint? declaredLastCloudPointCount = null)
{
using var ms = new MemoryStream();
ms.Write("CCB2"u8.ToArray());
Expand All @@ -3011,12 +3013,15 @@ public static byte[] CreateSyntheticCcBinMultiCloud(

for (int c = 0; c < clouds.Count; c++)
{
bool isLast = c == clouds.Count - 1;
WriteCcBinCloudEntity(
ms,
clouds[c],
name: $"Cloud_{c + 1}",
id: (uint)(100 + c),
version: version);
version: version,
truncatePoints: isLast && truncateLastCloudPoints,
declaredPointCount: isLast ? declaredLastCloudPointCount : null);
}

return ms.ToArray();
Expand All @@ -3040,7 +3045,8 @@ private static void WriteCcBinCloudEntity(
IReadOnlyDictionary<string, double>? doubleProperties = null,
bool truncatePoints = false,
bool truncateColorBlock = false,
bool truncateIntensityData = false)
bool truncateIntensityData = false,
uint? declaredPointCount = null)
{
Span<byte> u32Buf = stackalloc byte[4];

Expand Down Expand Up @@ -3108,7 +3114,7 @@ private static void WriteCcBinCloudEntity(
ms.WriteByte(0x03);

// 6. Point count (uint32 LE)
BinaryPrimitives.WriteUInt32LittleEndian(u32Buf, (uint)points.Count);
BinaryPrimitives.WriteUInt32LittleEndian(u32Buf, declaredPointCount ?? (uint)points.Count);
ms.Write(u32Buf);

// 7. Points data (12 bytes per point)
Expand Down
Loading