From 14f65717642f9cf6c1a9d56820fb9c03ebea2ef2 Mon Sep 17 00:00:00 2001 From: UnfamiliarLegacy <74633542+UnfamiliarLegacy@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:33:02 +0200 Subject: [PATCH] Fix base64 decoding on Windows 11 arm64 --- .../java/gearth/encoding/Base64Encoding.java | 25 ++++++++----------- G-Earth/src/test/java/TestBase64Encoding.java | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 G-Earth/src/test/java/TestBase64Encoding.java diff --git a/G-Earth/src/main/java/gearth/encoding/Base64Encoding.java b/G-Earth/src/main/java/gearth/encoding/Base64Encoding.java index ba1812e..02aaaf7 100644 --- a/G-Earth/src/main/java/gearth/encoding/Base64Encoding.java +++ b/G-Earth/src/main/java/gearth/encoding/Base64Encoding.java @@ -5,8 +5,6 @@ package gearth.encoding; * Kepler */ public class Base64Encoding { - public byte NEGATIVE = 64; - public byte POSITIVE = 65; public static byte[] encode(int i, int numBytes) { byte[] bzRes = new byte[numBytes]; @@ -19,20 +17,19 @@ public class Base64Encoding { return bzRes; } - public static int decode(byte[] bzData) { - int i = 0; - int j = 0; - for (int k = bzData.length - 1; k >= 0; k--) - { - int x = bzData[k] - 0x40; - if (j > 0) - x *= (int)Math.pow(64.0, (double)j); + public static int decode(byte[] data) { + int res = 0; - i += x; - j++; + for (int k = data.length - 1, i = 0; k >= 0; k--, i++) + { + int x = data[k] - 0x40; + if (i > 0){ + res += x << (i * 6); + } else { + res += x; + } } - return i; + return res; } } - diff --git a/G-Earth/src/test/java/TestBase64Encoding.java b/G-Earth/src/test/java/TestBase64Encoding.java new file mode 100644 index 0000000..a948e55 --- /dev/null +++ b/G-Earth/src/test/java/TestBase64Encoding.java @@ -0,0 +1,25 @@ +import gearth.encoding.Base64Encoding; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestBase64Encoding { + + @Test + public void testBase64Encoding() { + testDecode(0, "@@"); + testDecode(202, "CJ"); + testDecode(206, "CN"); + testDecode(277, "DU"); + testDecode(1337, "@Ty"); + } + + private void testDecode(int expected, String input) { + final byte[] header = input.getBytes(StandardCharsets.ISO_8859_1); + + assertEquals(expected, Base64Encoding.decode(header)); + } + +} \ No newline at end of file