2014-04-20 09:12:48 +02:00
|
|
|
package com.rarchives.ripme.utils;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base64 encoder/decoder
|
|
|
|
* From http://stackoverflow.com/a/4265472
|
|
|
|
*/
|
|
|
|
public class Base64 {
|
2017-06-06 19:13:28 +02:00
|
|
|
|
2014-04-20 09:12:48 +02:00
|
|
|
private final static char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
|
|
|
|
|
2017-05-10 01:13:42 +02:00
|
|
|
private static int[] toInt = new int[128];
|
2014-04-20 09:12:48 +02:00
|
|
|
|
|
|
|
static {
|
2017-05-10 01:13:42 +02:00
|
|
|
for (int i = 0; i < ALPHABET.length; i++) {
|
|
|
|
toInt[ALPHABET[i]] = i;
|
2014-04-20 09:12:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 19:13:28 +02:00
|
|
|
private Base64() {
|
|
|
|
}
|
|
|
|
|
2014-04-20 09:12:48 +02:00
|
|
|
/**
|
|
|
|
* Translates the specified byte array into Base64 string.
|
|
|
|
*
|
|
|
|
* @param buf the byte array (not null)
|
|
|
|
* @return the translated Base64 string (not null)
|
|
|
|
*/
|
2017-05-10 01:13:42 +02:00
|
|
|
public static String encode(byte[] buf) {
|
2014-04-20 09:12:48 +02:00
|
|
|
int size = buf.length;
|
|
|
|
char[] ar = new char[((size + 2) / 3) * 4];
|
|
|
|
int a = 0;
|
2017-05-10 01:13:42 +02:00
|
|
|
int i = 0;
|
2017-06-06 19:13:28 +02:00
|
|
|
|
2017-05-10 01:13:42 +02:00
|
|
|
while (i < size) {
|
2014-04-20 09:12:48 +02:00
|
|
|
byte b0 = buf[i++];
|
|
|
|
byte b1 = (i < size) ? buf[i++] : 0;
|
|
|
|
byte b2 = (i < size) ? buf[i++] : 0;
|
|
|
|
|
|
|
|
int mask = 0x3F;
|
|
|
|
ar[a++] = ALPHABET[(b0 >> 2) & mask];
|
|
|
|
ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
|
|
|
|
ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
|
|
|
|
ar[a++] = ALPHABET[b2 & mask];
|
|
|
|
}
|
2017-06-06 19:13:28 +02:00
|
|
|
|
|
|
|
if (size % 3 == 1 || size % 3 == 2) {
|
|
|
|
ar[--a] = '=';
|
2014-04-20 09:12:48 +02:00
|
|
|
}
|
2017-06-06 19:13:28 +02:00
|
|
|
|
2014-04-20 09:12:48 +02:00
|
|
|
return new String(ar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates the specified Base64 string into a byte array.
|
|
|
|
*
|
|
|
|
* @param s the Base64 string (not null)
|
|
|
|
* @return the byte array (not null)
|
|
|
|
*/
|
2017-05-10 01:13:42 +02:00
|
|
|
public static byte[] decode(String s) {
|
2017-06-06 19:13:28 +02:00
|
|
|
int delta = s.endsWith("==") ? 2 : (s.endsWith("=") ? 1 : 0);
|
2017-05-10 01:13:42 +02:00
|
|
|
byte[] buffer = new byte[s.length() * 3 / 4 - delta];
|
2014-04-20 09:12:48 +02:00
|
|
|
int mask = 0xFF;
|
|
|
|
int index = 0;
|
2017-06-06 19:13:28 +02:00
|
|
|
|
2017-05-10 01:13:42 +02:00
|
|
|
for (int i = 0; i < s.length(); i += 4) {
|
|
|
|
int c0 = toInt[s.charAt(i)];
|
|
|
|
int c1 = toInt[s.charAt(i + 1)];
|
|
|
|
buffer[index++] = (byte) (((c0 << 2) | (c1 >> 4)) & mask);
|
2017-06-06 19:13:28 +02:00
|
|
|
|
2017-05-10 01:13:42 +02:00
|
|
|
if (index >= buffer.length) {
|
2014-04-20 09:12:48 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
2017-05-10 01:13:42 +02:00
|
|
|
int c2 = toInt[s.charAt(i + 2)];
|
|
|
|
buffer[index++] = (byte) (((c1 << 4) | (c2 >> 2)) & mask);
|
2017-06-06 19:13:28 +02:00
|
|
|
|
2017-05-10 01:13:42 +02:00
|
|
|
if (index >= buffer.length) {
|
2014-04-20 09:12:48 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
2017-05-10 01:13:42 +02:00
|
|
|
int c3 = toInt[s.charAt(i + 3)];
|
|
|
|
buffer[index++] = (byte) (((c2 << 6) | c3) & mask);
|
2014-04-20 09:12:48 +02:00
|
|
|
}
|
2017-06-06 19:13:28 +02:00
|
|
|
|
2014-04-20 09:12:48 +02:00
|
|
|
return buffer;
|
2017-05-10 00:22:55 +02:00
|
|
|
}
|
2017-06-06 19:13:28 +02:00
|
|
|
|
|
|
|
}
|