From e5e9ece0989d3aa01fe19aff6cb0870d74d0203d Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 30 Aug 2020 21:39:29 +0100 Subject: [PATCH] Fix JS5 uncompression of large GZIP-compressed files Signed-off-by: Graham --- .../java/dev/openrs2/cache/Js5Compression.kt | 10 +++++++++- .../dev/openrs2/cache/Js5CompressionTest.kt | 11 +++++++++++ .../openrs2/cache/compression/gzip-large.dat | Bin 0 -> 12349 bytes .../dev/openrs2/cache/compression/large.dat | 1 + 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 cache/src/test/resources/dev/openrs2/cache/compression/gzip-large.dat create mode 100644 cache/src/test/resources/dev/openrs2/cache/compression/large.dat diff --git a/cache/src/main/java/dev/openrs2/cache/Js5Compression.kt b/cache/src/main/java/dev/openrs2/cache/Js5Compression.kt index 8cc8fa4f..d110b4e2 100644 --- a/cache/src/main/java/dev/openrs2/cache/Js5Compression.kt +++ b/cache/src/main/java/dev/openrs2/cache/Js5Compression.kt @@ -7,6 +7,7 @@ import dev.openrs2.crypto.xteaEncrypt import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBufInputStream import io.netty.buffer.ByteBufOutputStream +import java.io.EOFException public object Js5Compression { public fun compress(input: ByteBuf, type: Js5CompressionType, key: XteaKey = XteaKey.ZERO): ByteBuf { @@ -102,7 +103,14 @@ public object Js5Compression { plaintext.alloc().buffer(uncompressedLen, uncompressedLen).use { output -> type.createInputStream(ByteBufInputStream(plaintext, len), uncompressedLen).use { inputStream -> - output.writeBytes(inputStream, uncompressedLen) + var remaining = uncompressedLen + while (remaining > 0) { + val n = output.writeBytes(inputStream, remaining) + if (n == -1) { + throw EOFException() + } + remaining -= n + } } return output.retain() diff --git a/cache/src/test/java/dev/openrs2/cache/Js5CompressionTest.kt b/cache/src/test/java/dev/openrs2/cache/Js5CompressionTest.kt index 206ee8b3..2151283d 100644 --- a/cache/src/test/java/dev/openrs2/cache/Js5CompressionTest.kt +++ b/cache/src/test/java/dev/openrs2/cache/Js5CompressionTest.kt @@ -55,6 +55,17 @@ object Js5CompressionTest { } } + @Test + fun testUncompressLargeGzip() { + read("gzip-large.dat").use { input -> + Js5Compression.uncompress(input).use { actual -> + read("large.dat").use { expected -> + assertEquals(expected, actual) + } + } + } + } + @Test fun testCompressBzip2() { Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> diff --git a/cache/src/test/resources/dev/openrs2/cache/compression/gzip-large.dat b/cache/src/test/resources/dev/openrs2/cache/compression/gzip-large.dat new file mode 100644 index 0000000000000000000000000000000000000000..c6ec617a2c5acb00baf8f794be9849eacb9c7753 GIT binary patch literal 12349 zcmV-DFv8CQ001yF002M$ABzYGW(`bV0t0Mea%W{OWMOmw6`RF!gisJge+ic*?(T1g zyEE9=hh&wiP?f;zzW1D&jJ=neZA-s=WG3I3Xq?C}ppTwj-AKQ+V(h_VIGs#~=-bw2 z`9Lbi{M-HA;)_#l7UtcRmy>@sn&8!Z3(j!FN)t)AB9^bQGSRC;F<(z5LsHic%>yeZ zO5ONU*-y+C`~nb}_{yH#3%M{&rn0?9!Gx?^eEjLF~dTXnR74AvyYUa)4 zJ`I%bimUW|4`}gz9sH1^t+N5eDa>i{uyVspdxbp}C&y(!3JFPqueaQgHp1@Y_nTAm z>KRvga%SsdC=fYPo%w(s&TW6dX0IzR(F?IF! zojbJ5&2V&iUiZn+w#X5Duo!u{fc2W3p z1|Vo6U^}wiX;kw%io^l_nMQp3)q~KJCLR-x9D9&YgA2Vg-O63|*%BSV<+Blm94JsHJYDIQ*YCSCw z9q5viDNM8$;DoI}k0TN78&PJxT_KeFtN@G_B$!cocEVk>hIk;So1P#vSIOhUUaqAf zEWL(#=+#+$;p#Zso0APgtcFxJSUbuJZJ6<}l&nQ}hD!~}?D{eX%YK7O>22y9#2XKS z^D|uq^AB z(W2apzZb{d!*h}>U;I5ElQj)=Ba>XpS5!P`eK}!S$81j$-S5(If7;ND2VoCyACmyt zibL^zJw+ri$gV$^5>9n;5Id@HRz5x@3@)9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