summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdoardo La Greca2025-08-11 19:19:56 +0200
committerEdoardo La Greca2025-08-11 19:19:56 +0200
commitfcaeb6e9e83a04c0a2a16353b6d2ebc6bd291a40 (patch)
treef4a150779c79b1a13a7e211bc61b40470d38d5ce
parent76d58a416e0a244d858dedf7bb8376e6d00f57ce (diff)
turn writeInteger and writeString into convIntegerToBytes and convStringToBytes
-rw-r--r--src/main/kotlin/OutMessage.kt48
1 files changed, 26 insertions, 22 deletions
diff --git a/src/main/kotlin/OutMessage.kt b/src/main/kotlin/OutMessage.kt
index 230763d..3933440 100644
--- a/src/main/kotlin/OutMessage.kt
+++ b/src/main/kotlin/OutMessage.kt
@@ -52,14 +52,16 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li
}
writeMessageSizeTypeTag(tl, totalSize, type, tag)
for (field in fieldNames) {
- if (field in insecInts) {
- val valsize = fieldValuesInt[field]!!
- writeInteger(tl, valsize.first, valsize.second)
- } else if (field in insecStrs) {
- writeString(tl, fieldValuesStr[field]!!)
- } else {
- tl.transmit(fieldValuesRaw[field]!!)
- }
+ tl.transmit(
+ if (field in insecInts) {
+ val valsize = fieldValuesInt[field]!!
+ convIntegerToBytes(valsize.first, valsize.second)
+ } else if (field in insecStrs) {
+ convStringToBytes(fieldValuesStr[field]!!)
+ } else {
+ fieldValuesRaw[field]!!.toList()
+ }
+ )
}
}
@@ -72,9 +74,11 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li
* @param tag The 9P message tag.
*/
private fun writeMessageSizeTypeTag(tl: TransportLayer, size: UInt, type: NinePMessageType, tag: UShort) {
- writeInteger(tl, BigInteger(size.toString()), 4u)
- writeInteger(tl, BigInteger(type.value.toString()), 1u)
- writeInteger(tl, BigInteger(tag.toString()), 2u)
+ var bytes: List<UByte> = emptyList()
+ bytes += convIntegerToBytes(BigInteger(size.toString()), 4u)
+ bytes += convIntegerToBytes(BigInteger(type.value.toString()), 1u)
+ bytes += convIntegerToBytes(BigInteger(tag.toString()), 2u)
+ tl.transmit(bytes)
}
/**
@@ -86,18 +90,19 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li
companion object {
// TODO: Add size that the value is required to fit in
+
/**
- * Write an integer number to the connection.
+ * Convert an integer number to its byte representation.
*
* In 9P, binary numbers (non-textual) are specified in little-endian order (least significant byte first).
*
- * @param tl The networking API.
* @param value The number's value.
+ * @param size The number's size in bytes.
*/
- 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() })
+ fun convIntegerToBytes(value: BigInteger, size: UInt): List<UByte> {
+ var bytes: List<UByte> = value.toByteArray().toList().map { x -> x.toUByte() }
+ bytes += List(size.toInt() - bytes.size, {0u}) // add padding for missing bytes
+ return bytes
}
/**
@@ -106,15 +111,14 @@ class OutMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: Li
* In 9P, strings are represented as a 2-byte integer (the string's size) followed by the actual UTF-8 string. The
* null terminator is forbidden in 9P messages.
*
- * @param tl The networking API.
* @param value The string.
* @throws IllegalArgumentException if the value of the string's size does not fit into 2 bytes.
*/
- fun writeString(tl: TransportLayer, value: String) {
+ fun convStringToBytes(value: String): List<UByte> {
require(value.length <= 2.0.pow(16.0) - 1)
- writeInteger(tl, value.length.toBigInteger(), 2u)
- val bytes = value.toByteArray()
- tl.transmit(Array(bytes.size) { i -> bytes[i].toUByte() })
+ var bytes = convIntegerToBytes(value.length.toBigInteger(), 2u)
+ bytes += value.toByteArray().toList().map { x -> x.toUByte() }
+ return bytes
}
}
} \ No newline at end of file