feat(bytecodec): add bidirectional binary codec library
This commit is contained in:
1 parent
dbb027d021
commit
8048827121
14 files changed
+1266
No files matched your search
@@ -0,0 +1,140 @@
|
||||
package de.fiereu.bytecodec
|
||||
|
||||
import de.fiereu.bytecodec.test.assertBytesRoundtrip
|
||||
import de.fiereu.bytecodec.test.assertValueRoundtrip
|
||||
import de.fiereu.bytecodec.test.decodeBytes
|
||||
import de.fiereu.bytecodec.test.encodeToBytes
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
|
||||
class AtomsTest :
|
||||
FunSpec({
|
||||
test("Bool roundtrips") {
|
||||
Bool.assertValueRoundtrip(true)
|
||||
Bool.assertValueRoundtrip(false)
|
||||
Bool.encodeToBytes(true) shouldBe byteArrayOf(0x01)
|
||||
Bool.encodeToBytes(false) shouldBe byteArrayOf(0x00)
|
||||
Bool.decodeBytes(byteArrayOf(0x05)) shouldBe true
|
||||
}
|
||||
|
||||
test("U8 wire format") {
|
||||
U8.encodeToBytes(0) shouldBe byteArrayOf(0x00)
|
||||
U8.encodeToBytes(255) shouldBe byteArrayOf(0xFF.toByte())
|
||||
U8.decodeBytes(byteArrayOf(0xFF.toByte())) shouldBe 255
|
||||
shouldThrow<IllegalArgumentException> { U8.encodeToBytes(256) }
|
||||
}
|
||||
|
||||
test("S8 wire format") {
|
||||
S8.assertValueRoundtrip(0)
|
||||
S8.assertValueRoundtrip(-1)
|
||||
S8.assertValueRoundtrip(127)
|
||||
S8.assertValueRoundtrip(-128)
|
||||
}
|
||||
|
||||
test("U16LE wire format") {
|
||||
U16LE.encodeToBytes(0x1234) shouldBe byteArrayOf(0x34, 0x12)
|
||||
U16LE.decodeBytes(byteArrayOf(0x34, 0x12)) shouldBe 0x1234
|
||||
}
|
||||
|
||||
test("U16BE wire format") {
|
||||
U16BE.encodeToBytes(0x1234) shouldBe byteArrayOf(0x12, 0x34)
|
||||
U16BE.decodeBytes(byteArrayOf(0x12, 0x34)) shouldBe 0x1234
|
||||
}
|
||||
|
||||
test("S16LE signed") {
|
||||
S16LE.assertValueRoundtrip((-1).toShort())
|
||||
S16LE.assertValueRoundtrip(Short.MIN_VALUE)
|
||||
S16LE.assertValueRoundtrip(Short.MAX_VALUE)
|
||||
}
|
||||
|
||||
test("S32LE wire format") {
|
||||
S32LE.encodeToBytes(0x11223344) shouldBe byteArrayOf(0x44, 0x33, 0x22, 0x11)
|
||||
S32LE.decodeBytes(byteArrayOf(0x44, 0x33, 0x22, 0x11)) shouldBe 0x11223344
|
||||
S32LE.assertValueRoundtrip(Int.MIN_VALUE)
|
||||
S32LE.assertValueRoundtrip(Int.MAX_VALUE)
|
||||
}
|
||||
|
||||
test("S32BE wire format") {
|
||||
S32BE.encodeToBytes(0x11223344) shouldBe byteArrayOf(0x11, 0x22, 0x33, 0x44)
|
||||
S32BE.assertValueRoundtrip(Int.MIN_VALUE)
|
||||
}
|
||||
|
||||
test("U32LE roundtrip across full range") {
|
||||
U32LE.assertValueRoundtrip(0L)
|
||||
U32LE.assertValueRoundtrip(0xFFFFFFFFL)
|
||||
U32LE.encodeToBytes(0xFFFFFFFFL) shouldBe
|
||||
byteArrayOf(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte())
|
||||
}
|
||||
|
||||
test("U32BE roundtrip") {
|
||||
U32BE.encodeToBytes(0x11223344L) shouldBe byteArrayOf(0x11, 0x22, 0x33, 0x44)
|
||||
}
|
||||
|
||||
test("S64LE wire format") {
|
||||
S64LE.encodeToBytes(0x0102030405060708L) shouldBe
|
||||
byteArrayOf(0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01)
|
||||
S64LE.assertValueRoundtrip(Long.MIN_VALUE)
|
||||
S64LE.assertValueRoundtrip(Long.MAX_VALUE)
|
||||
}
|
||||
|
||||
test("S64BE wire format") {
|
||||
S64BE.encodeToBytes(0x0102030405060708L) shouldBe
|
||||
byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
|
||||
}
|
||||
|
||||
test("F32 / F64 roundtrip") {
|
||||
F32LE.assertValueRoundtrip(3.14f)
|
||||
F32BE.assertValueRoundtrip(-0.0f)
|
||||
F64LE.assertValueRoundtrip(2.718281828)
|
||||
F64BE.assertValueRoundtrip(Double.NEGATIVE_INFINITY)
|
||||
}
|
||||
|
||||
test("fixedBytes preserves payload") {
|
||||
val codec = fixedBytes(4)
|
||||
val payload = byteArrayOf(0x01, 0x02, 0x03, 0x04)
|
||||
codec.assertBytesRoundtrip(payload)
|
||||
shouldThrow<MalformedPacketException> { codec.encodeToBytes(byteArrayOf(0, 0, 0)) }
|
||||
}
|
||||
|
||||
test("unknownBytes is lossless") {
|
||||
val codec = unknownBytes(3)
|
||||
codec.assertBytesRoundtrip(byteArrayOf(0xDE.toByte(), 0xAD.toByte(), 0xBE.toByte()))
|
||||
}
|
||||
|
||||
test("reserved strict checks value") {
|
||||
val codec = reserved(0xAB, strict = true)
|
||||
codec.decodeBytes(byteArrayOf(0xAB.toByte())) shouldBe Unit
|
||||
shouldThrow<ReservedByteMismatchException> { codec.decodeBytes(byteArrayOf(0x01)) }
|
||||
}
|
||||
|
||||
test("reserved non-strict accepts anything on read but writes literal") {
|
||||
val codec = reserved(0x00)
|
||||
codec.decodeBytes(byteArrayOf(0xFF.toByte())) shouldBe Unit
|
||||
codec.encodeToBytes(Unit) shouldBe byteArrayOf(0x00)
|
||||
}
|
||||
|
||||
test("padding writes fill bytes") {
|
||||
val codec = padding(3, fill = 0xAA.toByte())
|
||||
codec.encodeToBytes(Unit) shouldBe byteArrayOf(0xAA.toByte(), 0xAA.toByte(), 0xAA.toByte())
|
||||
}
|
||||
|
||||
test("Utf16LeNullTerminated roundtrip") {
|
||||
Utf16LeNullTerminated.assertValueRoundtrip("hello")
|
||||
Utf16LeNullTerminated.assertValueRoundtrip("")
|
||||
Utf16LeNullTerminated.assertValueRoundtrip("naïve résumé")
|
||||
Utf16LeNullTerminated.encodeToBytes("ab") shouldBe
|
||||
byteArrayOf(0x61, 0x00, 0x62, 0x00, 0x00, 0x00)
|
||||
}
|
||||
|
||||
test("Utf16BeNullTerminated roundtrip") {
|
||||
Utf16BeNullTerminated.assertValueRoundtrip("hello")
|
||||
Utf16BeNullTerminated.encodeToBytes("ab") shouldBe
|
||||
byteArrayOf(0x00, 0x61, 0x00, 0x62, 0x00, 0x00)
|
||||
}
|
||||
|
||||
test("AsciiNullTerminated roundtrip") {
|
||||
AsciiNullTerminated.assertValueRoundtrip("hello")
|
||||
AsciiNullTerminated.encodeToBytes("ab") shouldBe byteArrayOf(0x61, 0x62, 0x00)
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,50 @@
|
||||
package de.fiereu.bytecodec
|
||||
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
class BuffersTest :
|
||||
FunSpec({
|
||||
test("ByteArrayReadBuffer sequential read") {
|
||||
val buf = ByteArrayReadBuffer(byteArrayOf(1, 2, 3, 4))
|
||||
buf.readByte() shouldBe 1
|
||||
buf.remaining() shouldBe 3
|
||||
val dst = ByteArray(2)
|
||||
buf.readBytes(dst)
|
||||
dst shouldBe byteArrayOf(2, 3)
|
||||
buf.skip(1)
|
||||
buf.remaining() shouldBe 0
|
||||
shouldThrow<IndexOutOfBoundsException> { buf.readByte() }
|
||||
}
|
||||
|
||||
test("ByteArrayReadBuffer respects offset and length") {
|
||||
val buf = ByteArrayReadBuffer(byteArrayOf(9, 1, 2, 3, 9), offset = 1, length = 3)
|
||||
buf.remaining() shouldBe 3
|
||||
val arr = ByteArray(3)
|
||||
buf.readBytes(arr)
|
||||
arr shouldBe byteArrayOf(1, 2, 3)
|
||||
}
|
||||
|
||||
test("GrowableWriteBuffer grows on demand") {
|
||||
val buf = GrowableWriteBuffer(initialCapacity = 2)
|
||||
repeat(10) { buf.writeByte(it.toByte()) }
|
||||
buf.size() shouldBe 10
|
||||
buf.toByteArray() shouldBe byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
}
|
||||
|
||||
test("NioReadBuffer / NioWriteBuffer interoperate") {
|
||||
val nio = ByteBuffer.allocate(8)
|
||||
val write = NioWriteBuffer(nio)
|
||||
write.writeByte(0x11)
|
||||
write.writeBytes(byteArrayOf(0x22, 0x33))
|
||||
nio.flip()
|
||||
val read = NioReadBuffer(nio)
|
||||
read.readByte() shouldBe 0x11
|
||||
val arr = ByteArray(2)
|
||||
read.readBytes(arr)
|
||||
arr shouldBe byteArrayOf(0x22, 0x33)
|
||||
read.remaining() shouldBe 0
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,134 @@
|
||||
package de.fiereu.bytecodec
|
||||
|
||||
import de.fiereu.bytecodec.test.assertBytesRoundtrip
|
||||
import de.fiereu.bytecodec.test.assertValueRoundtrip
|
||||
import de.fiereu.bytecodec.test.decodeBytes
|
||||
import de.fiereu.bytecodec.test.encodeToBytes
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
|
||||
private enum class Color {
|
||||
RED,
|
||||
GREEN,
|
||||
BLUE
|
||||
}
|
||||
|
||||
class CombinatorsTest :
|
||||
FunSpec({
|
||||
test("imap lifts an atom") {
|
||||
val codec = U8.imap(decode = { it + 1 }, encode = { it - 1 })
|
||||
codec.encodeToBytes(10) shouldBe byteArrayOf(0x09)
|
||||
codec.decodeBytes(byteArrayOf(0x09)) shouldBe 10
|
||||
}
|
||||
|
||||
test("imapCatching wraps non-CodecException") {
|
||||
val codec =
|
||||
U8.imapCatching(
|
||||
decode = { error("boom") },
|
||||
encode = { _: Int -> 0 },
|
||||
)
|
||||
shouldThrow<MalformedPacketException> { codec.decodeBytes(byteArrayOf(0x00)) }
|
||||
}
|
||||
|
||||
test("listPrefixed with U8 prefix") {
|
||||
val codec = U16LE.listPrefixed(U8)
|
||||
codec.assertValueRoundtrip(listOf(1, 2, 3))
|
||||
codec.encodeToBytes(emptyList()) shouldBe byteArrayOf(0x00)
|
||||
codec.encodeToBytes(listOf(0x1234, 0x5678)) shouldBe
|
||||
byteArrayOf(0x02, 0x34, 0x12, 0x78, 0x56)
|
||||
}
|
||||
|
||||
test("repeat is fixed-count") {
|
||||
val codec = U8.repeat(3)
|
||||
codec.assertBytesRoundtrip(byteArrayOf(1, 2, 3))
|
||||
shouldThrow<MalformedPacketException> { codec.encodeToBytes(listOf(1, 2)) }
|
||||
}
|
||||
|
||||
test("bytesPrefixed") {
|
||||
val codec = bytesPrefixed(U8)
|
||||
codec.assertBytesRoundtrip(byteArrayOf(0x03, 0xAA.toByte(), 0xBB.toByte(), 0xCC.toByte()))
|
||||
}
|
||||
|
||||
test("stringPrefixed UTF-8") {
|
||||
val codec = stringPrefixed(U8, Charsets.UTF_8)
|
||||
codec.assertValueRoundtrip("hello")
|
||||
val bytes = "héllo".toByteArray(Charsets.UTF_8)
|
||||
codec.encodeToBytes("héllo")[0].toInt() shouldBe bytes.size
|
||||
}
|
||||
|
||||
test("enumByOrdinalByte") {
|
||||
val codec = enumByOrdinalByte<Color>()
|
||||
codec.assertValueRoundtrip(Color.RED)
|
||||
codec.assertValueRoundtrip(Color.BLUE)
|
||||
codec.encodeToBytes(Color.BLUE) shouldBe byteArrayOf(0x02)
|
||||
shouldThrow<MalformedPacketException> { codec.decodeBytes(byteArrayOf(0x05)) }
|
||||
}
|
||||
|
||||
test("enumBy with custom mapping") {
|
||||
val keys = mapOf(Color.RED to 10, Color.GREEN to 20, Color.BLUE to 30)
|
||||
val reverse = keys.entries.associate { (k, v) -> v to k }
|
||||
val codec =
|
||||
enumBy<Color>(
|
||||
prefix = U8,
|
||||
lookup = { reverse[it] ?: throw MalformedPacketException("bad key $it") },
|
||||
keyOf = { keys.getValue(it) },
|
||||
)
|
||||
codec.encodeToBytes(Color.GREEN) shouldBe byteArrayOf(20)
|
||||
codec.decodeBytes(byteArrayOf(30)) shouldBe Color.BLUE
|
||||
}
|
||||
|
||||
test("choose dispatches on tag") {
|
||||
val u8List = U8.listPrefixed(U8)
|
||||
val codec: Codec<Any> =
|
||||
choose(
|
||||
tag = U8,
|
||||
identify = { v ->
|
||||
when (v) {
|
||||
is Int -> 0x01
|
||||
is String -> 0x02
|
||||
else -> error("unhandled $v")
|
||||
}
|
||||
},
|
||||
0x01 to U16LE as Codec<out Any>,
|
||||
0x02 to AsciiNullTerminated as Codec<out Any>,
|
||||
)
|
||||
codec.encodeToBytes(0x1234) shouldBe byteArrayOf(0x01, 0x34, 0x12)
|
||||
codec.encodeToBytes("ok") shouldBe byteArrayOf(0x02, 0x6F, 0x6B, 0x00)
|
||||
shouldThrow<UnknownTagException> { codec.decodeBytes(byteArrayOf(0xFE.toByte())) }
|
||||
u8List.encodeToBytes(emptyList()).size shouldBe 1
|
||||
}
|
||||
|
||||
test("optional with explicit flag") {
|
||||
val codec = U16LE.optional()
|
||||
codec.assertValueRoundtrip(null)
|
||||
codec.assertValueRoundtrip(0x1234)
|
||||
codec.encodeToBytes(null) shouldBe byteArrayOf(0x00)
|
||||
codec.encodeToBytes(0x1234) shouldBe byteArrayOf(0x01, 0x34, 0x12)
|
||||
}
|
||||
|
||||
test("then pairs two codecs") {
|
||||
val codec = U8.then(U16LE)
|
||||
codec.assertValueRoundtrip(5 to 0x1234)
|
||||
codec.encodeToBytes(5 to 0x1234) shouldBe byteArrayOf(0x05, 0x34, 0x12)
|
||||
}
|
||||
|
||||
test("bitsLE roundtrip") {
|
||||
val codec =
|
||||
bitsLE(U8) {
|
||||
bit(0, "a")
|
||||
bit(3, "b")
|
||||
bit(7, "c")
|
||||
}
|
||||
val set = codec.of("a" to true, "b" to false, "c" to true)
|
||||
set["a"] shouldBe true
|
||||
set["b"] shouldBe false
|
||||
set["c"] shouldBe true
|
||||
set[0] shouldBe true
|
||||
val bytes = codec.encodeToBytes(set)
|
||||
bytes shouldBe byteArrayOf(0b10000001.toByte())
|
||||
val decoded = codec.decodeBytes(bytes)
|
||||
decoded["a"] shouldBe true
|
||||
decoded["c"] shouldBe true
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,109 @@
|
||||
package de.fiereu.bytecodec
|
||||
|
||||
import de.fiereu.bytecodec.test.assertBytesRoundtrip
|
||||
import de.fiereu.bytecodec.test.assertValueRoundtrip
|
||||
import de.fiereu.bytecodec.test.encodeToBytes
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
|
||||
private data class GameServer(
|
||||
val id: Int,
|
||||
val name: String,
|
||||
val currentPlayers: Int,
|
||||
val maxPlayers: Int,
|
||||
val joinable: Boolean,
|
||||
)
|
||||
|
||||
private object GameServerCodec : PacketCodec<GameServer>() {
|
||||
override fun CodecScope<GameServer>.body() =
|
||||
GameServer(
|
||||
id = field(U8, GameServer::id),
|
||||
name = field(Utf16LeNullTerminated, GameServer::name),
|
||||
currentPlayers = field(U16LE, GameServer::currentPlayers),
|
||||
maxPlayers = field(U16LE, GameServer::maxPlayers),
|
||||
joinable = field(Bool, GameServer::joinable),
|
||||
)
|
||||
}
|
||||
|
||||
private data class LoadEntityPacket(
|
||||
val entityId: Long,
|
||||
val name: String,
|
||||
val gateA: ByteArray?,
|
||||
val gateB: Int?,
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is LoadEntityPacket &&
|
||||
entityId == other.entityId &&
|
||||
name == other.name &&
|
||||
(gateA?.contentEquals(other.gateA) ?: (other.gateA == null)) &&
|
||||
gateB == other.gateB
|
||||
|
||||
override fun hashCode(): Int = entityId.hashCode()
|
||||
}
|
||||
|
||||
private object LoadEntityCodec : PacketCodec<LoadEntityPacket>() {
|
||||
override fun CodecScope<LoadEntityPacket>.body(): LoadEntityPacket {
|
||||
val entityId = field(S64BE, LoadEntityPacket::entityId)
|
||||
reserved(0)
|
||||
val name = field(Utf16LeNullTerminated, LoadEntityPacket::name)
|
||||
val flags =
|
||||
field(U16LE) {
|
||||
var f = 0
|
||||
if (it.gateA != null) f = f or 0x01
|
||||
if (it.gateB != null) f = f or 0x02
|
||||
f
|
||||
}
|
||||
val gateA = if (flags and 0x01 != 0) field(unknownBytes(2)) { it.gateA!! } else null
|
||||
val gateB = if (flags and 0x02 != 0) field(U32LE) { it.gateB!!.toLong() }.toInt() else null
|
||||
return LoadEntityPacket(entityId, name, gateA, gateB)
|
||||
}
|
||||
}
|
||||
|
||||
class PacketCodecTest :
|
||||
FunSpec({
|
||||
test("simple PacketCodec roundtrip") {
|
||||
val pkt =
|
||||
GameServer(
|
||||
id = 1, name = "alpha", currentPlayers = 10, maxPlayers = 100, joinable = true)
|
||||
GameServerCodec.assertValueRoundtrip(pkt)
|
||||
}
|
||||
|
||||
test("byte-level layout of GameServer") {
|
||||
val bytes = GameServerCodec.encodeToBytes(GameServer(7, "ab", 3, 9, false))
|
||||
bytes shouldBe
|
||||
byteArrayOf(
|
||||
0x07,
|
||||
0x61,
|
||||
0x00,
|
||||
0x62,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x03,
|
||||
0x00,
|
||||
0x09,
|
||||
0x00,
|
||||
0x00,
|
||||
)
|
||||
}
|
||||
|
||||
test("complex packet with reserved, flags, optional sections") {
|
||||
LoadEntityCodec.assertValueRoundtrip(
|
||||
LoadEntityPacket(42L, "world", gateA = null, gateB = null),
|
||||
)
|
||||
LoadEntityCodec.assertValueRoundtrip(
|
||||
LoadEntityPacket(
|
||||
entityId = 0x0102030405060708L,
|
||||
name = "x",
|
||||
gateA = byteArrayOf(0xAA.toByte(), 0xBB.toByte()),
|
||||
gateB = 0x11223344,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
test("structural reserved bytes survive bytes roundtrip") {
|
||||
val pkt = LoadEntityPacket(1L, "y", null, null)
|
||||
val bytes = LoadEntityCodec.encodeToBytes(pkt)
|
||||
LoadEntityCodec.assertBytesRoundtrip(bytes)
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user