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 <gpe@openrs2.org>
Graham 4 years ago
parent 6a03ef1746
commit 7b9964461f
  1. 2
      protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoder.kt
  2. 4
      protocol/src/main/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoder.kt
  3. 14
      protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoderTest.kt
  4. 14
      protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseEncoderTest.kt
  5. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1020-prefetch.dat
  6. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1020-urgent.dat
  7. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1021-prefetch.dat
  8. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1021-urgent.dat
  9. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1021.dat
  10. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1531-prefetch.dat
  11. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1531-urgent.dat
  12. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1532-prefetch.dat
  13. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1532-urgent.dat
  14. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/1532.dat
  15. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/509-prefetch.dat
  16. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/509-urgent.dat
  17. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/510-prefetch.dat
  18. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/510-urgent.dat
  19. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/510.dat
  20. BIN
      protocol/src/test/resources/org/openrs2/protocol/js5/invalid-block-trailer.dat

@ -62,7 +62,7 @@ public class Js5ResponseDecoder : ByteToMessageDecoder() {
if (state == State.READ_DATA) { if (state == State.READ_DATA) {
while (data.isWritable) { 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 last = data.writableBytes() <= blockLen
val blockLenIncludingTrailer = if (last) { val blockLenIncludingTrailer = if (last) {

@ -24,7 +24,7 @@ public object Js5ResponseEncoder : MessageToByteEncoder<Js5Response>(Js5Response
} }
out.writeByte(compression) 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) { while (msg.data.isReadable) {
out.writeByte(0xFF) out.writeByte(0xFF)
@ -34,7 +34,7 @@ public object Js5ResponseEncoder : MessageToByteEncoder<Js5Response>(Js5Response
override fun allocateBuffer(ctx: ChannelHandlerContext, msg: Js5Response, preferDirect: Boolean): ByteBuf { override fun allocateBuffer(ctx: ChannelHandlerContext, msg: Js5Response, preferDirect: Boolean): ByteBuf {
val dataLen = msg.data.readableBytes() val dataLen = msg.data.readableBytes()
val len = 3 + dataLen + (3 + dataLen) / 511 val len = 3 + dataLen + (2 + dataLen) / 511
return if (preferDirect) { return if (preferDirect) {
ctx.alloc().ioBuffer(len, len) ctx.alloc().ioBuffer(len, len)

@ -14,23 +14,23 @@ import kotlin.test.assertEquals
object Js5ResponseDecoderTest { object Js5ResponseDecoderTest {
@Test @Test
fun testDecode() { 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-prefetch.dat", true)
testDecode("509.dat", "509-urgent.dat", false) testDecode("509.dat", "509-urgent.dat", false)
testDecode("1019.dat", "1019-prefetch.dat", true) testDecode("510.dat", "510-prefetch.dat", true)
testDecode("1019.dat", "1019-urgent.dat", false) testDecode("510.dat", "510-urgent.dat", false)
testDecode("1020.dat", "1020-prefetch.dat", true) testDecode("1020.dat", "1020-prefetch.dat", true)
testDecode("1020.dat", "1020-urgent.dat", false) testDecode("1020.dat", "1020-urgent.dat", false)
testDecode("1530.dat", "1530-prefetch.dat", true) testDecode("1021.dat", "1021-prefetch.dat", true)
testDecode("1530.dat", "1530-urgent.dat", false) testDecode("1021.dat", "1021-urgent.dat", false)
testDecode("1531.dat", "1531-prefetch.dat", true) testDecode("1531.dat", "1531-prefetch.dat", true)
testDecode("1531.dat", "1531-urgent.dat", false) testDecode("1531.dat", "1531-urgent.dat", false)
testDecode("1532.dat", "1532-prefetch.dat", true)
testDecode("1532.dat", "1532-urgent.dat", false)
} }
@Test @Test

@ -12,23 +12,23 @@ import kotlin.test.assertEquals
object Js5ResponseEncoderTest { object Js5ResponseEncoderTest {
@Test @Test
fun testEncode() { 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-prefetch.dat", true)
testEncode("509.dat", "509-urgent.dat", false) testEncode("509.dat", "509-urgent.dat", false)
testEncode("1019.dat", "1019-prefetch.dat", true) testEncode("510.dat", "510-prefetch.dat", true)
testEncode("1019.dat", "1019-urgent.dat", false) testEncode("510.dat", "510-urgent.dat", false)
testEncode("1020.dat", "1020-prefetch.dat", true) testEncode("1020.dat", "1020-prefetch.dat", true)
testEncode("1020.dat", "1020-urgent.dat", false) testEncode("1020.dat", "1020-urgent.dat", false)
testEncode("1530.dat", "1530-prefetch.dat", true) testEncode("1021.dat", "1021-prefetch.dat", true)
testEncode("1530.dat", "1530-urgent.dat", false) testEncode("1021.dat", "1021-urgent.dat", false)
testEncode("1531.dat", "1531-prefetch.dat", true) testEncode("1531.dat", "1531-prefetch.dat", true)
testEncode("1531.dat", "1531-urgent.dat", false) testEncode("1531.dat", "1531-urgent.dat", false)
testEncode("1532.dat", "1532-prefetch.dat", true)
testEncode("1532.dat", "1532-urgent.dat", false)
} }
@Test @Test

Loading…
Cancel
Save