Skip to content

Commit

Permalink
fix [adhere to spec]...
Browse files Browse the repository at this point in the history
  • Loading branch information
astinz committed Feb 11, 2024
1 parent c8e01f4 commit 7ec56c2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import xyz.mcxross.bcs.stream.BcsDataInputBuffer
class BcsDecoder(
private val depth: UInt = 0u,
private val inputBuffer: BcsDataInputBuffer,
private var elementsCount: Int = 0
private var elementsCount: Int = 0,
) : AbstractDecoder() {
private var elementIndex = 0

Expand All @@ -35,7 +35,7 @@ class BcsDecoder(

override fun decodeFloat(): Float = throw NotSupported("Not supported: serialize Float")

override fun decodeDouble(): Double = inputBuffer.readDouble()
override fun decodeDouble(): Double = throw NotSupported("Not supported: serialize Double")

override fun decodeChar(): Char = throw NotSupported("Not supported: serialize Char")

Expand All @@ -54,7 +54,7 @@ class BcsDecoder(
@Suppress("UNCHECKED_CAST")
override fun <T> decodeSerializableValue(
deserializer: DeserializationStrategy<T>,
previousValue: T?
previousValue: T?,
): T {
val unitDescriptor = serialDescriptor<Unit>()
return if (deserializer.descriptor == unitDescriptor) decodeUnit() as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BcsEncoder : AbstractEncoder() {

override fun encodeLong(value: Long) = output.writeLong(value)

override fun encodeDouble(value: Double) = output.writeDouble(value)
override fun encodeDouble(value: Double) = throw NotSupported("Not supported: serialize Double")

override fun encodeString(value: String) = output.writeUTF(value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,19 @@ class BcsDataInputBuffer(private val byteArray: ByteArray) : BcsDataBuffer {

fun readShort(): Short {
val bytes = takeNext(2)
return (bytes[0].toInt() shl 8 or (bytes[1].toInt() and 0xFF)).toShort()
return ((bytes[1].toInt() shl 8) or (bytes[0].toInt() and 0xFF)).toShort()
}

fun readInt(): Int {
val bytes = takeNext(4)
return (bytes[0].toInt() shl 24) or
(bytes[1].toInt() shl 16) or
(bytes[2].toInt() shl 8) or
(bytes[3].toInt() and 0xFF)
return bytes.foldIndexed(0) { index, acc, byte ->
acc or ((byte.toInt() and 0xFF) shl (index * 8))
}
}

fun readLong(): Long {
val bytes = takeNext(8)
return (bytes[0].toLong() shl 56) or
(bytes[1].toLong() shl 48) or
(bytes[2].toLong() shl 40) or
(bytes[3].toLong() shl 32) or
(bytes[4].toLong() shl 24) or
(bytes[5].toLong() shl 16) or
(bytes[6].toLong() shl 8) or
(bytes[7].toLong() and 0xFF)
}

fun readDouble(): Double {
val bits: Long = readLong()
// TODO doesn't work for all, fix.
return Double.fromBits(bits)
return bytes.indices.fold(0L) { acc, i -> acc or ((bytes[i].toLong() and 0xFF) shl (i * 8)) }
}

fun readUTF(): String = takeNext(readULEB128()).decodeToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ class BcsDataOutputBuffer : BcsDataBuffer {
}

fun writeShort(shortValue: Short) =
repeat(2) { bytes.add((shortValue.toInt() shr (8 - it * 8)).toByte()) }
repeat(2) { bytes.add((shortValue.toInt() shr (it * 8)).toByte()) }

fun writeInt(intValue: Int) = repeat(4) { bytes.add((intValue shr (24 - it * 8)).toByte()) }
fun writeInt(intValue: Int) = repeat(4) { bytes.add((intValue shr (it * 8)).toByte()) }

fun writeLong(longValue: Long) = repeat(8) { bytes.add((longValue shr (56 - it * 8)).toByte()) }

fun writeDouble(doubleValue: Double) = writeLong(doubleValue.toRawBits())
fun writeLong(longValue: Long) = repeat(8) { bytes.add((longValue shr (it * 8)).toByte()) }

fun writeUTF(text: String) {
val utfBytes = text.encodeToByteArray()
Expand Down

0 comments on commit 7ec56c2

Please sign in to comment.