|
| 1 | +package sjsonnet.stdlib |
| 2 | + |
| 3 | +import scala.scalanative.unsafe._ |
| 4 | +import scala.scalanative.unsigned._ |
| 5 | +import scala.scalanative.libc.string.memcpy |
| 6 | + |
| 7 | +/** |
| 8 | + * Scala Native implementation of base64 encode/decode. |
| 9 | + * |
| 10 | + * Uses the aklomp/base64 C library (BSD-2-Clause) which provides SIMD-accelerated base64 via |
| 11 | + * runtime CPU detection: |
| 12 | + * - x86_64: SSSE3 / SSE4.1 / SSE4.2 / AVX / AVX2 / AVX-512 |
| 13 | + * - AArch64: NEON |
| 14 | + * - Fallback: optimized generic C implementation |
| 15 | + * |
| 16 | + * The static library is built by CMake and linked via nativeLinkingOptions. |
| 17 | + * |
| 18 | + * Both aklomp/base64 and C++ jsonnet (the reference implementation) use strict RFC 4648 mode: |
| 19 | + * padding is required, unpadded input is rejected. This differs from java.util.Base64 on JVM which |
| 20 | + * is more lenient (accepts unpadded input) — that JVM leniency is a pre-existing sjsonnet bug, not |
| 21 | + * something we replicate here. |
| 22 | + */ |
| 23 | +@extern |
| 24 | +private[stdlib] object libbase64 { |
| 25 | + def base64_encode( |
| 26 | + src: Ptr[CChar], |
| 27 | + srclen: CSize, |
| 28 | + out: Ptr[CChar], |
| 29 | + outlen: Ptr[CSize], |
| 30 | + flags: CInt |
| 31 | + ): Unit = extern |
| 32 | + |
| 33 | + def base64_decode( |
| 34 | + src: Ptr[CChar], |
| 35 | + srclen: CSize, |
| 36 | + out: Ptr[CChar], |
| 37 | + outlen: Ptr[CSize], |
| 38 | + flags: CInt |
| 39 | + ): CInt = extern |
| 40 | +} |
| 41 | + |
| 42 | +object PlatformBase64 { |
| 43 | + |
| 44 | + private val DECODE_TABLE: Array[Int] = { |
| 45 | + val t = Array.fill[Int](256)(-1) |
| 46 | + var i = 0 |
| 47 | + while (i < 26) { t('A' + i) = i; i += 1 } |
| 48 | + i = 0 |
| 49 | + while (i < 26) { t('a' + i) = i + 26; i += 1 } |
| 50 | + i = 0 |
| 51 | + while (i < 10) { t('0' + i) = i + 52; i += 1 } |
| 52 | + t('+') = 62 |
| 53 | + t('/') = 63 |
| 54 | + t |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Diagnose why base64 decode failed and throw a JVM-compatible error message. Only called on the |
| 59 | + * error path (after aklomp/base64 returns failure), so zero overhead on the hot path. |
| 60 | + * |
| 61 | + * Error messages match java.util.Base64.Decoder behavior for golden test compatibility: |
| 62 | + * - Invalid character: "Illegal base64 character XX" (hex) |
| 63 | + * - Wrong length/padding: "Last unit does not have enough valid bits" |
| 64 | + */ |
| 65 | + private def throwDecodeError(srcBytes: Array[Byte]): Nothing = { |
| 66 | + val len = srcBytes.length |
| 67 | + |
| 68 | + var i = 0 |
| 69 | + while (i < len) { |
| 70 | + val b = srcBytes(i) & 0xff |
| 71 | + if (b != '='.toInt) { |
| 72 | + if (DECODE_TABLE(b) < 0) { |
| 73 | + throw new IllegalArgumentException( |
| 74 | + "Illegal base64 character " + Integer.toHexString(b) |
| 75 | + ) |
| 76 | + } |
| 77 | + } |
| 78 | + i += 1 |
| 79 | + } |
| 80 | + |
| 81 | + throw new IllegalArgumentException( |
| 82 | + "Last unit does not have enough valid bits" |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + def encodeToString(input: Array[Byte]): String = { |
| 87 | + if (input.length == 0) return "" |
| 88 | + val maxOutLen = ((input.length.toLong + 2) / 3) * 4 |
| 89 | + if (maxOutLen > Int.MaxValue) |
| 90 | + throw new IllegalArgumentException("Input too large for base64 encoding") |
| 91 | + val outSize = maxOutLen.toInt |
| 92 | + Zone.acquire { implicit z => |
| 93 | + val srcPtr = alloc[Byte](input.length.toUSize) |
| 94 | + memcpy(srcPtr, input.at(0), input.length.toUSize) |
| 95 | + val outPtr = alloc[Byte]((outSize + 1).toUSize) |
| 96 | + val outLenPtr = alloc[CSize](1.toUSize) |
| 97 | + libbase64.base64_encode(srcPtr, input.length.toUSize, outPtr, outLenPtr, 0) |
| 98 | + val actualLen = (!outLenPtr).toInt |
| 99 | + val result = new Array[Byte](actualLen) |
| 100 | + memcpy(result.at(0), outPtr, actualLen.toUSize) |
| 101 | + new String(result, "US-ASCII") |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + def decode(input: String): Array[Byte] = { |
| 106 | + if (input.isEmpty) return Array.emptyByteArray |
| 107 | + val srcBytes = input.getBytes("US-ASCII") |
| 108 | + val maxOutLen = ((srcBytes.length.toLong / 4) * 3) + 3 |
| 109 | + if (maxOutLen > Int.MaxValue) |
| 110 | + throw new IllegalArgumentException("Input too large for base64 decoding") |
| 111 | + val outSize = maxOutLen.toInt |
| 112 | + Zone.acquire { implicit z => |
| 113 | + val srcPtr = alloc[Byte](srcBytes.length.toUSize) |
| 114 | + memcpy(srcPtr, srcBytes.at(0), srcBytes.length.toUSize) |
| 115 | + val outPtr = alloc[Byte]((outSize + 1).toUSize) |
| 116 | + val outLenPtr = alloc[CSize](1.toUSize) |
| 117 | + val ret = |
| 118 | + libbase64.base64_decode(srcPtr, srcBytes.length.toUSize, outPtr, outLenPtr, 0) |
| 119 | + if (ret != 1) { |
| 120 | + throwDecodeError(srcBytes) |
| 121 | + } |
| 122 | + val actualLen = (!outLenPtr).toInt |
| 123 | + val result = new Array[Byte](actualLen) |
| 124 | + memcpy(result.at(0), outPtr, actualLen.toUSize) |
| 125 | + result |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments