From 85e4df2aad4728cbd3d7224253dac22612fb4299 Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 15:34:39 +0100 Subject: [PATCH 01/24] Rename .java to .kt --- .../com/displee/compress/type/BZIP2Compressor.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/{java/com/displee/cache/compress/type/BZIP2Compressor.java => kotlin/com/displee/compress/type/BZIP2Compressor.kt} (100%) diff --git a/src/main/java/com/displee/cache/compress/type/BZIP2Compressor.java b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt similarity index 100% rename from src/main/java/com/displee/cache/compress/type/BZIP2Compressor.java rename to src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt From 3b196866b104b112cf0a82b84a1e16a7c7ea7ecd Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 15:34:39 +0100 Subject: [PATCH 02/24] Convert BZIP2 compressor to kotlin #38 --- .../displee/cache/index/archive/Archive317.kt | 16 +- .../com/displee/compress/CompressionExt.kt | 13 +- .../displee/compress/type/BZIP2Compressor.kt | 1223 +++++++++-------- 3 files changed, 637 insertions(+), 615 deletions(-) diff --git a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt index 3fc8367..b7fed6b 100644 --- a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt +++ b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt @@ -2,7 +2,7 @@ package com.displee.cache.index.archive import com.displee.cache.index.archive.file.File import com.displee.compress.CompressionType -import com.displee.cache.compress.type.BZIP2Compressor +import com.displee.compress.type.BZIP2Compressor import com.displee.compress.type.GZIPCompressor import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer @@ -57,14 +57,20 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { val fileData = file.data ?: continue metaBuffer.writeInt(file.hashName).write24BitInt(fileData.size) val toWrite = if (extracted) fileData else BZIP2Compressor.compress(file.data) - metaBuffer.write24BitInt(toWrite.size) - filesBuffer.writeBytes(toWrite) + metaBuffer.write24BitInt(toWrite?.size ?: 0) + if (toWrite != null) { + filesBuffer.writeBytes(toWrite) + } } metaBuffer.writeBytes(filesBuffer.array()) val decompressed = metaBuffer.array() val compressed = if (extracted) BZIP2Compressor.compress(decompressed) else decompressed - val buffer = OutputBuffer(compressed.size + 6) - buffer.write24BitInt(decompressed.size).write24BitInt(compressed.size).writeBytes(compressed) + val compressedSize = compressed?.size ?: 0 + val buffer = OutputBuffer(compressedSize + 6) + buffer.write24BitInt(decompressed.size).write24BitInt(compressedSize) + if (compressed != null) { + buffer.writeBytes(compressed) + } return buffer.array() } diff --git a/src/main/kotlin/com/displee/compress/CompressionExt.kt b/src/main/kotlin/com/displee/compress/CompressionExt.kt index daa43f2..ac8e8a7 100644 --- a/src/main/kotlin/com/displee/compress/CompressionExt.kt +++ b/src/main/kotlin/com/displee/compress/CompressionExt.kt @@ -1,25 +1,28 @@ package com.displee.compress import com.displee.cache.index.archive.ArchiveSector -import com.displee.cache.compress.type.BZIP2Compressor +import com.displee.compress.type.BZIP2Compressor import com.displee.compress.type.GZIPCompressor import com.displee.compress.type.LZMACompressor import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer fun ByteArray.compress(compressionType: CompressionType, xteas: IntArray? = null, revision: Int = -1): ByteArray { - val compressed: ByteArray = when (compressionType) { + val compressed: ByteArray? = when (compressionType) { CompressionType.NONE -> this CompressionType.BZIP2 -> BZIP2Compressor.compress(this) CompressionType.GZIP -> GZIPCompressor.deflate(this) CompressionType.LZMA -> LZMACompressor.compress(this) } - val buffer = OutputBuffer(9 + compressed.size + if (revision == -1) 0 else 2) - buffer.writeByte(compressionType.ordinal).writeInt(compressed.size) + val compressedSize = compressed?.size ?: 0 + val buffer = OutputBuffer(9 + compressedSize + if (revision == -1) 0 else 2) + buffer.writeByte(compressionType.ordinal).writeInt(compressedSize) if (compressionType != CompressionType.NONE) { buffer.writeInt(size) } - buffer.writeBytes(compressed) + if (compressed != null) { + buffer.writeBytes(compressed) + } if (xteas != null && (xteas[0] != 0 || xteas[1] != 0 || xteas[2] != 0 || 0 != xteas[3])) { check(compressionType != CompressionType.BZIP2) { "BZIP2 compression doesn't work with xtea encryption." } buffer.encryptXTEA(xteas, 5, buffer.offset) diff --git a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt index 30475e0..80be97c 100644 --- a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt @@ -1,627 +1,640 @@ -package com.displee.cache.compress.type; +package com.displee.compress.type -import com.displee.io.impl.InputBuffer; -import org.apache.tools.bzip2.CBZip2OutputStream; - -import java.io.*; +import com.displee.io.impl.InputBuffer +import org.apache.tools.bzip2.CBZip2OutputStream +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream /** * A class representing the BZIP2 (de)compressor. * @author Jagex * @author Displee */ -public class BZIP2Compressor { - - /** - * The current bzip2 block entry. - */ - public static BZIP2BlockEntry bzip2BlockEntry = new BZIP2BlockEntry(); - - /** - * An unkown integer array. - */ - private static int[] anIntArray5786; - - /** - * Compress a decompressed BZIP2 file. - * @param bytes The uncompressed BZIP2 file. - * @return The compressed BZIP2 file. - */ - public static byte[] compress(byte[] bytes) { - try { - try (InputStream is = new ByteArrayInputStream(bytes)) { - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - try (OutputStream os = new CBZip2OutputStream(bout, 1)) { - byte[] buf = new byte[4096]; - int len; - while ((len = is.read(buf, 0, buf.length)) != -1) { - os.write(buf, 0, len); - } - } - bytes = bout.toByteArray(); - byte[] bzip2 = new byte[bytes.length - 2]; - System.arraycopy(bytes, 2, bzip2, 0, bzip2.length); - return bzip2; - } - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public static byte[] decompress317(int decompressedLength, int compressedLength, InputBuffer inputBuffer) { - byte[] decompressed = new byte[decompressedLength]; - byte[] compressed = inputBuffer.readBytes(compressedLength); - decompress(decompressed, decompressed.length, compressed, compressed.length, 0); - return decompressed; - } +object BZIP2Compressor { + /** + * The current bzip2 block entry. + */ + var bzip2BlockEntry: BZIP2BlockEntry? = BZIP2BlockEntry() - /** - * Decompress a compressed BZIP2 file. - * @param decompressed An empty byte array where we put the decompressed data in. - * @param decompressedLength The length to decompress. - * @param archiveData The compressed BZIP2 file. - * @param compressedSize The size of the compressed BZIP2 file. - * @param startOffset The start offset. - * @return The decompressed length. - */ - public static int decompress(byte[] decompressed, int decompressedLength, byte[] archiveData, int compressedSize, int startOffset) { - synchronized (bzip2BlockEntry) { - bzip2BlockEntry.compressed = archiveData; - bzip2BlockEntry.startOffset = startOffset; - bzip2BlockEntry.decompressed = decompressed; - bzip2BlockEntry.anInt3100 = 0; - bzip2BlockEntry.decompressedLength = decompressedLength; - bzip2BlockEntry.anInt3088 = 0; - bzip2BlockEntry.anInt3078 = 0; - bzip2BlockEntry.anInt3085 = 0; - bzip2BlockEntry.anInt3097 = 0; - decompress(bzip2BlockEntry); - decompressedLength -= bzip2BlockEntry.decompressedLength; - bzip2BlockEntry.compressed = null; - bzip2BlockEntry.decompressed = null; - return decompressedLength; - } - } + /** + * An unkown integer array. + */ + private var anIntArray5786: IntArray? = null - /** - * Decompress a BZIP2 block entry. - * @param bzip2BlockEntry {@link BZIP2BlockEntry} - */ - public static void decompress(BZIP2BlockEntry bzip2BlockEntry) { - boolean bool = false; - boolean bool_9_ = false; - boolean bool_10_ = false; - boolean bool_11_ = false; - boolean bool_12_ = false; - boolean bool_13_ = false; - boolean bool_14_ = false; - boolean bool_15_ = false; - boolean bool_16_ = false; - boolean bool_17_ = false; - boolean bool_18_ = false; - boolean bool_19_ = false; - boolean bool_20_ = false; - boolean bool_21_ = false; - boolean bool_22_ = false; - boolean bool_23_ = false; - boolean bool_24_ = false; - boolean bool_25_ = false; - int i = 0; - int[] is = null; - int[] is_26_ = null; - int[] is_27_ = null; - bzip2BlockEntry.anInt3096 = 1; - if (anIntArray5786 == null) { - anIntArray5786 = new int[bzip2BlockEntry.anInt3096 * 100000]; - } - boolean bool_28_ = true; - while (bool_28_) { - byte i_29_ = method147(bzip2BlockEntry); - if (i_29_ == 23) { - break; - } - i_29_ = method147(bzip2BlockEntry); - i_29_ = method147(bzip2BlockEntry); - i_29_ = method147(bzip2BlockEntry); - i_29_ = method147(bzip2BlockEntry); - i_29_ = method147(bzip2BlockEntry); - i_29_ = method147(bzip2BlockEntry); - i_29_ = method147(bzip2BlockEntry); - i_29_ = method147(bzip2BlockEntry); - i_29_ = method147(bzip2BlockEntry); - i_29_ = method153(bzip2BlockEntry); - bzip2BlockEntry.anInt3083 = 0; - int i_30_ = method147(bzip2BlockEntry); - bzip2BlockEntry.anInt3083 = bzip2BlockEntry.anInt3083 << 8 | i_30_ & 0xff; - i_30_ = method147(bzip2BlockEntry); - bzip2BlockEntry.anInt3083 = bzip2BlockEntry.anInt3083 << 8 | i_30_ & 0xff; - i_30_ = method147(bzip2BlockEntry); - bzip2BlockEntry.anInt3083 = bzip2BlockEntry.anInt3083 << 8 | i_30_ & 0xff; - for (int i_31_ = 0; i_31_ < 16; i_31_++) { - i_29_ = method153(bzip2BlockEntry); - if (i_29_ == 1) { - bzip2BlockEntry.aBooleanArray3072[i_31_] = true; - } else { - bzip2BlockEntry.aBooleanArray3072[i_31_] = false; - } - } - for (int i_32_ = 0; i_32_ < 256; i_32_++) { - bzip2BlockEntry.aBooleanArray3103[i_32_] = false; - } - for (int i_33_ = 0; i_33_ < 16; i_33_++) { - if (bzip2BlockEntry.aBooleanArray3072[i_33_]) { - for (int i_34_ = 0; i_34_ < 16; i_34_++) { - i_29_ = method153(bzip2BlockEntry); - if (i_29_ == 1) { - bzip2BlockEntry.aBooleanArray3103[i_33_ * 16 + i_34_] = true; - } - } - } - } - method149(bzip2BlockEntry); - int i_35_ = bzip2BlockEntry.anInt3073 + 2; - int i_36_ = method152(3, bzip2BlockEntry); - int i_37_ = method152(15, bzip2BlockEntry); - for (int i_38_ = 0; i_38_ < i_37_; i_38_++) { - int i_39_ = 0; - for (; ; ) { - i_29_ = method153(bzip2BlockEntry); - if (i_29_ == 0) { - break; - } - i_39_++; - } - bzip2BlockEntry.aByteArray3094[i_38_] = (byte) i_39_; - } - byte[] is_40_ = new byte[6]; - for (byte i_41_ = 0; i_41_ < i_36_; i_41_++) { - is_40_[i_41_] = i_41_; - } - for (int i_42_ = 0; i_42_ < i_37_; i_42_++) { - byte i_43_ = bzip2BlockEntry.aByteArray3094[i_42_]; - byte i_44_ = is_40_[i_43_]; - for (/**/; i_43_ > 0; i_43_--) { - is_40_[i_43_] = is_40_[i_43_ - 1]; - } - is_40_[0] = i_44_; - bzip2BlockEntry.aByteArray3076[i_42_] = i_44_; - } - for (int i_45_ = 0; i_45_ < i_36_; i_45_++) { - int i_46_ = method152(5, bzip2BlockEntry); - for (int i_47_ = 0; i_47_ < i_35_; i_47_++) { - for (; ; ) { - i_29_ = method153(bzip2BlockEntry); - if (i_29_ == 0) { - break; - } - i_29_ = method153(bzip2BlockEntry); - if (i_29_ == 0) { - i_46_++; - } else { - i_46_--; - } - } - bzip2BlockEntry.aByteArrayArray3098[i_45_][i_47_] = (byte) i_46_; - } - } - for (int i_48_ = 0; i_48_ < i_36_; i_48_++) { - int i_49_ = 32; - byte i_50_ = 0; - for (int i_51_ = 0; i_51_ < i_35_; i_51_++) { - if (bzip2BlockEntry.aByteArrayArray3098[i_48_][i_51_] > i_50_) { - i_50_ = bzip2BlockEntry.aByteArrayArray3098[i_48_][i_51_]; - } - if (bzip2BlockEntry.aByteArrayArray3098[i_48_][i_51_] < i_49_) { - i_49_ = bzip2BlockEntry.aByteArrayArray3098[i_48_][i_51_]; - } - } - method145(bzip2BlockEntry.anIntArrayArray3095[i_48_], bzip2BlockEntry.anIntArrayArray3082[i_48_], bzip2BlockEntry.anIntArrayArray3099[i_48_], bzip2BlockEntry.aByteArrayArray3098[i_48_], i_49_, i_50_, i_35_); - bzip2BlockEntry.anIntArray3090[i_48_] = i_49_; - } - int i_52_ = bzip2BlockEntry.anInt3073 + 1; - int i_53_ = -1; - int i_54_ = 0; - for (int i_55_ = 0; i_55_ <= 255; i_55_++) { - bzip2BlockEntry.anIntArray3075[i_55_] = 0; - } - int i_56_ = 4095; - for (int i_57_ = 15; i_57_ >= 0; i_57_--) { - for (int i_58_ = 15; i_58_ >= 0; i_58_--) { - bzip2BlockEntry.aByteArray3101[i_56_] = (byte) (i_57_ * 16 + i_58_); - i_56_--; - } - bzip2BlockEntry.anIntArray3092[i_57_] = i_56_ + 1; - } - int i_59_ = 0; - if (i_54_ == 0) { - i_53_++; - i_54_ = 50; - byte i_60_ = bzip2BlockEntry.aByteArray3076[i_53_]; - i = bzip2BlockEntry.anIntArray3090[i_60_]; - is = bzip2BlockEntry.anIntArrayArray3095[i_60_]; - is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_60_]; - is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_60_]; - } - i_54_--; - int i_61_ = i; - int i_62_; - int i_63_; - for (i_63_ = method152(i_61_, bzip2BlockEntry); i_63_ > is[i_61_]; i_63_ = i_63_ << 1 | i_62_) { - i_61_++; - i_62_ = method153(bzip2BlockEntry); - } - int i_64_ = is_27_[i_63_ - is_26_[i_61_]]; - while (i_64_ != i_52_) { - if (i_64_ == 0 || i_64_ == 1) { - int i_65_ = -1; - int i_66_ = 1; - do { - if (i_64_ == 0) { - i_65_ += i_66_; - } else if (i_64_ == 1) { - i_65_ += 2 * i_66_; - } - i_66_ *= 2; - if (i_54_ == 0) { - i_53_++; - i_54_ = 50; - byte i_67_ = bzip2BlockEntry.aByteArray3076[i_53_]; - i = bzip2BlockEntry.anIntArray3090[i_67_]; - is = bzip2BlockEntry.anIntArrayArray3095[i_67_]; - is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_67_]; - is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_67_]; - } - i_54_--; - i_61_ = i; - for (i_63_ = method152(i_61_, bzip2BlockEntry); i_63_ > is[i_61_]; i_63_ = i_63_ << 1 | i_62_) { - i_61_++; - i_62_ = method153(bzip2BlockEntry); - } - i_64_ = is_27_[i_63_ - is_26_[i_61_]]; - } while (i_64_ == 0 || i_64_ == 1); - i_65_++; - i_30_ = (bzip2BlockEntry.aByteArray3107[(bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[0]] & 0xff)]); - bzip2BlockEntry.anIntArray3075[i_30_ & 0xff] += i_65_; - for (/**/; i_65_ > 0; i_65_--) { - anIntArray5786[i_59_] = i_30_ & 0xff; - i_59_++; - } - } else { - int i_68_ = i_64_ - 1; - if (i_68_ < 16) { - int i_69_ = bzip2BlockEntry.anIntArray3092[0]; - i_29_ = bzip2BlockEntry.aByteArray3101[i_69_ + i_68_]; - for (/**/; i_68_ > 3; i_68_ -= 4) { - int i_70_ = i_69_ + i_68_; - bzip2BlockEntry.aByteArray3101[i_70_] = bzip2BlockEntry.aByteArray3101[i_70_ - 1]; - bzip2BlockEntry.aByteArray3101[i_70_ - 1] = bzip2BlockEntry.aByteArray3101[i_70_ - 2]; - bzip2BlockEntry.aByteArray3101[i_70_ - 2] = bzip2BlockEntry.aByteArray3101[i_70_ - 3]; - bzip2BlockEntry.aByteArray3101[i_70_ - 3] = bzip2BlockEntry.aByteArray3101[i_70_ - 4]; - } - for (/**/; i_68_ > 0; i_68_--) { - bzip2BlockEntry.aByteArray3101[i_69_ + i_68_] = bzip2BlockEntry.aByteArray3101[i_69_ + i_68_ - 1]; - } - bzip2BlockEntry.aByteArray3101[i_69_] = i_29_; - } else { - int i_71_ = i_68_ / 16; - int i_72_ = i_68_ % 16; - int i_73_ = bzip2BlockEntry.anIntArray3092[i_71_] + i_72_; - i_29_ = bzip2BlockEntry.aByteArray3101[i_73_]; - for (/**/; i_73_ > bzip2BlockEntry.anIntArray3092[i_71_]; i_73_--) { - bzip2BlockEntry.aByteArray3101[i_73_] = bzip2BlockEntry.aByteArray3101[i_73_ - 1]; - } - bzip2BlockEntry.anIntArray3092[i_71_]++; - for (/**/; i_71_ > 0; i_71_--) { - bzip2BlockEntry.anIntArray3092[i_71_]--; - bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[i_71_]] = (bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[i_71_ - 1] + 16 - 1]); - } - bzip2BlockEntry.anIntArray3092[0]--; - bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[0]] = i_29_; - if (bzip2BlockEntry.anIntArray3092[0] == 0) { - int i_74_ = 4095; - for (int i_75_ = 15; i_75_ >= 0; i_75_--) { - for (int i_76_ = 15; i_76_ >= 0; i_76_--) { - bzip2BlockEntry.aByteArray3101[i_74_] = (bzip2BlockEntry.aByteArray3101[(bzip2BlockEntry.anIntArray3092[i_75_] + i_76_)]); - i_74_--; - } - bzip2BlockEntry.anIntArray3092[i_75_] = i_74_ + 1; - } - } - } - bzip2BlockEntry.anIntArray3075[(bzip2BlockEntry.aByteArray3107[i_29_ & 0xff] & 0xff)]++; - anIntArray5786[i_59_] = bzip2BlockEntry.aByteArray3107[i_29_ & 0xff] & 0xff; - i_59_++; - if (i_54_ == 0) { - i_53_++; - i_54_ = 50; - byte i_77_ = bzip2BlockEntry.aByteArray3076[i_53_]; - i = bzip2BlockEntry.anIntArray3090[i_77_]; - is = bzip2BlockEntry.anIntArrayArray3095[i_77_]; - is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_77_]; - is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_77_]; - } - i_54_--; - i_61_ = i; - for (i_63_ = method152(i_61_, bzip2BlockEntry); i_63_ > is[i_61_]; i_63_ = i_63_ << 1 | i_62_) { - i_61_++; - i_62_ = method153(bzip2BlockEntry); - } - i_64_ = is_27_[i_63_ - is_26_[i_61_]]; - } - } - bzip2BlockEntry.anInt3080 = 0; - bzip2BlockEntry.aByte3108 = (byte) 0; - bzip2BlockEntry.anIntArray3091[0] = 0; - for (int i_78_ = 1; i_78_ <= 256; i_78_++) { - bzip2BlockEntry.anIntArray3091[i_78_] = bzip2BlockEntry.anIntArray3075[i_78_ - 1]; - } - for (int i_79_ = 1; i_79_ <= 256; i_79_++) { - bzip2BlockEntry.anIntArray3091[i_79_] += bzip2BlockEntry.anIntArray3091[i_79_ - 1]; - } - for (int i_80_ = 0; i_80_ < i_59_; i_80_++) { - i_30_ = (byte) (anIntArray5786[i_80_] & 0xff); - anIntArray5786[(bzip2BlockEntry.anIntArray3091[i_30_ & 0xff])] |= i_80_ << 8; - bzip2BlockEntry.anIntArray3091[i_30_ & 0xff]++; - } - bzip2BlockEntry.anInt3106 = anIntArray5786[bzip2BlockEntry.anInt3083] >> 8; - bzip2BlockEntry.anInt3071 = 0; - bzip2BlockEntry.anInt3106 = anIntArray5786[bzip2BlockEntry.anInt3106]; - bzip2BlockEntry.anInt3070 = (byte) (bzip2BlockEntry.anInt3106 & 0xff); - bzip2BlockEntry.anInt3106 >>= 8; - bzip2BlockEntry.anInt3071++; - bzip2BlockEntry.anInt3077 = i_59_; - method151(bzip2BlockEntry); - if (bzip2BlockEntry.anInt3071 == bzip2BlockEntry.anInt3077 + 1 && bzip2BlockEntry.anInt3080 == 0) { - bool_28_ = true; - } else { - bool_28_ = false; - } - } - } + /** + * Compress a decompressed BZIP2 file. + * @param bytes The uncompressed BZIP2 file. + * @return The compressed BZIP2 file. + */ + fun compress(bytes: ByteArray?): ByteArray? { + var bytes = bytes ?: return null + try { + ByteArrayInputStream(bytes).use { `is` -> + val bout = ByteArrayOutputStream() + CBZip2OutputStream(bout, 1).use { os -> + val buf = ByteArray(4096) + var len: Int + while (`is`.read(buf, 0, buf.size).also { len = it } != -1) { + os.write(buf, 0, len) + } + } + bytes = bout.toByteArray() + val bzip2 = ByteArray(bytes.size - 2) + System.arraycopy(bytes, 2, bzip2, 0, bzip2.size) + return bzip2 + } + } catch (e: Exception) { + e.printStackTrace() + return null + } + } - public static int method152(int arg0, BZIP2BlockEntry arg1) { - int i; - for (; ; ) { - if (arg1.anInt3088 >= arg0) { - int i_93_ = (arg1.anInt3078 >> arg1.anInt3088 - arg0 & (1 << arg0) - 1); - arg1.anInt3088 -= arg0; - i = i_93_; - break; - } - arg1.anInt3078 = (arg1.anInt3078 << 8 | arg1.compressed[arg1.startOffset] & 0xff); - arg1.anInt3088 += 8; - arg1.startOffset++; - arg1.anInt3085++; - } - return i; - } + fun decompress317(decompressedLength: Int, compressedLength: Int, inputBuffer: InputBuffer): ByteArray { + val decompressed = ByteArray(decompressedLength) + val compressed = inputBuffer.readBytes(compressedLength) + decompress(decompressed, decompressed.size, compressed, compressed.size, 0) + return decompressed + } - public static void method151(BZIP2BlockEntry arg0) { - byte i = arg0.aByte3108; - int i_81_ = arg0.anInt3080; - int i_82_ = arg0.anInt3071; - int i_83_ = arg0.anInt3070; - int[] is = anIntArray5786; - int i_84_ = arg0.anInt3106; - byte[] is_85_ = arg0.decompressed; - int i_86_ = arg0.anInt3100; - int i_87_ = arg0.decompressedLength; - int i_88_ = i_87_; - int i_89_ = arg0.anInt3077 + 1; - while_68_: - for (; ; ) { - if (i_81_ > 0) { - for (; ; ) { - if (i_87_ == 0) { - break while_68_; - } - if (i_81_ == 1) { - break; - } - is_85_[i_86_] = i; - i_81_--; - i_86_++; - i_87_--; - } - if (i_87_ == 0) { - i_81_ = 1; - break; - } - is_85_[i_86_] = i; - i_86_++; - i_87_--; - } - boolean bool = true; - while (bool) { - bool = false; - if (i_82_ == i_89_) { - i_81_ = 0; - break while_68_; - } - i = (byte) i_83_; - i_84_ = is[i_84_]; - int i_90_ = (byte) (i_84_ & 0xff); - i_84_ >>= 8; - i_82_++; - if (i_90_ != i_83_) { - i_83_ = i_90_; - if (i_87_ == 0) { - i_81_ = 1; - break while_68_; - } - is_85_[i_86_] = i; - i_86_++; - i_87_--; - bool = true; - } else if (i_82_ == i_89_) { - if (i_87_ == 0) { - i_81_ = 1; - break while_68_; - } - is_85_[i_86_] = i; - i_86_++; - i_87_--; - bool = true; - } - } - i_81_ = 2; - i_84_ = is[i_84_]; - int i_91_ = (byte) (i_84_ & 0xff); - i_84_ >>= 8; - if (++i_82_ != i_89_) { - if (i_91_ != i_83_) { - i_83_ = i_91_; - } else { - i_81_ = 3; - i_84_ = is[i_84_]; - i_91_ = (byte) (i_84_ & 0xff); - i_84_ >>= 8; - if (++i_82_ != i_89_) { - if (i_91_ != i_83_) { - i_83_ = i_91_; - } else { - i_84_ = is[i_84_]; - i_91_ = (byte) (i_84_ & 0xff); - i_84_ >>= 8; - i_82_++; - i_81_ = (i_91_ & 0xff) + 4; - i_84_ = is[i_84_]; - i_83_ = (byte) (i_84_ & 0xff); - i_84_ >>= 8; - i_82_++; - } - } - } - } - } - int i_92_ = arg0.anInt3097; - arg0.anInt3097 += i_88_ - i_87_; - arg0.aByte3108 = i; - arg0.anInt3080 = i_81_; - arg0.anInt3071 = i_82_; - arg0.anInt3070 = i_83_; - anIntArray5786 = is; - arg0.anInt3106 = i_84_; - arg0.decompressed = is_85_; - arg0.anInt3100 = i_86_; - arg0.decompressedLength = i_87_; - } + /** + * Decompress a compressed BZIP2 file. + * @param decompressed An empty byte array where we put the decompressed data in. + * @param decompressedLength The length to decompress. + * @param archiveData The compressed BZIP2 file. + * @param compressedSize The size of the compressed BZIP2 file. + * @param startOffset The start offset. + * @return The decompressed length. + */ + fun decompress(decompressed: ByteArray?, decompressedLength: Int, archiveData: ByteArray?, compressedSize: Int, startOffset: Int): Int { + var decompressedLength = decompressedLength + synchronized(bzip2BlockEntry!!) { + bzip2BlockEntry!!.compressed = archiveData + bzip2BlockEntry!!.startOffset = startOffset + bzip2BlockEntry!!.decompressed = decompressed + bzip2BlockEntry!!.anInt3100 = 0 + bzip2BlockEntry!!.decompressedLength = decompressedLength + bzip2BlockEntry!!.anInt3088 = 0 + bzip2BlockEntry!!.anInt3078 = 0 + bzip2BlockEntry!!.anInt3085 = 0 + bzip2BlockEntry!!.anInt3097 = 0 + decompress(bzip2BlockEntry) + decompressedLength -= bzip2BlockEntry!!.decompressedLength + bzip2BlockEntry!!.compressed = null + bzip2BlockEntry!!.decompressed = null + return decompressedLength + } + } - public static void method145(int[] arg0, int[] arg1, int[] arg2, byte[] arg3, int arg4, int arg5, int arg6) { - int i = 0; - for (int i_0_ = arg4; i_0_ <= arg5; i_0_++) { - for (int i_1_ = 0; i_1_ < arg6; i_1_++) { - if (arg3[i_1_] == i_0_) { - arg2[i] = i_1_; - i++; - } - } - } - for (int i_2_ = 0; i_2_ < 23; i_2_++) { - arg1[i_2_] = 0; - } - for (int i_3_ = 0; i_3_ < arg6; i_3_++) { - arg1[arg3[i_3_] + 1]++; - } - for (int i_4_ = 1; i_4_ < 23; i_4_++) { - arg1[i_4_] += arg1[i_4_ - 1]; - } - for (int i_5_ = 0; i_5_ < 23; i_5_++) { - arg0[i_5_] = 0; - } - int i_6_ = 0; - for (int i_7_ = arg4; i_7_ <= arg5; i_7_++) { - i_6_ += arg1[i_7_ + 1] - arg1[i_7_]; - arg0[i_7_] = i_6_ - 1; - i_6_ <<= 1; - } - for (int i_8_ = arg4 + 1; i_8_ <= arg5; i_8_++) { - arg1[i_8_] = (arg0[i_8_ - 1] + 1 << 1) - arg1[i_8_]; - } - } + /** + * Decompress a BZIP2 block entry. + * @param bzip2BlockEntry [BZIP2BlockEntry] + */ + fun decompress(bzip2BlockEntry: BZIP2BlockEntry?) { + val bool = false + val bool_9_ = false + val bool_10_ = false + val bool_11_ = false + val bool_12_ = false + val bool_13_ = false + val bool_14_ = false + val bool_15_ = false + val bool_16_ = false + val bool_17_ = false + val bool_18_ = false + val bool_19_ = false + val bool_20_ = false + val bool_21_ = false + val bool_22_ = false + val bool_23_ = false + val bool_24_ = false + val bool_25_ = false + var i = 0 + var `is`: IntArray? = null + var is_26_: IntArray? = null + var is_27_: IntArray? = null + bzip2BlockEntry!!.anInt3096 = 1 + if (anIntArray5786 == null) { + anIntArray5786 = IntArray(bzip2BlockEntry.anInt3096 * 100000) + } + var bool_28_ = true + while (bool_28_) { + var i_29_ = method147(bzip2BlockEntry) + if (i_29_.toInt() == 23) { + break + } + i_29_ = method147(bzip2BlockEntry) + i_29_ = method147(bzip2BlockEntry) + i_29_ = method147(bzip2BlockEntry) + i_29_ = method147(bzip2BlockEntry) + i_29_ = method147(bzip2BlockEntry) + i_29_ = method147(bzip2BlockEntry) + i_29_ = method147(bzip2BlockEntry) + i_29_ = method147(bzip2BlockEntry) + i_29_ = method147(bzip2BlockEntry) + i_29_ = method153(bzip2BlockEntry) + bzip2BlockEntry.anInt3083 = 0 + var i_30_ = method147(bzip2BlockEntry).toInt() + bzip2BlockEntry.anInt3083 = bzip2BlockEntry.anInt3083 shl 8 or (i_30_ and 0xff) + i_30_ = method147(bzip2BlockEntry).toInt() + bzip2BlockEntry.anInt3083 = bzip2BlockEntry.anInt3083 shl 8 or (i_30_ and 0xff) + i_30_ = method147(bzip2BlockEntry).toInt() + bzip2BlockEntry.anInt3083 = bzip2BlockEntry.anInt3083 shl 8 or (i_30_ and 0xff) + for (i_31_ in 0..15) { + i_29_ = method153(bzip2BlockEntry) + if (i_29_.toInt() == 1) { + bzip2BlockEntry.aBooleanArray3072[i_31_] = true + } else { + bzip2BlockEntry.aBooleanArray3072[i_31_] = false + } + } + for (i_32_ in 0..255) { + bzip2BlockEntry.aBooleanArray3103[i_32_] = false + } + for (i_33_ in 0..15) { + if (bzip2BlockEntry.aBooleanArray3072[i_33_]) { + for (i_34_ in 0..15) { + i_29_ = method153(bzip2BlockEntry) + if (i_29_.toInt() == 1) { + bzip2BlockEntry.aBooleanArray3103[i_33_ * 16 + i_34_] = true + } + } + } + } + method149(bzip2BlockEntry) + val i_35_ = bzip2BlockEntry.anInt3073 + 2 + val i_36_ = method152(3, bzip2BlockEntry) + val i_37_ = method152(15, bzip2BlockEntry) + for (i_38_ in 0 until i_37_) { + var i_39_ = 0 + while (true) { + i_29_ = method153(bzip2BlockEntry) + if (i_29_.toInt() == 0) { + break + } + i_39_++ + } + bzip2BlockEntry.aByteArray3094[i_38_] = i_39_.toByte() + } + val is_40_ = ByteArray(6) + for (i_41_ in 0 until i_36_) { + is_40_[i_41_] = i_41_.toByte() + } + for (i_42_ in 0 until i_37_) { + var i_43_ = bzip2BlockEntry.aByteArray3094[i_42_] + val i_44_ = is_40_[i_43_.toInt()] + while ( /**/i_43_ > 0) { + is_40_[i_43_.toInt()] = is_40_[i_43_ - 1] + i_43_-- + } + is_40_[0] = i_44_ + bzip2BlockEntry.aByteArray3076[i_42_] = i_44_ + } + for (i_45_ in 0 until i_36_) { + var i_46_ = method152(5, bzip2BlockEntry) + for (i_47_ in 0 until i_35_) { + while (true) { + i_29_ = method153(bzip2BlockEntry) + if (i_29_.toInt() == 0) { + break + } + i_29_ = method153(bzip2BlockEntry) + if (i_29_.toInt() == 0) { + i_46_++ + } else { + i_46_-- + } + } + bzip2BlockEntry.aByteArrayArray3098[i_45_][i_47_] = i_46_.toByte() + } + } + for (i_48_ in 0 until i_36_) { + var i_49_ = 32 + var i_50_: Byte = 0 + for (i_51_ in 0 until i_35_) { + if (bzip2BlockEntry.aByteArrayArray3098[i_48_][i_51_] > i_50_) { + i_50_ = bzip2BlockEntry.aByteArrayArray3098[i_48_][i_51_] + } + if (bzip2BlockEntry.aByteArrayArray3098[i_48_][i_51_] < i_49_) { + i_49_ = bzip2BlockEntry.aByteArrayArray3098[i_48_][i_51_].toInt() + } + } + method145(bzip2BlockEntry.anIntArrayArray3095[i_48_], + bzip2BlockEntry.anIntArrayArray3082[i_48_], + bzip2BlockEntry.anIntArrayArray3099[i_48_], + bzip2BlockEntry.aByteArrayArray3098[i_48_], + i_49_, + i_50_.toInt(), + i_35_) + bzip2BlockEntry.anIntArray3090[i_48_] = i_49_ + } + val i_52_ = bzip2BlockEntry.anInt3073 + 1 + var i_53_ = -1 + var i_54_ = 0 + for (i_55_ in 0..255) { + bzip2BlockEntry.anIntArray3075[i_55_] = 0 + } + var i_56_ = 4095 + for (i_57_ in 15 downTo 0) { + for (i_58_ in 15 downTo 0) { + bzip2BlockEntry.aByteArray3101[i_56_] = (i_57_ * 16 + i_58_).toByte() + i_56_-- + } + bzip2BlockEntry.anIntArray3092[i_57_] = i_56_ + 1 + } + var i_59_ = 0 + if (i_54_ == 0) { + i_53_++ + i_54_ = 50 + val i_60_ = bzip2BlockEntry.aByteArray3076[i_53_] + i = bzip2BlockEntry.anIntArray3090[i_60_.toInt()] + `is` = bzip2BlockEntry.anIntArrayArray3095[i_60_.toInt()] + is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_60_.toInt()] + is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_60_.toInt()] + } + i_54_-- + var i_61_ = i + var i_62_: Int + var i_63_: Int + i_63_ = method152(i_61_, bzip2BlockEntry) + while (i_63_ > `is`!![i_61_]) { + i_61_++ + i_62_ = method153(bzip2BlockEntry).toInt() + i_63_ = i_63_ shl 1 or i_62_ + } + var i_64_ = is_27_!![i_63_ - is_26_!![i_61_]] + while (i_64_ != i_52_) { + if (i_64_ == 0 || i_64_ == 1) { + var i_65_ = -1 + var i_66_ = 1 + do { + if (i_64_ == 0) { + i_65_ += i_66_ + } else if (i_64_ == 1) { + i_65_ += 2 * i_66_ + } + i_66_ *= 2 + if (i_54_ == 0) { + i_53_++ + i_54_ = 50 + val i_67_ = bzip2BlockEntry.aByteArray3076[i_53_] + i = bzip2BlockEntry.anIntArray3090[i_67_.toInt()] + `is` = bzip2BlockEntry.anIntArrayArray3095[i_67_.toInt()] + is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_67_.toInt()] + is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_67_.toInt()] + } + i_54_-- + i_61_ = i + i_63_ = method152(i_61_, bzip2BlockEntry) + while (i_63_ > `is`!![i_61_]) { + i_61_++ + i_62_ = method153(bzip2BlockEntry).toInt() + i_63_ = i_63_ shl 1 or i_62_ + } + i_64_ = is_27_!![i_63_ - is_26_!![i_61_]] + } while (i_64_ == 0 || i_64_ == 1) + i_65_++ + i_30_ = bzip2BlockEntry.aByteArray3107[bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[0]].toInt() and 0xff].toInt() + bzip2BlockEntry.anIntArray3075[i_30_ and 0xff] += i_65_ + while ( /**/i_65_ > 0) { + anIntArray5786!![i_59_] = i_30_ and 0xff + i_59_++ + i_65_-- + } + } else { + var i_68_ = i_64_ - 1 + if (i_68_ < 16) { + val i_69_ = bzip2BlockEntry.anIntArray3092[0] + i_29_ = bzip2BlockEntry.aByteArray3101[i_69_ + i_68_] + while ( /**/i_68_ > 3) { + val i_70_ = i_69_ + i_68_ + bzip2BlockEntry.aByteArray3101[i_70_] = bzip2BlockEntry.aByteArray3101[i_70_ - 1] + bzip2BlockEntry.aByteArray3101[i_70_ - 1] = bzip2BlockEntry.aByteArray3101[i_70_ - 2] + bzip2BlockEntry.aByteArray3101[i_70_ - 2] = bzip2BlockEntry.aByteArray3101[i_70_ - 3] + bzip2BlockEntry.aByteArray3101[i_70_ - 3] = bzip2BlockEntry.aByteArray3101[i_70_ - 4] + i_68_ -= 4 + } + while ( /**/i_68_ > 0) { + bzip2BlockEntry.aByteArray3101[i_69_ + i_68_] = bzip2BlockEntry.aByteArray3101[i_69_ + i_68_ - 1] + i_68_-- + } + bzip2BlockEntry.aByteArray3101[i_69_] = i_29_ + } else { + var i_71_ = i_68_ / 16 + val i_72_ = i_68_ % 16 + var i_73_ = bzip2BlockEntry.anIntArray3092[i_71_] + i_72_ + i_29_ = bzip2BlockEntry.aByteArray3101[i_73_] + while ( /**/i_73_ > bzip2BlockEntry.anIntArray3092[i_71_]) { + bzip2BlockEntry.aByteArray3101[i_73_] = bzip2BlockEntry.aByteArray3101[i_73_ - 1] + i_73_-- + } + bzip2BlockEntry.anIntArray3092[i_71_]++ + while ( /**/i_71_ > 0) { + bzip2BlockEntry.anIntArray3092[i_71_]-- + bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[i_71_]] = bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[i_71_ - 1] + 16 - 1] + i_71_-- + } + bzip2BlockEntry.anIntArray3092[0]-- + bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[0]] = i_29_ + if (bzip2BlockEntry.anIntArray3092[0] == 0) { + var i_74_ = 4095 + for (i_75_ in 15 downTo 0) { + for (i_76_ in 15 downTo 0) { + bzip2BlockEntry.aByteArray3101[i_74_] = bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[i_75_] + i_76_] + i_74_-- + } + bzip2BlockEntry.anIntArray3092[i_75_] = i_74_ + 1 + } + } + } + bzip2BlockEntry.anIntArray3075[bzip2BlockEntry.aByteArray3107[i_29_.toInt() and 0xff].toInt() and 0xff]++ + anIntArray5786!![i_59_] = bzip2BlockEntry.aByteArray3107[i_29_.toInt() and 0xff].toInt() and 0xff + i_59_++ + if (i_54_ == 0) { + i_53_++ + i_54_ = 50 + val i_77_ = bzip2BlockEntry.aByteArray3076[i_53_] + i = bzip2BlockEntry.anIntArray3090[i_77_.toInt()] + `is` = bzip2BlockEntry.anIntArrayArray3095[i_77_.toInt()] + is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_77_.toInt()] + is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_77_.toInt()] + } + i_54_-- + i_61_ = i + i_63_ = method152(i_61_, bzip2BlockEntry) + while (i_63_ > `is`!![i_61_]) { + i_61_++ + i_62_ = method153(bzip2BlockEntry).toInt() + i_63_ = i_63_ shl 1 or i_62_ + } + i_64_ = is_27_!![i_63_ - is_26_!![i_61_]] + } + } + bzip2BlockEntry.anInt3080 = 0 + bzip2BlockEntry.aByte3108 = 0.toByte() + bzip2BlockEntry.anIntArray3091[0] = 0 + for (i_78_ in 1..256) { + bzip2BlockEntry.anIntArray3091[i_78_] = bzip2BlockEntry.anIntArray3075[i_78_ - 1] + } + for (i_79_ in 1..256) { + bzip2BlockEntry.anIntArray3091[i_79_] += bzip2BlockEntry.anIntArray3091[i_79_ - 1] + } + for (i_80_ in 0 until i_59_) { + i_30_ = (anIntArray5786!![i_80_] and 0xff).toByte().toInt() + anIntArray5786!![bzip2BlockEntry.anIntArray3091[i_30_ and 0xff]] = anIntArray5786!![bzip2BlockEntry.anIntArray3091[i_30_ and 0xff]] or (i_80_ shl 8) + bzip2BlockEntry.anIntArray3091[i_30_ and 0xff]++ + } + bzip2BlockEntry.anInt3106 = anIntArray5786!![bzip2BlockEntry.anInt3083] shr 8 + bzip2BlockEntry.anInt3071 = 0 + bzip2BlockEntry.anInt3106 = anIntArray5786!![bzip2BlockEntry.anInt3106] + bzip2BlockEntry.anInt3070 = (bzip2BlockEntry.anInt3106 and 0xff).toByte().toInt() + bzip2BlockEntry.anInt3106 = bzip2BlockEntry.anInt3106 shr 8 + bzip2BlockEntry.anInt3071++ + bzip2BlockEntry.anInt3077 = i_59_ + method151(bzip2BlockEntry) + bool_28_ = if (bzip2BlockEntry.anInt3071 == bzip2BlockEntry.anInt3077 + 1 && bzip2BlockEntry.anInt3080 == 0) { + true + } else { + false + } + } + } - public static byte method147(BZIP2BlockEntry arg0) { - return (byte) method152(8, arg0); - } + fun method152(arg0: Int, arg1: BZIP2BlockEntry?): Int { + val i: Int + while (true) { + if (arg1!!.anInt3088 >= arg0) { + val i_93_ = arg1.anInt3078 shr arg1.anInt3088 - arg0 and (1 shl arg0) - 1 + arg1.anInt3088 -= arg0 + i = i_93_ + break + } + arg1.anInt3078 = arg1.anInt3078 shl 8 or (arg1.compressed!![arg1.startOffset].toInt() and 0xff) + arg1.anInt3088 += 8 + arg1.startOffset++ + arg1.anInt3085++ + } + return i + } - public static void method148() { - bzip2BlockEntry = null; - } + fun method151(arg0: BZIP2BlockEntry?) { + var i = arg0!!.aByte3108 + var i_81_ = arg0.anInt3080 + var i_82_ = arg0.anInt3071 + var i_83_ = arg0.anInt3070 + val `is` = anIntArray5786 + var i_84_ = arg0.anInt3106 + val is_85_ = arg0.decompressed + var i_86_ = arg0.anInt3100 + var i_87_ = arg0.decompressedLength + val i_88_ = i_87_ + val i_89_ = arg0.anInt3077 + 1 + while_68_@ while (true) { + if (i_81_ > 0) { + while (true) { + if (i_87_ == 0) { + break@while_68_ + } + if (i_81_ == 1) { + break + } + is_85_!![i_86_] = i + i_81_-- + i_86_++ + i_87_-- + } + if (i_87_ == 0) { + i_81_ = 1 + break + } + is_85_!![i_86_] = i + i_86_++ + i_87_-- + } + var bool = true + while (bool) { + bool = false + if (i_82_ == i_89_) { + i_81_ = 0 + break@while_68_ + } + i = i_83_.toByte() + i_84_ = `is`!![i_84_] + val i_90_ = (i_84_ and 0xff).toByte().toInt() + i_84_ = i_84_ shr 8 + i_82_++ + if (i_90_ != i_83_) { + i_83_ = i_90_ + if (i_87_ == 0) { + i_81_ = 1 + break@while_68_ + } + is_85_!![i_86_] = i + i_86_++ + i_87_-- + bool = true + } else if (i_82_ == i_89_) { + if (i_87_ == 0) { + i_81_ = 1 + break@while_68_ + } + is_85_!![i_86_] = i + i_86_++ + i_87_-- + bool = true + } + } + i_81_ = 2 + i_84_ = `is`!![i_84_] + var i_91_ = (i_84_ and 0xff).toByte().toInt() + i_84_ = i_84_ shr 8 + if (++i_82_ != i_89_) { + if (i_91_ != i_83_) { + i_83_ = i_91_ + } else { + i_81_ = 3 + i_84_ = `is`[i_84_] + i_91_ = (i_84_ and 0xff).toByte().toInt() + i_84_ = i_84_ shr 8 + if (++i_82_ != i_89_) { + if (i_91_ != i_83_) { + i_83_ = i_91_ + } else { + i_84_ = `is`[i_84_] + i_91_ = (i_84_ and 0xff).toByte().toInt() + i_84_ = i_84_ shr 8 + i_82_++ + i_81_ = (i_91_ and 0xff) + 4 + i_84_ = `is`[i_84_] + i_83_ = (i_84_ and 0xff).toByte().toInt() + i_84_ = i_84_ shr 8 + i_82_++ + } + } + } + } + } + val i_92_ = arg0.anInt3097 + arg0.anInt3097 += i_88_ - i_87_ + arg0.aByte3108 = i + arg0.anInt3080 = i_81_ + arg0.anInt3071 = i_82_ + arg0.anInt3070 = i_83_ + anIntArray5786 = `is` + arg0.anInt3106 = i_84_ + arg0.decompressed = is_85_ + arg0.anInt3100 = i_86_ + arg0.decompressedLength = i_87_ + } - public static void method149(BZIP2BlockEntry arg0) { - arg0.anInt3073 = 0; - for (int i = 0; i < 256; i++) { - if (arg0.aBooleanArray3103[i]) { - arg0.aByteArray3107[arg0.anInt3073] = (byte) i; - arg0.anInt3073++; - } - } - } + fun method145(arg0: IntArray, arg1: IntArray, arg2: IntArray, arg3: ByteArray, arg4: Int, arg5: Int, arg6: Int) { + var i = 0 + for (i_0_ in arg4..arg5) { + for (i_1_ in 0 until arg6) { + if (arg3[i_1_].toInt() == i_0_) { + arg2[i] = i_1_ + i++ + } + } + } + for (i_2_ in 0..22) { + arg1[i_2_] = 0 + } + for (i_3_ in 0 until arg6) { + arg1[arg3[i_3_] + 1]++ + } + for (i_4_ in 1..22) { + arg1[i_4_] += arg1[i_4_ - 1] + } + for (i_5_ in 0..22) { + arg0[i_5_] = 0 + } + var i_6_ = 0 + for (i_7_ in arg4..arg5) { + i_6_ += arg1[i_7_ + 1] - arg1[i_7_] + arg0[i_7_] = i_6_ - 1 + i_6_ = i_6_ shl 1 + } + for (i_8_ in arg4 + 1..arg5) { + arg1[i_8_] = (arg0[i_8_ - 1] + 1 shl 1) - arg1[i_8_] + } + } - public static byte method153(BZIP2BlockEntry arg0) { - return (byte) method152(1, arg0); - } + fun method147(arg0: BZIP2BlockEntry?): Byte { + return method152(8, arg0).toByte() + } - public static class BZIP2BlockEntry { + fun method148() { + bzip2BlockEntry = null + } - public int anInt3070; - public int anInt3071; - public boolean[] aBooleanArray3072; - public int anInt3073; - public int startOffset; - public int[] anIntArray3075; - public byte[] aByteArray3076; - public int anInt3077; - public int anInt3078; - public byte[] decompressed; - public int anInt3080; - public int anInt3081 = 0; - public int[][] anIntArrayArray3082 = new int[6][258]; - public int anInt3083; - public int[] anIntArray3084; - public int anInt3085; - public int[] anIntArray3086 = {2, 1, 1, 1, 2, 2, 2, 1, 3, 3, 3, 2, 0, 4, 0}; - public int anInt3087; - public int anInt3088; - public int anInt3089; - public int[] anIntArray3090; - public int[] anIntArray3091; - public int[] anIntArray3092; - public int decompressedLength; - public byte[] aByteArray3094 = new byte[18002]; - public int[][] anIntArrayArray3095; - public int anInt3096; - public int anInt3097; - public byte[][] aByteArrayArray3098; - public int[][] anIntArrayArray3099; - public int anInt3100; - public byte[] aByteArray3101; - public int anInt3102; - public boolean[] aBooleanArray3103; - public int anInt3105; - public int anInt3106; - public byte[] aByteArray3107; - public byte aByte3108; - public byte[] compressed; + fun method149(arg0: BZIP2BlockEntry?) { + arg0!!.anInt3073 = 0 + for (i in 0..255) { + if (arg0.aBooleanArray3103[i]) { + arg0.aByteArray3107[arg0.anInt3073] = i.toByte() + arg0.anInt3073++ + } + } + } - public BZIP2BlockEntry() { - aBooleanArray3072 = new boolean[16]; - anIntArray3090 = new int[6]; - startOffset = 0; - anInt3100 = 0; - aByteArray3101 = new byte[4096]; - anIntArray3091 = new int[257]; - aBooleanArray3103 = new boolean[256]; - anIntArray3075 = new int[256]; - anIntArray3092 = new int[16]; - anIntArrayArray3099 = new int[6][258]; - aByteArray3076 = new byte[18002]; - anIntArrayArray3095 = new int[6][258]; - aByteArrayArray3098 = new byte[6][258]; - aByteArray3107 = new byte[256]; - } + fun method153(arg0: BZIP2BlockEntry?): Byte { + return method152(1, arg0).toByte() + } - } + class BZIP2BlockEntry { + var anInt3070 = 0 + var anInt3071 = 0 + var aBooleanArray3072: BooleanArray + var anInt3073 = 0 + var startOffset = 0 + var anIntArray3075: IntArray + var aByteArray3076: ByteArray + var anInt3077 = 0 + var anInt3078 = 0 + var decompressed: ByteArray? = null + var anInt3080 = 0 + var anInt3081 = 0 + var anIntArrayArray3082 = Array(6) { IntArray(258) } + var anInt3083 = 0 + var anIntArray3084: IntArray = intArrayOf() + var anInt3085 = 0 + var anIntArray3086 = intArrayOf(2, 1, 1, 1, 2, 2, 2, 1, 3, 3, 3, 2, 0, 4, 0) + var anInt3087 = 0 + var anInt3088 = 0 + var anInt3089 = 0 + var anIntArray3090: IntArray + var anIntArray3091: IntArray + var anIntArray3092: IntArray + var decompressedLength = 0 + var aByteArray3094 = ByteArray(18002) + var anIntArrayArray3095: Array + var anInt3096 = 0 + var anInt3097 = 0 + var aByteArrayArray3098: Array + var anIntArrayArray3099: Array + var anInt3100 = 0 + var aByteArray3101: ByteArray + var anInt3102 = 0 + var aBooleanArray3103: BooleanArray + var anInt3105 = 0 + var anInt3106 = 0 + var aByteArray3107: ByteArray + var aByte3108: Byte = 0 + var compressed: ByteArray? = null + init { + aBooleanArray3072 = BooleanArray(16) + anIntArray3090 = IntArray(6) + aByteArray3101 = ByteArray(4096) + anIntArray3091 = IntArray(257) + aBooleanArray3103 = BooleanArray(256) + anIntArray3075 = IntArray(256) + anIntArray3092 = IntArray(16) + anIntArrayArray3099 = Array(6) { IntArray(258) } + aByteArray3076 = ByteArray(18002) + anIntArrayArray3095 = Array(6) { IntArray(258) } + aByteArrayArray3098 = Array(6) { ByteArray(258) } + aByteArray3107 = ByteArray(256) + } + } } \ No newline at end of file From a2b4be7580ba88421742445a4ddd99771d78b004 Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 15:37:24 +0100 Subject: [PATCH 03/24] Tidy up BZIP2 compressor --- .../displee/compress/type/BZIP2Compressor.kt | 39 ++++++------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt index 80be97c..f19909f 100644 --- a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt @@ -584,11 +584,11 @@ object BZIP2Compressor { class BZIP2BlockEntry { var anInt3070 = 0 var anInt3071 = 0 - var aBooleanArray3072: BooleanArray + var aBooleanArray3072: BooleanArray = BooleanArray(16) var anInt3073 = 0 var startOffset = 0 - var anIntArray3075: IntArray - var aByteArray3076: ByteArray + var anIntArray3075: IntArray = IntArray(256) + var aByteArray3076: ByteArray = ByteArray(18002) var anInt3077 = 0 var anInt3078 = 0 var decompressed: ByteArray? = null @@ -602,39 +602,24 @@ object BZIP2Compressor { var anInt3087 = 0 var anInt3088 = 0 var anInt3089 = 0 - var anIntArray3090: IntArray - var anIntArray3091: IntArray - var anIntArray3092: IntArray + var anIntArray3090: IntArray = IntArray(6) + var anIntArray3091: IntArray = IntArray(257) + var anIntArray3092: IntArray = IntArray(16) var decompressedLength = 0 var aByteArray3094 = ByteArray(18002) - var anIntArrayArray3095: Array + var anIntArrayArray3095: Array = Array(6) { IntArray(258) } var anInt3096 = 0 var anInt3097 = 0 - var aByteArrayArray3098: Array - var anIntArrayArray3099: Array + var aByteArrayArray3098: Array = Array(6) { ByteArray(258) } + var anIntArrayArray3099: Array = Array(6) { IntArray(258) } var anInt3100 = 0 - var aByteArray3101: ByteArray + var aByteArray3101: ByteArray = ByteArray(4096) var anInt3102 = 0 - var aBooleanArray3103: BooleanArray + var aBooleanArray3103: BooleanArray = BooleanArray(256) var anInt3105 = 0 var anInt3106 = 0 - var aByteArray3107: ByteArray + var aByteArray3107: ByteArray = ByteArray(256) var aByte3108: Byte = 0 var compressed: ByteArray? = null - - init { - aBooleanArray3072 = BooleanArray(16) - anIntArray3090 = IntArray(6) - aByteArray3101 = ByteArray(4096) - anIntArray3091 = IntArray(257) - aBooleanArray3103 = BooleanArray(256) - anIntArray3075 = IntArray(256) - anIntArray3092 = IntArray(16) - anIntArrayArray3099 = Array(6) { IntArray(258) } - aByteArray3076 = ByteArray(18002) - anIntArrayArray3095 = Array(6) { IntArray(258) } - aByteArrayArray3098 = Array(6) { ByteArray(258) } - aByteArray3107 = ByteArray(256) - } } } \ No newline at end of file From 0e09989c291521e753c7ba29aa7f8c13382f2e4b Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 15:38:23 +0100 Subject: [PATCH 04/24] Rename .java to .kt --- .../util/Whirlpool.java => kotlin/com/displee/util/Whirlpool.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/{java/com/displee/util/Whirlpool.java => kotlin/com/displee/util/Whirlpool.kt} (100%) diff --git a/src/main/java/com/displee/util/Whirlpool.java b/src/main/kotlin/com/displee/util/Whirlpool.kt similarity index 100% rename from src/main/java/com/displee/util/Whirlpool.java rename to src/main/kotlin/com/displee/util/Whirlpool.kt From 09d7aa34389c59af36efd4097d93ebf226711389 Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 15:38:23 +0100 Subject: [PATCH 05/24] Convert Whirlpool to Kotlin #38 --- src/main/kotlin/com/displee/util/Whirlpool.kt | 386 +++++++++--------- 1 file changed, 202 insertions(+), 184 deletions(-) diff --git a/src/main/kotlin/com/displee/util/Whirlpool.kt b/src/main/kotlin/com/displee/util/Whirlpool.kt index d8f7afe..6df2893 100644 --- a/src/main/kotlin/com/displee/util/Whirlpool.kt +++ b/src/main/kotlin/com/displee/util/Whirlpool.kt @@ -1,191 +1,209 @@ -package com.displee.util; +package com.displee.util -public class Whirlpool { +class Whirlpool { + private val block = LongArray(8) + private val hash = LongArray(8) + private val aLongArray6637 = LongArray(8) + private val aLongArray6638 = LongArray(8) + private val state = LongArray(8) + private val bitLength = ByteArray(32) + private val buffer = ByteArray(64) + private var bufferBits = 0 + private var bufferPosition = 0 + fun NESSIEinit() { + for (i in 0..31) { + bitLength[i] = 0.toByte() + } + bufferPosition = 0 + bufferBits = 0 + buffer[0] = 0.toByte() + for (i_3_ in 0..7) { + hash[i_3_] = 0L + } + } - private static long[][] aLongArrayArray6630 = new long[8][256]; - private static long[] aLongArray6631 = new long[11]; + private fun processBuffer() { + var i_4_ = 0 + var i_5_ = 0 + while (i_4_ < 8) { + block[i_4_] = + buffer[i_5_].toLong() shl 56 xor (buffer[i_5_ + 1].toLong() and 0xffL shl 48) xor (buffer[2 + i_5_].toLong() and 0xffL shl 40) xor (buffer[i_5_ + 3].toLong() and 0xffL shl 32) xor (buffer[i_5_ + 4].toLong() and 0xffL shl 24) xor (buffer[5 + i_5_].toLong() and 0xffL shl 16) xor (buffer[i_5_ + 6].toLong() and 0xffL shl 8) xor (buffer[7 + i_5_].toLong() and 0xffL) + i_4_++ + i_5_ += 8 + } + i_4_ = 0 + while (i_4_ < 8) { + state[i_4_] = block[i_4_] xor hash[i_4_].also { aLongArray6637[i_4_] = it } + i_4_++ + } + i_4_ = 1 + while (i_4_ <= 10) { + i_5_ = 0 + while (i_5_ < 8) { + aLongArray6638[i_5_] = 0L + var i_6_ = 0 + var i_7_ = 56 + while (i_6_ < 8) { + aLongArray6638[i_5_] = aLongArray6638[i_5_] xor aLongArrayArray6630[i_6_][(aLongArray6637[i_5_ - i_6_ and 0x7] ushr i_7_).toInt() and 0xff] + i_6_++ + i_7_ -= 8 + } + i_5_++ + } + i_5_ = 0 + while (i_5_ < 8) { + aLongArray6637[i_5_] = aLongArray6638[i_5_] + i_5_++ + } + aLongArray6637[0] = aLongArray6637[0] xor aLongArray6631[i_4_] + i_5_ = 0 + while (i_5_ < 8) { + aLongArray6638[i_5_] = aLongArray6637[i_5_] + var i_8_ = 0 + var i_9_ = 56 + while (i_8_ < 8) { + aLongArray6638[i_5_] = aLongArray6638[i_5_] xor aLongArrayArray6630[i_8_][(state[i_5_ - i_8_ and 0x7] ushr i_9_).toInt() and 0xff] + i_8_++ + i_9_ -= 8 + } + i_5_++ + } + i_5_ = 0 + while (i_5_ < 8) { + state[i_5_] = aLongArray6638[i_5_] + i_5_++ + } + i_4_++ + } + i_4_ = 0 + while (i_4_ < 8) { + hash[i_4_] = hash[i_4_] xor (state[i_4_] xor block[i_4_]) + i_4_++ + } + } - private long[] block = new long[8]; - private long[] hash = new long[8]; - private long[] aLongArray6637 = new long[8]; - private long[] aLongArray6638 = new long[8]; - private long[] state = new long[8]; - private byte[] bitLength = new byte[32]; - private byte[] buffer = new byte[64]; - private int bufferBits = 0; - private int bufferPosition = 0; + fun NESSIEfinalize(digest: ByteArray, i: Int) { + buffer[bufferPosition] = (buffer[bufferPosition].toInt() or (128 ushr (bufferBits and 0x7))).toByte() + ++bufferPosition + if (bufferPosition > 32) { + while (bufferPosition < 64) { + buffer[bufferPosition++] = 0.toByte() + } + processBuffer() + bufferPosition = 0 + } + while (bufferPosition < 32) { + buffer[bufferPosition++] = 0.toByte() + } + System.arraycopy(bitLength, 0, buffer, 32, 32) + processBuffer() + var i_16_ = 0 + var i_17_ = i + while (i_16_ < 8) { + val l = hash[i_16_] + digest[i_17_] = (l ushr 56).toInt().toByte() + digest[i_17_ + 1] = (l ushr 48).toInt().toByte() + digest[i_17_ + 2] = (l ushr 40).toInt().toByte() + digest[i_17_ + 3] = (l ushr 32).toInt().toByte() + digest[i_17_ + 4] = (l ushr 24).toInt().toByte() + digest[i_17_ + 5] = (l ushr 16).toInt().toByte() + digest[i_17_ + 6] = (l ushr 8).toInt().toByte() + digest[i_17_ + 7] = l.toInt().toByte() + i_16_++ + i_17_ += 8 + } + } - static { - for (int i = 0; i < 256; i++) { - int i_37_ = "\u1823\uc6e8\u87b8\u014f\u36a6\ud2f5\u796f\u9152\u60bc\u9b8e\ua30c\u7b35\u1de0\ud7c2\u2e4b\ufe57\u1577\u37e5\u9ff0\u4ada\u58c9\u290a\ub1a0\u6b85\ubd5d\u10f4\ucb3e\u0567\ue427\u418b\ua77d\u95d8\ufbee\u7c66\udd17\u479e\uca2d\ubf07\uad5a\u8333\u6302\uaa71\uc819\u49d9\uf2e3\u5b88\u9a26\u32b0\ue90f\ud580\ubecd\u3448\uff7a\u905f\u2068\u1aae\ub454\u9322\u64f1\u7312\u4008\uc3ec\udba1\u8d3d\u9700\ucf2b\u7682\ud61b\ub5af\u6a50\u45f3\u30ef\u3f55\ua2ea\u65ba\u2fc0\ude1c\ufd4d\u9275\u068a\ub2e6\u0e1f\u62d4\ua896\uf9c5\u2559\u8472\u394c\u5e78\u388c\ud1a5\ue261\ub321\u9c1e\u43c7\ufc04\u5199\u6d0d\ufadf\u7e24\u3bab\uce11\u8f4e\ub7eb\u3c81\u94f7\ub913\u2cd3\ue76e\uc403\u5644\u7fa9\u2abb\uc153\udc0b\u9d6c\u3174\uf646\uac89\u14e1\u163a\u6909\u70b6\ud0ed\ucc42\u98a4\u285c\uf886".charAt(i / 2); - long l = ((i & 0x1) == 0 ? (long) (i_37_ >>> 8) : (long) (i_37_ & 0xff)); - long l_38_ = l << 1; - if (l_38_ >= 256L) { - l_38_ ^= 0x11dL; - } - long l_39_ = l_38_ << 1; - if (l_39_ >= 256L) { - l_39_ ^= 0x11dL; - } - long l_40_ = l_39_ ^ l; - long l_41_ = l_39_ << 1; - if (l_41_ >= 256L) { - l_41_ ^= 0x11dL; - } - long l_42_ = l_41_ ^ l; - aLongArrayArray6630[0][i] = (l << 56 | l << 48 | l_39_ << 40 | l << 32 | l_41_ << 24 | l_40_ << 16 | l_38_ << 8 | l_42_); - for (int i_43_ = 1; i_43_ < 8; i_43_++) { - aLongArrayArray6630[i_43_][i] = (aLongArrayArray6630[i_43_ - 1][i] >>> 8 | aLongArrayArray6630[i_43_ - 1][i] << 56); - } - } - aLongArray6631[0] = 0L; - for (int i = 1; i <= 10; i++) { - int i_44_ = 8 * (i - 1); - aLongArray6631[i] = (aLongArrayArray6630[0][i_44_] & ~0xffffffffffffffL ^ aLongArrayArray6630[1][i_44_ + 1] & 0xff000000000000L ^ aLongArrayArray6630[2][i_44_ + 2] & 0xff0000000000L ^ aLongArrayArray6630[3][i_44_ + 3] & 0xff00000000L ^ aLongArrayArray6630[4][4 + i_44_] & 0xff000000L ^ aLongArrayArray6630[5][5 + i_44_] & 0xff0000L ^ aLongArrayArray6630[6][6 + i_44_] & 0xff00L ^ aLongArrayArray6630[7][7 + i_44_] & 0xffL); - } - } + fun NESSIEadd(source: ByteArray, sourceBits: Long) { + var sourceBits = sourceBits + var i = 0 + val i_23_ = 8 - (sourceBits.toInt() and 0x7) and 0x7 + val i_24_ = bufferBits and 0x7 + var l_25_ = sourceBits + var i_26_ = 31 + var i_27_ = 0 + while ( /**/i_26_ >= 0) { + i_27_ += (bitLength[i_26_].toInt() and 0xff) + (l_25_.toInt() and 0xff) + bitLength[i_26_] = i_27_.toByte() + i_27_ = i_27_ ushr 8 + l_25_ = l_25_ ushr 8 + i_26_-- + } + while (sourceBits > 8L) { + val i_28_ = source[i].toInt() shl i_23_ and 0xff or (source[i + 1].toInt() and 0xff ushr 8 - i_23_) + if (i_28_ < 0 || i_28_ >= 256) { + throw RuntimeException() + } + buffer[bufferPosition] = (buffer[bufferPosition].toInt() or (i_28_ ushr i_24_)).toByte() + ++bufferPosition + bufferBits += 8 - i_24_ + if (bufferBits == 512) { + processBuffer() + bufferPosition = 0 + bufferBits = 0 + } + buffer[bufferPosition] = (i_28_ shl 8 - i_24_ and 0xff).toByte() + bufferBits += i_24_ + sourceBits -= 8L + i++ + } + val i_29_: Int + if (sourceBits > 0L) { + i_29_ = source[i].toInt() shl i_23_ and 0xff + buffer[bufferPosition] = (buffer[bufferPosition].toInt() or (i_29_ ushr i_24_)).toByte() + } else { + i_29_ = 0 + } + if (i_24_.toLong() + sourceBits < 8L) { + bufferBits += sourceBits.toInt() + } else { + ++bufferPosition + bufferBits += 8 - i_24_ + sourceBits -= (8 - i_24_).toLong() + if (bufferBits == 512) { + processBuffer() + bufferPosition = 0 + bufferBits = 0 + } + buffer[bufferPosition] = (i_29_ shl 8 - i_24_ and 0xff).toByte() + bufferBits += sourceBits.toInt() + } + } - void NESSIEinit() { - for (int i = 0; i < 32; i++) { - bitLength[i] = (byte) 0; - } - bufferPosition = 0; - bufferBits = 0; - buffer[0] = (byte) 0; - for (int i_3_ = 0; i_3_ < 8; i_3_++) { - hash[i_3_] = 0L; - } - } - - private void processBuffer() { - int i_4_ = 0; - int i_5_ = 0; - while (i_4_ < 8) { - block[i_4_] = ((long) buffer[i_5_] << 56 ^ ((long) buffer[i_5_ + 1] & 0xffL) << 48 ^ ((long) buffer[2 + i_5_] & 0xffL) << 40 ^ ((long) buffer[i_5_ + 3] & 0xffL) << 32 ^ ((long) buffer[i_5_ + 4] & 0xffL) << 24 ^ ((long) buffer[5 + i_5_] & 0xffL) << 16 ^ ((long) buffer[i_5_ + 6] & 0xffL) << 8 ^ ((long) buffer[7 + i_5_] & 0xffL)); - i_4_++; - i_5_ += 8; - } - for (i_4_ = 0; i_4_ < 8; i_4_++) { - state[i_4_] = block[i_4_] ^ (aLongArray6637[i_4_] = hash[i_4_]); - } - for (i_4_ = 1; i_4_ <= 10; i_4_++) { - for (i_5_ = 0; i_5_ < 8; i_5_++) { - aLongArray6638[i_5_] = 0L; - int i_6_ = 0; - int i_7_ = 56; - while (i_6_ < 8) { - aLongArray6638[i_5_] ^= (aLongArrayArray6630[i_6_][(int) ((aLongArray6637[i_5_ - i_6_ & 0x7]) >>> i_7_) & 0xff]); - i_6_++; - i_7_ -= 8; - } - } - for (i_5_ = 0; i_5_ < 8; i_5_++) { - aLongArray6637[i_5_] = aLongArray6638[i_5_]; - } - aLongArray6637[0] ^= aLongArray6631[i_4_]; - for (i_5_ = 0; i_5_ < 8; i_5_++) { - aLongArray6638[i_5_] = aLongArray6637[i_5_]; - int i_8_ = 0; - int i_9_ = 56; - while (i_8_ < 8) { - aLongArray6638[i_5_] ^= (aLongArrayArray6630[i_8_][(int) ((state[i_5_ - i_8_ & 0x7]) >>> i_9_) & 0xff]); - i_8_++; - i_9_ -= 8; - } - } - for (i_5_ = 0; i_5_ < 8; i_5_++) { - state[i_5_] = aLongArray6638[i_5_]; - } - } - for (i_4_ = 0; i_4_ < 8; i_4_++) { - hash[i_4_] ^= state[i_4_] ^ block[i_4_]; - } - } - - void NESSIEfinalize(byte[] digest, int i) { - buffer[(bufferPosition)] |= 128 >>> (bufferBits & 0x7); - ++bufferPosition; - if (bufferPosition > 32) { - while (bufferPosition < 64) { - buffer[bufferPosition++] = (byte) 0; - } - processBuffer(); - bufferPosition = 0; - } - while (bufferPosition < 32) { - buffer[bufferPosition++] = (byte) 0; - } - System.arraycopy(bitLength, 0, buffer, 32, 32); - processBuffer(); - int i_16_ = 0; - int i_17_ = i; - while (i_16_ < 8) { - long l = hash[i_16_]; - digest[i_17_] = (byte) (int) (l >>> 56); - digest[i_17_ + 1] = (byte) (int) (l >>> 48); - digest[i_17_ + 2] = (byte) (int) (l >>> 40); - digest[i_17_ + 3] = (byte) (int) (l >>> 32); - digest[i_17_ + 4] = (byte) (int) (l >>> 24); - digest[i_17_ + 5] = (byte) (int) (l >>> 16); - digest[i_17_ + 6] = (byte) (int) (l >>> 8); - digest[i_17_ + 7] = (byte) (int) l; - i_16_++; - i_17_ += 8; - } - } - - void NESSIEadd(byte[] source, long sourceBits) { - int i = 0; - int i_23_ = 8 - ((int) sourceBits & 0x7) & 0x7; - int i_24_ = bufferBits & 0x7; - long l_25_ = sourceBits; - int i_26_ = 31; - int i_27_ = 0; - for (/**/; i_26_ >= 0; i_26_--) { - i_27_ += ((bitLength[i_26_] & 0xff) + ((int) l_25_ & 0xff)); - bitLength[i_26_] = (byte) i_27_; - i_27_ >>>= 8; - l_25_ >>>= 8; - } - while (sourceBits > 8L) { - int i_28_ = source[i] << i_23_ & 0xff | (source[i + 1] & 0xff) >>> 8 - i_23_; - if (i_28_ < 0 || i_28_ >= 256) { - throw new RuntimeException(); - } - buffer[bufferPosition] |= i_28_ >>> i_24_; - ++bufferPosition; - bufferBits += 8 - i_24_; - if (bufferBits == 512) { - processBuffer(); - bufferPosition = 0; - bufferBits = 0; - } - buffer[bufferPosition] = (byte) (i_28_ << 8 - i_24_ & 0xff); - bufferBits += i_24_; - sourceBits -= 8L; - i++; - } - int i_29_; - if (sourceBits > 0L) { - i_29_ = source[i] << i_23_ & 0xff; - buffer[bufferPosition] |= i_29_ >>> i_24_; - } else { - i_29_ = 0; - } - if ((long) i_24_ + sourceBits < 8L) { - bufferBits += sourceBits; - } else { - ++bufferPosition; - bufferBits += 8 - i_24_; - sourceBits -= (long) (8 - i_24_); - if (bufferBits == 512) { - processBuffer(); - bufferPosition = 0; - bufferBits = 0; - } - buffer[bufferPosition] = (byte) (i_29_ << 8 - i_24_ & 0xff); - bufferBits += (int) sourceBits; - } - } + companion object { + private val aLongArrayArray6630 = Array(8) { LongArray(256) } + private val aLongArray6631 = LongArray(11) + init { + for (i in 0..255) { + val i_37_ = + "\u1823\uc6e8\u87b8\u014f\u36a6\ud2f5\u796f\u9152\u60bc\u9b8e\ua30c\u7b35\u1de0\ud7c2\u2e4b\ufe57\u1577\u37e5\u9ff0\u4ada\u58c9\u290a\ub1a0\u6b85\ubd5d\u10f4\ucb3e\u0567\ue427\u418b\ua77d\u95d8\ufbee\u7c66\udd17\u479e\uca2d\ubf07\uad5a\u8333\u6302\uaa71\uc819\u49d9\uf2e3\u5b88\u9a26\u32b0\ue90f\ud580\ubecd\u3448\uff7a\u905f\u2068\u1aae\ub454\u9322\u64f1\u7312\u4008\uc3ec\udba1\u8d3d\u9700\ucf2b\u7682\ud61b\ub5af\u6a50\u45f3\u30ef\u3f55\ua2ea\u65ba\u2fc0\ude1c\ufd4d\u9275\u068a\ub2e6\u0e1f\u62d4\ua896\uf9c5\u2559\u8472\u394c\u5e78\u388c\ud1a5\ue261\ub321\u9c1e\u43c7\ufc04\u5199\u6d0d\ufadf\u7e24\u3bab\uce11\u8f4e\ub7eb\u3c81\u94f7\ub913\u2cd3\ue76e\uc403\u5644\u7fa9\u2abb\uc153\udc0b\u9d6c\u3174\uf646\uac89\u14e1\u163a\u6909\u70b6\ud0ed\ucc42\u98a4\u285c\uf886"[i / 2].toInt() + val l = if (i and 0x1 == 0) (i_37_ ushr 8).toLong() else (i_37_ and 0xff).toLong() + var l_38_ = l shl 1 + if (l_38_ >= 256L) { + l_38_ = l_38_ xor 0x11dL + } + var l_39_ = l_38_ shl 1 + if (l_39_ >= 256L) { + l_39_ = l_39_ xor 0x11dL + } + val l_40_ = l_39_ xor l + var l_41_ = l_39_ shl 1 + if (l_41_ >= 256L) { + l_41_ = l_41_ xor 0x11dL + } + val l_42_ = l_41_ xor l + aLongArrayArray6630[0][i] = l shl 56 or (l shl 48) or (l_39_ shl 40) or (l shl 32) or (l_41_ shl 24) or (l_40_ shl 16) or (l_38_ shl 8) or l_42_ + for (i_43_ in 1..7) { + aLongArrayArray6630[i_43_][i] = aLongArrayArray6630[i_43_ - 1][i] ushr 8 or (aLongArrayArray6630[i_43_ - 1][i] shl 56) + } + } + aLongArray6631[0] = 0L + for (i in 1..10) { + val i_44_ = 8 * (i - 1) + aLongArray6631[i] = + aLongArrayArray6630[0][i_44_] and 0xffffffffffffffL.inv() xor (aLongArrayArray6630[1][i_44_ + 1] and 0xff000000000000L) xor (aLongArrayArray6630[2][i_44_ + 2] and 0xff0000000000L) xor (aLongArrayArray6630[3][i_44_ + 3] and 0xff00000000L) xor (aLongArrayArray6630[4][4 + i_44_] and 0xff000000L) xor (aLongArrayArray6630[5][5 + i_44_] and 0xff0000L) xor (aLongArrayArray6630[6][6 + i_44_] and 0xff00L) xor (aLongArrayArray6630[7][7 + i_44_] and 0xffL) + } + } + } } \ No newline at end of file From 3d02774b4b9de49b02b8d40b82c6d00494470441 Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 16:09:38 +0100 Subject: [PATCH 06/24] Extract compressor interface --- .../displee/cache/index/archive/Archive317.kt | 40 ++++++++----------- .../com/displee/compress/CompressionExt.kt | 28 ++++--------- .../displee/compress/type/BZIP2Compressor.kt | 15 ++++--- .../com/displee/compress/type/Compressor.kt | 22 ++++++++++ .../displee/compress/type/EmptyCompressor.kt | 13 ++++++ .../displee/compress/type/GZIPCompressor.kt | 27 +++++-------- .../displee/compress/type/LZMACompressor.kt | 14 +++---- 7 files changed, 82 insertions(+), 77 deletions(-) create mode 100644 src/main/kotlin/com/displee/compress/type/Compressor.kt create mode 100644 src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt diff --git a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt index b7fed6b..302c9e2 100644 --- a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt +++ b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt @@ -3,6 +3,7 @@ package com.displee.cache.index.archive import com.displee.cache.index.archive.file.File import com.displee.compress.CompressionType import com.displee.compress.type.BZIP2Compressor +import com.displee.compress.type.EmptyCompressor import com.displee.compress.type.GZIPCompressor import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer @@ -17,60 +18,53 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { override fun read(buffer: InputBuffer) { read = true if (compressionType == CompressionType.GZIP) { - files[0] = File(0, GZIPCompressor.deflate317(buffer.raw())) + val compressor = GZIPCompressor + files[0] = File(0, compressor.deflate317(buffer.raw())) return } - val decompressed: ByteArray var decompressedLength = buffer.read24BitInt() var compressedLength = buffer.read24BitInt() if (decompressedLength != compressedLength) { - decompressed = BZIP2Compressor.decompress317(decompressedLength, compressedLength, buffer) extracted = true - } else { - decompressed = buffer.readBytes(buffer.remaining()) } + var compressor = if (extracted) BZIP2Compressor else EmptyCompressor + val decompressed = compressor.decompress(buffer, byteArrayOf(), compressedLength, decompressedLength, 0) val metaBuffer = InputBuffer(decompressed) val filesLength = metaBuffer.readUnsignedShort() val filesBuffer = InputBuffer(decompressed) filesBuffer.offset = metaBuffer.offset + filesLength * 10 + compressor = if (extracted) EmptyCompressor else BZIP2Compressor for (i in 0 until filesLength) { val fileName = metaBuffer.readInt() decompressedLength = metaBuffer.read24BitInt() compressedLength = metaBuffer.read24BitInt() - val data: ByteArray = if (extracted) { - filesBuffer.readBytes(decompressedLength) - } else { - BZIP2Compressor.decompress317(decompressedLength, compressedLength, filesBuffer) - } + val data: ByteArray = compressor.decompress(buffer, byteArrayOf(), compressedLength, decompressedLength, 0) files[i] = File(i, data, fileName) } } override fun write(): ByteArray { if (compressionType == CompressionType.GZIP) { - return GZIPCompressor.inflate317(first()?.data ?: byteArrayOf()) + val compressor = GZIPCompressor + return compressor.compress(first()?.data ?: byteArrayOf()) } val metaBuffer = OutputBuffer(2 + files.size * 10) metaBuffer.writeShort(files.size) val filesBuffer = OutputBuffer(2048) + var compressor = if (extracted) EmptyCompressor else BZIP2Compressor for (file in files.values) { val fileData = file.data ?: continue metaBuffer.writeInt(file.hashName).write24BitInt(fileData.size) - val toWrite = if (extracted) fileData else BZIP2Compressor.compress(file.data) - metaBuffer.write24BitInt(toWrite?.size ?: 0) - if (toWrite != null) { - filesBuffer.writeBytes(toWrite) - } + val toWrite = compressor.compress(fileData) + metaBuffer.write24BitInt(toWrite.size) + filesBuffer.writeBytes(toWrite) } metaBuffer.writeBytes(filesBuffer.array()) val decompressed = metaBuffer.array() - val compressed = if (extracted) BZIP2Compressor.compress(decompressed) else decompressed - val compressedSize = compressed?.size ?: 0 - val buffer = OutputBuffer(compressedSize + 6) - buffer.write24BitInt(decompressed.size).write24BitInt(compressedSize) - if (compressed != null) { - buffer.writeBytes(compressed) - } + compressor = if (extracted) BZIP2Compressor else EmptyCompressor + val compressed = compressor.compress(decompressed) + val buffer = OutputBuffer(compressed.size + 6) + buffer.write24BitInt(decompressed.size).write24BitInt(compressed.size).writeBytes(compressed) return buffer.array() } diff --git a/src/main/kotlin/com/displee/compress/CompressionExt.kt b/src/main/kotlin/com/displee/compress/CompressionExt.kt index ac8e8a7..43c0ab7 100644 --- a/src/main/kotlin/com/displee/compress/CompressionExt.kt +++ b/src/main/kotlin/com/displee/compress/CompressionExt.kt @@ -1,28 +1,20 @@ package com.displee.compress import com.displee.cache.index.archive.ArchiveSector -import com.displee.compress.type.BZIP2Compressor -import com.displee.compress.type.GZIPCompressor -import com.displee.compress.type.LZMACompressor +import com.displee.compress.type.* import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer fun ByteArray.compress(compressionType: CompressionType, xteas: IntArray? = null, revision: Int = -1): ByteArray { - val compressed: ByteArray? = when (compressionType) { - CompressionType.NONE -> this - CompressionType.BZIP2 -> BZIP2Compressor.compress(this) - CompressionType.GZIP -> GZIPCompressor.deflate(this) - CompressionType.LZMA -> LZMACompressor.compress(this) - } - val compressedSize = compressed?.size ?: 0 + val compressor: Compressor = Compressor.get(compressionType) + val compressed: ByteArray = compressor.compress(this) + val compressedSize = compressed.size val buffer = OutputBuffer(9 + compressedSize + if (revision == -1) 0 else 2) buffer.writeByte(compressionType.ordinal).writeInt(compressedSize) if (compressionType != CompressionType.NONE) { buffer.writeInt(size) } - if (compressed != null) { - buffer.writeBytes(compressed) - } + buffer.writeBytes(compressed) if (xteas != null && (xteas[0] != 0 || xteas[1] != 0 || xteas[2] != 0 || 0 != xteas[3])) { check(compressionType != CompressionType.BZIP2) { "BZIP2 compression doesn't work with xtea encryption." } buffer.encryptXTEA(xteas, 5, buffer.offset) @@ -46,12 +38,6 @@ fun ArchiveSector.decompress(keys: IntArray? = null): ByteArray { if (compressionType != CompressionType.NONE) { decompressedSize = buffer.readInt() and 0xFFFFFF } - var decompressed = ByteArray(decompressedSize) - when (compressionType) { - CompressionType.NONE -> decompressed = buffer.readBytes(compressedSize) - CompressionType.BZIP2 -> BZIP2Compressor.decompress(decompressed, decompressed.size, compressedData, compressedSize, 9) - CompressionType.GZIP -> if (!GZIPCompressor.inflate(buffer, decompressed)) return byteArrayOf() - CompressionType.LZMA -> decompressed = LZMACompressor.decompress(buffer, decompressedSize) - } - return decompressed + val decompressor: Compressor = Compressor.get(compressionType) + return decompressor.decompress(buffer, compressedData, compressedSize, decompressedSize) } \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt index f19909f..31681ea 100644 --- a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt @@ -10,7 +10,7 @@ import java.io.ByteArrayOutputStream * @author Jagex * @author Displee */ -object BZIP2Compressor { +object BZIP2Compressor : Compressor { /** * The current bzip2 block entry. */ @@ -26,8 +26,8 @@ object BZIP2Compressor { * @param bytes The uncompressed BZIP2 file. * @return The compressed BZIP2 file. */ - fun compress(bytes: ByteArray?): ByteArray? { - var bytes = bytes ?: return null + override fun compress(bytes: ByteArray): ByteArray { + var bytes = bytes try { ByteArrayInputStream(bytes).use { `is` -> val bout = ByteArrayOutputStream() @@ -45,14 +45,13 @@ object BZIP2Compressor { } } catch (e: Exception) { e.printStackTrace() - return null + return byteArrayOf() } } - fun decompress317(decompressedLength: Int, compressedLength: Int, inputBuffer: InputBuffer): ByteArray { - val decompressed = ByteArray(decompressedLength) - val compressed = inputBuffer.readBytes(compressedLength) - decompress(decompressed, decompressed.size, compressed, compressed.size, 0) + override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + val decompressed = ByteArray(decompressedSize) + decompress(decompressed, decompressed.size, compressedData, compressedSize, offset) return decompressed } diff --git a/src/main/kotlin/com/displee/compress/type/Compressor.kt b/src/main/kotlin/com/displee/compress/type/Compressor.kt new file mode 100644 index 0000000..d25a4e6 --- /dev/null +++ b/src/main/kotlin/com/displee/compress/type/Compressor.kt @@ -0,0 +1,22 @@ +package com.displee.compress.type + +import com.displee.compress.CompressionType +import com.displee.io.impl.InputBuffer + +interface Compressor { + + fun compress(bytes: ByteArray): ByteArray + + fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int = 9): ByteArray + + companion object { + fun get(type: CompressionType): Compressor { + return when (type) { + CompressionType.NONE -> EmptyCompressor + CompressionType.BZIP2 -> BZIP2Compressor + CompressionType.GZIP -> GZIPCompressor + CompressionType.LZMA -> LZMACompressor + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt b/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt new file mode 100644 index 0000000..e33173a --- /dev/null +++ b/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt @@ -0,0 +1,13 @@ +package com.displee.compress.type + +import com.displee.io.impl.InputBuffer + +object EmptyCompressor : Compressor { + override fun compress(bytes: ByteArray): ByteArray { + return bytes + } + + override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + return buffer.readBytes(compressedSize) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt index 309ed9a..f14d784 100644 --- a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt @@ -8,11 +8,19 @@ import java.util.zip.GZIPInputStream import java.util.zip.GZIPOutputStream import java.util.zip.Inflater -object GZIPCompressor { +object GZIPCompressor : Compressor { private var inflater: Inflater? = null private val gzipBuffer = ByteArray(1000_000) //because in 317 Jagex stores a lot of data in one file + override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + val decompressed = ByteArray(decompressedSize) + if (!inflate(buffer, decompressed)) { + return byteArrayOf() + } + return decompressed + } + fun inflate(buffer: InputBuffer, data: ByteArray): Boolean { val bytes = buffer.raw() val offset = buffer.offset @@ -31,11 +39,11 @@ object GZIPCompressor { return true } - fun deflate(data: ByteArray): ByteArray { + override fun compress(bytes: ByteArray): ByteArray { val compressed = ByteArrayOutputStream() try { val gzipOutputStream = GZIPOutputStream(compressed) - gzipOutputStream.write(data) + gzipOutputStream.write(bytes) gzipOutputStream.finish() gzipOutputStream.close() } catch (e: Exception) { @@ -44,19 +52,6 @@ object GZIPCompressor { return compressed.toByteArray() } - fun inflate317(uncompressed: ByteArray): ByteArray { - val bout = ByteArrayOutputStream() - try { - GZIPOutputStream(bout).use { os -> - os.write(uncompressed) - os.finish() - } - } catch (e: IOException) { - e.printStackTrace() - } - return bout.toByteArray() - } - fun deflate317(data: ByteArray?): ByteArray { var read = 0 try { diff --git a/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt b/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt index 6b27097..f12f60d 100644 --- a/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt @@ -13,7 +13,7 @@ import java.io.* * A class handling LZMA compression. * @author Displee (credits to Techdaan) */ -object LZMACompressor { +object LZMACompressor : Compressor { private val DECODER = Decoder() private val ENCODER = Encoder() @@ -33,10 +33,10 @@ object LZMACompressor { ENCODER.setEndMarkerMode(false) } - fun compress(decompressed: ByteArray): ByteArray { + override fun compress(bytes: ByteArray): ByteArray { val baos = ByteArrayOutputStream() try { - val bais = ByteArrayInputStream(decompressed) + val bais = ByteArrayInputStream(bytes) val lzma = LzmaOutputStream(baos, ENCODER_WRAPPER) bais.writeTo(lzma) baos.close() @@ -48,15 +48,11 @@ object LZMACompressor { return baos.toByteArray() } - fun decompress(buffer: InputBuffer, decompressedLength: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { val output = OutputBuffer(buffer.remaining()) output.writeBytes(buffer.raw(), buffer.offset, buffer.remaining()) - return decompress(output.raw(), decompressedLength) - } - - fun decompress(compressed: ByteArray, decompressedLength: Int): ByteArray { return try { - decompress(ByteArrayInputStream(compressed), decompressedLength) + decompress(ByteArrayInputStream(output.raw()), decompressedSize) } catch (e: IOException) { e.printStackTrace() byteArrayOf() From ec9f54a283de7e09a9b3cdafb4795f223bf9c2a6 Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 16:22:01 +0100 Subject: [PATCH 07/24] Store compressor instances inside the cache --- src/main/kotlin/com/displee/cache/index/Index.kt | 8 ++++++-- .../com/displee/cache/index/ReferenceTable.kt | 8 ++++---- .../com/displee/cache/index/archive/Archive.kt | 16 +++++++++++++--- .../displee/cache/index/archive/Archive317.kt | 15 ++++++++------- .../displee/cache/index/archive/ArchiveSector.kt | 2 ++ .../com/displee/compress/CompressionExt.kt | 9 ++++----- 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/com/displee/cache/index/Index.kt b/src/main/kotlin/com/displee/cache/index/Index.kt index 63aac8c..18791b6 100644 --- a/src/main/kotlin/com/displee/cache/index/Index.kt +++ b/src/main/kotlin/com/displee/cache/index/Index.kt @@ -7,6 +7,8 @@ import com.displee.cache.index.archive.ArchiveSector import com.displee.compress.CompressionType import com.displee.compress.compress import com.displee.compress.decompress +import com.displee.compress.type.Compressor +import com.displee.compress.type.EmptyCompressor import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer import com.displee.util.generateCrc @@ -18,6 +20,7 @@ open class Index(origin: CacheLibrary, id: Int, val raf: RandomAccessFile) : Ref var crc = 0 var whirlpool: ByteArray? = null var compressionType: CompressionType = CompressionType.NONE + var compressor: Compressor = EmptyCompressor private var cached = false protected var closed = false @@ -37,6 +40,7 @@ open class Index(origin: CacheLibrary, id: Int, val raf: RandomAccessFile) : Ref whirlpool = archiveSectorData.generateWhirlpool() read(InputBuffer(archiveSector.decompress())) compressionType = archiveSector.compressionType + compressor = archiveSector.compressor } fun cache() { @@ -71,7 +75,7 @@ open class Index(origin: CacheLibrary, id: Int, val raf: RandomAccessFile) : Ref it.revision++ it.unFlag() listener?.notify((i / flaggedArchives.size) * 0.80, "Repacking archive ${it.id}...") - val compressed = it.write().compress(it.compressionType ?: CompressionType.GZIP, it.xtea, it.revision) + val compressed = it.write().compress(it.compressionType, it.compressor, it.xtea, it.revision) it.crc = compressed.generateCrc(length = compressed.size - 2) it.whirlpool = compressed.generateWhirlpool(length = compressed.size - 2) val written = writeArchiveSector(it.id, compressed) @@ -87,7 +91,7 @@ open class Index(origin: CacheLibrary, id: Int, val raf: RandomAccessFile) : Ref if (flagged()) { unFlag() revision++ - val indexData = write().compress(compressionType) + val indexData = write().compress(compressionType, compressor) crc = indexData.generateCrc() whirlpool = indexData.generateWhirlpool() val written = origin.index255?.writeArchiveSector(this.id, indexData) ?: false diff --git a/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt b/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt index 9522544..4467652 100644 --- a/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt +++ b/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt @@ -231,11 +231,11 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com if (this is Index317) { existing = Archive317(id, if (hashName == -1) 0 else hashName) if (this.id != 0) { - existing.compressionType = CompressionType.GZIP + existing.setCompression(CompressionType.GZIP) } } else { existing = Archive(id, if (hashName == -1) 0 else hashName, xtea) - existing.compressionType = CompressionType.GZIP + existing.setCompression(CompressionType.GZIP) } if (hashName != -1) { archiveNames.add(existing.hashName) @@ -294,11 +294,11 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com } else { val is317 = is317() if (is317) { - (archive as Archive317).compressionType = if (this.id == 0) CompressionType.BZIP2 else CompressionType.GZIP + archive.setCompression(if (this.id == 0) CompressionType.BZIP2 else CompressionType.GZIP) archive.read(InputBuffer(sector.data)) } else { val decompressed = sector.decompress(xtea) - archive.compressionType = sector.compressionType + archive.setCompression(sector.compressionType, sector.compressor) if (decompressed.isNotEmpty()) { archive.read(InputBuffer(decompressed)) archive.xtea = xtea diff --git a/src/main/kotlin/com/displee/cache/index/archive/Archive.kt b/src/main/kotlin/com/displee/cache/index/archive/Archive.kt index 10e6827..cba3441 100644 --- a/src/main/kotlin/com/displee/cache/index/archive/Archive.kt +++ b/src/main/kotlin/com/displee/cache/index/archive/Archive.kt @@ -2,13 +2,23 @@ package com.displee.cache.index.archive import com.displee.cache.index.archive.file.File import com.displee.compress.CompressionType +import com.displee.compress.type.Compressor +import com.displee.compress.type.GZIPCompressor import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer import java.util.* open class Archive(val id: Int, var hashName: Int = 0, internal var xtea: IntArray? = null) : Comparable { - var compressionType: CompressionType? = null + var compressionType: CompressionType = CompressionType.GZIP + private set + var compressor: Compressor = GZIPCompressor + private set + + fun setCompression(type: CompressionType, compressor: Compressor? = null) { + compressionType = type + this.compressor = compressor ?: Compressor.get(type) + } var revision = 0 private var needUpdate = false @@ -85,7 +95,7 @@ open class Archive(val id: Int, var hashName: Int = 0, internal var xtea: IntArr var size = 0 files.forEach { size += it.data?.size ?: 0 } val buffer = OutputBuffer(size + files.size * 4) - val emptyByteArray = byteArrayOf() + val emptyByteArray = byteArrayOf() if (files.size == 1) { return first()?.data ?: emptyByteArray } else { @@ -272,7 +282,7 @@ open class Archive(val id: Int, var hashName: Int = 0, internal var xtea: IntArr this.xtea = xtea if (read) { //bzip2 compression fails when xteas are set for some reason, cheap fix - compressionType = CompressionType.GZIP + setCompression(CompressionType.GZIP) flag() } } diff --git a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt index 302c9e2..dc165a3 100644 --- a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt +++ b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt @@ -13,13 +13,15 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { private var extracted = false + private val bzip2 = BZIP2Compressor + private val none = EmptyCompressor + constructor(id: Int) : this(id, 0) override fun read(buffer: InputBuffer) { read = true if (compressionType == CompressionType.GZIP) { - val compressor = GZIPCompressor - files[0] = File(0, compressor.deflate317(buffer.raw())) + files[0] = File(0, (compressor as GZIPCompressor).deflate317(buffer.raw())) return } var decompressedLength = buffer.read24BitInt() @@ -27,13 +29,13 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { if (decompressedLength != compressedLength) { extracted = true } - var compressor = if (extracted) BZIP2Compressor else EmptyCompressor + var compressor = if (extracted) bzip2 else none val decompressed = compressor.decompress(buffer, byteArrayOf(), compressedLength, decompressedLength, 0) val metaBuffer = InputBuffer(decompressed) val filesLength = metaBuffer.readUnsignedShort() val filesBuffer = InputBuffer(decompressed) filesBuffer.offset = metaBuffer.offset + filesLength * 10 - compressor = if (extracted) EmptyCompressor else BZIP2Compressor + compressor = if (extracted) none else bzip2 for (i in 0 until filesLength) { val fileName = metaBuffer.readInt() decompressedLength = metaBuffer.read24BitInt() @@ -45,13 +47,12 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { override fun write(): ByteArray { if (compressionType == CompressionType.GZIP) { - val compressor = GZIPCompressor return compressor.compress(first()?.data ?: byteArrayOf()) } val metaBuffer = OutputBuffer(2 + files.size * 10) metaBuffer.writeShort(files.size) val filesBuffer = OutputBuffer(2048) - var compressor = if (extracted) EmptyCompressor else BZIP2Compressor + var compressor = if (extracted) none else bzip2 for (file in files.values) { val fileData = file.data ?: continue metaBuffer.writeInt(file.hashName).write24BitInt(fileData.size) @@ -61,7 +62,7 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { } metaBuffer.writeBytes(filesBuffer.array()) val decompressed = metaBuffer.array() - compressor = if (extracted) BZIP2Compressor else EmptyCompressor + compressor = if (extracted) bzip2 else none val compressed = compressor.compress(decompressed) val buffer = OutputBuffer(compressed.size + 6) buffer.write24BitInt(decompressed.size).write24BitInt(compressed.size).writeBytes(compressed) diff --git a/src/main/kotlin/com/displee/cache/index/archive/ArchiveSector.kt b/src/main/kotlin/com/displee/cache/index/archive/ArchiveSector.kt index 50e555e..822f0f7 100644 --- a/src/main/kotlin/com/displee/cache/index/archive/ArchiveSector.kt +++ b/src/main/kotlin/com/displee/cache/index/archive/ArchiveSector.kt @@ -3,6 +3,7 @@ package com.displee.cache.index.archive import com.displee.cache.index.Index.Companion.SECTOR_HEADER_SIZE_BIG import com.displee.cache.index.Index.Companion.SECTOR_HEADER_SIZE_SMALL import com.displee.compress.CompressionType +import com.displee.compress.type.Compressor import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer @@ -13,6 +14,7 @@ class ArchiveSector(private val bigSector: Boolean, val size: Int, var position: var data = ByteArray(size) lateinit var compressionType: CompressionType + lateinit var compressor: Compressor fun read(buffer: InputBuffer) { id = if (bigSector) { diff --git a/src/main/kotlin/com/displee/compress/CompressionExt.kt b/src/main/kotlin/com/displee/compress/CompressionExt.kt index 43c0ab7..a7c7d7f 100644 --- a/src/main/kotlin/com/displee/compress/CompressionExt.kt +++ b/src/main/kotlin/com/displee/compress/CompressionExt.kt @@ -5,8 +5,7 @@ import com.displee.compress.type.* import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer -fun ByteArray.compress(compressionType: CompressionType, xteas: IntArray? = null, revision: Int = -1): ByteArray { - val compressor: Compressor = Compressor.get(compressionType) +fun ByteArray.compress(compressionType: CompressionType, compressor: Compressor, xteas: IntArray? = null, revision: Int = -1): ByteArray { val compressed: ByteArray = compressor.compress(this) val compressedSize = compressed.size val buffer = OutputBuffer(9 + compressedSize + if (revision == -1) 0 else 2) @@ -32,12 +31,12 @@ fun ArchiveSector.decompress(keys: IntArray? = null): ByteArray { buffer.decryptXTEA(keys, 5, compressedData.size) } val type = buffer.readUnsignedByte() - val compressionType = CompressionType.compressionTypes[type].also { compressionType = it } + compressionType = CompressionType.compressionTypes[type] + compressor = Compressor.get(compressionType) val compressedSize = buffer.readInt() and 0xFFFFFF var decompressedSize = 0 if (compressionType != CompressionType.NONE) { decompressedSize = buffer.readInt() and 0xFFFFFF } - val decompressor: Compressor = Compressor.get(compressionType) - return decompressor.decompress(buffer, compressedData, compressedSize, decompressedSize) + return compressor.decompress(buffer, compressedData, compressedSize, decompressedSize) } \ No newline at end of file From 3af8aad8e197034203989a82774b15f979912bea Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 16:42:27 +0100 Subject: [PATCH 08/24] Convert compressors to classes and reuse them --- .../kotlin/com/displee/cache/CacheLibrary.kt | 2 ++ .../kotlin/com/displee/cache/index/Index.kt | 2 +- .../kotlin/com/displee/cache/index/Index317.kt | 2 +- .../com/displee/cache/index/ReferenceTable.kt | 16 ++++++++++------ .../com/displee/cache/index/archive/Archive.kt | 14 +++++--------- .../displee/cache/index/archive/Archive317.kt | 15 ++++++--------- .../com/displee/compress/CompressionExt.kt | 4 ++-- .../displee/compress/type/BZIP2Compressor.kt | 2 +- .../com/displee/compress/type/Compressor.kt | 11 ----------- .../com/displee/compress/type/Compressors.kt | 18 ++++++++++++++++++ .../displee/compress/type/GZIPCompressor.kt | 2 +- .../displee/compress/type/LZMACompressor.kt | 2 +- 12 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 src/main/kotlin/com/displee/compress/type/Compressors.kt diff --git a/src/main/kotlin/com/displee/cache/CacheLibrary.kt b/src/main/kotlin/com/displee/cache/CacheLibrary.kt index 50471fe..28db550 100644 --- a/src/main/kotlin/com/displee/cache/CacheLibrary.kt +++ b/src/main/kotlin/com/displee/cache/CacheLibrary.kt @@ -11,6 +11,7 @@ import com.displee.cache.index.ReferenceTable.Companion.FLAG_NAME import com.displee.cache.index.ReferenceTable.Companion.FLAG_WHIRLPOOL import com.displee.cache.index.archive.Archive import com.displee.compress.CompressionType +import com.displee.compress.type.Compressors import com.displee.io.Buffer import com.displee.io.impl.OutputBuffer import com.displee.util.generateWhirlpool @@ -26,6 +27,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa lateinit var mainFile: RandomAccessFile private val indices: SortedMap = TreeMap() + internal val compressors = Compressors() var index255: Index255? = null private var rs3 = false diff --git a/src/main/kotlin/com/displee/cache/index/Index.kt b/src/main/kotlin/com/displee/cache/index/Index.kt index 18791b6..5b1afeb 100644 --- a/src/main/kotlin/com/displee/cache/index/Index.kt +++ b/src/main/kotlin/com/displee/cache/index/Index.kt @@ -38,7 +38,7 @@ open class Index(origin: CacheLibrary, id: Int, val raf: RandomAccessFile) : Ref val archiveSectorData = archiveSector.data crc = archiveSectorData.generateCrc() whirlpool = archiveSectorData.generateWhirlpool() - read(InputBuffer(archiveSector.decompress())) + read(InputBuffer(archiveSector.decompress(origin.compressors))) compressionType = archiveSector.compressionType compressor = archiveSector.compressor } diff --git a/src/main/kotlin/com/displee/cache/index/Index317.kt b/src/main/kotlin/com/displee/cache/index/Index317.kt index 6e2f095..1d92736 100644 --- a/src/main/kotlin/com/displee/cache/index/Index317.kt +++ b/src/main/kotlin/com/displee/cache/index/Index317.kt @@ -29,7 +29,7 @@ class Index317(origin: CacheLibrary, id: Int, raf: RandomAccessFile) : Index(ori crcs = readArchiveProperties(CRC_FILES[id - 1], BufferType.INT) } for (i in 0 until archiveLength) { - val archive = Archive317(i) + val archive = Archive317(origin.compressors.bzip2, i) archives[i] = archive if (versions == null || crcs == null || i >= versions.size) { continue diff --git a/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt b/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt index 4467652..81b7e2d 100644 --- a/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt +++ b/src/main/kotlin/com/displee/cache/index/ReferenceTable.kt @@ -229,13 +229,15 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com } if (existing == null) { if (this is Index317) { - existing = Archive317(id, if (hashName == -1) 0 else hashName) + existing = Archive317(origin.compressors.bzip2, id, if (hashName == -1) 0 else hashName) if (this.id != 0) { - existing.setCompression(CompressionType.GZIP) + existing.compressionType = CompressionType.GZIP + existing.compressor = origin.compressors.gzip } } else { existing = Archive(id, if (hashName == -1) 0 else hashName, xtea) - existing.setCompression(CompressionType.GZIP) + existing.compressionType = CompressionType.GZIP + existing.compressor = origin.compressors.gzip } if (hashName != -1) { archiveNames.add(existing.hashName) @@ -294,11 +296,13 @@ open class ReferenceTable(protected val origin: CacheLibrary, val id: Int) : Com } else { val is317 = is317() if (is317) { - archive.setCompression(if (this.id == 0) CompressionType.BZIP2 else CompressionType.GZIP) + archive.compressionType = if (this.id == 0) CompressionType.BZIP2 else CompressionType.GZIP + archive.compressor = origin.compressors.get(archive.compressionType) archive.read(InputBuffer(sector.data)) } else { - val decompressed = sector.decompress(xtea) - archive.setCompression(sector.compressionType, sector.compressor) + val decompressed = sector.decompress(origin.compressors, xtea) + archive.compressionType = sector.compressionType + archive.compressor = sector.compressor if (decompressed.isNotEmpty()) { archive.read(InputBuffer(decompressed)) archive.xtea = xtea diff --git a/src/main/kotlin/com/displee/cache/index/archive/Archive.kt b/src/main/kotlin/com/displee/cache/index/archive/Archive.kt index cba3441..0574a30 100644 --- a/src/main/kotlin/com/displee/cache/index/archive/Archive.kt +++ b/src/main/kotlin/com/displee/cache/index/archive/Archive.kt @@ -3,6 +3,8 @@ package com.displee.cache.index.archive import com.displee.cache.index.archive.file.File import com.displee.compress.CompressionType import com.displee.compress.type.Compressor +import com.displee.compress.type.Compressors +import com.displee.compress.type.EmptyCompressor import com.displee.compress.type.GZIPCompressor import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer @@ -11,14 +13,7 @@ import java.util.* open class Archive(val id: Int, var hashName: Int = 0, internal var xtea: IntArray? = null) : Comparable { var compressionType: CompressionType = CompressionType.GZIP - private set - var compressor: Compressor = GZIPCompressor - private set - - fun setCompression(type: CompressionType, compressor: Compressor? = null) { - compressionType = type - this.compressor = compressor ?: Compressor.get(type) - } + var compressor: Compressor = EmptyCompressor var revision = 0 private var needUpdate = false @@ -282,7 +277,8 @@ open class Archive(val id: Int, var hashName: Int = 0, internal var xtea: IntArr this.xtea = xtea if (read) { //bzip2 compression fails when xteas are set for some reason, cheap fix - setCompression(CompressionType.GZIP) + compressionType = CompressionType.GZIP + compressor = GZIPCompressor() flag() } } diff --git a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt index dc165a3..48349b0 100644 --- a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt +++ b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt @@ -9,14 +9,11 @@ import com.displee.io.impl.InputBuffer import com.displee.io.impl.OutputBuffer import com.displee.util.hashCode317 -class Archive317(id: Int, name: Int) : Archive(id, name) { +class Archive317(private val bzip2: BZIP2Compressor, id: Int, name: Int) : Archive(id, name) { private var extracted = false - private val bzip2 = BZIP2Compressor - private val none = EmptyCompressor - - constructor(id: Int) : this(id, 0) + constructor(bzip2: BZIP2Compressor, id: Int) : this(bzip2, id, 0) override fun read(buffer: InputBuffer) { read = true @@ -29,13 +26,13 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { if (decompressedLength != compressedLength) { extracted = true } - var compressor = if (extracted) bzip2 else none + var compressor = if (extracted) bzip2 else EmptyCompressor val decompressed = compressor.decompress(buffer, byteArrayOf(), compressedLength, decompressedLength, 0) val metaBuffer = InputBuffer(decompressed) val filesLength = metaBuffer.readUnsignedShort() val filesBuffer = InputBuffer(decompressed) filesBuffer.offset = metaBuffer.offset + filesLength * 10 - compressor = if (extracted) none else bzip2 + compressor = if (extracted) EmptyCompressor else bzip2 for (i in 0 until filesLength) { val fileName = metaBuffer.readInt() decompressedLength = metaBuffer.read24BitInt() @@ -52,7 +49,7 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { val metaBuffer = OutputBuffer(2 + files.size * 10) metaBuffer.writeShort(files.size) val filesBuffer = OutputBuffer(2048) - var compressor = if (extracted) none else bzip2 + var compressor = if (extracted) EmptyCompressor else bzip2 for (file in files.values) { val fileData = file.data ?: continue metaBuffer.writeInt(file.hashName).write24BitInt(fileData.size) @@ -62,7 +59,7 @@ class Archive317(id: Int, name: Int) : Archive(id, name) { } metaBuffer.writeBytes(filesBuffer.array()) val decompressed = metaBuffer.array() - compressor = if (extracted) bzip2 else none + compressor = if (extracted) bzip2 else EmptyCompressor val compressed = compressor.compress(decompressed) val buffer = OutputBuffer(compressed.size + 6) buffer.write24BitInt(decompressed.size).write24BitInt(compressed.size).writeBytes(compressed) diff --git a/src/main/kotlin/com/displee/compress/CompressionExt.kt b/src/main/kotlin/com/displee/compress/CompressionExt.kt index a7c7d7f..44e5d51 100644 --- a/src/main/kotlin/com/displee/compress/CompressionExt.kt +++ b/src/main/kotlin/com/displee/compress/CompressionExt.kt @@ -24,7 +24,7 @@ fun ByteArray.compress(compressionType: CompressionType, compressor: Compressor, return buffer.array() } -fun ArchiveSector.decompress(keys: IntArray? = null): ByteArray { +fun ArchiveSector.decompress(compressors: Compressors, keys: IntArray? = null): ByteArray { val compressedData = data val buffer = InputBuffer(compressedData) if (keys != null && (keys[0] != 0 || keys[1] != 0 || keys[2] != 0 || 0 != keys[3])) { @@ -32,7 +32,7 @@ fun ArchiveSector.decompress(keys: IntArray? = null): ByteArray { } val type = buffer.readUnsignedByte() compressionType = CompressionType.compressionTypes[type] - compressor = Compressor.get(compressionType) + compressor = compressors.get(compressionType) val compressedSize = buffer.readInt() and 0xFFFFFF var decompressedSize = 0 if (compressionType != CompressionType.NONE) { diff --git a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt index 31681ea..951f8b4 100644 --- a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt @@ -10,7 +10,7 @@ import java.io.ByteArrayOutputStream * @author Jagex * @author Displee */ -object BZIP2Compressor : Compressor { +class BZIP2Compressor : Compressor { /** * The current bzip2 block entry. */ diff --git a/src/main/kotlin/com/displee/compress/type/Compressor.kt b/src/main/kotlin/com/displee/compress/type/Compressor.kt index d25a4e6..217f917 100644 --- a/src/main/kotlin/com/displee/compress/type/Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/Compressor.kt @@ -1,6 +1,5 @@ package com.displee.compress.type -import com.displee.compress.CompressionType import com.displee.io.impl.InputBuffer interface Compressor { @@ -9,14 +8,4 @@ interface Compressor { fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int = 9): ByteArray - companion object { - fun get(type: CompressionType): Compressor { - return when (type) { - CompressionType.NONE -> EmptyCompressor - CompressionType.BZIP2 -> BZIP2Compressor - CompressionType.GZIP -> GZIPCompressor - CompressionType.LZMA -> LZMACompressor - } - } - } } \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/Compressors.kt b/src/main/kotlin/com/displee/compress/type/Compressors.kt new file mode 100644 index 0000000..68d21b7 --- /dev/null +++ b/src/main/kotlin/com/displee/compress/type/Compressors.kt @@ -0,0 +1,18 @@ +package com.displee.compress.type + +import com.displee.compress.CompressionType + +class Compressors { + val bzip2: BZIP2Compressor by lazy { BZIP2Compressor() } + val gzip: GZIPCompressor by lazy { GZIPCompressor() } + private val lzma: LZMACompressor by lazy { LZMACompressor() } + + fun get(type: CompressionType): Compressor { + return when (type) { + CompressionType.NONE -> EmptyCompressor + CompressionType.BZIP2 -> bzip2 + CompressionType.GZIP -> gzip + CompressionType.LZMA -> lzma + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt index f14d784..728fe87 100644 --- a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt @@ -8,7 +8,7 @@ import java.util.zip.GZIPInputStream import java.util.zip.GZIPOutputStream import java.util.zip.Inflater -object GZIPCompressor : Compressor { +class GZIPCompressor : Compressor { private var inflater: Inflater? = null private val gzipBuffer = ByteArray(1000_000) //because in 317 Jagex stores a lot of data in one file diff --git a/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt b/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt index f12f60d..25350d3 100644 --- a/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt @@ -13,7 +13,7 @@ import java.io.* * A class handling LZMA compression. * @author Displee (credits to Techdaan) */ -object LZMACompressor : Compressor { +class LZMACompressor : Compressor { private val DECODER = Decoder() private val ENCODER = Encoder() From 4fbb338bf07b4994a8237024f746eac572fa3c06 Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 16 Jun 2023 16:47:12 +0100 Subject: [PATCH 09/24] Reuse whirlpool --- src/main/kotlin/com/displee/cache/CacheLibrary.kt | 4 +++- src/main/kotlin/com/displee/cache/index/Index.kt | 6 +++--- src/main/kotlin/com/displee/cache/index/Index317.kt | 2 +- src/main/kotlin/com/displee/util/OtherExt.kt | 3 +-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/displee/cache/CacheLibrary.kt b/src/main/kotlin/com/displee/cache/CacheLibrary.kt index 28db550..45d42b9 100644 --- a/src/main/kotlin/com/displee/cache/CacheLibrary.kt +++ b/src/main/kotlin/com/displee/cache/CacheLibrary.kt @@ -14,6 +14,7 @@ import com.displee.compress.CompressionType import com.displee.compress.type.Compressors import com.displee.io.Buffer import com.displee.io.impl.OutputBuffer +import com.displee.util.Whirlpool import com.displee.util.generateWhirlpool import java.io.File import java.io.FileNotFoundException @@ -28,6 +29,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa private val indices: SortedMap = TreeMap() internal val compressors = Compressors() + internal val whirlpool = Whirlpool() var index255: Index255? = null private var rs3 = false @@ -284,7 +286,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa val indexData = buffer.array() val whirlpoolBuffer = OutputBuffer(WHIRLPOOL_SIZE + 1) .writeByte(0) - .writeBytes(indexData.generateWhirlpool(5, indexData.size - 5)) + .writeBytes(indexData.generateWhirlpool(whirlpool, 5, indexData.size - 5)) if (exponent != null && modulus != null) { buffer.writeBytes(Buffer.cryptRSA(whirlpoolBuffer.array(), exponent, modulus)) } diff --git a/src/main/kotlin/com/displee/cache/index/Index.kt b/src/main/kotlin/com/displee/cache/index/Index.kt index 5b1afeb..3771921 100644 --- a/src/main/kotlin/com/displee/cache/index/Index.kt +++ b/src/main/kotlin/com/displee/cache/index/Index.kt @@ -37,7 +37,7 @@ open class Index(origin: CacheLibrary, id: Int, val raf: RandomAccessFile) : Ref val archiveSector = origin.index255?.readArchiveSector(id) ?: return val archiveSectorData = archiveSector.data crc = archiveSectorData.generateCrc() - whirlpool = archiveSectorData.generateWhirlpool() + whirlpool = archiveSectorData.generateWhirlpool(origin.whirlpool) read(InputBuffer(archiveSector.decompress(origin.compressors))) compressionType = archiveSector.compressionType compressor = archiveSector.compressor @@ -77,7 +77,7 @@ open class Index(origin: CacheLibrary, id: Int, val raf: RandomAccessFile) : Ref listener?.notify((i / flaggedArchives.size) * 0.80, "Repacking archive ${it.id}...") val compressed = it.write().compress(it.compressionType, it.compressor, it.xtea, it.revision) it.crc = compressed.generateCrc(length = compressed.size - 2) - it.whirlpool = compressed.generateWhirlpool(length = compressed.size - 2) + it.whirlpool = compressed.generateWhirlpool(origin.whirlpool, length = compressed.size - 2) val written = writeArchiveSector(it.id, compressed) check(written) { "Unable to write data to archive sector. Your cache may be corrupt." } if (origin.clearDataAfterUpdate) { @@ -93,7 +93,7 @@ open class Index(origin: CacheLibrary, id: Int, val raf: RandomAccessFile) : Ref revision++ val indexData = write().compress(compressionType, compressor) crc = indexData.generateCrc() - whirlpool = indexData.generateWhirlpool() + whirlpool = indexData.generateWhirlpool(origin.whirlpool) val written = origin.index255?.writeArchiveSector(this.id, indexData) ?: false check(written) { "Unable to write data to checksum table. Your cache may be corrupt." } } diff --git a/src/main/kotlin/com/displee/cache/index/Index317.kt b/src/main/kotlin/com/displee/cache/index/Index317.kt index 1d92736..ae6c757 100644 --- a/src/main/kotlin/com/displee/cache/index/Index317.kt +++ b/src/main/kotlin/com/displee/cache/index/Index317.kt @@ -59,7 +59,7 @@ class Index317(origin: CacheLibrary, id: Int, raf: RandomAccessFile) : Index(ori listener?.notify((i / flaggedArchives.size) * 0.80, "Repacking archive ${it.id}...") val compressed = it.write() it.crc = compressed.generateCrc() - it.whirlpool = compressed.generateWhirlpool() + it.whirlpool = compressed.generateWhirlpool(origin.whirlpool) val written = writeArchiveSector(it.id, compressed) check(written) { "Unable to write data to archive sector. Your cache may be corrupt." } if (origin.clearDataAfterUpdate) { diff --git a/src/main/kotlin/com/displee/util/OtherExt.kt b/src/main/kotlin/com/displee/util/OtherExt.kt index 18f95ce..f08e9db 100644 --- a/src/main/kotlin/com/displee/util/OtherExt.kt +++ b/src/main/kotlin/com/displee/util/OtherExt.kt @@ -25,7 +25,7 @@ fun ByteArray.generateCrc(offset: Int = 0, length: Int = size): Int { return crc } -fun ByteArray.generateWhirlpool(offset: Int = 0, length: Int = size): ByteArray { +fun ByteArray.generateWhirlpool(whirlpool: Whirlpool, offset: Int = 0, length: Int = size): ByteArray { val source: ByteArray if (offset > 0) { source = ByteArray(length) @@ -33,7 +33,6 @@ fun ByteArray.generateWhirlpool(offset: Int = 0, length: Int = size): ByteArray } else { source = this } - val whirlpool = Whirlpool() whirlpool.NESSIEinit() whirlpool.NESSIEadd(source, (length * 8).toLong()) val digest = ByteArray(WHIRLPOOL_SIZE) From 87273a535606785acf777b150e9144624eb9cc33 Mon Sep 17 00:00:00 2001 From: GregHib Date: Sun, 27 Aug 2023 15:29:31 +0100 Subject: [PATCH 10/24] Use the largest index as size --- src/main/kotlin/com/displee/cache/CacheLibrary.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/displee/cache/CacheLibrary.kt b/src/main/kotlin/com/displee/cache/CacheLibrary.kt index 50471fe..12cac1d 100644 --- a/src/main/kotlin/com/displee/cache/CacheLibrary.kt +++ b/src/main/kotlin/com/displee/cache/CacheLibrary.kt @@ -123,7 +123,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa fun createIndex(compressionType: CompressionType = CompressionType.GZIP, version: Int = 6, revision: Int = 0, named: Boolean = false, whirlpool: Boolean = false, flag4: Boolean = false, flag8: Boolean = false, writeReferenceTable: Boolean = true): Index { - val id = indices.size + val id = indexCount() val raf = RandomAccessFile(File(path, "$CACHE_FILE_NAME.idx$id"), "rw") val index = (if (is317()) Index317(this, id, raf) else Index(this, id, raf)).also { indices[id] = it } if (!writeReferenceTable) { @@ -254,7 +254,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa if (is317()) { throw UnsupportedOperationException("317 not supported to remove indices yet.") } - val id = indices.size - 1 + val id = indexCount() - 1 val index = indices[id] ?: return index.close() val file = File(path, "$CACHE_FILE_NAME.idx$id") @@ -267,9 +267,9 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa @JvmOverloads fun generateUkeys(writeWhirlpool: Boolean = true, exponent: BigInteger? = null, modulus: BigInteger? = null): ByteArray { - val buffer = OutputBuffer(6 + indices.size * 72) + val buffer = OutputBuffer(6 + indexCount() * 72) if (writeWhirlpool) { - buffer.writeByte(indices.size) + buffer.writeByte(indexCount()) } val emptyWhirlpool = ByteArray(WHIRLPOOL_SIZE) for (index in indices()) { @@ -360,9 +360,11 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa fun isOSRS(): Boolean { val index = index(2) - return index.revision >= 300 && indices.size <= 23 + return index.revision >= 300 && indexCount() <= 23 } + private fun indexCount() = indices.maxBy { it.key }?.key ?: 0 + fun isRS3(): Boolean { return rs3 } From c0bdfbd5589f42b018a7457e90c200bfee3ed6c7 Mon Sep 17 00:00:00 2001 From: GregHib Date: Sun, 27 Aug 2023 15:39:19 +0100 Subject: [PATCH 11/24] Simplified --- src/main/kotlin/com/displee/cache/CacheLibrary.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/displee/cache/CacheLibrary.kt b/src/main/kotlin/com/displee/cache/CacheLibrary.kt index 12cac1d..d0c5cb1 100644 --- a/src/main/kotlin/com/displee/cache/CacheLibrary.kt +++ b/src/main/kotlin/com/displee/cache/CacheLibrary.kt @@ -363,7 +363,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa return index.revision >= 300 && indexCount() <= 23 } - private fun indexCount() = indices.maxBy { it.key }?.key ?: 0 + private fun indexCount() = indices.keys.max() ?: 0 fun isRS3(): Boolean { return rs3 From 7aac31a49a60bc907a0200cc3ad3b05ab8f38ede Mon Sep 17 00:00:00 2001 From: GregHib Date: Wed, 8 Nov 2023 17:00:59 +0000 Subject: [PATCH 12/24] Fix creating index ids and index duplication --- src/main/kotlin/com/displee/cache/CacheLibrary.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/displee/cache/CacheLibrary.kt b/src/main/kotlin/com/displee/cache/CacheLibrary.kt index d0c5cb1..6dc5850 100644 --- a/src/main/kotlin/com/displee/cache/CacheLibrary.kt +++ b/src/main/kotlin/com/displee/cache/CacheLibrary.kt @@ -122,8 +122,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa @JvmOverloads fun createIndex(compressionType: CompressionType = CompressionType.GZIP, version: Int = 6, revision: Int = 0, named: Boolean = false, whirlpool: Boolean = false, flag4: Boolean = false, flag8: Boolean = false, - writeReferenceTable: Boolean = true): Index { - val id = indexCount() + writeReferenceTable: Boolean = true, id: Int = if (indices.isEmpty()) 0 else indexCount() + 1): Index { val raf = RandomAccessFile(File(path, "$CACHE_FILE_NAME.idx$id"), "rw") val index = (if (is317()) Index317(this, id, raf) else Index(this, id, raf)).also { indices[id] = it } if (!writeReferenceTable) { @@ -153,7 +152,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa fun createIndex(index: Index, writeReferenceTable: Boolean = true): Index { return createIndex(index.compressionType, index.version, index.revision, - index.isNamed(), index.hasWhirlpool(), index.hasFlag4(), index.hasFlag8(), writeReferenceTable) + index.isNamed(), index.hasWhirlpool(), index.hasFlag4(), index.hasFlag8(), writeReferenceTable, index.id) } fun exists(id: Int): Boolean { From ffd31dd253d4d7fc7a5725a4a49753b407814ba9 Mon Sep 17 00:00:00 2001 From: GregHib Date: Wed, 10 Jan 2024 12:43:59 +0000 Subject: [PATCH 13/24] Remove nulls to improve performance --- .../displee/compress/type/BZIP2Compressor.kt | 199 +++++++----------- 1 file changed, 78 insertions(+), 121 deletions(-) diff --git a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt index 951f8b4..8ee0e44 100644 --- a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt @@ -14,12 +14,12 @@ class BZIP2Compressor : Compressor { /** * The current bzip2 block entry. */ - var bzip2BlockEntry: BZIP2BlockEntry? = BZIP2BlockEntry() + var bzip2BlockEntry: BZIP2BlockEntry = BZIP2BlockEntry() /** * An unkown integer array. */ - private var anIntArray5786: IntArray? = null + private var anIntArray5786: IntArray = IntArray(100000) /** * Compress a decompressed BZIP2 file. @@ -29,12 +29,12 @@ class BZIP2Compressor : Compressor { override fun compress(bytes: ByteArray): ByteArray { var bytes = bytes try { - ByteArrayInputStream(bytes).use { `is` -> + ByteArrayInputStream(bytes).use { stream -> val bout = ByteArrayOutputStream() CBZip2OutputStream(bout, 1).use { os -> val buf = ByteArray(4096) var len: Int - while (`is`.read(buf, 0, buf.size).also { len = it } != -1) { + while (stream.read(buf, 0, buf.size).also { len = it } != -1) { os.write(buf, 0, len) } } @@ -64,23 +64,21 @@ class BZIP2Compressor : Compressor { * @param startOffset The start offset. * @return The decompressed length. */ - fun decompress(decompressed: ByteArray?, decompressedLength: Int, archiveData: ByteArray?, compressedSize: Int, startOffset: Int): Int { - var decompressedLength = decompressedLength - synchronized(bzip2BlockEntry!!) { - bzip2BlockEntry!!.compressed = archiveData - bzip2BlockEntry!!.startOffset = startOffset - bzip2BlockEntry!!.decompressed = decompressed - bzip2BlockEntry!!.anInt3100 = 0 - bzip2BlockEntry!!.decompressedLength = decompressedLength - bzip2BlockEntry!!.anInt3088 = 0 - bzip2BlockEntry!!.anInt3078 = 0 - bzip2BlockEntry!!.anInt3085 = 0 - bzip2BlockEntry!!.anInt3097 = 0 + fun decompress(decompressed: ByteArray, decompressedLength: Int, archiveData: ByteArray, compressedSize: Int, startOffset: Int): Int { + synchronized(bzip2BlockEntry) { + bzip2BlockEntry.compressed = archiveData + bzip2BlockEntry.startOffset = startOffset + bzip2BlockEntry.decompressed = decompressed + bzip2BlockEntry.anInt3100 = 0 + bzip2BlockEntry.decompressedLength = decompressedLength + bzip2BlockEntry.anInt3088 = 0 + bzip2BlockEntry.anInt3078 = 0 + bzip2BlockEntry.anInt3085 = 0 + bzip2BlockEntry.anInt3097 = 0 decompress(bzip2BlockEntry) - decompressedLength -= bzip2BlockEntry!!.decompressedLength - bzip2BlockEntry!!.compressed = null - bzip2BlockEntry!!.decompressed = null - return decompressedLength + bzip2BlockEntry.compressed = ByteArray(0) + bzip2BlockEntry.decompressed = ByteArray(0) + return decompressedLength - bzip2BlockEntry.decompressedLength } } @@ -88,49 +86,27 @@ class BZIP2Compressor : Compressor { * Decompress a BZIP2 block entry. * @param bzip2BlockEntry [BZIP2BlockEntry] */ - fun decompress(bzip2BlockEntry: BZIP2BlockEntry?) { - val bool = false - val bool_9_ = false - val bool_10_ = false - val bool_11_ = false - val bool_12_ = false - val bool_13_ = false - val bool_14_ = false - val bool_15_ = false - val bool_16_ = false - val bool_17_ = false - val bool_18_ = false - val bool_19_ = false - val bool_20_ = false - val bool_21_ = false - val bool_22_ = false - val bool_23_ = false - val bool_24_ = false - val bool_25_ = false - var i = 0 - var `is`: IntArray? = null - var is_26_: IntArray? = null - var is_27_: IntArray? = null - bzip2BlockEntry!!.anInt3096 = 1 - if (anIntArray5786 == null) { - anIntArray5786 = IntArray(bzip2BlockEntry.anInt3096 * 100000) - } + fun decompress(bzip2BlockEntry: BZIP2BlockEntry) { + var i: Int + var is_25_: IntArray + var is_26_: IntArray + var is_27_: IntArray var bool_28_ = true while (bool_28_) { var i_29_ = method147(bzip2BlockEntry) if (i_29_.toInt() == 23) { break } - i_29_ = method147(bzip2BlockEntry) - i_29_ = method147(bzip2BlockEntry) - i_29_ = method147(bzip2BlockEntry) - i_29_ = method147(bzip2BlockEntry) - i_29_ = method147(bzip2BlockEntry) - i_29_ = method147(bzip2BlockEntry) - i_29_ = method147(bzip2BlockEntry) - i_29_ = method147(bzip2BlockEntry) - i_29_ = method147(bzip2BlockEntry) - i_29_ = method153(bzip2BlockEntry) + method147(bzip2BlockEntry) + method147(bzip2BlockEntry) + method147(bzip2BlockEntry) + method147(bzip2BlockEntry) + method147(bzip2BlockEntry) + method147(bzip2BlockEntry) + method147(bzip2BlockEntry) + method147(bzip2BlockEntry) + method147(bzip2BlockEntry) + method153(bzip2BlockEntry) bzip2BlockEntry.anInt3083 = 0 var i_30_ = method147(bzip2BlockEntry).toInt() bzip2BlockEntry.anInt3083 = bzip2BlockEntry.anInt3083 shl 8 or (i_30_ and 0xff) @@ -228,7 +204,6 @@ class BZIP2Compressor : Compressor { } val i_52_ = bzip2BlockEntry.anInt3073 + 1 var i_53_ = -1 - var i_54_ = 0 for (i_55_ in 0..255) { bzip2BlockEntry.anIntArray3075[i_55_] = 0 } @@ -241,26 +216,24 @@ class BZIP2Compressor : Compressor { bzip2BlockEntry.anIntArray3092[i_57_] = i_56_ + 1 } var i_59_ = 0 - if (i_54_ == 0) { - i_53_++ - i_54_ = 50 - val i_60_ = bzip2BlockEntry.aByteArray3076[i_53_] - i = bzip2BlockEntry.anIntArray3090[i_60_.toInt()] - `is` = bzip2BlockEntry.anIntArrayArray3095[i_60_.toInt()] - is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_60_.toInt()] - is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_60_.toInt()] - } + i_53_++ + var i_54_ = 50 + val i_60_ = bzip2BlockEntry.aByteArray3076[i_53_] + i = bzip2BlockEntry.anIntArray3090[i_60_.toInt()] + is_25_ = bzip2BlockEntry.anIntArrayArray3095[i_60_.toInt()] + is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_60_.toInt()] + is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_60_.toInt()] i_54_-- var i_61_ = i var i_62_: Int var i_63_: Int i_63_ = method152(i_61_, bzip2BlockEntry) - while (i_63_ > `is`!![i_61_]) { + while (i_63_ > is_25_[i_61_]) { i_61_++ i_62_ = method153(bzip2BlockEntry).toInt() i_63_ = i_63_ shl 1 or i_62_ } - var i_64_ = is_27_!![i_63_ - is_26_!![i_61_]] + var i_64_ = is_27_[i_63_ - is_26_[i_61_]] while (i_64_ != i_52_) { if (i_64_ == 0 || i_64_ == 1) { var i_65_ = -1 @@ -277,25 +250,25 @@ class BZIP2Compressor : Compressor { i_54_ = 50 val i_67_ = bzip2BlockEntry.aByteArray3076[i_53_] i = bzip2BlockEntry.anIntArray3090[i_67_.toInt()] - `is` = bzip2BlockEntry.anIntArrayArray3095[i_67_.toInt()] + is_25_ = bzip2BlockEntry.anIntArrayArray3095[i_67_.toInt()] is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_67_.toInt()] is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_67_.toInt()] } i_54_-- i_61_ = i i_63_ = method152(i_61_, bzip2BlockEntry) - while (i_63_ > `is`!![i_61_]) { + while (i_63_ > is_25_[i_61_]) { i_61_++ i_62_ = method153(bzip2BlockEntry).toInt() i_63_ = i_63_ shl 1 or i_62_ } - i_64_ = is_27_!![i_63_ - is_26_!![i_61_]] + i_64_ = is_27_[i_63_ - is_26_[i_61_]] } while (i_64_ == 0 || i_64_ == 1) i_65_++ i_30_ = bzip2BlockEntry.aByteArray3107[bzip2BlockEntry.aByteArray3101[bzip2BlockEntry.anIntArray3092[0]].toInt() and 0xff].toInt() bzip2BlockEntry.anIntArray3075[i_30_ and 0xff] += i_65_ while ( /**/i_65_ > 0) { - anIntArray5786!![i_59_] = i_30_ and 0xff + anIntArray5786[i_59_] = i_30_ and 0xff i_59_++ i_65_-- } @@ -346,26 +319,26 @@ class BZIP2Compressor : Compressor { } } bzip2BlockEntry.anIntArray3075[bzip2BlockEntry.aByteArray3107[i_29_.toInt() and 0xff].toInt() and 0xff]++ - anIntArray5786!![i_59_] = bzip2BlockEntry.aByteArray3107[i_29_.toInt() and 0xff].toInt() and 0xff + anIntArray5786[i_59_] = bzip2BlockEntry.aByteArray3107[i_29_.toInt() and 0xff].toInt() and 0xff i_59_++ if (i_54_ == 0) { i_53_++ i_54_ = 50 val i_77_ = bzip2BlockEntry.aByteArray3076[i_53_] i = bzip2BlockEntry.anIntArray3090[i_77_.toInt()] - `is` = bzip2BlockEntry.anIntArrayArray3095[i_77_.toInt()] + is_25_ = bzip2BlockEntry.anIntArrayArray3095[i_77_.toInt()] is_27_ = bzip2BlockEntry.anIntArrayArray3099[i_77_.toInt()] is_26_ = bzip2BlockEntry.anIntArrayArray3082[i_77_.toInt()] } i_54_-- i_61_ = i i_63_ = method152(i_61_, bzip2BlockEntry) - while (i_63_ > `is`!![i_61_]) { + while (i_63_ > is_25_[i_61_]) { i_61_++ i_62_ = method153(bzip2BlockEntry).toInt() i_63_ = i_63_ shl 1 or i_62_ } - i_64_ = is_27_!![i_63_ - is_26_!![i_61_]] + i_64_ = is_27_[i_63_ - is_26_[i_61_]] } } bzip2BlockEntry.anInt3080 = 0 @@ -378,36 +351,32 @@ class BZIP2Compressor : Compressor { bzip2BlockEntry.anIntArray3091[i_79_] += bzip2BlockEntry.anIntArray3091[i_79_ - 1] } for (i_80_ in 0 until i_59_) { - i_30_ = (anIntArray5786!![i_80_] and 0xff).toByte().toInt() - anIntArray5786!![bzip2BlockEntry.anIntArray3091[i_30_ and 0xff]] = anIntArray5786!![bzip2BlockEntry.anIntArray3091[i_30_ and 0xff]] or (i_80_ shl 8) + i_30_ = (anIntArray5786[i_80_] and 0xff).toByte().toInt() + anIntArray5786[bzip2BlockEntry.anIntArray3091[i_30_ and 0xff]] = anIntArray5786[bzip2BlockEntry.anIntArray3091[i_30_ and 0xff]] or (i_80_ shl 8) bzip2BlockEntry.anIntArray3091[i_30_ and 0xff]++ } - bzip2BlockEntry.anInt3106 = anIntArray5786!![bzip2BlockEntry.anInt3083] shr 8 + bzip2BlockEntry.anInt3106 = anIntArray5786[bzip2BlockEntry.anInt3083] shr 8 bzip2BlockEntry.anInt3071 = 0 - bzip2BlockEntry.anInt3106 = anIntArray5786!![bzip2BlockEntry.anInt3106] + bzip2BlockEntry.anInt3106 = anIntArray5786[bzip2BlockEntry.anInt3106] bzip2BlockEntry.anInt3070 = (bzip2BlockEntry.anInt3106 and 0xff).toByte().toInt() bzip2BlockEntry.anInt3106 = bzip2BlockEntry.anInt3106 shr 8 bzip2BlockEntry.anInt3071++ bzip2BlockEntry.anInt3077 = i_59_ method151(bzip2BlockEntry) - bool_28_ = if (bzip2BlockEntry.anInt3071 == bzip2BlockEntry.anInt3077 + 1 && bzip2BlockEntry.anInt3080 == 0) { - true - } else { - false - } + bool_28_ = bzip2BlockEntry.anInt3071 == bzip2BlockEntry.anInt3077 + 1 && bzip2BlockEntry.anInt3080 == 0 } } - fun method152(arg0: Int, arg1: BZIP2BlockEntry?): Int { + fun method152(arg0: Int, arg1: BZIP2BlockEntry): Int { val i: Int while (true) { - if (arg1!!.anInt3088 >= arg0) { + if (arg1.anInt3088 >= arg0) { val i_93_ = arg1.anInt3078 shr arg1.anInt3088 - arg0 and (1 shl arg0) - 1 arg1.anInt3088 -= arg0 i = i_93_ break } - arg1.anInt3078 = arg1.anInt3078 shl 8 or (arg1.compressed!![arg1.startOffset].toInt() and 0xff) + arg1.anInt3078 = arg1.anInt3078 shl 8 or (arg1.compressed[arg1.startOffset].toInt() and 0xff) arg1.anInt3088 += 8 arg1.startOffset++ arg1.anInt3085++ @@ -415,12 +384,12 @@ class BZIP2Compressor : Compressor { return i } - fun method151(arg0: BZIP2BlockEntry?) { - var i = arg0!!.aByte3108 + fun method151(arg0: BZIP2BlockEntry) { + var i = arg0.aByte3108 var i_81_ = arg0.anInt3080 var i_82_ = arg0.anInt3071 var i_83_ = arg0.anInt3070 - val `is` = anIntArray5786 + val i_90_ = anIntArray5786 var i_84_ = arg0.anInt3106 val is_85_ = arg0.decompressed var i_86_ = arg0.anInt3100 @@ -436,7 +405,7 @@ class BZIP2Compressor : Compressor { if (i_81_ == 1) { break } - is_85_!![i_86_] = i + is_85_[i_86_] = i i_81_-- i_86_++ i_87_-- @@ -445,7 +414,7 @@ class BZIP2Compressor : Compressor { i_81_ = 1 break } - is_85_!![i_86_] = i + is_85_[i_86_] = i i_86_++ i_87_-- } @@ -457,7 +426,7 @@ class BZIP2Compressor : Compressor { break@while_68_ } i = i_83_.toByte() - i_84_ = `is`!![i_84_] + i_84_ = i_90_[i_84_] val i_90_ = (i_84_ and 0xff).toByte().toInt() i_84_ = i_84_ shr 8 i_82_++ @@ -467,7 +436,7 @@ class BZIP2Compressor : Compressor { i_81_ = 1 break@while_68_ } - is_85_!![i_86_] = i + is_85_[i_86_] = i i_86_++ i_87_-- bool = true @@ -476,14 +445,14 @@ class BZIP2Compressor : Compressor { i_81_ = 1 break@while_68_ } - is_85_!![i_86_] = i + is_85_[i_86_] = i i_86_++ i_87_-- bool = true } } i_81_ = 2 - i_84_ = `is`!![i_84_] + i_84_ = i_90_[i_84_] var i_91_ = (i_84_ and 0xff).toByte().toInt() i_84_ = i_84_ shr 8 if (++i_82_ != i_89_) { @@ -491,19 +460,19 @@ class BZIP2Compressor : Compressor { i_83_ = i_91_ } else { i_81_ = 3 - i_84_ = `is`[i_84_] + i_84_ = i_90_[i_84_] i_91_ = (i_84_ and 0xff).toByte().toInt() i_84_ = i_84_ shr 8 if (++i_82_ != i_89_) { if (i_91_ != i_83_) { i_83_ = i_91_ } else { - i_84_ = `is`[i_84_] + i_84_ = i_90_[i_84_] i_91_ = (i_84_ and 0xff).toByte().toInt() i_84_ = i_84_ shr 8 i_82_++ i_81_ = (i_91_ and 0xff) + 4 - i_84_ = `is`[i_84_] + i_84_ = i_90_[i_84_] i_83_ = (i_84_ and 0xff).toByte().toInt() i_84_ = i_84_ shr 8 i_82_++ @@ -512,13 +481,13 @@ class BZIP2Compressor : Compressor { } } } - val i_92_ = arg0.anInt3097 + arg0.anInt3097 arg0.anInt3097 += i_88_ - i_87_ arg0.aByte3108 = i arg0.anInt3080 = i_81_ arg0.anInt3071 = i_82_ arg0.anInt3070 = i_83_ - anIntArray5786 = `is` + anIntArray5786 = i_90_ arg0.anInt3106 = i_84_ arg0.decompressed = is_85_ arg0.anInt3100 = i_86_ @@ -558,16 +527,12 @@ class BZIP2Compressor : Compressor { } } - fun method147(arg0: BZIP2BlockEntry?): Byte { + fun method147(arg0: BZIP2BlockEntry): Byte { return method152(8, arg0).toByte() } - fun method148() { - bzip2BlockEntry = null - } - - fun method149(arg0: BZIP2BlockEntry?) { - arg0!!.anInt3073 = 0 + fun method149(arg0: BZIP2BlockEntry) { + arg0.anInt3073 = 0 for (i in 0..255) { if (arg0.aBooleanArray3103[i]) { arg0.aByteArray3107[arg0.anInt3073] = i.toByte() @@ -576,7 +541,7 @@ class BZIP2Compressor : Compressor { } } - fun method153(arg0: BZIP2BlockEntry?): Byte { + fun method153(arg0: BZIP2BlockEntry): Byte { return method152(1, arg0).toByte() } @@ -590,35 +555,27 @@ class BZIP2Compressor : Compressor { var aByteArray3076: ByteArray = ByteArray(18002) var anInt3077 = 0 var anInt3078 = 0 - var decompressed: ByteArray? = null + var decompressed: ByteArray = ByteArray(0) var anInt3080 = 0 - var anInt3081 = 0 var anIntArrayArray3082 = Array(6) { IntArray(258) } var anInt3083 = 0 - var anIntArray3084: IntArray = intArrayOf() var anInt3085 = 0 - var anIntArray3086 = intArrayOf(2, 1, 1, 1, 2, 2, 2, 1, 3, 3, 3, 2, 0, 4, 0) - var anInt3087 = 0 var anInt3088 = 0 - var anInt3089 = 0 var anIntArray3090: IntArray = IntArray(6) var anIntArray3091: IntArray = IntArray(257) var anIntArray3092: IntArray = IntArray(16) var decompressedLength = 0 var aByteArray3094 = ByteArray(18002) var anIntArrayArray3095: Array = Array(6) { IntArray(258) } - var anInt3096 = 0 var anInt3097 = 0 var aByteArrayArray3098: Array = Array(6) { ByteArray(258) } var anIntArrayArray3099: Array = Array(6) { IntArray(258) } var anInt3100 = 0 var aByteArray3101: ByteArray = ByteArray(4096) - var anInt3102 = 0 var aBooleanArray3103: BooleanArray = BooleanArray(256) - var anInt3105 = 0 var anInt3106 = 0 var aByteArray3107: ByteArray = ByteArray(256) var aByte3108: Byte = 0 - var compressed: ByteArray? = null + var compressed: ByteArray = ByteArray(0) } } \ No newline at end of file From fa2de82d2fe7ed495ceb4c51d27e63f17068f244 Mon Sep 17 00:00:00 2001 From: GregHib Date: Wed, 10 Jan 2024 12:56:35 +0000 Subject: [PATCH 14/24] Create byte array after type check to improve performance --- .../displee/compress/type/GZIPCompressor.kt | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt index 728fe87..83fbd69 100644 --- a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt @@ -11,32 +11,29 @@ import java.util.zip.Inflater class GZIPCompressor : Compressor { private var inflater: Inflater? = null - private val gzipBuffer = ByteArray(1000_000) //because in 317 Jagex stores a lot of data in one file + private var gzipBuffer: ByteArray? = null override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { - val decompressed = ByteArray(decompressedSize) - if (!inflate(buffer, decompressed)) { - return byteArrayOf() - } - return decompressed + return inflate(buffer, decompressedSize) } - fun inflate(buffer: InputBuffer, data: ByteArray): Boolean { + private fun inflate(buffer: InputBuffer, decompressedSize: Int): ByteArray { val bytes = buffer.raw() val offset = buffer.offset if (bytes[offset].toInt() != 31 || bytes[offset + 1].toInt() != -117) { - return false + return byteArrayOf() } val inflater = this.inflater ?: Inflater(true).also { inflater = it } - try { + return try { inflater.setInput(bytes, offset + 10, bytes.size - (10 + offset + 8)) - inflater.inflate(data) + val decompressed = ByteArray(decompressedSize) + inflater.inflate(decompressed) + decompressed } catch (exception: Exception) { + byteArrayOf() + } finally { inflater.reset() - return false } - inflater.reset() - return true } override fun compress(bytes: ByteArray): ByteArray { @@ -56,6 +53,11 @@ class GZIPCompressor : Compressor { var read = 0 try { val gis = GZIPInputStream(ByteArrayInputStream(data)) + var gzipBuffer = gzipBuffer + if (gzipBuffer == null) { + gzipBuffer = ByteArray(1_000_000) //because in 317 Jagex stores a lot of data in one file + this.gzipBuffer = gzipBuffer + } while (true) { if (read == gzipBuffer.size) { throw RuntimeException("buffer overflow!") From 8bead5f23791956f115f8d2626d6d9b3c5cb4faf Mon Sep 17 00:00:00 2001 From: GregHib Date: Thu, 11 Jan 2024 21:51:56 +0000 Subject: [PATCH 15/24] Convert index count to variable --- src/main/kotlin/com/displee/cache/CacheLibrary.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/displee/cache/CacheLibrary.kt b/src/main/kotlin/com/displee/cache/CacheLibrary.kt index 6dc5850..b7a0fa6 100644 --- a/src/main/kotlin/com/displee/cache/CacheLibrary.kt +++ b/src/main/kotlin/com/displee/cache/CacheLibrary.kt @@ -31,6 +31,9 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa var closed = false + private val indexCount: Int + get() = indices.keys.max() ?: 0 + init { init() } @@ -122,7 +125,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa @JvmOverloads fun createIndex(compressionType: CompressionType = CompressionType.GZIP, version: Int = 6, revision: Int = 0, named: Boolean = false, whirlpool: Boolean = false, flag4: Boolean = false, flag8: Boolean = false, - writeReferenceTable: Boolean = true, id: Int = if (indices.isEmpty()) 0 else indexCount() + 1): Index { + writeReferenceTable: Boolean = true, id: Int = if (indices.isEmpty()) 0 else indexCount + 1): Index { val raf = RandomAccessFile(File(path, "$CACHE_FILE_NAME.idx$id"), "rw") val index = (if (is317()) Index317(this, id, raf) else Index(this, id, raf)).also { indices[id] = it } if (!writeReferenceTable) { @@ -253,7 +256,7 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa if (is317()) { throw UnsupportedOperationException("317 not supported to remove indices yet.") } - val id = indexCount() - 1 + val id = indexCount - 1 val index = indices[id] ?: return index.close() val file = File(path, "$CACHE_FILE_NAME.idx$id") @@ -266,9 +269,9 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa @JvmOverloads fun generateUkeys(writeWhirlpool: Boolean = true, exponent: BigInteger? = null, modulus: BigInteger? = null): ByteArray { - val buffer = OutputBuffer(6 + indexCount() * 72) + val buffer = OutputBuffer(6 + indexCount * 72) if (writeWhirlpool) { - buffer.writeByte(indexCount()) + buffer.writeByte(indexCount) } val emptyWhirlpool = ByteArray(WHIRLPOOL_SIZE) for (index in indices()) { @@ -359,11 +362,9 @@ open class CacheLibrary(val path: String, val clearDataAfterUpdate: Boolean = fa fun isOSRS(): Boolean { val index = index(2) - return index.revision >= 300 && indexCount() <= 23 + return index.revision >= 300 && indexCount <= 23 } - private fun indexCount() = indices.keys.max() ?: 0 - fun isRS3(): Boolean { return rs3 } From 4939f3fdd6cd9b2e4a89fd60cf9a5c25c9efc775 Mon Sep 17 00:00:00 2001 From: GregHib Date: Thu, 11 Jan 2024 23:43:10 +0000 Subject: [PATCH 16/24] Fix 317 BZip2 decompression and remove unused compressedData parameter --- .../kotlin/com/displee/cache/index/archive/Archive317.kt | 4 ++-- src/main/kotlin/com/displee/compress/CompressionExt.kt | 2 +- src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt | 5 +++-- src/main/kotlin/com/displee/compress/type/Compressor.kt | 2 +- src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt | 2 +- src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt | 2 +- src/main/kotlin/com/displee/compress/type/LZMACompressor.kt | 2 +- 7 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt index 48349b0..f14f060 100644 --- a/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt +++ b/src/main/kotlin/com/displee/cache/index/archive/Archive317.kt @@ -27,7 +27,7 @@ class Archive317(private val bzip2: BZIP2Compressor, id: Int, name: Int) : Archi extracted = true } var compressor = if (extracted) bzip2 else EmptyCompressor - val decompressed = compressor.decompress(buffer, byteArrayOf(), compressedLength, decompressedLength, 0) + val decompressed = compressor.decompress(buffer, compressedLength, decompressedLength) val metaBuffer = InputBuffer(decompressed) val filesLength = metaBuffer.readUnsignedShort() val filesBuffer = InputBuffer(decompressed) @@ -37,7 +37,7 @@ class Archive317(private val bzip2: BZIP2Compressor, id: Int, name: Int) : Archi val fileName = metaBuffer.readInt() decompressedLength = metaBuffer.read24BitInt() compressedLength = metaBuffer.read24BitInt() - val data: ByteArray = compressor.decompress(buffer, byteArrayOf(), compressedLength, decompressedLength, 0) + val data: ByteArray = compressor.decompress(filesBuffer, compressedLength, decompressedLength) files[i] = File(i, data, fileName) } } diff --git a/src/main/kotlin/com/displee/compress/CompressionExt.kt b/src/main/kotlin/com/displee/compress/CompressionExt.kt index 44e5d51..5e57325 100644 --- a/src/main/kotlin/com/displee/compress/CompressionExt.kt +++ b/src/main/kotlin/com/displee/compress/CompressionExt.kt @@ -38,5 +38,5 @@ fun ArchiveSector.decompress(compressors: Compressors, keys: IntArray? = null): if (compressionType != CompressionType.NONE) { decompressedSize = buffer.readInt() and 0xFFFFFF } - return compressor.decompress(buffer, compressedData, compressedSize, decompressedSize) + return compressor.decompress(buffer, compressedSize, decompressedSize, 9) } \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt index 8ee0e44..c83a85a 100644 --- a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt @@ -49,9 +49,10 @@ class BZIP2Compressor : Compressor { } } - override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { val decompressed = ByteArray(decompressedSize) - decompress(decompressed, decompressed.size, compressedData, compressedSize, offset) + val compressed = buffer.readBytes(compressedSize) + decompress(decompressed, decompressed.size, compressed, compressedSize, offset) return decompressed } diff --git a/src/main/kotlin/com/displee/compress/type/Compressor.kt b/src/main/kotlin/com/displee/compress/type/Compressor.kt index 217f917..d56a859 100644 --- a/src/main/kotlin/com/displee/compress/type/Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/Compressor.kt @@ -6,6 +6,6 @@ interface Compressor { fun compress(bytes: ByteArray): ByteArray - fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int = 9): ByteArray + fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int = 0): ByteArray } \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt b/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt index e33173a..290e0c3 100644 --- a/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt @@ -7,7 +7,7 @@ object EmptyCompressor : Compressor { return bytes } - override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { return buffer.readBytes(compressedSize) } } \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt index 83fbd69..8e64c4c 100644 --- a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt @@ -13,7 +13,7 @@ class GZIPCompressor : Compressor { private var inflater: Inflater? = null private var gzipBuffer: ByteArray? = null - override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { return inflate(buffer, decompressedSize) } diff --git a/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt b/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt index 25350d3..5b04fab 100644 --- a/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt @@ -48,7 +48,7 @@ class LZMACompressor : Compressor { return baos.toByteArray() } - override fun decompress(buffer: InputBuffer, compressedData: ByteArray, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { val output = OutputBuffer(buffer.remaining()) output.writeBytes(buffer.raw(), buffer.offset, buffer.remaining()) return try { From a75b0b278b35045354607a6c48851e7c3c3fbbfe Mon Sep 17 00:00:00 2001 From: Displee Date: Fri, 12 Jan 2024 01:04:18 +0100 Subject: [PATCH 17/24] Move from Groovy to kts. Update Gradle and Kotlin. --- build.gradle | 105 ----------------------- build.gradle.kts | 101 ++++++++++++++++++++++ gradle/wrapper/gradle-wrapper.properties | 2 +- 3 files changed, 102 insertions(+), 106 deletions(-) delete mode 100644 build.gradle create mode 100644 build.gradle.kts diff --git a/build.gradle b/build.gradle deleted file mode 100644 index 828f183..0000000 --- a/build.gradle +++ /dev/null @@ -1,105 +0,0 @@ -apply plugin: 'java' -apply plugin: 'kotlin' -apply plugin: 'maven' -apply plugin: 'signing' - -buildscript { - ext.kotlin_version = '1.3.61' - repositories { - mavenCentral() - maven { - url 'https://plugins.gradle.org/m2/' - } - } - dependencies { - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - -group = 'com.displee' -version = '6.9' - -description = "A library written in Kotlin used to read and write to all cache formats of RuneScape." - -sourceCompatibility = 8 -targetCompatibility = 8 - -repositories { - jcenter() - mavenCentral() -} - -compileKotlin { - kotlinOptions { - jvmTarget = "1.8" - } -} -compileTestKotlin { - kotlinOptions { - jvmTarget = "1.8" - } -} - -dependencies { - implementation 'com.github.jponge:lzma-java:1.3' - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation 'org.apache.ant:ant:1.10.7' - implementation 'com.displee:disio:2.2' - testCompile 'junit:junit:4.12' -} - -task javadocJar(type: Jar) { - classifier = 'javadoc' - from javadoc -} - -task sourcesJar(type: Jar) { - classifier = 'sources' - from sourceSets.main.allSource -} - -artifacts { - archives javadocJar, sourcesJar -} - -signing { - sign configurations.archives -} - -uploadArchives { - repositories { - mavenDeployer { - beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } - repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { - authentication(userName: ossrhUsername, password: ossrhPassword) - } - snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { - authentication(userName: ossrhUsername, password: ossrhPassword) - } - pom.project { - name rootProject.getName() - packaging 'jar' - description description - url 'https://github.com/Displee/rs-cache-library' - scm { - connection 'scm:git:git://github.com/Displee/rs-cache-library.git' - developerConnection 'scm:git:ssh://git@github.com/Displee/rs-cache-library.git' - url 'https://github.com/Displee/rs-cache-library' - } - licenses { - license { - name 'MIT License' - url 'https://github.com/Displee/rs-cache-library/blob/master/LICENSE' - } - } - developers { - developer { - id 'Displee' - name 'Yassin Amhagi' - email 'displee@hotmail.com' - } - } - } - } - } -} diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..70cee4a --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,101 @@ +import org.gradle.internal.impldep.org.jsoup.safety.Safelist.basic + +plugins { + kotlin("jvm") version "1.9.21" + `maven-publish` + signing +} + +group = "com.displee" +version = "7.0" + +description = "A library written in Kotlin used to read and write to all cache formats of RuneScape." + +repositories { + mavenCentral() +} + +dependencies { + implementation("com.github.jponge:lzma-java:1.3") + //implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version") + implementation("org.apache.ant:ant:1.10.11") + implementation("com.displee:disio:2.2") + testImplementation("junit:junit:4.13.1") +} + +tasks { + val sourcesJar by creating(Jar::class) { + archiveClassifier.set("sources") + from(sourceSets.main.get().allSource) + } + + val javadocJar by creating(Jar::class) { + dependsOn.add(javadoc) + archiveClassifier.set("javadoc") + from(javadoc) + } + + artifacts { + archives(sourcesJar) + archives(javadocJar) + archives(jar) + } +} + +val ossrhUsername: String by project +val ossrhPassword: String by project + +publishing { + repositories { + maven { + val releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" + val snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/" + url = uri(if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl) + credentials { + username = ossrhUsername + password = ossrhPassword + } + } + } + publications { + create("mavenJava") { + pom { + name = rootProject.name + description = rootProject.description + url = "https://github.com/Displee/rs-cache-library" + packaging = "jar" + licenses { + license { + name = "MIT License" + url = "https://github.com/Displee/rs-cache-library/blob/master/LICENSE" + } + } + developers { + developer { + id = "Displee" + name = "Yassin Amhagi" + email = "displee@hotmail.com" + } + developer { + id = "Greg" + name = "Greg" + email = "greg@gregs.world" + } + } + scm { + connection = "scm:git:git://github.com/Displee/rs-cache-library.git" + developerConnection = "scm:git:ssh://git@github.com/Displee/rs-cache-library.git" + url = "https://github.com/Displee/rs-cache-library" + } + } + } + } +} + +signing { + sign(publishing.publications["mavenJava"]) +} + +kotlin { + jvmToolchain(8) +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5b62fbc..a9487fd 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ #Tue May 19 01:09:51 CEST 2020 -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStorePath=wrapper/dists From 181f1ee9966d04aff92b1cf95270cedd7fd76f43 Mon Sep 17 00:00:00 2001 From: Displee Date: Fri, 12 Jan 2024 01:24:25 +0100 Subject: [PATCH 18/24] Set decompression offset to 0. --- src/main/kotlin/com/displee/compress/CompressionExt.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/displee/compress/CompressionExt.kt b/src/main/kotlin/com/displee/compress/CompressionExt.kt index 5e57325..4758ef7 100644 --- a/src/main/kotlin/com/displee/compress/CompressionExt.kt +++ b/src/main/kotlin/com/displee/compress/CompressionExt.kt @@ -38,5 +38,5 @@ fun ArchiveSector.decompress(compressors: Compressors, keys: IntArray? = null): if (compressionType != CompressionType.NONE) { decompressedSize = buffer.readInt() and 0xFFFFFF } - return compressor.decompress(buffer, compressedSize, decompressedSize, 9) + return compressor.decompress(buffer, compressedSize, decompressedSize, 0) } \ No newline at end of file From c6064b68bd734e90c1afdca099de4c4958909ccc Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 12 Jan 2024 00:29:40 +0000 Subject: [PATCH 19/24] Remove unused offset parameter --- src/main/kotlin/com/displee/compress/CompressionExt.kt | 2 +- src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt | 4 ++-- src/main/kotlin/com/displee/compress/type/Compressor.kt | 2 +- src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt | 2 +- src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt | 2 +- src/main/kotlin/com/displee/compress/type/LZMACompressor.kt | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/displee/compress/CompressionExt.kt b/src/main/kotlin/com/displee/compress/CompressionExt.kt index 4758ef7..a70326b 100644 --- a/src/main/kotlin/com/displee/compress/CompressionExt.kt +++ b/src/main/kotlin/com/displee/compress/CompressionExt.kt @@ -38,5 +38,5 @@ fun ArchiveSector.decompress(compressors: Compressors, keys: IntArray? = null): if (compressionType != CompressionType.NONE) { decompressedSize = buffer.readInt() and 0xFFFFFF } - return compressor.decompress(buffer, compressedSize, decompressedSize, 0) + return compressor.decompress(buffer, compressedSize, decompressedSize) } \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt index c83a85a..994a071 100644 --- a/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/BZIP2Compressor.kt @@ -49,10 +49,10 @@ class BZIP2Compressor : Compressor { } } - override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int): ByteArray { val decompressed = ByteArray(decompressedSize) val compressed = buffer.readBytes(compressedSize) - decompress(decompressed, decompressed.size, compressed, compressedSize, offset) + decompress(decompressed, decompressed.size, compressed, compressedSize, 0) return decompressed } diff --git a/src/main/kotlin/com/displee/compress/type/Compressor.kt b/src/main/kotlin/com/displee/compress/type/Compressor.kt index d56a859..423467e 100644 --- a/src/main/kotlin/com/displee/compress/type/Compressor.kt +++ b/src/main/kotlin/com/displee/compress/type/Compressor.kt @@ -6,6 +6,6 @@ interface Compressor { fun compress(bytes: ByteArray): ByteArray - fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int = 0): ByteArray + fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int): ByteArray } \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt b/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt index 290e0c3..e07833b 100644 --- a/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt @@ -7,7 +7,7 @@ object EmptyCompressor : Compressor { return bytes } - override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int): ByteArray { return buffer.readBytes(compressedSize) } } \ No newline at end of file diff --git a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt index 8e64c4c..4b9afc4 100644 --- a/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/GZIPCompressor.kt @@ -13,7 +13,7 @@ class GZIPCompressor : Compressor { private var inflater: Inflater? = null private var gzipBuffer: ByteArray? = null - override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int): ByteArray { return inflate(buffer, decompressedSize) } diff --git a/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt b/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt index 5b04fab..a1d940c 100644 --- a/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/LZMACompressor.kt @@ -48,7 +48,7 @@ class LZMACompressor : Compressor { return baos.toByteArray() } - override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int, offset: Int): ByteArray { + override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int): ByteArray { val output = OutputBuffer(buffer.remaining()) output.writeBytes(buffer.raw(), buffer.offset, buffer.remaining()) return try { From dcc5c2c20a745c529c64fcc566e5eedf73d6344b Mon Sep 17 00:00:00 2001 From: GregHib Date: Fri, 12 Jan 2024 00:29:54 +0000 Subject: [PATCH 20/24] Use decompressed size in empty compressor --- src/main/kotlin/com/displee/compress/CompressionExt.kt | 2 +- src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/displee/compress/CompressionExt.kt b/src/main/kotlin/com/displee/compress/CompressionExt.kt index a70326b..f56642d 100644 --- a/src/main/kotlin/com/displee/compress/CompressionExt.kt +++ b/src/main/kotlin/com/displee/compress/CompressionExt.kt @@ -34,7 +34,7 @@ fun ArchiveSector.decompress(compressors: Compressors, keys: IntArray? = null): compressionType = CompressionType.compressionTypes[type] compressor = compressors.get(compressionType) val compressedSize = buffer.readInt() and 0xFFFFFF - var decompressedSize = 0 + var decompressedSize = compressedSize if (compressionType != CompressionType.NONE) { decompressedSize = buffer.readInt() and 0xFFFFFF } diff --git a/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt b/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt index e07833b..97c9637 100644 --- a/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt +++ b/src/main/kotlin/com/displee/compress/type/EmptyCompressor.kt @@ -8,6 +8,6 @@ object EmptyCompressor : Compressor { } override fun decompress(buffer: InputBuffer, compressedSize: Int, decompressedSize: Int): ByteArray { - return buffer.readBytes(compressedSize) + return buffer.readBytes(decompressedSize) } } \ No newline at end of file From bb398e4754fac335bdad5fac83d0b13d4df7ca40 Mon Sep 17 00:00:00 2001 From: Displee Date: Fri, 12 Jan 2024 01:38:02 +0100 Subject: [PATCH 21/24] Clean up build file. --- build.gradle.kts | 3 --- 1 file changed, 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 70cee4a..5876167 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,3 @@ -import org.gradle.internal.impldep.org.jsoup.safety.Safelist.basic - plugins { kotlin("jvm") version "1.9.21" `maven-publish` @@ -17,7 +15,6 @@ repositories { dependencies { implementation("com.github.jponge:lzma-java:1.3") - //implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version") implementation("org.apache.ant:ant:1.10.11") implementation("com.displee:disio:2.2") testImplementation("junit:junit:4.13.1") From db38be12babcccf4126488d96be8b2f2d0ca5557 Mon Sep 17 00:00:00 2001 From: Displee Date: Fri, 12 Jan 2024 01:49:36 +0100 Subject: [PATCH 22/24] Fix sources and java doc. --- build.gradle.kts | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 5876167..03f3035 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,23 +20,9 @@ dependencies { testImplementation("junit:junit:4.13.1") } -tasks { - val sourcesJar by creating(Jar::class) { - archiveClassifier.set("sources") - from(sourceSets.main.get().allSource) - } - - val javadocJar by creating(Jar::class) { - dependsOn.add(javadoc) - archiveClassifier.set("javadoc") - from(javadoc) - } - - artifacts { - archives(sourcesJar) - archives(javadocJar) - archives(jar) - } +java { + withJavadocJar() + withSourcesJar() } val ossrhUsername: String by project From ba2ea505640199d65fc590f4575d1a538ed803d6 Mon Sep 17 00:00:00 2001 From: Displee Date: Fri, 12 Jan 2024 01:56:03 +0100 Subject: [PATCH 23/24] Add Java components to Maven publication. --- build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index 03f3035..9cd7d36 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,6 +42,8 @@ publishing { } publications { create("mavenJava") { + from(components["java"]) + pom { name = rootProject.name description = rootProject.description From bd82a78357894f5d10f972882de9b477ae73aae1 Mon Sep 17 00:00:00 2001 From: Displee Date: Fri, 12 Jan 2024 15:03:36 +0100 Subject: [PATCH 24/24] Update README. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index de9accd..458d38f 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ This library is able to read this data, and write manipulated data back to the c ## Gradle ``` -implementation 'com.displee:rs-cache-library:6.9' +implementation 'com.displee:rs-cache-library:7.0' ``` ## Initialize your cache ```kotlin