Improve zlib test and ImmutableBinaryBlob API

This commit is contained in:
Nikolay Igotti
2018-08-16 18:34:31 +03:00
parent 3ad52b8736
commit 31f27fb246
2 changed files with 16 additions and 14 deletions
+14 -13
View File
@@ -1,21 +1,22 @@
import kotlinx.cinterop.* import kotlinx.cinterop.*
import platform.zlib.* import platform.zlib.*
val source = immutableBinaryBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer(0)!! val source = immutableBinaryBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer()
val golden = immutableBinaryBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer(0)!! val golden = immutableBinaryBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer()
fun main(args: Array<String>) = memScoped { fun main(args: Array<String>) = memScoped {
val buffer = allocArray<ByteVar>(32) val buffer = ByteArray(32)
buffer.usePinned { pinned ->
val z = alloc<z_stream>().apply { val z = alloc<z_stream>().apply {
next_in = source next_in = source
avail_in = 8 avail_in = 8
next_out = buffer next_out = pinned.addressOf(0)
avail_out = 32 avail_out = buffer.size
}.ptr }.ptr
if (inflateInit2(z, -15) == Z_OK && inflate(z, Z_FINISH) == Z_STREAM_END && inflateEnd(z) == Z_OK)
println(buffer.toKString())
if (inflateInit2(z, -15) == Z_OK && inflate(z, Z_FINISH) == Z_STREAM_END && inflateEnd(z) == Z_OK)
println(buffer.stringFromUtf8())
}
println(golden.toKString()) println(golden.toKString())
} }
@@ -61,7 +61,8 @@ public fun ImmutableBinaryBlob.toByteArray() = toByteArray(0, size)
// Returns stable C pointer to data at certain offset, useful as a way to pass resource // Returns stable C pointer to data at certain offset, useful as a way to pass resource
// to C API. // to C API.
public fun ImmutableBinaryBlob.asCPointer(offset: Int) = interpretCPointer<ByteVar>(asCPointerImpl(offset)) public fun ImmutableBinaryBlob.asCPointer(offset: Int = 0) =
interpretCPointer<ByteVar>(asCPointerImpl(offset))!!
@SymbolName("Kotlin_ImmutableBinaryBlob_asCPointerImpl") @SymbolName("Kotlin_ImmutableBinaryBlob_asCPointerImpl")
private external fun ImmutableBinaryBlob.asCPointerImpl(offset: Int): kotlin.native.internal.NativePtr private external fun ImmutableBinaryBlob.asCPointerImpl(offset: Int): kotlin.native.internal.NativePtr