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/UTF_8.java
41159 views
1
/*
2
* Copyright (c) 2000, 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 jdk.internal.access.JavaLangAccess;
29
import jdk.internal.access.SharedSecrets;
30
31
import java.nio.Buffer;
32
import java.nio.ByteBuffer;
33
import java.nio.CharBuffer;
34
import java.nio.charset.Charset;
35
import java.nio.charset.CharsetDecoder;
36
import java.nio.charset.CharsetEncoder;
37
import java.nio.charset.CoderResult;
38
import java.nio.charset.CodingErrorAction;
39
40
/* Legal UTF-8 Byte Sequences
41
*
42
* # Code Points Bits Bit/Byte pattern
43
* 1 7 0xxxxxxx
44
* U+0000..U+007F 00..7F
45
*
46
* 2 11 110xxxxx 10xxxxxx
47
* U+0080..U+07FF C2..DF 80..BF
48
*
49
* 3 16 1110xxxx 10xxxxxx 10xxxxxx
50
* U+0800..U+0FFF E0 A0..BF 80..BF
51
* U+1000..U+FFFF E1..EF 80..BF 80..BF
52
*
53
* 4 21 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
54
* U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
55
* U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
56
* U+100000..U10FFFF F4 80..8F 80..BF 80..BF
57
*
58
*/
59
60
public final class UTF_8 extends Unicode {
61
62
public static final UTF_8 INSTANCE = new UTF_8();
63
64
public UTF_8() {
65
super("UTF-8", StandardCharsets.aliases_UTF_8());
66
}
67
68
public String historicalName() {
69
return "UTF8";
70
}
71
72
public CharsetDecoder newDecoder() {
73
return new Decoder(this);
74
}
75
76
public CharsetEncoder newEncoder() {
77
return new Encoder(this);
78
}
79
80
static final void updatePositions(Buffer src, int sp,
81
Buffer dst, int dp) {
82
src.position(sp - src.arrayOffset());
83
dst.position(dp - dst.arrayOffset());
84
}
85
86
private static class Decoder extends CharsetDecoder {
87
88
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
89
90
private Decoder(Charset cs) {
91
super(cs, 1.0f, 1.0f);
92
}
93
94
private static boolean isNotContinuation(int b) {
95
return (b & 0xc0) != 0x80;
96
}
97
98
// [E0] [A0..BF] [80..BF]
99
// [E1..EF] [80..BF] [80..BF]
100
private static boolean isMalformed3(int b1, int b2, int b3) {
101
return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
102
(b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80;
103
}
104
105
// only used when there is only one byte left in src buffer
106
private static boolean isMalformed3_2(int b1, int b2) {
107
return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
108
(b2 & 0xc0) != 0x80;
109
}
110
111
// [F0] [90..BF] [80..BF] [80..BF]
112
// [F1..F3] [80..BF] [80..BF] [80..BF]
113
// [F4] [80..8F] [80..BF] [80..BF]
114
// only check 80-be range here, the [0xf0,0x80...] and [0xf4,0x90-...]
115
// will be checked by Character.isSupplementaryCodePoint(uc)
116
private static boolean isMalformed4(int b2, int b3, int b4) {
117
return (b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80 ||
118
(b4 & 0xc0) != 0x80;
119
}
120
121
// only used when there is less than 4 bytes left in src buffer.
122
// both b1 and b2 should be "& 0xff" before passed in.
123
private static boolean isMalformed4_2(int b1, int b2) {
124
return (b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) ||
125
(b1 == 0xf4 && (b2 & 0xf0) != 0x80) ||
126
(b2 & 0xc0) != 0x80;
127
}
128
129
// tests if b1 and b2 are malformed as the first 2 bytes of a
130
// legal`4-byte utf-8 byte sequence.
131
// only used when there is less than 4 bytes left in src buffer,
132
// after isMalformed4_2 has been invoked.
133
private static boolean isMalformed4_3(int b3) {
134
return (b3 & 0xc0) != 0x80;
135
}
136
137
private static CoderResult malformedN(ByteBuffer src, int nb) {
138
switch (nb) {
139
case 1:
140
case 2: // always 1
141
return CoderResult.malformedForLength(1);
142
case 3:
143
int b1 = src.get();
144
int b2 = src.get(); // no need to lookup b3
145
return CoderResult.malformedForLength(
146
((b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
147
isNotContinuation(b2)) ? 1 : 2);
148
case 4: // we don't care the speed here
149
b1 = src.get() & 0xff;
150
b2 = src.get() & 0xff;
151
if (b1 > 0xf4 ||
152
(b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) ||
153
(b1 == 0xf4 && (b2 & 0xf0) != 0x80) ||
154
isNotContinuation(b2))
155
return CoderResult.malformedForLength(1);
156
if (isNotContinuation(src.get()))
157
return CoderResult.malformedForLength(2);
158
return CoderResult.malformedForLength(3);
159
default:
160
assert false;
161
return null;
162
}
163
}
164
165
private static CoderResult malformed(ByteBuffer src, int sp,
166
CharBuffer dst, int dp,
167
int nb)
168
{
169
src.position(sp - src.arrayOffset());
170
CoderResult cr = malformedN(src, nb);
171
updatePositions(src, sp, dst, dp);
172
return cr;
173
}
174
175
176
private static CoderResult malformed(ByteBuffer src,
177
int mark, int nb)
178
{
179
src.position(mark);
180
CoderResult cr = malformedN(src, nb);
181
src.position(mark);
182
return cr;
183
}
184
185
private static CoderResult malformedForLength(ByteBuffer src,
186
int sp,
187
CharBuffer dst,
188
int dp,
189
int malformedNB)
190
{
191
updatePositions(src, sp, dst, dp);
192
return CoderResult.malformedForLength(malformedNB);
193
}
194
195
private static CoderResult malformedForLength(ByteBuffer src,
196
int mark,
197
int malformedNB)
198
{
199
src.position(mark);
200
return CoderResult.malformedForLength(malformedNB);
201
}
202
203
204
private static CoderResult xflow(Buffer src, int sp, int sl,
205
Buffer dst, int dp, int nb) {
206
updatePositions(src, sp, dst, dp);
207
return (nb == 0 || sl - sp < nb)
208
? CoderResult.UNDERFLOW : CoderResult.OVERFLOW;
209
}
210
211
private static CoderResult xflow(Buffer src, int mark, int nb) {
212
src.position(mark);
213
return (nb == 0 || src.remaining() < nb)
214
? CoderResult.UNDERFLOW : CoderResult.OVERFLOW;
215
}
216
217
private CoderResult decodeArrayLoop(ByteBuffer src,
218
CharBuffer dst)
219
{
220
// This method is optimized for ASCII input.
221
byte[] sa = src.array();
222
int soff = src.arrayOffset();
223
int sp = soff + src.position();
224
int sl = soff + src.limit();
225
226
char[] da = dst.array();
227
int doff = dst.arrayOffset();
228
int dp = doff + dst.position();
229
int dl = doff + dst.limit();
230
231
int n = JLA.decodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
232
sp += n;
233
dp += n;
234
235
while (sp < sl) {
236
int b1 = sa[sp];
237
if (b1 >= 0) {
238
// 1 byte, 7 bits: 0xxxxxxx
239
if (dp >= dl)
240
return xflow(src, sp, sl, dst, dp, 1);
241
da[dp++] = (char) b1;
242
sp++;
243
} else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) {
244
// 2 bytes, 11 bits: 110xxxxx 10xxxxxx
245
// [C2..DF] [80..BF]
246
if (sl - sp < 2 || dp >= dl)
247
return xflow(src, sp, sl, dst, dp, 2);
248
int b2 = sa[sp + 1];
249
// Now we check the first byte of 2-byte sequence as
250
// if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0)
251
// no longer need to check b1 against c1 & c0 for
252
// malformed as we did in previous version
253
// (b1 & 0x1e) == 0x0 || (b2 & 0xc0) != 0x80;
254
// only need to check the second byte b2.
255
if (isNotContinuation(b2))
256
return malformedForLength(src, sp, dst, dp, 1);
257
da[dp++] = (char) (((b1 << 6) ^ b2)
258
^
259
(((byte) 0xC0 << 6) ^
260
((byte) 0x80 << 0)));
261
sp += 2;
262
} else if ((b1 >> 4) == -2) {
263
// 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx
264
int srcRemaining = sl - sp;
265
if (srcRemaining < 3 || dp >= dl) {
266
if (srcRemaining > 1 && isMalformed3_2(b1, sa[sp + 1]))
267
return malformedForLength(src, sp, dst, dp, 1);
268
return xflow(src, sp, sl, dst, dp, 3);
269
}
270
int b2 = sa[sp + 1];
271
int b3 = sa[sp + 2];
272
if (isMalformed3(b1, b2, b3))
273
return malformed(src, sp, dst, dp, 3);
274
char c = (char)
275
((b1 << 12) ^
276
(b2 << 6) ^
277
(b3 ^
278
(((byte) 0xE0 << 12) ^
279
((byte) 0x80 << 6) ^
280
((byte) 0x80 << 0))));
281
if (Character.isSurrogate(c))
282
return malformedForLength(src, sp, dst, dp, 3);
283
da[dp++] = c;
284
sp += 3;
285
} else if ((b1 >> 3) == -2) {
286
// 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
287
int srcRemaining = sl - sp;
288
if (srcRemaining < 4 || dl - dp < 2) {
289
b1 &= 0xff;
290
if (b1 > 0xf4 ||
291
srcRemaining > 1 && isMalformed4_2(b1, sa[sp + 1] & 0xff))
292
return malformedForLength(src, sp, dst, dp, 1);
293
if (srcRemaining > 2 && isMalformed4_3(sa[sp + 2]))
294
return malformedForLength(src, sp, dst, dp, 2);
295
return xflow(src, sp, sl, dst, dp, 4);
296
}
297
int b2 = sa[sp + 1];
298
int b3 = sa[sp + 2];
299
int b4 = sa[sp + 3];
300
int uc = ((b1 << 18) ^
301
(b2 << 12) ^
302
(b3 << 6) ^
303
(b4 ^
304
(((byte) 0xF0 << 18) ^
305
((byte) 0x80 << 12) ^
306
((byte) 0x80 << 6) ^
307
((byte) 0x80 << 0))));
308
if (isMalformed4(b2, b3, b4) ||
309
// shortest form check
310
!Character.isSupplementaryCodePoint(uc)) {
311
return malformed(src, sp, dst, dp, 4);
312
}
313
da[dp++] = Character.highSurrogate(uc);
314
da[dp++] = Character.lowSurrogate(uc);
315
sp += 4;
316
} else
317
return malformed(src, sp, dst, dp, 1);
318
}
319
return xflow(src, sp, sl, dst, dp, 0);
320
}
321
322
private CoderResult decodeBufferLoop(ByteBuffer src,
323
CharBuffer dst)
324
{
325
int mark = src.position();
326
int limit = src.limit();
327
while (mark < limit) {
328
int b1 = src.get();
329
if (b1 >= 0) {
330
// 1 byte, 7 bits: 0xxxxxxx
331
if (dst.remaining() < 1)
332
return xflow(src, mark, 1); // overflow
333
dst.put((char) b1);
334
mark++;
335
} else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) {
336
// 2 bytes, 11 bits: 110xxxxx 10xxxxxx
337
if (limit - mark < 2|| dst.remaining() < 1)
338
return xflow(src, mark, 2);
339
int b2 = src.get();
340
if (isNotContinuation(b2))
341
return malformedForLength(src, mark, 1);
342
dst.put((char) (((b1 << 6) ^ b2)
343
^
344
(((byte) 0xC0 << 6) ^
345
((byte) 0x80 << 0))));
346
mark += 2;
347
} else if ((b1 >> 4) == -2) {
348
// 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx
349
int srcRemaining = limit - mark;
350
if (srcRemaining < 3 || dst.remaining() < 1) {
351
if (srcRemaining > 1 && isMalformed3_2(b1, src.get()))
352
return malformedForLength(src, mark, 1);
353
return xflow(src, mark, 3);
354
}
355
int b2 = src.get();
356
int b3 = src.get();
357
if (isMalformed3(b1, b2, b3))
358
return malformed(src, mark, 3);
359
char c = (char)
360
((b1 << 12) ^
361
(b2 << 6) ^
362
(b3 ^
363
(((byte) 0xE0 << 12) ^
364
((byte) 0x80 << 6) ^
365
((byte) 0x80 << 0))));
366
if (Character.isSurrogate(c))
367
return malformedForLength(src, mark, 3);
368
dst.put(c);
369
mark += 3;
370
} else if ((b1 >> 3) == -2) {
371
// 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
372
int srcRemaining = limit - mark;
373
if (srcRemaining < 4 || dst.remaining() < 2) {
374
b1 &= 0xff;
375
if (b1 > 0xf4 ||
376
srcRemaining > 1 && isMalformed4_2(b1, src.get() & 0xff))
377
return malformedForLength(src, mark, 1);
378
if (srcRemaining > 2 && isMalformed4_3(src.get()))
379
return malformedForLength(src, mark, 2);
380
return xflow(src, mark, 4);
381
}
382
int b2 = src.get();
383
int b3 = src.get();
384
int b4 = src.get();
385
int uc = ((b1 << 18) ^
386
(b2 << 12) ^
387
(b3 << 6) ^
388
(b4 ^
389
(((byte) 0xF0 << 18) ^
390
((byte) 0x80 << 12) ^
391
((byte) 0x80 << 6) ^
392
((byte) 0x80 << 0))));
393
if (isMalformed4(b2, b3, b4) ||
394
// shortest form check
395
!Character.isSupplementaryCodePoint(uc)) {
396
return malformed(src, mark, 4);
397
}
398
dst.put(Character.highSurrogate(uc));
399
dst.put(Character.lowSurrogate(uc));
400
mark += 4;
401
} else {
402
return malformed(src, mark, 1);
403
}
404
}
405
return xflow(src, mark, 0);
406
}
407
408
protected CoderResult decodeLoop(ByteBuffer src,
409
CharBuffer dst)
410
{
411
if (src.hasArray() && dst.hasArray())
412
return decodeArrayLoop(src, dst);
413
else
414
return decodeBufferLoop(src, dst);
415
}
416
}
417
418
private static final class Encoder extends CharsetEncoder {
419
420
private Encoder(Charset cs) {
421
super(cs, 1.1f, 3.0f);
422
}
423
424
public boolean canEncode(char c) {
425
return !Character.isSurrogate(c);
426
}
427
428
public boolean isLegalReplacement(byte[] repl) {
429
return ((repl.length == 1 && repl[0] >= 0) ||
430
super.isLegalReplacement(repl));
431
}
432
433
private static CoderResult overflow(CharBuffer src, int sp,
434
ByteBuffer dst, int dp) {
435
updatePositions(src, sp, dst, dp);
436
return CoderResult.OVERFLOW;
437
}
438
439
private static CoderResult overflow(CharBuffer src, int mark) {
440
src.position(mark);
441
return CoderResult.OVERFLOW;
442
}
443
444
private Surrogate.Parser sgp;
445
private CoderResult encodeArrayLoop(CharBuffer src,
446
ByteBuffer dst)
447
{
448
char[] sa = src.array();
449
int sp = src.arrayOffset() + src.position();
450
int sl = src.arrayOffset() + src.limit();
451
452
byte[] da = dst.array();
453
int dp = dst.arrayOffset() + dst.position();
454
int dl = dst.arrayOffset() + dst.limit();
455
int dlASCII = dp + Math.min(sl - sp, dl - dp);
456
457
// ASCII only loop
458
while (dp < dlASCII && sa[sp] < '\u0080')
459
da[dp++] = (byte) sa[sp++];
460
while (sp < sl) {
461
char c = sa[sp];
462
if (c < 0x80) {
463
// Have at most seven bits
464
if (dp >= dl)
465
return overflow(src, sp, dst, dp);
466
da[dp++] = (byte)c;
467
} else if (c < 0x800) {
468
// 2 bytes, 11 bits
469
if (dl - dp < 2)
470
return overflow(src, sp, dst, dp);
471
da[dp++] = (byte)(0xc0 | (c >> 6));
472
da[dp++] = (byte)(0x80 | (c & 0x3f));
473
} else if (Character.isSurrogate(c)) {
474
// Have a surrogate pair
475
if (sgp == null)
476
sgp = new Surrogate.Parser();
477
int uc = sgp.parse(c, sa, sp, sl);
478
if (uc < 0) {
479
updatePositions(src, sp, dst, dp);
480
return sgp.error();
481
}
482
if (dl - dp < 4)
483
return overflow(src, sp, dst, dp);
484
da[dp++] = (byte)(0xf0 | ((uc >> 18)));
485
da[dp++] = (byte)(0x80 | ((uc >> 12) & 0x3f));
486
da[dp++] = (byte)(0x80 | ((uc >> 6) & 0x3f));
487
da[dp++] = (byte)(0x80 | (uc & 0x3f));
488
sp++; // 2 chars
489
} else {
490
// 3 bytes, 16 bits
491
if (dl - dp < 3)
492
return overflow(src, sp, dst, dp);
493
da[dp++] = (byte)(0xe0 | ((c >> 12)));
494
da[dp++] = (byte)(0x80 | ((c >> 6) & 0x3f));
495
da[dp++] = (byte)(0x80 | (c & 0x3f));
496
}
497
sp++;
498
}
499
updatePositions(src, sp, dst, dp);
500
return CoderResult.UNDERFLOW;
501
}
502
503
private CoderResult encodeBufferLoop(CharBuffer src,
504
ByteBuffer dst)
505
{
506
int mark = src.position();
507
while (src.hasRemaining()) {
508
char c = src.get();
509
if (c < 0x80) {
510
// Have at most seven bits
511
if (!dst.hasRemaining())
512
return overflow(src, mark);
513
dst.put((byte)c);
514
} else if (c < 0x800) {
515
// 2 bytes, 11 bits
516
if (dst.remaining() < 2)
517
return overflow(src, mark);
518
dst.put((byte)(0xc0 | (c >> 6)));
519
dst.put((byte)(0x80 | (c & 0x3f)));
520
} else if (Character.isSurrogate(c)) {
521
// Have a surrogate pair
522
if (sgp == null)
523
sgp = new Surrogate.Parser();
524
int uc = sgp.parse(c, src);
525
if (uc < 0) {
526
src.position(mark);
527
return sgp.error();
528
}
529
if (dst.remaining() < 4)
530
return overflow(src, mark);
531
dst.put((byte)(0xf0 | ((uc >> 18))));
532
dst.put((byte)(0x80 | ((uc >> 12) & 0x3f)));
533
dst.put((byte)(0x80 | ((uc >> 6) & 0x3f)));
534
dst.put((byte)(0x80 | (uc & 0x3f)));
535
mark++; // 2 chars
536
} else {
537
// 3 bytes, 16 bits
538
if (dst.remaining() < 3)
539
return overflow(src, mark);
540
dst.put((byte)(0xe0 | ((c >> 12))));
541
dst.put((byte)(0x80 | ((c >> 6) & 0x3f)));
542
dst.put((byte)(0x80 | (c & 0x3f)));
543
}
544
mark++;
545
}
546
src.position(mark);
547
return CoderResult.UNDERFLOW;
548
}
549
550
protected final CoderResult encodeLoop(CharBuffer src,
551
ByteBuffer dst)
552
{
553
if (src.hasArray() && dst.hasArray())
554
return encodeArrayLoop(src, dst);
555
else
556
return encodeBufferLoop(src, dst);
557
}
558
559
}
560
}
561
562