Skip to content
Open
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
57 changes: 46 additions & 11 deletions src/XstReader.Api/LTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -828,21 +828,44 @@ private List<HNDataBlock> ReadHeapOnNode(UInt64 dataBid)
private byte[] GetBytesForHNID(List<HNDataBlock> blocks, BTree<Node> subNodeTree, HNID hnid)
{
byte[] buf = null;

if (hnid.hidType == EnidType.HID)
try
{
if (hnid.GetIndex(ndb.IsUnicode4K) != 0)
if (hnid.hidType == EnidType.HID)
{
var index = hnid.GetIndex(ndb.IsUnicode4K);

if (index > 0)
{
var blockIndex = hnid.HID.GetBlockIndex(ndb.IsUnicode4K);

// Validate block index
if (blockIndex >= 0 && blockIndex < blocks.Count)
{
var size = HidSize(blocks, hnid.HID);

if (size > 0)
{
buf = MapArray<byte>(blocks, hnid.HID, size);
}
}
}
}
else if (hnid.nidType == EnidType.LTP)
{
buf = MapArray<byte>(blocks, hnid.HID, HidSize(blocks, hnid.HID));
buf = ndb.ReadSubNodeDataBlock(subNodeTree, hnid.NID);
}
else
{
throw new XstException("Data storage style not implemented");
}
}
else if (hnid.nidType == EnidType.LTP)
catch (IndexOutOfRangeException)
{
buf = ndb.ReadSubNodeDataBlock(subNodeTree, hnid.NID);
// Corrupt heap allocation or bad HID reference
return null;
}
else
throw new XstException("Data storage style not implemented");


return buf;
}

Expand All @@ -851,9 +874,21 @@ private byte[] GetBytesForHNID(List<HNDataBlock> blocks, BTree<Node> subNodeTree
private int HidSize(List<HNDataBlock> blocks, HID hid)
{
var index = hid.GetIndex(ndb.IsUnicode4K);
if (index == 0) // Check for empty
if (index <= 0)
return 0;
var b = blocks[hid.GetBlockIndex(ndb.IsUnicode4K)];

var blockIndex = hid.GetBlockIndex(ndb.IsUnicode4K);

// Validate block index
if (blockIndex < 0 || blockIndex >= blocks.Count)
return 0;

var b = blocks[blockIndex];

// Validate allocation index
if (index >= b.rgibAlloc.Length || index - 1 < 0)
return 0;

return b.rgibAlloc[index] - b.rgibAlloc[index - 1];
}

Expand Down
15 changes: 9 additions & 6 deletions src/XstReader.Api/NDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,16 @@ private byte[] ReadAndDecompress(DataRef rb, out int read, byte[] buffer = null,
{
if (buffer == null)
buffer = new byte[rb.InflatedLength];

var writable = new MemoryStream(buffer, offset, rb.InflatedLength, true);
decompressionStream.CopyTo(writable);

if (writable.Position != writable.Length)
int curPos = 0;
int buffLen = buffer.Length;
int batchSize = buffLen > 65536 ? 16384 : 4096;
while (curPos < buffer.Length)
{
throw new EndOfStreamException();
int count = Math.Min(batchSize, buffLen - curPos);
int bytesRead = decompressionStream.Read(buffer, curPos, count);
if (bytesRead == 0)
throw new EndOfStreamException(); // restore safety;
curPos += bytesRead;
}
}
read = rb.InflatedLength;
Expand Down