Add in more error detection.
This commit is contained in:
parent
eb251c32ea
commit
7be01e2217
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue