cache: fix compression with encryption

This commit is contained in:
Adam
2017-12-16 18:24:00 -05:00
parent cba9866304
commit 21f2543059
2 changed files with 38 additions and 15 deletions

View File

@@ -24,6 +24,8 @@
*/
package net.runelite.cache.fs.jagex;
import static com.google.common.primitives.Bytes.concat;
import com.google.common.primitives.Ints;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
@@ -376,7 +378,6 @@ public class DataFile implements Closeable
res.data = data;
res.revision = revision;
res.crc = crc32.getHash();
int length = revision != -1 ? b.length - 2 : b.length;
res.compression = compression;
return res;
}
@@ -384,33 +385,31 @@ public class DataFile implements Closeable
public static byte[] compress(byte[] data, int compression, int revision, int[] keys) throws IOException
{
OutputStream stream = new OutputStream();
stream.writeByte(compression);
byte[] compressedData;
int length;
switch (compression)
{
case CompressionType.NONE:
compressedData = data;
compressedData = encrypt(compressedData, compressedData.length, keys);
stream.writeInt(data.length);
length = compressedData.length;
break;
case CompressionType.BZ2:
compressedData = BZip2.compress(data);
compressedData = encrypt(compressedData, compressedData.length, keys);
stream.writeInt(compressedData.length);
stream.writeInt(data.length);
compressedData = concat(Ints.toByteArray(data.length), BZip2.compress(data));
length = compressedData.length - 4;
break;
case CompressionType.GZ:
compressedData = GZip.compress(data);
compressedData = encrypt(compressedData, compressedData.length, keys);
stream.writeInt(compressedData.length);
stream.writeInt(data.length);
compressedData = concat(Ints.toByteArray(data.length), GZip.compress(data));
length = compressedData.length - 4;
break;
default:
throw new RuntimeException("Unknown compression type");
}
compressedData = encrypt(compressedData, compressedData.length, keys);
stream.writeByte(compression);
stream.writeInt(length);
stream.writeBytes(compressedData);
if (revision != -1)
{

View File

@@ -131,7 +131,7 @@ public class DataFileTest
}
@Test
public void testKeys() throws IOException
public void testEnc() throws IOException
{
File file = folder.newFile();
int[] keys = new int[]
@@ -153,4 +153,28 @@ public class DataFileTest
Assert.assertEquals(res.crc, res2.crc);
Assert.assertEquals(42, res2.revision);
}
@Test
public void testEncGz() throws IOException
{
File file = folder.newFile();
int[] keys = new int[]
{
4, 8, 15, 16
};
DataFile df = new DataFile(file);
byte[] compressedData = DataFile.compress("testtesttesttest1".getBytes(), CompressionType.GZ, 42, keys);
DataFileWriteResult res = df.write(42, 3, compressedData, 0);
compressedData = df.read(42, 3, res.sector, res.compressedLength);
DataFileReadResult res2 = DataFile.decompress(compressedData, keys);
byte[] buf = res2.data;
String str = new String(buf);
Assert.assertEquals("testtesttesttest1", str);
Assert.assertEquals(res.crc, res2.crc);
Assert.assertEquals(42, res2.revision);
}
}