scoop:scoop.charset:0.1.0
src/native.scoop
Selected production source · src/native.scoop
1
package scoop.charset3
import scoop.platform.memory.NativeByteBuffer5
@CLayout internal struct NativeCharsetText(val data: Ptr<Byte>?, val size: Long, val code: Int, val backend: Int)6
@CLayout internal struct NativeCharsetResult(val handle: ULong, val code: Int, val backend: Int)7
@CLayout internal struct NativeCharsetOptions(8
val inputBom: Int, val sourceOrder: Int, val destinationOrder: Int,9
val emitBom: Int, val replaceInput: Int, val replaceOutput: Int10
)11
@CLayout internal struct NativeCharsetStep(12
val consumed: Long, val produced: Long, val offset: Long, val status: Int, val code: Int, val backend: Int13
)15
internal fun canonicalCharset(name: String): String {16
@Unsafe {17
val bytes = name.toByteArray()18
val result = bytes.withDataPointer { data, count -> @Unsafe { return@withDataPointer charsetName(data, count) } }19
if (result.code == 1) throw UnsupportedCharsetException(name)20
return charsetText(result, "resolve charset", name)21
}22
}24
internal fun charsetText(result: NativeCharsetText, operation: String, name: String = ""): String {25
@Unsafe {26
try {27
checkCharset(result.code, result.backend, operation, name, "UTF-32LE")28
val bytes = Array<Byte>(result.size) { index -> @Unsafe { return@Array result.data!!.load(index) } }29
return String.fromUtf8(bytes)30
} finally { charsetTextFree(result.data, result.size) }31
}32
}34
internal fun createCharset(source: Charset, destination: Charset, options: CharsetTranscodeOptions): ULong {35
@Unsafe {36
val config = charsetOptions(options)37
val left = source.name.toByteArray()38
val right = destination.name.toByteArray()39
val result = left.withDataPointer { input, inputSize ->40
@Unsafe { return@withDataPointer right.withDataPointer { output, outputSize ->41
@Unsafe { return@withDataPointer charsetNew(input, inputSize, output, outputSize, config) }42
} }43
}44
checkCharset(result.code, result.backend, "create transcoder", source.name, destination.name)45
return result.handle46
}47
}49
internal fun processCharset(handle: ULong, input: NativeByteBuffer, output: NativeByteBuffer, endInput: Boolean): NativeCharsetStep {50
@Unsafe {51
val read = input.borrowReadable(input.readableBytes)52
val result = try {53
val write = output.borrowWritable(output.writableBytes)54
val step = try { charsetProcess(handle, read.pointer(), read.length, write.pointer(), write.length, if (endInput) 1 else 0) }55
catch (error: Throwable) { write.complete(0L); throw error }56
write.complete(step.produced)57
step58
} catch (error: Throwable) { read.complete(0L); throw error }59
read.complete(result.consumed)60
return result61
}62
}64
@Extern(name = "scoop_charset_name") private fun charsetName(data: Ptr<Byte>, count: Long): NativeCharsetText65
@Extern(name = "scoop_text_free") internal fun charsetTextFree(data: Ptr<Byte>?, size: Long)66
@Extern(name = "scoop_charset_names") internal fun charsetNames(): NativeCharsetText67
@Extern(name = "scoop_charset_version") internal fun charsetVersion(): Ptr<Byte>68
@Extern(name = "scoop_charset_new") private fun charsetNew(source: Ptr<Byte>, sourceSize: Long, destination: Ptr<Byte>, destinationSize: Long, options: NativeCharsetOptions): NativeCharsetResult69
@Extern(name = "scoop_charset_process") private fun charsetProcess(handle: ULong, input: Ptr<Byte>?, inputSize: Long, output: Ptr<Byte>?, outputSize: Long, endInput: Int): NativeCharsetStep70
@Extern(name = "scoop_charset_free") internal fun charsetFree(handle: ULong)