-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathBase85.java
157 lines (138 loc) · 4.57 KB
/
Base85.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Ascii85 is also called Base85
*/
package cryptography.encoding.base85;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.regex.Pattern;
import cryptography.Mode;
public class Base85 {
private final static int ASCII_SHIFT = 33;
private static int[] BASE85_POW = { 1, 85, 85 * 85, 85 * 85 * 85, 85 * 85 * 85 * 85 };
private static Pattern REMOVE_WHITESPACE = Pattern.compile("\\s+");
public static void main(String[] args) {
}
public static String base85(String input, Mode mode) {
String output = "";
if (mode == Mode.ENCODE) {
output = encode(input.getBytes());
}
if (mode == Mode.DECODE) {
output = decode(input);
}
return output;
}
public static String encode(byte[] payload) {
if (payload == null || payload.length == 0) {
throw new IllegalArgumentException("You must provide a non-zero length input");
}
StringBuilder stringBuff = new StringBuilder(payload.length * 5 / 4);
byte[] chunk = new byte[4];
int chunkIndex = 0;
for (int i = 0; i < payload.length; i++) {
byte currByte = payload[i];
chunk[chunkIndex++] = currByte;
if (chunkIndex == 4) {
int value = byteToInt(chunk);
if (value == 0) {
stringBuff.append('z');
} else {
stringBuff.append(encodeChunk(value));
}
Arrays.fill(chunk, (byte) 0);
chunkIndex = 0;
}
}
if (chunkIndex > 0) {
int numPadded = chunk.length - chunkIndex;
Arrays.fill(chunk, chunkIndex, chunk.length, (byte) 0);
int value = byteToInt(chunk);
char[] encodedChunk = encodeChunk(value);
for (int i = 0; i < encodedChunk.length - numPadded; i++) {
stringBuff.append(encodedChunk[i]);
}
}
return stringBuff.toString();
}
private static char[] encodeChunk(int value) {
char[] encodedChunk = new char[5];
for (int i = 0; i < encodedChunk.length; i++) {
encodedChunk[i] = (char) ((value / BASE85_POW[4 - i]) + ASCII_SHIFT);
value = value % BASE85_POW[4 - i];
}
return encodedChunk;
}
// Decode
public static String decode(String chars) {
try {
if (chars == null || chars.length() == 0) {
throw new IllegalArgumentException("You must provide a non-zero length input");
}
BigDecimal decodedLength = BigDecimal.valueOf(chars.length()).multiply(BigDecimal.valueOf(4))
.divide(BigDecimal.valueOf(5));
ByteBuffer bytebuff = ByteBuffer.allocate(decodedLength.intValue());
chars = REMOVE_WHITESPACE.matcher(chars).replaceAll("");
byte[] payload = chars.getBytes("UTF-8");
byte[] chunk = new byte[5];
int chunkIndex = 0;
for (int i = 0; i < payload.length; i++) {
byte currByte = payload[i];
if (currByte == 'z') {
if (chunkIndex > 0) {
throw new IllegalArgumentException("The payload is not base 85 encoded.");
}
chunk[chunkIndex++] = '!';
chunk[chunkIndex++] = '!';
chunk[chunkIndex++] = '!';
chunk[chunkIndex++] = '!';
chunk[chunkIndex++] = '!';
} else {
chunk[chunkIndex++] = currByte;
}
if (chunkIndex == 5) {
bytebuff.put(decodeChunk(chunk));
Arrays.fill(chunk, (byte) 0);
chunkIndex = 0;
}
}
if (chunkIndex > 0) {
int numPadded = chunk.length - chunkIndex;
Arrays.fill(chunk, chunkIndex, chunk.length, (byte) 'u');
byte[] paddedDecode = decodeChunk(chunk);
for (int i = 0; i < paddedDecode.length - numPadded; i++) {
bytebuff.put(paddedDecode[i]);
}
}
bytebuff.flip();
return new String(Arrays.copyOf(bytebuff.array(), bytebuff.limit()));
} catch (UnsupportedEncodingException e) {
return "Error; (Invalid Base85|Ascii85 input) " + e.toString();
} catch (BufferOverflowException e) {
return "Error; " + e.toString();
}
}
private static byte[] decodeChunk(byte[] chunk) {
if (chunk.length != 5) {
throw new IllegalArgumentException("You can only decode chunks of size 5.");
}
int value = 0;
value += (chunk[0] - ASCII_SHIFT) * BASE85_POW[4];
value += (chunk[1] - ASCII_SHIFT) * BASE85_POW[3];
value += (chunk[2] - ASCII_SHIFT) * BASE85_POW[2];
value += (chunk[3] - ASCII_SHIFT) * BASE85_POW[1];
value += (chunk[4] - ASCII_SHIFT) * BASE85_POW[0];
return intToByte(value);
}
private static int byteToInt(byte[] value) {
if (value == null || value.length != 4) {
throw new IllegalArgumentException("You cannot create an int without exactly 4 bytes.");
}
return ByteBuffer.wrap(value).getInt();
}
private static byte[] intToByte(int value) {
return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) (value) };
}
}