diff --git a/bytecodec/src/main/kotlin/de/fiereu/bytecodec/Atoms.kt b/bytecodec/src/main/kotlin/de/fiereu/bytecodec/Atoms.kt index f71810e..fe8723d 100644 --- a/bytecodec/src/main/kotlin/de/fiereu/bytecodec/Atoms.kt +++ b/bytecodec/src/main/kotlin/de/fiereu/bytecodec/Atoms.kt @@ -268,6 +268,34 @@ fun reserved(byte: Int, strict: Boolean = false): Codec { } } +/** A fixed protocol byte: writes [byte] verbatim and skips one byte when reading. */ +fun constant(byte: Int): Codec { + val literal = (byte and 0xFF).toByte() + return object : Codec { + override fun read(buf: ReadBuffer) { + buf.skip(1) + } + + override fun write(buf: WriteBuffer, value: Unit) { + buf.writeByte(literal) + } + } +} + +/** A fixed protocol segment: writes [bytes] verbatim and skips over them when reading. */ +fun constant(bytes: ByteArray): Codec { + val literal = bytes.copyOf() + return object : Codec { + override fun read(buf: ReadBuffer) { + buf.skip(literal.size) + } + + override fun write(buf: WriteBuffer, value: Unit) { + buf.writeBytes(literal) + } + } +} + fun padding(n: Int, fill: Byte = 0): Codec { require(n >= 0) { "padding negative size: $n" } return object : Codec { diff --git a/bytecodec/src/main/kotlin/de/fiereu/bytecodec/PacketCodec.kt b/bytecodec/src/main/kotlin/de/fiereu/bytecodec/PacketCodec.kt index 2bec106..953251d 100644 --- a/bytecodec/src/main/kotlin/de/fiereu/bytecodec/PacketCodec.kt +++ b/bytecodec/src/main/kotlin/de/fiereu/bytecodec/PacketCodec.kt @@ -43,7 +43,7 @@ abstract class PacketCodec

: Codec

{ } } -fun CodecScope<*>.reserved(byte: Int = 0, strict: Boolean = false) { +fun CodecScope<*>.reserved(byte: Int, strict: Boolean = false) { structural(de.fiereu.bytecodec.reserved(byte = byte, strict = strict)) } @@ -54,3 +54,11 @@ fun CodecScope<*>.padding(n: Int, fill: Byte = 0) { fun CodecScope<*>.skip(n: Int) { structural(de.fiereu.bytecodec.skip(n = n)) } + +fun CodecScope<*>.constant(byte: Int) { + structural(de.fiereu.bytecodec.constant(byte = byte)) +} + +fun CodecScope<*>.constant(bytes: ByteArray) { + structural(de.fiereu.bytecodec.constant(bytes = bytes)) +} diff --git a/bytecodec/src/test/kotlin/de/fiereu/bytecodec/AtomsTest.kt b/bytecodec/src/test/kotlin/de/fiereu/bytecodec/AtomsTest.kt index 40f2717..912a627 100644 --- a/bytecodec/src/test/kotlin/de/fiereu/bytecodec/AtomsTest.kt +++ b/bytecodec/src/test/kotlin/de/fiereu/bytecodec/AtomsTest.kt @@ -114,6 +114,18 @@ class AtomsTest : codec.encodeToBytes(Unit) shouldBe byteArrayOf(0x00) } + test("constant byte writes the literal and skips it on read") { + val codec = constant(0x2A) + codec.encodeToBytes(Unit) shouldBe byteArrayOf(0x2A) + codec.decodeBytes(byteArrayOf(0x0A)) shouldBe Unit + } + + test("constant segment writes the literal and skips it on read") { + val codec = constant(byteArrayOf(0x01, 0x02, 0x03)) + codec.encodeToBytes(Unit) shouldBe byteArrayOf(0x01, 0x02, 0x03) + codec.decodeBytes(byteArrayOf(0x0A, 0x0B, 0x0C)) shouldBe Unit + } + test("padding writes fill bytes") { val codec = padding(3, fill = 0xAA.toByte()) codec.encodeToBytes(Unit) shouldBe byteArrayOf(0xAA.toByte(), 0xAA.toByte(), 0xAA.toByte())