diff options
author | Edoardo La Greca | 2025-08-09 20:02:27 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-08-09 20:02:27 +0200 |
commit | fe32742b5b049768e524999af452dd6b902db6c1 (patch) | |
tree | e23de147a7e859058b7508c451a03780b4137b57 | |
parent | a07d4fe60ab0eac2a20723508db8159c6955b855 (diff) |
fix integer sizes and add missing documentation in OutMessage
-rw-r--r-- | src/main/kotlin/OutMessage.kt | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/src/main/kotlin/OutMessage.kt b/src/main/kotlin/OutMessage.kt index 9a3bcfb..6540967 100644 --- a/src/main/kotlin/OutMessage.kt +++ b/src/main/kotlin/OutMessage.kt @@ -11,10 +11,12 @@ import kotlin.math.pow * @param type The 9P message type. * @param tag The tag given to the message. * @param fieldNames The names of the message fields, in the same order they are expected to be sent. - * @param fieldValuesInt A map of each field name into its value. This map only stores integer values. - * @param fieldValuesStr A map of each field name into its value. This map only stores string values. + * @param fieldValuesInt A map of each integer field's name into its value and size in bytes. + * @param fieldValuesStr A map of each string field's name into its value. + * @param fieldValuesRaw A map of each raw field's name into its value. + * @param maxSize The maximum message size. */ -class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: List<String>, val fieldValuesInt: Map<String, BigInteger>, val fieldValuesStr: Map<String, String>, val fieldValuesRaw: Map<String, Array<UByte>>, val maxSize: UInt) { +class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: List<String>, val fieldValuesInt: Map<String, Pair<BigInteger, UInt>>, val fieldValuesStr: Map<String, String>, val fieldValuesRaw: Map<String, Array<UByte>>, val maxSize: UInt) { /** * Intersection between [fieldNames] and [fieldValuesInt]. In other words: the integer fields that are going to be * used when writing the message. @@ -49,10 +51,11 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li throw IllegalArgumentException("Message size exceeded.") } writeMessageSizeType(tl, totalSize, type) - writeInteger(tl, tag.toInt().toBigInteger()) + writeInteger(tl, tag.toInt().toBigInteger(), 2u) for (field in fieldNames) { if (field in insecInts) { - writeInteger(tl, fieldValuesInt[field]!!) + val valsize = fieldValuesInt[field]!! + writeInteger(tl, valsize.first, valsize.second) } else if (field in insecStrs) { writeString(tl, fieldValuesStr[field]!!) } else { @@ -69,15 +72,15 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li * @param type The 9P message type as a [NinePMessageType] constant. */ private fun writeMessageSizeType(tl: TransportLayer, size: UInt, type: NinePMessageType) { - writeInteger(tl, size.toInt().toBigInteger()) - writeInteger(tl, type.value.toInt().toBigInteger()) + writeInteger(tl, size.toInt().toBigInteger(), 4u) + writeInteger(tl, type.value.toInt().toBigInteger(), 1u) } /** * Calculate the expected size of the message. */ fun size(): UInt { - return 4u + 1u + 2u + this.insecInts.sumOf { this.fieldValuesInt[it]!!.bitLength().toUInt() } + this.insecStrs.sumOf { 2u + this.fieldValuesStr[it]!!.length.toUInt() } + this.insecRaws.sumOf { this.fieldValuesRaw[it]!!.size.toUInt() } + return 4u + 1u + 2u + this.insecInts.sumOf { this.fieldValuesInt[it]!!.second } + this.insecStrs.sumOf { 2u + this.fieldValuesStr[it]!!.length.toUInt() } + this.insecRaws.sumOf { this.fieldValuesRaw[it]!!.size.toUInt() } } companion object { @@ -90,8 +93,9 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li * @param tl The networking API. * @param value The number's value. */ - fun writeInteger(tl: TransportLayer, value: BigInteger) { - val bytes = value.toByteArray() + fun writeInteger(tl: TransportLayer, value: BigInteger, size: UInt) { + var bytes = value.toByteArray() + bytes = ByteArray(size.toInt() - bytes.size, {0}) // add padding for missing bytes tl.transmit(Array(bytes.size) { i -> bytes[i].toUByte() }) } @@ -107,7 +111,7 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li */ fun writeString(tl: TransportLayer, value: String) { require(value.length <= 2.0.pow(16.0) - 1) - writeInteger(tl, value.length.toBigInteger()) + writeInteger(tl, value.length.toBigInteger(), 2u) val bytes = value.toByteArray() tl.transmit(Array(bytes.size) { i -> bytes[i].toUByte() }) } |