BaseFlex.java
5.33 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
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Decompiled with CFR 0_118.
*/
package com.day.crx.explorer.impl.util;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
public class BaseFlex {
public static final String base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public static final String url64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
public static final char base64Pad = '=';
public static final char url64Pad = '~';
private BaseFlex() {
}
public static String encodeBase64(String in) {
return BaseFlex.encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", '=', in);
}
public static String decodeBase64(String in) {
return BaseFlex.decode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", '=', in);
}
public static String encode(String encodingTable, char padChar, String srcString) {
try {
return BaseFlex.encode(encodingTable, padChar, srcString.getBytes("utf-8"));
}
catch (UnsupportedEncodingException e) {
throw new InternalError("System should have UTF8: " + e.toString());
}
}
public static String encode(String encodingTable, char padChar, byte[] src) {
if (encodingTable == null || encodingTable.length() == 0) {
return "";
}
if (src == null || src.length == 0) {
return "";
}
int srcsize = src.length;
int tbllen = encodingTable.length();
if (tbllen != 2 && tbllen != 4 && tbllen != 8 && tbllen != 16 && tbllen != 32 && tbllen != 64 && tbllen != 128) {
return "";
}
StringBuffer result = new StringBuffer(srcsize);
int tblpos = 0;
int bitpos = 0;
int bitsread = -1;
int inpos = 0;
byte pos = 0;
while (inpos <= srcsize) {
if (bitsread < 0) {
if (inpos >= srcsize) break;
pos = src[inpos++];
bitsread = 7;
}
tblpos = 0;
bitpos = tbllen / 2;
while (bitpos > 0) {
if (bitsread < 0) {
pos = inpos < srcsize ? src[inpos] : 0;
++inpos;
bitsread = 7;
}
if ((1 << bitsread & pos) != 0) {
tblpos += bitpos;
}
bitpos /= 2;
--bitsread;
}
result.append(encodingTable.charAt(tblpos));
}
while (bitsread != -1) {
bitpos = tbllen / 2;
while (bitpos > 0) {
if (bitsread < 0) {
bitsread = 7;
}
bitpos /= 2;
--bitsread;
}
result.append(padChar);
}
return result.toString();
}
public static byte[] decodeToBytes(String decodingTable, char padChar, String srcString) {
int j;
int[] x;
int shift;
if (decodingTable == null || decodingTable.length() == 0 || srcString == null || srcString.length() == 0) {
return new byte[0];
}
int srcsize = srcString.length();
int tbllen = decodingTable.length();
for (shift = 1; shift < 8 && 1 << shift != tbllen; ++shift) {
}
if (shift == 8) {
throw new IllegalArgumentException("Deconding table size must be a power of two");
}
char[] decTable = new char[256];
for (int i = 0; i < tbllen; ++i) {
if (decTable[decodingTable.charAt(i)] > '\u0000') {
throw new IllegalArgumentException("Multiply defined codes are not allowed.");
}
decTable[decodingTable.charAt((int)i)] = (char)i;
}
ByteArrayOutputStream result = new ByteArrayOutputStream(srcsize);
int inpos = 0;
char pos = srcString.charAt(inpos++);
long incoming = 0;
int bitsread = 0;
while (inpos <= srcsize) {
incoming = (incoming << shift) + (long)decTable[pos];
if ((bitsread += shift) % 8 == 0) {
x = new int[8];
for (j = 0; j < bitsread >> 3; ++j) {
x[j] = (int)(incoming & 255);
incoming >>= 8;
}
bitsread = 0;
while (j > 0) {
if (x[--j] == 0) continue;
result.write(x[j]);
}
}
pos = inpos < srcsize ? srcString.charAt(inpos) : '\u0000';
++inpos;
}
if (bitsread > 0) {
x = new int[8];
incoming >>= bitsread % 8;
for (j = 0; j < bitsread >> 3; ++j) {
x[j] = (int)(incoming & 255);
incoming >>= 8;
}
while (j > 0) {
if (x[--j] == 0) continue;
result.write(x[j]);
}
}
return result.toByteArray();
}
public static String decode(String decodingTable, char padChar, String srcString) {
try {
return new String(BaseFlex.decodeToBytes(decodingTable, padChar, srcString), "utf-8");
}
catch (UnsupportedEncodingException e) {
throw new InternalError("System should have UTF8: " + e.toString());
}
}
}