more support for ubytearrays

This commit is contained in:
Sergey Chernov 2023-10-26 10:35:16 +03:00
parent b5db8bb8cc
commit 30bf5fefe9
7 changed files with 42 additions and 3 deletions

View File

@ -8,7 +8,7 @@ plugins {
val serialization_version = "1.3.4"
group = "net.sergeych"
version = "0.0.4"
version = "0.0.6-SNAPSHOT"
repositories {
mavenCentral()
@ -61,6 +61,7 @@ kotlin {
sourceSets {
all {
languageSettings.optIn("kotlinx.serialization.ExperimentalSerializationApi")
languageSettings.optIn("kotlin.ExperimentalUnsignedTypes")
}
val commonMain by getting {
dependencies {

View File

@ -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

View File

@ -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

View File

@ -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>,

View File

@ -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) }

View File

@ -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>)

View File

@ -0,0 +1,16 @@
package bintools
import net.sergeych.bintools.CRC
import net.sergeych.bintools.encodeToHex
import kotlin.test.Test
class CrcTest {
@Test
fun testVectors() {
val x = byteArrayOf(1,2,3,4,5)
val crc = CRC.crc8(x)
println("->> ${x.toList()}")
println("->> ${crc.encodeToHex()}")
println("->> ${crc}")
}
}