diff --git a/build.gradle.kts b/build.gradle.kts index 2e0eac6..b60e66a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { diff --git a/src/commonMain/kotlin/net.sergeych.bintools/CRC.kt b/src/commonMain/kotlin/net.sergeych.bintools/CRC.kt index 5b5a16e..85f4547 100644 --- a/src/commonMain/kotlin/net.sergeych.bintools/CRC.kt +++ b/src/commonMain/kotlin/net.sergeych.bintools/CRC.kt @@ -27,6 +27,9 @@ interface CRC { 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 diff --git a/src/commonMain/kotlin/net.sergeych.bintools/DataSource.kt b/src/commonMain/kotlin/net.sergeych.bintools/DataSource.kt index f5072fd..a786635 100644 --- a/src/commonMain/kotlin/net.sergeych.bintools/DataSource.kt +++ b/src/commonMain/kotlin/net.sergeych.bintools/DataSource.kt @@ -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 DataSource.readNumber(): T = Smartint.decode(this) as T diff --git a/src/commonMain/kotlin/net.sergeych.bintools/MotherPack.kt b/src/commonMain/kotlin/net.sergeych.bintools/MotherPack.kt index 4b463a6..16f4a0e 100644 --- a/src/commonMain/kotlin/net.sergeych.bintools/MotherPack.kt +++ b/src/commonMain/kotlin/net.sergeych.bintools/MotherPack.kt @@ -24,6 +24,7 @@ class JsonPacker : MotherPacker { return Json.encodeToString(serializer(type), payload).encodeToByteArray() } + @Suppress("UNCHECKED_CAST") override fun unpack(type: KType, packed: ByteArray): T { return Json.decodeFromString( serializer(type) as KSerializer, diff --git a/src/commonMain/kotlin/net.sergeych.bintools/simple_codecs.kt b/src/commonMain/kotlin/net.sergeych.bintools/simple_codecs.kt index e7c076a..02b78fc 100644 --- a/src/commonMain/kotlin/net.sergeych.bintools/simple_codecs.kt +++ b/src/commonMain/kotlin/net.sergeych.bintools/simple_codecs.kt @@ -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.encodeToHex(separator: String = " "): String = joinToString(separator) { it.toUByte().encodeToHex(2) } diff --git a/src/commonMain/kotlin/net.sergeych.bipack/MotherBipack.kt b/src/commonMain/kotlin/net.sergeych.bipack/MotherBipack.kt index 3714699..0d4d863 100644 --- a/src/commonMain/kotlin/net.sergeych.bipack/MotherBipack.kt +++ b/src/commonMain/kotlin/net.sergeych.bipack/MotherBipack.kt @@ -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 pack(type: KType, payload: T): ByteArray { return BipackEncoder.encode(serializer(type), payload) } + @Suppress("UNCHECKED_CAST") override fun unpack(type: KType, packed: ByteArray): T { return BipackDecoder.decode(packed.toDataSource(), serializer(type) as KSerializer) diff --git a/src/commonTest/kotlin/bintools/CrcTest.kt b/src/commonTest/kotlin/bintools/CrcTest.kt new file mode 100644 index 0000000..a626ad6 --- /dev/null +++ b/src/commonTest/kotlin/bintools/CrcTest.kt @@ -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}") + } +} \ No newline at end of file