Bubble up gzip/bzip errors, fix map dumper test, add datafile xtea testcase
This commit is contained in:
@@ -128,7 +128,7 @@ public class CacheClient
|
|||||||
return clientRevision;
|
return clientRevision;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void download() throws InterruptedException, ExecutionException, FileNotFoundException
|
public void download() throws InterruptedException, ExecutionException, FileNotFoundException, IOException
|
||||||
{
|
{
|
||||||
FileResult result = requestFile(255, 255).get();
|
FileResult result = requestFile(255, 255).get();
|
||||||
result.decompress(null);
|
result.decompress(null);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
package net.runelite.cache.downloader;
|
package net.runelite.cache.downloader;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import net.runelite.cache.fs.DataFile;
|
import net.runelite.cache.fs.DataFile;
|
||||||
import net.runelite.cache.fs.DataFileReadResult;
|
import net.runelite.cache.fs.DataFileReadResult;
|
||||||
|
|
||||||
@@ -67,7 +68,7 @@ public class FileResult
|
|||||||
return compressedData;
|
return compressedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void decompress(int[] keys)
|
public void decompress(int[] keys) throws IOException
|
||||||
{
|
{
|
||||||
DataFileReadResult res = DataFile.decompress(compressedData, keys);
|
DataFileReadResult res = DataFile.decompress(compressedData, keys);
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
package net.runelite.cache.fs;
|
package net.runelite.cache.fs;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -134,7 +135,7 @@ public class Archive
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void decompressAndLoad(int[] keys)
|
public void decompressAndLoad(int[] keys) throws IOException
|
||||||
{
|
{
|
||||||
byte[] encryptedData = this.getData();
|
byte[] encryptedData = this.getData();
|
||||||
|
|
||||||
|
|||||||
@@ -280,7 +280,7 @@ public class DataFile implements Closeable
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DataFileReadResult decompress(byte[] b, int[] keys)
|
public static DataFileReadResult decompress(byte[] b, int[] keys) throws IOException
|
||||||
{
|
{
|
||||||
InputStream stream = new InputStream(b);
|
InputStream stream = new InputStream(b);
|
||||||
|
|
||||||
@@ -420,24 +420,6 @@ public class DataFile implements Closeable
|
|||||||
|
|
||||||
return stream.flip();
|
return stream.flip();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int checkRevision(InputStream stream, int compressedLength)
|
|
||||||
{
|
|
||||||
int offset = stream.getOffset();
|
|
||||||
int revision;
|
|
||||||
if (stream.getLength() - (compressedLength + stream.getOffset()) >= 2)
|
|
||||||
{
|
|
||||||
stream.setOffset(stream.getLength() - 2);
|
|
||||||
revision = stream.readUnsignedShort();
|
|
||||||
assert revision != -1;
|
|
||||||
stream.setOffset(offset);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
revision = -1;
|
|
||||||
}
|
|
||||||
return revision;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static byte[] decrypt(byte[] data, int length, int[] keys)
|
private static byte[] decrypt(byte[] data, int length, int[] keys)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,7 +27,6 @@
|
|||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.runelite.cache.util;
|
package net.runelite.cache.util;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
@@ -46,62 +45,47 @@ public class BZip2
|
|||||||
{
|
{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(BZip2.class);
|
private static final Logger logger = LoggerFactory.getLogger(BZip2.class);
|
||||||
|
|
||||||
private static final byte[] BZIP_HEADER = new byte[] {
|
private static final byte[] BZIP_HEADER = new byte[]
|
||||||
|
{
|
||||||
'B', 'Z', // magic
|
'B', 'Z', // magic
|
||||||
'h', // 'h' for Bzip2 ('H'uffman coding)
|
'h', // 'h' for Bzip2 ('H'uffman coding)
|
||||||
'1' // block size
|
'1' // block size
|
||||||
};
|
};
|
||||||
|
|
||||||
public static byte[] compress(byte[] bytes)
|
public static byte[] compress(byte[] bytes) throws IOException
|
||||||
{
|
{
|
||||||
try
|
InputStream is = new ByteArrayInputStream(bytes);
|
||||||
|
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||||
|
try (OutputStream os = new BZip2CompressorOutputStream(bout, 1))
|
||||||
{
|
{
|
||||||
InputStream is = new ByteArrayInputStream(bytes);
|
IOUtils.copy(is, os);
|
||||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
|
||||||
try (OutputStream os = new BZip2CompressorOutputStream(bout, 1))
|
|
||||||
{
|
|
||||||
IOUtils.copy(is, os);
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] out = bout.toByteArray();
|
|
||||||
|
|
||||||
assert BZIP_HEADER[0] == out[0];
|
|
||||||
assert BZIP_HEADER[1] == out[1];
|
|
||||||
assert BZIP_HEADER[2] == out[2];
|
|
||||||
assert BZIP_HEADER[3] == out[3];
|
|
||||||
|
|
||||||
return Arrays.copyOfRange(out, BZIP_HEADER.length, out.length); // remove header..
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
logger.warn(null, ex);
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byte[] out = bout.toByteArray();
|
||||||
|
|
||||||
|
assert BZIP_HEADER[0] == out[0];
|
||||||
|
assert BZIP_HEADER[1] == out[1];
|
||||||
|
assert BZIP_HEADER[2] == out[2];
|
||||||
|
assert BZIP_HEADER[3] == out[3];
|
||||||
|
|
||||||
|
return Arrays.copyOfRange(out, BZIP_HEADER.length, out.length); // remove header..
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] decompress(byte[] bytes, int len)
|
public static byte[] decompress(byte[] bytes, int len) throws IOException
|
||||||
{
|
{
|
||||||
try
|
byte[] data = new byte[len + BZIP_HEADER.length];
|
||||||
|
|
||||||
|
// add header
|
||||||
|
System.arraycopy(BZIP_HEADER, 0, data, 0, BZIP_HEADER.length);
|
||||||
|
System.arraycopy(bytes, 0, data, BZIP_HEADER.length, len);
|
||||||
|
|
||||||
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
try (InputStream is = new BZip2CompressorInputStream(new ByteArrayInputStream(data)))
|
||||||
{
|
{
|
||||||
byte[] data = new byte[len + BZIP_HEADER.length];
|
IOUtils.copy(is, os);
|
||||||
|
|
||||||
// add header
|
|
||||||
System.arraycopy(BZIP_HEADER, 0, data, 0, BZIP_HEADER.length);
|
|
||||||
System.arraycopy(bytes, 0, data, BZIP_HEADER.length, len);
|
|
||||||
|
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
try (InputStream is = new BZip2CompressorInputStream(new ByteArrayInputStream(data)))
|
|
||||||
{
|
|
||||||
IOUtils.copy(is, os);
|
|
||||||
}
|
|
||||||
|
|
||||||
return os.toByteArray();
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
logger.warn(null, ex);
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return os.toByteArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class GZip
|
|||||||
{
|
{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(GZip.class);
|
private static final Logger logger = LoggerFactory.getLogger(GZip.class);
|
||||||
|
|
||||||
public static byte[] compress(byte[] bytes)
|
public static byte[] compress(byte[] bytes) throws IOException
|
||||||
{
|
{
|
||||||
InputStream is = new ByteArrayInputStream(bytes);
|
InputStream is = new ByteArrayInputStream(bytes);
|
||||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||||
@@ -54,16 +54,11 @@ public class GZip
|
|||||||
{
|
{
|
||||||
IOUtils.copy(is, os);
|
IOUtils.copy(is, os);
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
logger.warn(null, ex);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bout.toByteArray();
|
return bout.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] decompress(byte[] bytes, int len)
|
public static byte[] decompress(byte[] bytes, int len) throws IOException
|
||||||
{
|
{
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
|
||||||
@@ -71,11 +66,6 @@ public class GZip
|
|||||||
{
|
{
|
||||||
IOUtils.copy(is, os);
|
IOUtils.copy(is, os);
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
logger.warn(null, ex);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return os.toByteArray();
|
return os.toByteArray();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, Adam <Adam@sigterm.info>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. All advertising materials mentioning features or use of this software
|
||||||
|
* must display the following acknowledgement:
|
||||||
|
* This product includes software developed by Adam <Adam@sigterm.info>
|
||||||
|
* 4. Neither the name of the Adam <Adam@sigterm.info> nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY Adam <Adam@sigterm.info> ''AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL Adam <Adam@sigterm.info> BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
package net.runelite.cache;
|
package net.runelite.cache;
|
||||||
|
|
||||||
import com.google.common.io.Files;
|
import com.google.common.io.Files;
|
||||||
@@ -26,7 +56,7 @@ public class MapDumperTest
|
|||||||
public void dump() throws IOException
|
public void dump() throws IOException
|
||||||
{
|
{
|
||||||
File base = StoreLocation.LOCATION,
|
File base = StoreLocation.LOCATION,
|
||||||
outDir = new java.io.File("d:/rs/07/cache/maps");//folder.newFolder();
|
outDir = folder.newFolder();
|
||||||
|
|
||||||
try (Store store = new Store(base))
|
try (Store store = new Store(base))
|
||||||
{
|
{
|
||||||
@@ -63,13 +93,18 @@ public class MapDumperTest
|
|||||||
|
|
||||||
if (keys != null)
|
if (keys != null)
|
||||||
{
|
{
|
||||||
land.decompressAndLoad(keys);
|
try
|
||||||
|
{
|
||||||
|
land.decompressAndLoad(keys);
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
logger.warn("Unable to decompress and load land " + x + "/" + y + " (bad keys?)", ex);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
data = land.getFiles().get(0).getContents();
|
data = land.getFiles().get(0).getContents();
|
||||||
|
|
||||||
if (data == null)
|
|
||||||
continue; // key is probably wrong
|
|
||||||
|
|
||||||
Files.write(data, new File(outDir, "l" + x + "_" + y + ".dat"));
|
Files.write(data, new File(outDir, "l" + x + "_" + y + ".dat"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,4 +150,28 @@ public class DataFileTest
|
|||||||
Assert.assertEquals(42, res2.revision);
|
Assert.assertEquals(42, res2.revision);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testKeys() throws IOException
|
||||||
|
{
|
||||||
|
File file = folder.newFile();
|
||||||
|
int[] keys = new int[] { 4, 8, 15, 16 };
|
||||||
|
|
||||||
|
try (Store store = new Store(folder.getRoot()))
|
||||||
|
{
|
||||||
|
DataFile df = new DataFile(store, file);
|
||||||
|
|
||||||
|
byte[] compressedData = DataFile.compress("testtesttesttest1".getBytes(), CompressionType.NONE, 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