diff options
author | Edoardo La Greca | 2025-08-07 14:46:37 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-08-07 14:46:37 +0200 |
commit | aa40ce28f6001947208d308c9fcb96940788a243 (patch) | |
tree | 19e30756ac842a158bf04eebe54c53ee173c1963 /src | |
parent | 473a9054fd4527fec070e9146fa21a04628e1ff7 (diff) |
move convInteger and convString to InMessage companion object
Diffstat (limited to 'src')
-rw-r--r-- | src/main/kotlin/InMessage.kt | 68 |
1 files changed, 35 insertions, 33 deletions
diff --git a/src/main/kotlin/InMessage.kt b/src/main/kotlin/InMessage.kt index c111d62..03f3428 100644 --- a/src/main/kotlin/InMessage.kt +++ b/src/main/kotlin/InMessage.kt @@ -82,39 +82,6 @@ class InMessage(val tl: TransportLayer, maxSize: UInt, val reqTag: UShort) { } /** - * Convert an [len] bytes long unsigned integer number from raw bytes. - * - * In 9P, binary numbers (non-textual) are specified in little-endian order (least significant byte first). - * - * @param len The length of the integer number in bytes. If zero, nothing is read. - * @return the number's value. - * @throws IllegalArgumentException if either [offset] or [len] are negative. - */ - fun convInteger(bytes: Iterable<UByte>, offset: Int, len: Int): BigInteger { - val bytes = bytes.drop(offset).take(len) - var value = 0.toBigInteger() - for (i in 0..<bytes.size) { - value += bytes[i].toInt().toBigInteger().shl(i*8) - } - return value - } - - /** - * Convert a string from raw bytes. - * - * 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. - * - * @return the string. - * @throws IllegalArgumentException if either [offset] is negative. - */ - fun convString(bytes: Iterable<UByte>, offset: Int): String { - val length = convInteger(bytes, 0, 2).toInt() - val bytes = bytes.drop(offset).take(length) - return String(ByteArray(bytes.size) { i -> bytes[i].toByte() }) - } - - /** * Apply the given field to the raw data and put it in one of [fieldsInt], [fieldsStr], or [fieldsRaw]. Fields must * be applied strictly in order, as their application is not commutative. * @@ -158,4 +125,39 @@ class InMessage(val tl: TransportLayer, maxSize: UInt, val reqTag: UShort) { applyField(field) } } + + companion object { + /** + * Convert an [len] bytes long unsigned integer number from raw bytes. + * + * In 9P, binary numbers (non-textual) are specified in little-endian order (least significant byte first). + * + * @param len The length of the integer number in bytes. If zero, nothing is read. + * @return the number's value. + * @throws IllegalArgumentException if either [offset] or [len] are negative. + */ + fun convInteger(bytes: Iterable<UByte>, offset: Int, len: Int): BigInteger { + val bytes = bytes.drop(offset).take(len) + var value = 0.toBigInteger() + for (i in 0..<bytes.size) { + value += bytes[i].toInt().toBigInteger().shl(i*8) + } + return value + } + + /** + * Convert a string from raw bytes. + * + * 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. + * + * @return the string. + * @throws IllegalArgumentException if either [offset] is negative. + */ + fun convString(bytes: Iterable<UByte>, offset: Int): String { + val length = convInteger(bytes, 0, 2).toInt() + val bytes = bytes.drop(offset).take(length) + return String(ByteArray(bytes.size) { i -> bytes[i].toByte() }) + } + } }
\ No newline at end of file |