Base64.java
4.49 KB
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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.pdfg.common;
public class Base64 {
private static final String toChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static int[] toInt;
static final char[] digits;
static byte[] charVal;
private Base64() {
}
private static void loadMap() {
int i;
toInt = new int[123];
for (i = 0; i < toInt.length; ++i) {
Base64.toInt[i] = 0;
}
i = 0;
while (i < "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".length()) {
Base64.toInt["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt((int)i)] = i++;
}
}
public static final String encode(String in) {
return Base64.encode(in.getBytes());
}
public static final String encode(byte[] in) {
int v;
int i;
StringBuffer out = new StringBuffer(in.length * 4 / 3);
char[] buf = new char[4];
int lim = in.length - 2;
for (i = 0; i < lim; i += 3) {
v = (in[i] & 255) << 16 | (in[i + 1] & 255) << 8 | in[i + 2] & 255;
buf[0] = digits[v >> 18];
buf[1] = digits[v >> 12 & 63];
buf[2] = digits[v >> 6 & 63];
buf[3] = digits[v & 63];
out.append(buf);
}
if (i < in.length) {
buf[3] = 61;
buf[2] = 61;
v = (in[i] & 255) << 16;
if (i + 1 < in.length) {
buf[2] = digits[(v |= (in[i + 1] & 255) << 8) >> 6 & 63];
}
buf[0] = digits[v >> 18];
buf[1] = digits[v >> 12 & 63];
out.append(buf);
}
return out.toString();
}
public static final String decodeAsString(String from) {
if (toInt == null) {
Base64.loadMap();
}
StringBuffer to = new StringBuffer();
int value = 0;
int pad = 0;
int fromlen = from.length();
int len = fromlen + 2 & -4;
for (int i = 0; i < len; ++i) {
value <<= 6;
char ch = from.charAt(i);
if (i < fromlen && (ch >= '0' && ch <= 'z' || ch == '+' || ch == '/') && ch != '=') {
value += toInt[ch];
} else {
++pad;
}
if ((i & 3) != 3) continue;
to.append((char)(value >> 16 & 255));
if (pad < 2) {
to.append((char)(value >> 8 & 255));
}
if (pad < 1) {
to.append((char)(value & 255));
}
value = 0;
}
return to.toString();
}
public static final byte[] decode(String in) {
byte[] buf = in.getBytes();
int lng = Base64.decode(buf);
byte[] out = new byte[lng];
for (int i = 0; i < lng; ++i) {
out[i] = buf[i];
}
return out;
}
private static final int decode(byte[] buf) {
int i = 0;
int j = 0;
int lng = buf.length;
do {
int v1 = -1;
while (i < lng && v1 == -1) {
v1 = charVal[buf[i++] + 128];
}
if (v1 == -1) break;
int v2 = -1;
while (i < lng && v2 == -1) {
v2 = charVal[buf[i++] + 128];
}
if (v2 == -1) break;
buf[j++] = (byte)(v1 << 2 | v2 >> 4);
int v3 = -1;
while (i < lng && v3 == -1) {
v3 = charVal[buf[i++] + 128];
}
if (v3 == -1) break;
buf[j++] = (byte)(v2 << 4 | v3 >> 2);
int v4 = -1;
while (i < lng && v4 == -1) {
v4 = charVal[buf[i++] + 128];
}
if (v4 == -1) break;
buf[j++] = (byte)(v3 << 6 | v4);
} while (true);
return j;
}
static {
int i;
toInt = null;
digits = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
charVal = new byte[256];
for (i = 0; i < charVal.length; ++i) {
Base64.charVal[i] = -1;
}
for (i = 0; i < digits.length; i = (int)((byte)(i + 1))) {
Base64.charVal[Base64.digits[i] + 128] = i;
}
}
}