From 7b9964461f357a821c6e1fa8c9a3c3b547fccc22 Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 22 Dec 2020 23:50:23 +0000 Subject: [PATCH] Fix off-by-one erorr in the JS5 codecs Excluding the 0xFF trailers, the first block is actually 512 bytes, and the remaining blocks are all 511 bytes. I had mistakenly written the code and tests on the basis of all blocks being 511 bytes. The Js5ResponseDecoder now works with OSRS. Signed-off-by: Graham --- .../protocol/js5/Js5ResponseDecoder.kt | 2 +- .../protocol/js5/Js5ResponseEncoder.kt | 4 ++-- .../protocol/js5/Js5ResponseDecoderTest.kt | 14 +++++++------- .../protocol/js5/Js5ResponseEncoderTest.kt | 14 +++++++------- .../openrs2/protocol/js5/1020-prefetch.dat | Bin 1025 -> 1024 bytes .../org/openrs2/protocol/js5/1020-urgent.dat | Bin 1025 -> 1024 bytes .../{1019-prefetch.dat => 1021-prefetch.dat} | Bin 1023 -> 1026 bytes .../js5/{1019-urgent.dat => 1021-urgent.dat} | Bin 1023 -> 1026 bytes .../protocol/js5/{1019.dat => 1021.dat} | Bin 1019 -> 1021 bytes .../openrs2/protocol/js5/1531-prefetch.dat | Bin 1537 -> 1536 bytes .../org/openrs2/protocol/js5/1531-urgent.dat | Bin 1537 -> 1536 bytes .../{1530-urgent.dat => 1532-prefetch.dat} | Bin 1535 -> 1538 bytes .../{1530-prefetch.dat => 1532-urgent.dat} | Bin 1535 -> 1538 bytes .../protocol/js5/{1530.dat => 1532.dat} | Bin 1530 -> 1532 bytes .../org/openrs2/protocol/js5/509-prefetch.dat | Bin 513 -> 512 bytes .../org/openrs2/protocol/js5/509-urgent.dat | Bin 513 -> 512 bytes .../js5/{508-urgent.dat => 510-prefetch.dat} | Bin 511 -> 514 bytes .../js5/{508-prefetch.dat => 510-urgent.dat} | Bin 511 -> 514 bytes .../openrs2/protocol/js5/{508.dat => 510.dat} | Bin 508 -> 510 bytes .../protocol/js5/invalid-block-trailer.dat | Bin 513 -> 514 bytes 20 files changed, 17 insertions(+), 17 deletions(-) rename protocol/src/test/resources/org/openrs2/protocol/js5/{1019-prefetch.dat => 1021-prefetch.dat} (81%) rename protocol/src/test/resources/org/openrs2/protocol/js5/{1019-urgent.dat => 1021-urgent.dat} (81%) rename protocol/src/test/resources/org/openrs2/protocol/js5/{1019.dat => 1021.dat} (87%) rename protocol/src/test/resources/org/openrs2/protocol/js5/{1530-urgent.dat => 1532-prefetch.dat} (83%) rename protocol/src/test/resources/org/openrs2/protocol/js5/{1530-prefetch.dat => 1532-urgent.dat} (83%) rename protocol/src/test/resources/org/openrs2/protocol/js5/{1530.dat => 1532.dat} (91%) rename protocol/src/test/resources/org/openrs2/protocol/js5/{508-urgent.dat => 510-prefetch.dat} (74%) rename protocol/src/test/resources/org/openrs2/protocol/js5/{508-prefetch.dat => 510-urgent.dat} (74%) rename protocol/src/test/resources/org/openrs2/protocol/js5/{508.dat => 510.dat} (75%) diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoder.kt b/protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoder.kt index 888eb07c..19b06c40 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoder.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoder.kt @@ -62,7 +62,7 @@ public class Js5ResponseDecoder : ByteToMessageDecoder() { if (state == State.READ_DATA) { while (data.isWritable) { - val blockLen = min(511 - ((data.readableBytes() + 3) % 511), data.writableBytes()) + val blockLen = min(511 - ((data.readableBytes() + 2) % 511), data.writableBytes()) val last = data.writableBytes() <= blockLen val blockLenIncludingTrailer = if (last) { diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoder.kt b/protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoder.kt index d2650ef1..be7342e9 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoder.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoder.kt @@ -24,7 +24,7 @@ public object Js5ResponseEncoder : MessageToByteEncoder(Js5Response } out.writeByte(compression) - out.writeBytes(msg.data, min(msg.data.readableBytes(), 507)) + out.writeBytes(msg.data, min(msg.data.readableBytes(), 508)) while (msg.data.isReadable) { out.writeByte(0xFF) @@ -34,7 +34,7 @@ public object Js5ResponseEncoder : MessageToByteEncoder(Js5Response override fun allocateBuffer(ctx: ChannelHandlerContext, msg: Js5Response, preferDirect: Boolean): ByteBuf { val dataLen = msg.data.readableBytes() - val len = 3 + dataLen + (3 + dataLen) / 511 + val len = 3 + dataLen + (2 + dataLen) / 511 return if (preferDirect) { ctx.alloc().ioBuffer(len, len) diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoderTest.kt b/protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoderTest.kt index 007d42f8..3301b2a5 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoderTest.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoderTest.kt @@ -14,23 +14,23 @@ import kotlin.test.assertEquals object Js5ResponseDecoderTest { @Test fun testDecode() { - testDecode("508.dat", "508-prefetch.dat", true) - testDecode("508.dat", "508-urgent.dat", false) - testDecode("509.dat", "509-prefetch.dat", true) testDecode("509.dat", "509-urgent.dat", false) - testDecode("1019.dat", "1019-prefetch.dat", true) - testDecode("1019.dat", "1019-urgent.dat", false) + testDecode("510.dat", "510-prefetch.dat", true) + testDecode("510.dat", "510-urgent.dat", false) testDecode("1020.dat", "1020-prefetch.dat", true) testDecode("1020.dat", "1020-urgent.dat", false) - testDecode("1530.dat", "1530-prefetch.dat", true) - testDecode("1530.dat", "1530-urgent.dat", false) + testDecode("1021.dat", "1021-prefetch.dat", true) + testDecode("1021.dat", "1021-urgent.dat", false) testDecode("1531.dat", "1531-prefetch.dat", true) testDecode("1531.dat", "1531-urgent.dat", false) + + testDecode("1532.dat", "1532-prefetch.dat", true) + testDecode("1532.dat", "1532-urgent.dat", false) } @Test diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoderTest.kt b/protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoderTest.kt index 45671736..f559c98c 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoderTest.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoderTest.kt @@ -12,23 +12,23 @@ import kotlin.test.assertEquals object Js5ResponseEncoderTest { @Test fun testEncode() { - testEncode("508.dat", "508-prefetch.dat", true) - testEncode("508.dat", "508-urgent.dat", false) - testEncode("509.dat", "509-prefetch.dat", true) testEncode("509.dat", "509-urgent.dat", false) - testEncode("1019.dat", "1019-prefetch.dat", true) - testEncode("1019.dat", "1019-urgent.dat", false) + testEncode("510.dat", "510-prefetch.dat", true) + testEncode("510.dat", "510-urgent.dat", false) testEncode("1020.dat", "1020-prefetch.dat", true) testEncode("1020.dat", "1020-urgent.dat", false) - testEncode("1530.dat", "1530-prefetch.dat", true) - testEncode("1530.dat", "1530-urgent.dat", false) + testEncode("1021.dat", "1021-prefetch.dat", true) + testEncode("1021.dat", "1021-urgent.dat", false) testEncode("1531.dat", "1531-prefetch.dat", true) testEncode("1531.dat", "1531-urgent.dat", false) + + testEncode("1532.dat", "1532-prefetch.dat", true) + testEncode("1532.dat", "1532-urgent.dat", false) } @Test diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1020-prefetch.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1020-prefetch.dat index 7afdc6b3791092d129cac6e9a90e715ad437ca8e..1c134669a2b7dd4926203553932efa2b6c077689 100644 GIT binary patch delta 17 YcmZqVXyDlJpOMMv|7J#}e~gSq066mn-2eap delta 19 bcmZqRXyn-NpONwZM#J-r6CDhg{u==RO+p8# diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1020-urgent.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1020-urgent.dat index 57aa6a813ce790620a10b8bd350b08b87dee97ba..5126a2fdd051908314c46b747d41353c5f7e9bb6 100644 GIT binary patch delta 17 YcmZqVXyDlJpOMMv|7J#}e~gSq066mn-2eap delta 19 bcmZqRXyn-NpONwZM#J-r6CDhg{u==RO+p8# diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1019-prefetch.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1021-prefetch.dat similarity index 81% rename from protocol/src/test/resources/org/openrs2/protocol/js5/1019-prefetch.dat rename to protocol/src/test/resources/org/openrs2/protocol/js5/1021-prefetch.dat index 5d2ee5cede5cc0d6023d62b63d9a8d71d9d1dc22..b1730c5443275071497ab01698f51c3166f40396 100644 GIT binary patch delta 29 jcmey*-o(Md#K7FZz`*=tGb7UvM#ld@>K`Ms(SLscdHV?f delta 27 jcmZqT_|MM4#K7FZz`*=%Gb7V?M#lde4bL-9bT9w_YH$d6 diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1019-urgent.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1021-urgent.dat similarity index 81% rename from protocol/src/test/resources/org/openrs2/protocol/js5/1019-urgent.dat rename to protocol/src/test/resources/org/openrs2/protocol/js5/1021-urgent.dat index 5efce6780fbe1e8627d9db1af6d94a86a10bb7c4..102e9283f627a0bfdc76fd623118c795473ee932 100644 GIT binary patch delta 29 jcmey*-o(Md#K6qJz`*=tGb7UvM#ld@>K`Ms(SLscZ+i%U delta 27 jcmZqT_|MM4#K6qJz`*=%Gb7V?M#lde4bL-9bT9w_VDJa{ diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1019.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1021.dat similarity index 87% rename from protocol/src/test/resources/org/openrs2/protocol/js5/1019.dat rename to protocol/src/test/resources/org/openrs2/protocol/js5/1021.dat index a8a624b63cafd58f83dea7e9eab8a297d7bb7a9e..5c9754df90f839e5481652bf842c30b9f0b29a9d 100644 GIT binary patch delta 17 Ycmey({+FGVfq{Yf$413l%o7#l05o|8g#Z8m delta 17 Ycmey%{+peZfq{Yf+eXDZ%o7cy05p>YkpKVy diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1531-prefetch.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1531-prefetch.dat index fe8e66b4f1f98e04391515d3343d40cf37812ee5..0113c1a7a7b1cc3385d17d292e41aed6dcd70a21 100644 GIT binary patch delta 25 ZcmZqVY2exLpOMMv|7J#}e{h=72mpDx2+aTh delta 28 ccmZqRY2?}PpONwZM#J-r6CDiT45t4^0H?SMV*mgE diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1531-urgent.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1531-urgent.dat index 09993f120bbd9c3c09722b45dfaca9416fa6610b..7778ed81c50eedd157881e205b4b2ee5ff190f92 100644 GIT binary patch delta 25 ZcmZqVY2exLpOMMv|7J#}e{h=72mpDx2+aTh delta 28 ccmZqRY2?}PpONwZM#J-r6CDiT45t4^0H?SMV*mgE diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1530-urgent.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1532-prefetch.dat similarity index 83% rename from protocol/src/test/resources/org/openrs2/protocol/js5/1530-urgent.dat rename to protocol/src/test/resources/org/openrs2/protocol/js5/1532-prefetch.dat index 22c4d38a7a5b3953ddd4f471a73d93337346dc8c..323db5b1761e20ca92bb947c9fa28ac45a5a6fbf 100644 GIT binary patch delta 37 ncmey*-NeJe#K7FZz`*)_Gb7UvM#ld@>K`MM5rk$o`tJ__&!h|S literal 1535 qcmZQ#U}j)oVEyV}keU}1Y&1%ZtRVP5vb;O$TeK`b8vi4^&;tP3ED%%x diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1530-prefetch.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1532-urgent.dat similarity index 83% rename from protocol/src/test/resources/org/openrs2/protocol/js5/1530-prefetch.dat rename to protocol/src/test/resources/org/openrs2/protocol/js5/1532-urgent.dat index eee39ab81733408a963fab6d768de48b7a45fcbc..77ea56c5c3790caaa8f0162b75a4ab0f3d5ec17a 100644 GIT binary patch delta 37 ncmey*-NeJe#K6qJz`*)_Gb7UvM#ld@>K`MM5rk$o`tJ__!6XZD literal 1535 qcmZQ#U~XVwVEyV}keU}1Y&1%ZtRVP5vb;O$TeK`b8vi4^&;tP1SrF6! diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/1530.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/1532.dat similarity index 91% rename from protocol/src/test/resources/org/openrs2/protocol/js5/1530.dat rename to protocol/src/test/resources/org/openrs2/protocol/js5/1532.dat index af31cb743e7dec06d454af45d5117d5795402af5..0b2b379ca8b308ce878c5e27b0e594dd648aa4ee 100644 GIT binary patch delta 17 Ycmeyx{fC>Cfq{Yb`$ol^tP>UF05skOjQ{`u delta 17 Ycmeyv{fnEGfq{Yb>qf=ftP>5S05tdonE(I) diff --git a/protocol/src/test/resources/org/openrs2/protocol/js5/509-prefetch.dat b/protocol/src/test/resources/org/openrs2/protocol/js5/509-prefetch.dat index 8b7b5f6e7dcdb3121deeef51d62bbb64444f55d4..bc2732b2388ae861ac7211bf1bc34c9703ec761b 100644 GIT binary patch delta 9 QcmZo