Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/nio/cs/CharsetMapping.java
41159 views
1
/*
2
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.nio.cs;
27
28
import java.io.InputStream;
29
import java.io.InputStreamReader;
30
import java.io.OutputStream;
31
import java.io.BufferedReader;
32
import java.io.IOException;
33
import java.util.regex.Matcher;
34
import java.util.regex.Pattern;
35
import java.util.*;
36
import java.security.*;
37
38
public class CharsetMapping {
39
public static final char UNMAPPABLE_DECODING = '\uFFFD';
40
public static final int UNMAPPABLE_ENCODING = 0xFFFD;
41
42
char[] b2cSB; //singlebyte b->c
43
char[] b2cDB1; //dobulebyte b->c /db1
44
char[] b2cDB2; //dobulebyte b->c /db2
45
46
int b2Min, b2Max; //min/max(start/end) value of 2nd byte
47
int b1MinDB1, b1MaxDB1; //min/Max(start/end) value of 1st byte/db1
48
int b1MinDB2, b1MaxDB2; //min/Max(start/end) value of 1st byte/db2
49
int dbSegSize;
50
51
char[] c2b;
52
char[] c2bIndex;
53
54
// Supplementary
55
char[] b2cSupp;
56
char[] c2bSupp;
57
58
// Composite
59
Entry[] b2cComp;
60
Entry[] c2bComp;
61
62
public char decodeSingle(int b) {
63
return b2cSB[b];
64
}
65
66
public char decodeDouble(int b1, int b2) {
67
if (b2 >= b2Min && b2 < b2Max) {
68
b2 -= b2Min;
69
if (b1 >= b1MinDB1 && b1 <= b1MaxDB1) {
70
b1 -= b1MinDB1;
71
return b2cDB1[b1 * dbSegSize + b2];
72
}
73
if (b1 >= b1MinDB2 && b1 <= b1MaxDB2) {
74
b1 -= b1MinDB2;
75
return b2cDB2[b1 * dbSegSize + b2];
76
}
77
}
78
return UNMAPPABLE_DECODING;
79
}
80
81
// for jis0213 all supplementary characters are in 0x2xxxx range,
82
// so only the xxxx part is now stored, should actually store the
83
// codepoint value instead.
84
public char[] decodeSurrogate(int db, char[] cc) {
85
int end = b2cSupp.length / 2;
86
int i = Arrays.binarySearch(b2cSupp, 0, end, (char)db);
87
if (i >= 0) {
88
Character.toChars(b2cSupp[end + i] + 0x20000, cc, 0);
89
return cc;
90
}
91
return null;
92
}
93
94
public char[] decodeComposite(Entry comp, char[] cc) {
95
int i = findBytes(b2cComp, comp);
96
if (i >= 0) {
97
cc[0] = (char)b2cComp[i].cp;
98
cc[1] = (char)b2cComp[i].cp2;
99
return cc;
100
}
101
return null;
102
}
103
104
public int encodeChar(char ch) {
105
int index = c2bIndex[ch >> 8];
106
if (index == 0xffff)
107
return UNMAPPABLE_ENCODING;
108
return c2b[index + (ch & 0xff)];
109
}
110
111
public int encodeSurrogate(char hi, char lo) {
112
int cp = Character.toCodePoint(hi, lo);
113
if (cp < 0x20000 || cp >= 0x30000)
114
return UNMAPPABLE_ENCODING;
115
int end = c2bSupp.length / 2;
116
int i = Arrays.binarySearch(c2bSupp, 0, end, (char)cp);
117
if (i >= 0)
118
return c2bSupp[end + i];
119
return UNMAPPABLE_ENCODING;
120
}
121
122
public boolean isCompositeBase(Entry comp) {
123
if (comp.cp <= 0x31f7 && comp.cp >= 0xe6) {
124
return (findCP(c2bComp, comp) >= 0);
125
}
126
return false;
127
}
128
129
public int encodeComposite(Entry comp) {
130
int i = findComp(c2bComp, comp);
131
if (i >= 0)
132
return c2bComp[i].bs;
133
return UNMAPPABLE_ENCODING;
134
}
135
136
// init the CharsetMapping object from the .dat binary file
137
@SuppressWarnings("removal")
138
public static CharsetMapping get(final InputStream is) {
139
return AccessController.doPrivileged(new PrivilegedAction<>() {
140
public CharsetMapping run() {
141
return new CharsetMapping().load(is);
142
}
143
});
144
}
145
146
public static class Entry {
147
public int bs; //byte sequence reps
148
public int cp; //Unicode codepoint
149
public int cp2; //CC of composite
150
}
151
152
static Comparator<Entry> comparatorBytes =
153
new Comparator<Entry>() {
154
public int compare(Entry m1, Entry m2) {
155
return m1.bs - m2.bs;
156
}
157
public boolean equals(Object obj) {
158
return this == obj;
159
}
160
};
161
162
static Comparator<Entry> comparatorCP =
163
new Comparator<Entry>() {
164
public int compare(Entry m1, Entry m2) {
165
return m1.cp - m2.cp;
166
}
167
public boolean equals(Object obj) {
168
return this == obj;
169
}
170
};
171
172
static Comparator<Entry> comparatorComp =
173
new Comparator<Entry>() {
174
public int compare(Entry m1, Entry m2) {
175
int v = m1.cp - m2.cp;
176
if (v == 0)
177
v = m1.cp2 - m2.cp2;
178
return v;
179
}
180
public boolean equals(Object obj) {
181
return this == obj;
182
}
183
};
184
185
static int findBytes(Entry[] a, Entry k) {
186
return Arrays.binarySearch(a, 0, a.length, k, comparatorBytes);
187
}
188
189
static int findCP(Entry[] a, Entry k) {
190
return Arrays.binarySearch(a, 0, a.length, k, comparatorCP);
191
}
192
193
static int findComp(Entry[] a, Entry k) {
194
return Arrays.binarySearch(a, 0, a.length, k, comparatorComp);
195
}
196
197
/*****************************************************************************/
198
// tags of different charset mapping tables
199
private static final int MAP_SINGLEBYTE = 0x1; // 0..256 : c
200
private static final int MAP_DOUBLEBYTE1 = 0x2; // min..max: c
201
private static final int MAP_DOUBLEBYTE2 = 0x3; // min..max: c [DB2]
202
private static final int MAP_SUPPLEMENT = 0x5; // db,c
203
private static final int MAP_SUPPLEMENT_C2B = 0x6; // c,db
204
private static final int MAP_COMPOSITE = 0x7; // db,base,cc
205
private static final int MAP_INDEXC2B = 0x8; // index table of c->bb
206
207
private static final boolean readNBytes(InputStream in, byte[] bb, int N)
208
throws IOException
209
{
210
int off = 0;
211
while (N > 0) {
212
int n = in.read(bb, off, N);
213
if (n == -1)
214
return false;
215
N = N - n;
216
off += n;
217
}
218
return true;
219
}
220
221
int off = 0;
222
byte[] bb;
223
private char[] readCharArray() {
224
// first 2 bytes are the number of "chars" stored in this table
225
int size = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
226
char [] cc = new char[size];
227
for (int i = 0; i < size; i++) {
228
cc[i] = (char)(((bb[off++]&0xff)<<8) | (bb[off++]&0xff));
229
}
230
return cc;
231
}
232
233
void readSINGLEBYTE() {
234
char[] map = readCharArray();
235
for (int i = 0; i < map.length; i++) {
236
char c = map[i];
237
if (c != UNMAPPABLE_DECODING) {
238
c2b[c2bIndex[c >> 8] + (c&0xff)] = (char)i;
239
}
240
}
241
b2cSB = map;
242
}
243
244
void readINDEXC2B() {
245
char[] map = readCharArray();
246
for (int i = map.length - 1; i >= 0; i--) {
247
if (c2b == null && map[i] != -1) {
248
c2b = new char[map[i] + 256];
249
Arrays.fill(c2b, (char)UNMAPPABLE_ENCODING);
250
break;
251
}
252
}
253
c2bIndex = map;
254
}
255
256
char[] readDB(int b1Min, int b2Min, int segSize) {
257
char[] map = readCharArray();
258
for (int i = 0; i < map.length; i++) {
259
char c = map[i];
260
if (c != UNMAPPABLE_DECODING) {
261
int b1 = i / segSize;
262
int b2 = i % segSize;
263
int b = (b1 + b1Min)* 256 + (b2 + b2Min);
264
//System.out.printf(" DB %x\t%x%n", b, c & 0xffff);
265
c2b[c2bIndex[c >> 8] + (c&0xff)] = (char)(b);
266
}
267
}
268
return map;
269
}
270
271
void readDOUBLEBYTE1() {
272
b1MinDB1 = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
273
b1MaxDB1 = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
274
b2Min = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
275
b2Max = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
276
dbSegSize = b2Max - b2Min + 1;
277
b2cDB1 = readDB(b1MinDB1, b2Min, dbSegSize);
278
}
279
280
void readDOUBLEBYTE2() {
281
b1MinDB2 = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
282
b1MaxDB2 = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
283
b2Min = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
284
b2Max = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
285
dbSegSize = b2Max - b2Min + 1;
286
b2cDB2 = readDB(b1MinDB2, b2Min, dbSegSize);
287
}
288
289
void readCOMPOSITE() {
290
char[] map = readCharArray();
291
int mLen = map.length/3;
292
b2cComp = new Entry[mLen];
293
c2bComp = new Entry[mLen];
294
for (int i = 0, j= 0; i < mLen; i++) {
295
Entry m = new Entry();
296
m.bs = map[j++];
297
m.cp = map[j++];
298
m.cp2 = map[j++];
299
b2cComp[i] = m;
300
c2bComp[i] = m;
301
}
302
Arrays.sort(c2bComp, 0, c2bComp.length, comparatorComp);
303
}
304
305
CharsetMapping load(InputStream in) {
306
try {
307
// The first 4 bytes are the size of the total data followed in
308
// this .dat file.
309
int len = ((in.read()&0xff) << 24) | ((in.read()&0xff) << 16) |
310
((in.read()&0xff) << 8) | (in.read()&0xff);
311
bb = new byte[len];
312
off = 0;
313
//System.out.printf("In : Total=%d%n", len);
314
// Read in all bytes
315
if (!readNBytes(in, bb, len))
316
throw new RuntimeException("Corrupted data file");
317
in.close();
318
319
while (off < len) {
320
int type = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
321
switch(type) {
322
case MAP_INDEXC2B:
323
readINDEXC2B();
324
break;
325
case MAP_SINGLEBYTE:
326
readSINGLEBYTE();
327
break;
328
case MAP_DOUBLEBYTE1:
329
readDOUBLEBYTE1();
330
break;
331
case MAP_DOUBLEBYTE2:
332
readDOUBLEBYTE2();
333
break;
334
case MAP_SUPPLEMENT:
335
b2cSupp = readCharArray();
336
break;
337
case MAP_SUPPLEMENT_C2B:
338
c2bSupp = readCharArray();
339
break;
340
case MAP_COMPOSITE:
341
readCOMPOSITE();
342
break;
343
default:
344
throw new RuntimeException("Corrupted data file");
345
}
346
}
347
bb = null;
348
return this;
349
} catch (IOException x) {
350
x.printStackTrace();
351
return null;
352
}
353
}
354
}
355
356