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