Ensure uncompressIfKeyValid always consumes the entire container

Signed-off-by: Graham <gpe@openrs2.org>
bzip2
Graham 4 years ago
parent fffc96a83f
commit 75d4446b92
  1. 8
      cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt

@ -195,6 +195,7 @@ public object Js5Compression {
* We therefore assume all uncompressed groups are unencrypted. * We therefore assume all uncompressed groups are unencrypted.
*/ */
if (!key.isZero) { if (!key.isZero) {
input.skipBytes(len)
return null return null
} }
@ -230,6 +231,7 @@ public object Js5Compression {
decrypt(input.slice(), 16, key).use { plaintext -> decrypt(input.slice(), 16, key).use { plaintext ->
val uncompressedLen = plaintext.readInt() val uncompressedLen = plaintext.readInt()
if (uncompressedLen < 0) { if (uncompressedLen < 0) {
input.skipBytes(lenWithUncompressedLen)
return null return null
} }
@ -239,18 +241,21 @@ public object Js5Compression {
val magic = ByteArray(BZIP2_MAGIC.size) val magic = ByteArray(BZIP2_MAGIC.size)
plaintext.readBytes(magic) plaintext.readBytes(magic)
if (!magic.contentEquals(BZIP2_MAGIC)) { if (!magic.contentEquals(BZIP2_MAGIC)) {
input.skipBytes(lenWithUncompressedLen)
return null return null
} }
} }
Js5CompressionType.GZIP -> { Js5CompressionType.GZIP -> {
val magic = plaintext.readUnsignedShort() val magic = plaintext.readUnsignedShort()
if (magic != GZIP_MAGIC) { if (magic != GZIP_MAGIC) {
input.skipBytes(lenWithUncompressedLen)
return null return null
} }
// Jagex's implementation only supports DEFLATE. // Jagex's implementation only supports DEFLATE.
val compressionMethod = plaintext.readUnsignedByte().toInt() val compressionMethod = plaintext.readUnsignedByte().toInt()
if (compressionMethod != GZIP_COMPRESSION_METHOD_DEFLATE) { if (compressionMethod != GZIP_COMPRESSION_METHOD_DEFLATE) {
input.skipBytes(lenWithUncompressedLen)
return null return null
} }
} }
@ -264,6 +269,7 @@ public object Js5Compression {
val pb = properties / 45 val pb = properties / 45
if (pb > LZMA_PB_MAX) { if (pb > LZMA_PB_MAX) {
input.skipBytes(lenWithUncompressedLen)
return null return null
} }
@ -286,8 +292,10 @@ public object Js5Compression {
*/ */
val dictSize = plaintext.readIntLE() val dictSize = plaintext.readIntLE()
if (dictSize < 0) { if (dictSize < 0) {
input.skipBytes(lenWithUncompressedLen)
return null return null
} else if (dictSize > LZMA_PRESET_DICT_SIZE_MAX) { } else if (dictSize > LZMA_PRESET_DICT_SIZE_MAX) {
input.skipBytes(lenWithUncompressedLen)
return null return null
} }
} }

Loading…
Cancel
Save