coletz
12/13/2018, 6:38 PMJurriaan Mous
12/13/2018, 6:59 PM/** Encode [bytes] array into a base64 String */
actual fun encode(bytes: ByteArray) = memScoped {
@Suppress("EXPERIMENTAL_API_USAGE")
NSData.create(
bytesNoCopy = bytes.toCValues().getPointer(this),
length = bytes.size.toULong()
).base64EncodedStringWithOptions(0)
}
Jurriaan Mous
12/13/2018, 7:00 PM/** Decode [base64] string into bytes array */
actual fun decode(base64: String): ByteArray {
// For this library end padding is mandatory so add possible missing padding
val value = base64.padEnd(
length = ((base64.length+3)/4)*4,
padChar = '='
)
val data = NSData.create(value, 0) ?: throw ParseException("Invalid Base64 value $base64")
@Suppress("UNCHECKED_CAST")
val bytePtr = (data.bytes as CPointer<uint8_tVar>)
@Suppress("EXPERIMENTAL_API_USAGE")
return ByteArray(data.length.toInt()) { index ->
bytePtr[index].toByte()
}
}
coletz
12/13/2018, 7:02 PMJurriaan Mous
12/13/2018, 7:03 PMcoletz
12/13/2018, 7:03 PMDico
12/13/2018, 10:54 PMmemScope
you can just use .ptr
coletz
12/13/2018, 11:27 PMsvyatoslav.scherbina
12/14/2018, 7:21 AMBtw, to avoid copying something like this can be used:bytes.toCValues().getPointer(this)
bytes.usePinned {
val ptr = it.addressOf(0)
// remaining code using this pointer.
}