cache: fix compression with encryption
This commit is contained in:
@@ -24,6 +24,8 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.cache.fs.jagex;
|
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.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@@ -376,7 +378,6 @@ public class DataFile implements Closeable
|
|||||||
res.data = data;
|
res.data = data;
|
||||||
res.revision = revision;
|
res.revision = revision;
|
||||||
res.crc = crc32.getHash();
|
res.crc = crc32.getHash();
|
||||||
int length = revision != -1 ? b.length - 2 : b.length;
|
|
||||||
res.compression = compression;
|
res.compression = compression;
|
||||||
return res;
|
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
|
public static byte[] compress(byte[] data, int compression, int revision, int[] keys) throws IOException
|
||||||
{
|
{
|
||||||
OutputStream stream = new OutputStream();
|
OutputStream stream = new OutputStream();
|
||||||
stream.writeByte(compression);
|
|
||||||
byte[] compressedData;
|
byte[] compressedData;
|
||||||
|
int length;
|
||||||
switch (compression)
|
switch (compression)
|
||||||
{
|
{
|
||||||
case CompressionType.NONE:
|
case CompressionType.NONE:
|
||||||
compressedData = data;
|
compressedData = data;
|
||||||
compressedData = encrypt(compressedData, compressedData.length, keys);
|
length = compressedData.length;
|
||||||
stream.writeInt(data.length);
|
|
||||||
break;
|
break;
|
||||||
case CompressionType.BZ2:
|
case CompressionType.BZ2:
|
||||||
compressedData = BZip2.compress(data);
|
compressedData = concat(Ints.toByteArray(data.length), BZip2.compress(data));
|
||||||
compressedData = encrypt(compressedData, compressedData.length, keys);
|
length = compressedData.length - 4;
|
||||||
|
|
||||||
stream.writeInt(compressedData.length);
|
|
||||||
stream.writeInt(data.length);
|
|
||||||
break;
|
break;
|
||||||
case CompressionType.GZ:
|
case CompressionType.GZ:
|
||||||
compressedData = GZip.compress(data);
|
compressedData = concat(Ints.toByteArray(data.length), GZip.compress(data));
|
||||||
compressedData = encrypt(compressedData, compressedData.length, keys);
|
length = compressedData.length - 4;
|
||||||
|
|
||||||
stream.writeInt(compressedData.length);
|
|
||||||
stream.writeInt(data.length);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("Unknown compression type");
|
throw new RuntimeException("Unknown compression type");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compressedData = encrypt(compressedData, compressedData.length, keys);
|
||||||
|
|
||||||
|
stream.writeByte(compression);
|
||||||
|
stream.writeInt(length);
|
||||||
|
|
||||||
stream.writeBytes(compressedData);
|
stream.writeBytes(compressedData);
|
||||||
if (revision != -1)
|
if (revision != -1)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ public class DataFileTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testKeys() throws IOException
|
public void testEnc() throws IOException
|
||||||
{
|
{
|
||||||
File file = folder.newFile();
|
File file = folder.newFile();
|
||||||
int[] keys = new int[]
|
int[] keys = new int[]
|
||||||
@@ -153,4 +153,28 @@ public class DataFileTest
|
|||||||
Assert.assertEquals(res.crc, res2.crc);
|
Assert.assertEquals(res.crc, res2.crc);
|
||||||
Assert.assertEquals(42, res2.revision);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user