Implement Base64 without need of JDK8. (#78)

This commit is contained in:
Nikolay Igotti
2016-11-21 10:42:49 +03:00
committed by GitHub
parent 5161155934
commit 96797a4b2f
4 changed files with 15 additions and 10 deletions
@@ -116,14 +116,7 @@ internal interface ContextUtils {
*/
val String.globalHashBase64: String
get() {
val bytes = this.toByteArray(Charsets.UTF_8)
val hashBytes = this.globalHashBytes
val base64Bytes = java.util.Base64.getEncoder().encode(globalHashBytes)
// TODO: do not use JRE for base64
// (it is impossible right now due to native interop limitations)
return String(base64Bytes, Charsets.US_ASCII)
return base64Encode(globalHashBytes)
}
/**
@@ -21,4 +21,15 @@ internal fun globalHash(data: ByteArray, retValPlacement: Placement): GlobalHash
return res
}
internal fun base64Encode(data: ByteArray): String {
memScoped {
val resultSize = 4 * data.size / 3 + 3 + 1
val result = alloc(NativeArray of Int8Box length resultSize)
val bytes = allocNativeArrayOf(data)
Base64Encode(bytes.ptr, data.size, result.ptr, resultSize)
// TODO: any better way to do that without two copies?
return CString.fromArray(result).toString()
}
}
internal class LocalHash(val value: Long) : ConstValue by Int64(value)
+2 -1
View File
@@ -36,7 +36,8 @@ extern "C" {
#endif
int Base64Encode(
const void* dataBuf, uint32_t dataLength, char* result, uint32_t resultSize) {
const void* dataBuf, uint32_t dataLength, void* resultBuf, uint32_t resultSize) {
char *result = reinterpret_cast<char*>(resultBuf);
const uint8_t *data = reinterpret_cast<const uint8_t*>(dataBuf);
size_t resultIndex = 0;
size_t x;
+1 -1
View File
@@ -8,7 +8,7 @@ extern "C" {
#endif
int Base64Encode(
const void* input, uint32_t inputLen, char* output, uint32_t outputLen);
const void* input, uint32_t inputLen, void* output, uint32_t outputLen);
int Base64Decode(
const char* input, uint32_t inputLen, void* output, uint32_t* outputLen);