Jeff Lockhart
02/05/2021, 6:02 AMByteArray
to and from CFDataRef
? I found this for converting to/from NSData
, but having a hard time figuring out how to do something similar for CFDataRef
. I need to create a SecCertificateRef
, which I’m using SecCertificateCreateWithData()
and SecCertificateCopyData()
to create and get the data from.Jeff Lockhart
02/05/2021, 6:53 AMfun CFDataRef.toByteArray(): ByteArray {
val length = CFDataGetLength(this)
return UByteArray(length.toInt()).apply {
val range = CFRangeMake(0, length)
CFDataGetBytes(this@toByteArray, range, refTo(0))
}.toByteArray()
}
Is it required to create a UByteArray
first or can the refTo()
of ByteArray
be safely cast from CValueRef<ByteVarOf<Byte>>
to the CValuesRef<UByteVarOf<UByte>>
that CFDataGetBytes()
requires?Jeff Lockhart
02/05/2021, 7:01 AMfun ByteArray.toCFData(): CFDataRef =
CFDataCreate(null,
toUByteArray().refTo(0),
size.toLong())!!
Same question about the need to convert to UByteArray
, or if casting the address would work without needing to copy data twice.
Am I on the right track with these?Jeff Lockhart
02/05/2021, 10:55 PMtoUByteArray()
and toByteArray()
with asUByteArray()
and asByteArray()
to avoid the extra copy.