Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunked archive readers for large files #127

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix boundary issues
  • Loading branch information
yretenai committed Feb 9, 2024
commit fcf259f20d12128e5aa90aa9a82582b3c599d075
4 changes: 2 additions & 2 deletions CUE4Parse/UE4/Pak/PakFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,11 @@ public Span<byte> Read(FPakEntry pakEntry, long offset, long size)
// Calculate the uncompressed size,
// its either just the compression block size
// or if its the last block its the remaining data size
var uncompressedSize = (int)Math.Min(pakEntry.CompressionBlockSize, size - uncompressedOff);
var uncompressedSize = (int)Math.Min(pakEntry.CompressionBlockSize, pakEntry.UncompressedSize - uncompressedOff);
Decompress(compressed, 0, blockSize, uncompressed, uncompressedOff, uncompressedSize, pakEntry.CompressionMethod);
uncompressedOff += (int)pakEntry.CompressionBlockSize;

if (uncompressedOff >= uncompressed.Length)
if (uncompressedOff >= size)
break;
}

Expand Down
10 changes: 8 additions & 2 deletions CUE4Parse/UE4/Readers/FChunkedArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ public override int Read(byte[] buffer, int offset, int count)
if (Position.Align(BUFFER_SIZE) != Position)
BufferOffset = Position.Align(BUFFER_SIZE) - BUFFER_SIZE;

ReadChunks(BufferOffset, blockSize).CopyTo(Buffer);
if ((int) (Position - BufferOffset) + n <= BUFFER_SIZE) //overflow check
ReadChunks(BufferOffset, blockSize).CopyTo(Buffer);
else
BufferOffset = -1; // reset buffer position because we didn't actually read
}

data = Buffer.AsSpan().Slice((int)(Position - BufferOffset), n);
if (BufferOffset == -1 || (int) (Position - BufferOffset) + n > BUFFER_SIZE)
data = ReadChunks(Position, n);
else
data = Buffer.AsSpan().Slice((int) (Position - BufferOffset), n);
}
else
{
Expand Down