diff --git a/src.java/crosby/binary/file/FileBlock.java b/src.java/crosby/binary/file/FileBlock.java index 6be0616..b8fc198 100644 --- a/src.java/crosby/binary/file/FileBlock.java +++ b/src.java/crosby/binary/file/FileBlock.java @@ -28,14 +28,16 @@ public class FileBlock extends FileBlockBase { public static FileBlock newInstance(String type, ByteString blob, ByteString indexdata) { - return new FileBlock(type, blob, indexdata); + if (blob != null && blob.size() > MAX_BODY_SIZE/2) { + System.err.println("Warning: Fileblock has body size too large and may be considered corrupt"); + } + if (indexdata != null && indexdata.size() > MAX_HEADER_SIZE/2) { + System.err.println("Warning: Fileblock has indexdata too large and may be considered corrupt"); + } + return new FileBlock(type, blob, indexdata); } - public static FileBlock newInstance(String type, ByteString indexdata) { - return new FileBlock(type, null, indexdata); - } - - protected void deflateInto(crosby.binary.Fileformat.Blob.Builder blobbuilder) { + protected void deflateInto(crosby.binary.Fileformat.Blob.Builder blobbuilder) { int size = data.size(); Deflater deflater = new Deflater(); deflater.setInput(data.toByteArray()); @@ -77,7 +79,7 @@ public class FileBlock extends FileBlockBase { if (flags == CompressFlags.DEFLATE) deflateInto(blobbuilder); else - assert false : "TODO"; // TODO + throw new Error("Compression flag not understood"); } Fileformat.Blob blob = blobbuilder.build(); diff --git a/src.java/crosby/binary/file/FileBlockBase.java b/src.java/crosby/binary/file/FileBlockBase.java index 33644cc..4be4efa 100644 --- a/src.java/crosby/binary/file/FileBlockBase.java +++ b/src.java/crosby/binary/file/FileBlockBase.java @@ -13,6 +13,9 @@ import com.google.protobuf.ByteString; */ public class FileBlockBase { + static final int MAX_HEADER_SIZE = 64*1024; + static final int MAX_BODY_SIZE = 8*1024*1024; + protected FileBlockBase(String type, ByteString indexdata) { this.type = type; this.indexdata = indexdata; diff --git a/src.java/crosby/binary/file/FileBlockHead.java b/src.java/crosby/binary/file/FileBlockHead.java index 64204f7..6942b93 100644 --- a/src.java/crosby/binary/file/FileBlockHead.java +++ b/src.java/crosby/binary/file/FileBlockHead.java @@ -29,6 +29,10 @@ public class FileBlockHead extends FileBlockReference { DataInputStream datinput = new DataInputStream(input); int headersize = datinput.readInt(); // System.out.format("Header size %d %x\n",headersize,headersize); + if (headersize > MAX_HEADER_SIZE) { + throw new FileFormatException("Unexpectedly long header "+MAX_HEADER_SIZE+ " bytes. Possibly corrupt file."); + } + byte buf[] = new byte[headersize]; datinput.readFully(buf); // System.out.format("Read buffer for header of %d bytes\n",buf.length); @@ -38,7 +42,10 @@ public class FileBlockHead extends FileBlockReference { .getIndexdata()); fileblock.datasize = header.getDatasize(); - // data_offset = + if (header.getDatasize() > MAX_BODY_SIZE) { + throw new FileFormatException("Unexpectedly long body "+MAX_BODY_SIZE+ " bytes. Possibly corrupt file."); + } + fileblock.input = input; if (input instanceof FileInputStream) fileblock.data_offset = ((FileInputStream) input).getChannel() diff --git a/src.java/crosby/binary/file/FileBlockPosition.java b/src.java/crosby/binary/file/FileBlockPosition.java index 51a11a6..f563d16 100644 --- a/src.java/crosby/binary/file/FileBlockPosition.java +++ b/src.java/crosby/binary/file/FileBlockPosition.java @@ -27,7 +27,7 @@ public class FileBlockPosition extends FileBlockBase { /** Parse out and decompress the data part of a fileblock helper function. */ FileBlock parseData(byte buf[]) throws InvalidProtocolBufferException { - FileBlock out = FileBlock.newInstance(type, indexdata); + FileBlock out = FileBlock.newInstance(type, null, indexdata); Fileformat.Blob blob = Fileformat.Blob.parseFrom(buf); if (blob.hasRaw()) { out.data = blob.getRaw(); @@ -81,14 +81,12 @@ public class FileBlockPosition extends FileBlockBase { * stored. */ public ByteString serialize() { - assert false; // TODO - return null; + throw new Error("TODO"); } /** TODO: Parse a serialized representation of this block reference */ static FileBlockPosition parseFrom(ByteString b) { - assert false; // TODO - return null; + throw new Error("TODO"); } protected int datasize; diff --git a/src.java/crosby/binary/file/FileFormatException.java b/src.java/crosby/binary/file/FileFormatException.java new file mode 100644 index 0000000..178c958 --- /dev/null +++ b/src.java/crosby/binary/file/FileFormatException.java @@ -0,0 +1,16 @@ +package crosby.binary.file; + +import java.io.IOException; + +public class FileFormatException extends IOException { + + public FileFormatException(String string) { + super(string); + } + + /** + * + */ + private static final long serialVersionUID = -8128010128748910923L; + +}