Skip to content

Commit

Permalink
Implement/fix iOS native DataBuffer properly
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Oct 19, 2021
1 parent aee650f commit 64998a8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ kotlin.code.style=official

group=io.rebble.libpebblecommon
version=0.0.25
org.gradle.jvmargs=-Xms1G
org.gradle.jvmargs=-Xms2G -Xmx2G
57 changes: 32 additions & 25 deletions src/iosMain/kotlin/util/DataBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,55 @@ actual class DataBuffer {
get() = _readPosition

actual constructor(size: Int) {
actualBuf = NSMutableData.dataWithLength(size.toULong())!!
actualBuf.setLength(size.toULong())
actualBuf = NSMutableData.dataWithCapacity(size.toULong())!!
}

actual constructor(bytes: UByteArray) {
actualBuf = NSMutableData()
actualBuf.setData(
NSString.create(string = bytes.toString())
.dataUsingEncoding(NSUTF8StringEncoding, false)!!
)
memScoped {
actualBuf.setData(
NSData.create(bytes = allocArrayOf(bytes.toByteArray()), length = bytes.size.toULong())
)
}
}

private fun shouldReverse(): Boolean {
return if (isPlatformBigEndian() && !littleEndian) {
false
}else if (isPlatformBigEndian() && littleEndian) {
true
}else !isPlatformBigEndian() && !littleEndian
}

actual fun putUShort(short: UShort) {
memScoped {
val pShort = alloc<UShortVar>()
pShort.value = short
pShort.value = if (shouldReverse()) reverseOrd(short) else short
actualBuf.appendBytes(pShort.ptr, UShort.SIZE_BYTES.toULong())
}
}
actual fun getUShort(): UShort {
memScoped {
val pShort = alloc<UShortVar>()
actualBuf.getBytes(pShort.ptr, UShort.SIZE_BYTES.toULong())
actualBuf.getBytes(pShort.ptr, NSMakeRange(_readPosition.toULong(), UShort.SIZE_BYTES.toULong()))
_readPosition += UShort.SIZE_BYTES
return pShort.value
return if (shouldReverse()) reverseOrd(pShort.value) else pShort.value
}
}

actual fun putShort(short: Short) {
memScoped {
val pShort = alloc<ShortVar>()
pShort.value = short
pShort.value = if (shouldReverse()) reverseOrd(short.toUShort()).toShort() else short
actualBuf.appendBytes(pShort.ptr, Short.SIZE_BYTES.toULong())
}
}
actual fun getShort(): Short {
memScoped {
val pShort = alloc<ShortVar>()
actualBuf.getBytes(pShort.ptr, Short.SIZE_BYTES.toULong())
actualBuf.getBytes(pShort.ptr, NSMakeRange(_readPosition.toULong(), Short.SIZE_BYTES.toULong()))
_readPosition += Short.SIZE_BYTES
return pShort.value
return if (shouldReverse()) reverseOrd(pShort.value.toUShort()).toShort() else pShort.value
}
}

Expand All @@ -76,7 +84,7 @@ actual class DataBuffer {
actual fun getUByte(): UByte {
memScoped {
val pByte = alloc<UByteVar>()
actualBuf.appendBytes(pByte.ptr, UByte.SIZE_BYTES.toULong())
actualBuf.getBytes(pByte.ptr, NSMakeRange(_readPosition.toULong(), UByte.SIZE_BYTES.toULong()))
_readPosition += UByte.SIZE_BYTES
return pByte.value
}
Expand All @@ -92,7 +100,7 @@ actual class DataBuffer {
actual fun getByte(): Byte {
memScoped {
val pByte = alloc<ByteVar>()
actualBuf.appendBytes(pByte.ptr, Byte.SIZE_BYTES.toULong())
actualBuf.getBytes(pByte.ptr, NSMakeRange(_readPosition.toULong(), Byte.SIZE_BYTES.toULong()))
_readPosition += Byte.SIZE_BYTES
return pByte.value
}
Expand All @@ -107,7 +115,7 @@ actual class DataBuffer {
actual fun getBytes(count: Int): UByteArray {
memScoped {
val pBytes = allocArray<UByteVar>(count)
actualBuf.getBytes(pBytes.getPointer(this), length = count.toULong())
actualBuf.getBytes(pBytes.getPointer(this), NSMakeRange(_readPosition.toULong(), count.toULong()))
_readPosition += count
return pBytes.readBytes(count).toUByteArray()
}
Expand All @@ -117,54 +125,53 @@ actual class DataBuffer {

actual fun setEndian(endian: Char) {
littleEndian = endian == '<'
if (littleEndian) TODO("iOS little endian")
}

actual fun putUInt(uint: UInt) {
memScoped {
val pUInt = alloc<UIntVar>()
pUInt.value = uint
pUInt.value = if (shouldReverse()) reverseOrd(uint) else uint
actualBuf.appendBytes(pUInt.ptr, UInt.SIZE_BYTES.toULong())
}
}
actual fun getUInt(): UInt {
memScoped {
val pUInt = alloc<UIntVar>()
actualBuf.getBytes(pUInt.ptr, UInt.SIZE_BYTES.toULong())
actualBuf.getBytes(pUInt.ptr, NSMakeRange(_readPosition.toULong(), UInt.SIZE_BYTES.toULong()))
_readPosition += UInt.SIZE_BYTES
return pUInt.value
return if (shouldReverse()) reverseOrd(pUInt.value) else pUInt.value
}
}

actual fun putInt(int: Int) {
memScoped {
val pInt = alloc<IntVar>()
pInt.value = int
pInt.value = if (shouldReverse()) reverseOrd(int.toUInt()).toInt() else int
actualBuf.appendBytes(pInt.ptr, Int.SIZE_BYTES.toULong())
}
}
actual fun getInt(): Int {
memScoped {
val pInt = alloc<IntVar>()
actualBuf.getBytes(pInt.ptr, Int.SIZE_BYTES.toULong())
actualBuf.getBytes(pInt.ptr, NSMakeRange(_readPosition.toULong(), Int.SIZE_BYTES.toULong()))
_readPosition += Int.SIZE_BYTES
return pInt.value
return if (shouldReverse()) reverseOrd(pInt.value.toUInt()).toInt() else pInt.value
}
}

actual fun putULong(ulong: ULong) {
memScoped {
val pULong = alloc<ULongVar>()
pULong.value = ulong
pULong.value = if (shouldReverse()) reverseOrd(ulong) else ulong
actualBuf.appendBytes(pULong.ptr, ULong.SIZE_BYTES.toULong())
}
}
actual fun getULong(): ULong {
memScoped {
val pULong = alloc<ULongVar>()
actualBuf.getBytes(pULong.ptr, ULong.SIZE_BYTES.toULong())
actualBuf.getBytes(pULong.ptr, NSMakeRange(_readPosition.toULong(), ULong.SIZE_BYTES.toULong()))
_readPosition += ULong.SIZE_BYTES
return pULong.value
return if (shouldReverse()) reverseOrd(pULong.value) else pULong.value
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/iosMain/kotlin/util/UtilFunctions.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
package io.rebble.libpebblecommon.util

actual fun runBlocking(block: suspend () -> Unit) = kotlinx.coroutines.runBlocking{block()}
import kotlinx.cinterop.*

actual fun runBlocking(block: suspend () -> Unit) = kotlinx.coroutines.runBlocking{block()}

internal fun isPlatformBigEndian(): Boolean {
memScoped {
val i = alloc<IntVar>()
i.value = 1
val bytes = i.reinterpret<ByteVar>()
return bytes.value == 0.toByte()
}
}

internal fun reverseOrd(varr: UShort): UShort = (((varr.toInt() and 0xff) shl 8) or ((varr.toInt() and 0xffff) ushr 8)).toUShort()

internal fun reverseOrd(varr: UInt): UInt = ((reverseOrd((varr and 0xffffu).toUShort()).toInt() shl 16) or (reverseOrd((varr shr 16).toUShort()).toInt() and 0xffff)).toUInt()

internal fun reverseOrd(varr: ULong): ULong = ((reverseOrd((varr and 0xffffffffu).toUInt()).toLong() shl 32) or (reverseOrd((varr shr 32).toUInt()).toLong() and 0xffffffff)).toULong()

0 comments on commit 64998a8

Please sign in to comment.