Skip to content

Commit 1bfb443

Browse files
committed
Add sector offset/length helper methods in ChunkRegionHeader
1 parent 553cc5a commit 1bfb443

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/main/java/org/glavo/nbt/chunk/ChunkRegion.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static ChunkRegion readRegion(RawDataReader rawReader) throws IOException {
4545
assert rawReader.position() == fileStart + 2 * ChunkUtils.SECTOR_BYTES;
4646

4747
for (int localIndex : header.localIndexesSortedByOffset) {
48-
long sectorStart = fileStart + (long) header.getSectorOffset(localIndex) * ChunkUtils.SECTOR_BYTES;
48+
long sectorStart = fileStart + header.getSectorOffsetBytes(localIndex);
4949
long position = rawReader.position();
5050
if (position != sectorStart) {
5151
if (position < sectorStart) {
@@ -62,6 +62,10 @@ static ChunkRegion readRegion(RawDataReader rawReader) throws IOException {
6262
throw new IOException("Invalid chunk data length: " + chunkRawLength);
6363
}
6464

65+
if (chunkRawLength + 4L > header.getSectorLengthBytes(localIndex)) {
66+
throw new IOException("Invalid chunk data length: " + chunkRawLength + " (expected <= " + header.getSectorLengthBytes(localIndex) + " - 4)");
67+
}
68+
6569
long chunkRawContentLength = chunkRawLength - 1L;
6670

6771
int compressType = rawReader.readUnsignedByte();

src/main/java/org/glavo/nbt/internal/ChunkRegionHeader.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.stream.IntStream;
2626

2727
import static org.glavo.nbt.internal.ChunkUtils.CHUNKS_PRE_REGION;
28+
import static org.glavo.nbt.internal.ChunkUtils.SECTOR_BYTES;
2829

2930
public final class ChunkRegionHeader {
3031
public static ChunkRegionHeader readHeader(DataReader reader) throws IOException {
@@ -60,4 +61,12 @@ public int getSectorOffset(int index) {
6061
public int getSectorLength(int index) {
6162
return ChunkUtils.getSectorLength(sectorInfo[index]);
6263
}
64+
65+
public long getSectorOffsetBytes(int index) {
66+
return (long) getSectorOffset(index) * SECTOR_BYTES;
67+
}
68+
69+
public long getSectorLengthBytes(int index) {
70+
return (long) getSectorLength(index) * SECTOR_BYTES;
71+
}
6372
}

0 commit comments

Comments
 (0)