more support for ubytearrays

This commit is contained in:
sergeych committed 2023-10-26 10:35:41 +03:00
1 parent b5db8bb8cc
commit 30bf5fefe9
7 files changed
+42 -3

No files matched your search

@@ -27,6 +27,9 @@ interface CRC<T> {
fun crc8(data: ByteArray, polynomial: UByte = 0xA7.toUByte()): UByte =
CRC8(polynomial).also { it.update(data) }.value
fun crc8(data: UByteArray, polynomial: UByte = 0xA7.toUByte()): UByte =
CRC8(polynomial).also { it.update(data) }.value
/**
* Calculate CRC16 for a data array using a given polynomial (CRC16-CCITT polynomial (0x1021) by default)
*/
@@ -45,6 +48,7 @@ infix fun UShort.shl(bitCount: Int): UShort = (this.toUInt() shl bitCount).toUSh
infix fun UShort.shr(bitCount: Int): UShort = (this.toUInt() shr bitCount).toUShort()
infix fun UByte.shl(bitCount: Int): UByte = (this.toUInt() shl bitCount).toUByte()
@Suppress("unused")
infix fun UByte.shr(bitCount: Int): UByte = (this.toUInt() shr bitCount).toUByte()
fun UByte.toBigEndianUShort(): UShort = this.toUShort() shl 8
@@ -6,6 +6,7 @@ package net.sergeych.bintools
* like multiplatform version of DataInput
*
*/
@Suppress("unused")
interface DataSource {
/**
@@ -33,7 +34,7 @@ interface DataSource {
fun readDouble() = Double.fromBits(readI64())
fun readFloat() = Float.fromBits(readI32()).toFloat()
fun readFloat() = Float.fromBits(readI32())
fun readSmartUInt(): UInt = Smartint.decodeUnsigned(this).toUInt()
fun readSmartInt(): Int = Smartint.decodeSigned(this).toInt()
@@ -44,7 +45,6 @@ interface DataSource {
}
fun ByteArray.toDataSource(): DataSource =
object : DataSource {
var position = 0
@@ -59,5 +59,19 @@ fun ByteArray.toDataSource(): DataSource =
}
}
fun UByteArray.toDataSource(): DataSource =
object : DataSource {
var position = 0
private set
override fun readByte(): Byte =
if (position < size) this@toDataSource[position++].toByte()
else throw DataSource.EndOfData()
override fun toString(): String {
return "ASrc[$position]: ${encodeToHex()}"
}
}
inline fun <reified T : Any> DataSource.readNumber(): T = Smartint.decode(this) as T
@@ -24,6 +24,7 @@ class JsonPacker : MotherPacker {
return Json.encodeToString(serializer(type), payload).encodeToByteArray()
}
@Suppress("UNCHECKED_CAST")
override fun <T> unpack(type: KType, packed: ByteArray): T {
return Json.decodeFromString<T>(
serializer(type) as KSerializer<T>,
@@ -106,6 +106,7 @@ fun UByte.encodeToHex(length: Int = 0) = toLong().encodeToHex(length)
fun ULong.encodeToHex(length: Int = 0) = toLong().encodeToHex(length)
fun ByteArray.encodeToHex(separator: String = " "): String = joinToString(separator) { it.toUByte().encodeToHex(2) }
fun UByteArray.encodeToHex(separator: String = " "): String = joinToString(separator) { it.encodeToHex(2) }
@Suppress("unused")
fun Collection<Byte>.encodeToHex(separator: String = " "): String = joinToString(separator) { it.toUByte().encodeToHex(2) }
@@ -6,11 +6,13 @@ import net.sergeych.bintools.MotherPacker
import net.sergeych.bintools.toDataSource
import kotlin.reflect.KType
@Suppress("unused")
class MotherBipack : MotherPacker {
override fun <T> pack(type: KType, payload: T): ByteArray {
return BipackEncoder.encode(serializer(type), payload)
}
@Suppress("UNCHECKED_CAST")
override fun <T> unpack(type: KType, packed: ByteArray): T {
return BipackDecoder.decode<T>(packed.toDataSource(),
serializer(type) as KSerializer<T>)