encoder and decoder now support extendable formats.

This commit is contained in:
sergeych committed 2023-03-12 22:48:21 +01:00
1 parent 6e65a216c5
commit 63b231975b
13 files changed
+288 -23

No files matched your search

@@ -6,16 +6,41 @@ interface DataSink {
fun writeByte(data: Int) = writeByte(data.toByte())
open fun writeUByte(data: UByte) {
fun writeUByte(data: UByte) {
writeByte(data.toByte())
}
fun writeDouble(value: Double) {
writeI64(value.toRawBits())
}
fun writeFloat(value: Float) {
writeI32(value.toRawBits())
}
@Suppress("unused")
open fun writeBytes(data: ByteArray) {
fun writeBytes(data: ByteArray) {
for(d in data) writeByte(d)
}
}
inline fun <reified T:Any>DataSink.writeNumber(value: T) {
when(value) {
is Float -> writeFloat(value)
is Double -> writeDouble(value)
else -> Smartint.encode(value, this)
}
}
fun DataSink.writeI32(value: Int) {
writeBytes(intToBytes(value))
}
fun DataSink.writeI64(value: Long) {
writeBytes(longToBytes(value))
}
class ArrayDataSink : DataSink {
private val result = mutableListOf<Byte>()
@@ -1,5 +1,7 @@
package net.sergeych.bintools
import kotlin.reflect.typeOf
/**
* data input stream-like abstraction. We need it because
* kotlinx serialization is synchronous and there us nothing
@@ -8,23 +10,40 @@ package net.sergeych.bintools
*/
interface DataSource {
class EndOfData() : Exception("no more data available")
fun readByte(): Byte
val position: Int
fun readUByte() = readByte().toUByte()
@Suppress("unused")
fun readBytes(size: Int): ByteArray =
ByteArray(size).also { a ->
for( i in 0..size)
for (i in 0 until size)
a[i] = readByte()
}
fun readI32(): Int = bytesToInt(readBytes(4))
fun readI64(): Long = bytesToLong(readBytes(8))
fun readDouble() = Double.fromBits(readI64())
fun readFloat() = Float.fromBits(readI32())
}
fun ByteArray.toDataSource(): DataSource =
object : DataSource {
override var position = 0
var position = 0
private set
override fun readByte(): Byte = this@toDataSource[position++]
}
override fun readByte(): Byte =
if( position < size ) this@toDataSource[position++]
else throw DataSource.EndOfData()
}
inline fun <reified T : Any> DataSource.readNumber(): T = when(typeOf<T>()) {
typeOf<Double>() -> readDouble() as T
typeOf<Float>() -> readFloat() as T
else -> Smartint.decode(this)
}
@@ -1,6 +1,5 @@
package net.sergeych.bintools
import com.icodici.ubdata.Varint
import kotlin.reflect.typeOf
/**
@@ -17,7 +16,7 @@ interface IntCodec {
/**
* Default signed codec uses bit 0 as a sign (to keep packed as small as possible)
*/
fun encodeSigned(value: Long, sink: DataSink): Unit {
fun encodeSigned(value: Long, sink: DataSink) {
var sigBit: ULong
var x: ULong
if (value < 0) {
@@ -62,9 +61,11 @@ inline fun <reified T : Any> IntCodec.decode(source: DataSource): T {
return when (typeOf<T>()) {
typeOf<UByte>() -> decodeUnsigned(source).toUByte()
typeOf<UInt>() -> decodeUnsigned(source).toUInt()
typeOf<UShort>() -> decodeUnsigned(source).toUShort()
typeOf<ULong>() -> decodeUnsigned(source).toULong()
typeOf<Byte>() -> decodeSigned(source).toByte()
typeOf<Int>() -> decodeSigned(source).toInt()
typeOf<Short>() -> decodeSigned(source).toShort()
typeOf<Long>() -> decodeSigned(source).toLong()
else ->
throw IllegalArgumentException("can't decode to ${T::class.simpleName}")
@@ -75,9 +76,11 @@ inline fun <reified T : Any> IntCodec.encode(x: T, dout: DataSink) {
when (x) {
is UByte -> encodeUnsigned(x.toULong(), dout)
is UInt -> encodeUnsigned(x.toULong(), dout)
is UShort -> encodeUnsigned(x.toULong(), dout)
is ULong -> encodeUnsigned(x, dout)
is Byte -> encodeSigned(x.toLong(), dout)
is Int -> encodeSigned(x.toLong(), dout)
is Short -> encodeSigned(x.toLong(), dout)
is Long -> encodeSigned(x, dout)
else -> throw IllegalArgumentException("can't encode with varitn ${x::class.simpleName}: $x")
}
@@ -11,6 +11,16 @@ fun longToBytes(value: Long): ByteArray {
return result
}
fun intToBytes(value: Int): ByteArray {
var l = value
val result = ByteArray(8)
for (i in 3 downTo 0) {
result[i] = (l and 0xFF).toByte()
l = l shr 8
}
return result
}
/**
* Convert 8 bytes to LE long
*/
@@ -23,6 +33,15 @@ fun bytesToLong(b: ByteArray): Long {
return result
}
fun bytesToInt(b: ByteArray): Int {
var result: Int = 0
for (i in 0 until 4) {
result = result shl 8
result = result or (b[i].toInt() and 0xFF)
}
return result
}
private val hexDigits = "0123456789ABCDEF"
@@ -1,6 +1,6 @@
@file:OptIn(ExperimentalUnsignedTypes::class)
package com.icodici.ubdata
package net.sergeych.bintools
import net.sergeych.bintools.*
@@ -1,8 +1,5 @@
@file:OptIn(ExperimentalUnsignedTypes::class)
package com.icodici.ubdata
import net.sergeych.bintools.*
package net.sergeych.bintools
/**
* Variable-length long integer encoding. the MSB (0x80) bit of each byte flags
@@ -14,15 +11,15 @@ import net.sergeych.bintools.*
* same or even worse, see [Smartint] docs.
*/
object Varint: IntCodec {
override fun encodeUnsigned(value: ULong, dout: DataSink) {
override fun encodeUnsigned(value: ULong, sink: DataSink) {
var rest = value
do {
val x = (rest and 127u).toInt()
rest = rest shr 7
if (rest > 0u)
dout.writeByte(x or 0x80)
sink.writeByte(x or 0x80)
else
dout.writeByte(x)
sink.writeByte(x)
} while (rest > 0u)
}