I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 81dd267e authored by zzz's avatar zzz
Browse files

throw IOE if uncompressed data too big, instead of silently truncating

parent bda6d781
No related branches found
No related tags found
No related merge requests found
...@@ -1298,16 +1298,30 @@ public class DataHelper { ...@@ -1298,16 +1298,30 @@ public class DataHelper {
private static final int MAX_UNCOMPRESSED = 40*1024; private static final int MAX_UNCOMPRESSED = 40*1024;
public static final int MAX_COMPRESSION = Deflater.BEST_COMPRESSION; public static final int MAX_COMPRESSION = Deflater.BEST_COMPRESSION;
public static final int NO_COMPRESSION = Deflater.NO_COMPRESSION; public static final int NO_COMPRESSION = Deflater.NO_COMPRESSION;
/** compress the data and return a new GZIP compressed array */
/**
* Compress the data and return a new GZIP compressed byte array.
* @throws IllegalArgumentException if size is over 40KB
*/
public static byte[] compress(byte orig[]) { public static byte[] compress(byte orig[]) {
return compress(orig, 0, orig.length); return compress(orig, 0, orig.length);
} }
/**
* Compress the data and return a new GZIP compressed byte array.
* @throws IllegalArgumentException if size is over 40KB
*/
public static byte[] compress(byte orig[], int offset, int size) { public static byte[] compress(byte orig[], int offset, int size) {
return compress(orig, offset, size, MAX_COMPRESSION); return compress(orig, offset, size, MAX_COMPRESSION);
} }
/**
* Compress the data and return a new GZIP compressed byte array.
* @throws IllegalArgumentException if size is over 40KB
*/
public static byte[] compress(byte orig[], int offset, int size, int level) { public static byte[] compress(byte orig[], int offset, int size, int level) {
if ((orig == null) || (orig.length <= 0)) return orig; if ((orig == null) || (orig.length <= 0)) return orig;
if (size >= MAX_UNCOMPRESSED) if (size > MAX_UNCOMPRESSED)
throw new IllegalArgumentException("tell jrandom size=" + size); throw new IllegalArgumentException("tell jrandom size=" + size);
ReusableGZIPOutputStream out = ReusableGZIPOutputStream.acquire(); ReusableGZIPOutputStream out = ReusableGZIPOutputStream.acquire();
out.setLevel(level); out.setLevel(level);
...@@ -1335,10 +1349,18 @@ public class DataHelper { ...@@ -1335,10 +1349,18 @@ public class DataHelper {
} }
/** decompress the GZIP compressed data (returning null on error) */ /**
* Decompress the GZIP compressed data (returning null on error).
* @throws IOE if uncompressed is over 40 KB
*/
public static byte[] decompress(byte orig[]) throws IOException { public static byte[] decompress(byte orig[]) throws IOException {
return (orig != null ? decompress(orig, 0, orig.length) : null); return (orig != null ? decompress(orig, 0, orig.length) : null);
} }
/**
* Decompress the GZIP compressed data (returning null on error).
* @throws IOE if uncompressed is over 40 KB
*/
public static byte[] decompress(byte orig[], int offset, int length) throws IOException { public static byte[] decompress(byte orig[], int offset, int length) throws IOException {
if ((orig == null) || (orig.length <= 0)) return orig; if ((orig == null) || (orig.length <= 0)) return orig;
...@@ -1354,6 +1376,11 @@ public class DataHelper { ...@@ -1354,6 +1376,11 @@ public class DataHelper {
if (read == -1) if (read == -1)
break; break;
written += read; written += read;
if (written >= MAX_UNCOMPRESSED) {
if (in.available() > 0)
throw new IOException("Uncompressed data larger than " + MAX_UNCOMPRESSED);
break;
}
} }
byte rv[] = new byte[written]; byte rv[] = new byte[written];
System.arraycopy(outBuf.getData(), 0, rv, 0, written); System.arraycopy(outBuf.getData(), 0, rv, 0, written);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment