aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/sun/nio/cs/ext/ISO2022.java
blob: ad4e5978b12beb30029d727d399842e659ec7d21 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
 * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 */

package sun.nio.cs.ext;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import sun.nio.cs.Surrogate;

abstract class ISO2022
    extends Charset
{

    private static final byte ISO_ESC = 0x1b;
    private static final byte ISO_SI = 0x0f;
    private static final byte ISO_SO = 0x0e;
    private static final byte ISO_SS2_7 = 0x4e;
    private static final byte ISO_SS3_7 = 0x4f;
    private static final byte MSB = (byte)0x80;
    private static final char REPLACE_CHAR = '\uFFFD';
    private static final byte minDesignatorLength = 3;

    public ISO2022(String csname, String[] aliases) {
        super(csname, aliases);
    }

    public CharsetDecoder newDecoder() {
        return new Decoder(this);
    }

    public CharsetEncoder newEncoder() {
        return new Encoder(this);
    }

    protected static class Decoder extends CharsetDecoder {

        // Value to be filled by subclass
        protected byte SODesig[][];
        protected byte SS2Desig[][] = null;
        protected byte SS3Desig[][] = null;

        protected CharsetDecoder SODecoder[];
        protected CharsetDecoder SS2Decoder[] = null;
        protected CharsetDecoder SS3Decoder[] = null;

        private static final byte SOFlag = 0;
        private static final byte SS2Flag = 1;
        private static final byte SS3Flag = 2;

        private int curSODes, curSS2Des, curSS3Des;
        private boolean shiftout;
        private CharsetDecoder tmpDecoder[];

        protected Decoder(Charset cs) {
            super(cs, 1.0f, 1.0f);
        }

        protected void implReset() {
            curSODes = 0;
            curSS2Des = 0;
            curSS3Des = 0;
            shiftout = false;
        }

        private char decode(byte byte1, byte byte2, byte shiftFlag)
        {
            byte1 |= MSB;
            byte2 |= MSB;

            byte[] tmpByte = { byte1,byte2 };
            char[] tmpChar = new char[1];
            int     i = 0,
                    tmpIndex = 0;

            switch(shiftFlag) {
            case SOFlag:
                tmpIndex = curSODes;
                tmpDecoder = SODecoder;
                break;
            case SS2Flag:
                tmpIndex = curSS2Des;
                tmpDecoder = SS2Decoder;
                break;
            case SS3Flag:
                tmpIndex = curSS3Des;
                tmpDecoder = SS3Decoder;
                break;
            }

            if (tmpDecoder != null) {
                for(i = 0; i < tmpDecoder.length; i++) {
                    if(tmpIndex == i) {
                        try {
                            ByteBuffer bb = ByteBuffer.wrap(tmpByte,0,2);
                            CharBuffer cc = CharBuffer.wrap(tmpChar,0,1);
                            tmpDecoder[i].decode(bb, cc, true);
                            cc.flip();
                            return cc.get();
                        } catch (Exception e) {}
                    }
                }
            }
            return REPLACE_CHAR;
        }

        private int findDesig(byte[] in, int sp, int sl, byte[][] desigs) {
            if (desigs == null) return -1;
            int i = 0;
            while (i < desigs.length) {
                if (desigs[i] != null && sl - sp >= desigs[i].length) {
                    int j = 0;
                    while (j < desigs[i].length && in[sp+j] == desigs[i][j]) { j++; }
                    if (j == desigs[i].length)
                        return i;
                }
                i++;
            }
            return -1;
        }

        private int findDesigBuf(ByteBuffer in, byte[][] desigs) {
            if (desigs == null) return -1;
            int i = 0;
            while (i < desigs.length) {
                if (desigs[i] != null && in.remaining() >= desigs[i].length) {
                    int j = 0;
                    in.mark();
                    while (j < desigs[i].length && in.get() == desigs[i][j]) { j++; }
                    if (j == desigs[i].length)
                        return i;
                    in.reset();
                }
                i++;
            }
            return -1;
        }

        private CoderResult decodeArrayLoop(ByteBuffer src,
                                            CharBuffer dst)
        {
            byte[] sa = src.array();
            int sp = src.arrayOffset() + src.position();
            int sl = src.arrayOffset() + src.limit();
            assert (sp <= sl);
            sp = (sp <= sl ? sp : sl);

            char[] da = dst.array();
            int dp = dst.arrayOffset() + dst.position();
            int dl = dst.arrayOffset() + dst.limit();
            assert (dp <= dl);
            dp = (dp <= dl ? dp : dl);

            int b1 = 0, b2 = 0, b3 = 0;

            try {
                while (sp < sl) {
                    b1 = sa[sp] & 0xff;
                    int inputSize = 1;
                    switch (b1) {
                        case ISO_SO:
                            shiftout = true;
                            inputSize = 1;
                            break;
                        case ISO_SI:
                            shiftout = false;
                            inputSize = 1;
                            break;
                        case ISO_ESC:
                            if (sl - sp - 1 < minDesignatorLength)
                                return CoderResult.UNDERFLOW;

                            int desig = findDesig(sa, sp + 1, sl, SODesig);
                            if (desig != -1) {
                                curSODes = desig;
                                inputSize = SODesig[desig].length + 1;
                                break;
                            }
                            desig = findDesig(sa, sp + 1, sl, SS2Desig);
                            if (desig != -1) {
                                curSS2Des = desig;
                                inputSize = SS2Desig[desig].length + 1;
                                break;
                            }
                            desig = findDesig(sa, sp + 1, sl, SS3Desig);
                            if (desig != -1) {
                                curSS3Des = desig;
                                inputSize = SS3Desig[desig].length + 1;
                                break;
                            }
                            if (sl - sp < 2)
                                return CoderResult.UNDERFLOW;
                            b1 = sa[sp + 1];
                            switch(b1) {
                            case ISO_SS2_7:
                                if (sl - sp < 4)
                                    return CoderResult.UNDERFLOW;
                                b2 = sa[sp +2];
                                b3 = sa[sp +3];
                                if (dl - dp <1)
                                    return CoderResult.OVERFLOW;
                                da[dp] = decode((byte)b2,
                                                (byte)b3,
                                                SS2Flag);
                                dp++;
                                inputSize = 4;
                                break;
                            case ISO_SS3_7:
                                if (sl - sp < 4)
                                    return CoderResult.UNDERFLOW;
                                b2 = sa[sp + 2];
                                b3 = sa[sp + 3];
                                if (dl - dp <1)
                                    return CoderResult.OVERFLOW;
                                da[dp] = decode((byte)b2,
                                                (byte)b3,
                                                SS3Flag);
                                dp++;
                                inputSize = 4;
                                break;
                            default:
                                return CoderResult.malformedForLength(2);
                            }
                            break;
                        default:
                            if (dl - dp < 1)
                                return CoderResult.OVERFLOW;
                            if (!shiftout) {
                                da[dp++]=(char)(sa[sp] & 0xff);
                            } else {
                                if (dl - dp < 1)
                                    return CoderResult.OVERFLOW;
                                if (sl - sp < 2)
                                    return CoderResult.UNDERFLOW;
                                b2 = sa[sp+1] & 0xff;
                                da[dp++] = decode((byte)b1,
                                                  (byte)b2,
                                                   SOFlag);
                                inputSize = 2;
                            }
                            break;
                    }
                    sp += inputSize;
                }
                return CoderResult.UNDERFLOW;
            } finally {
                src.position(sp - src.arrayOffset());
                dst.position(dp - dst.arrayOffset());
            }
        }

        private CoderResult decodeBufferLoop(ByteBuffer src,
                                             CharBuffer dst)
        {
            int mark = src.position();
            int b1 = 0, b2 = 0, b3 = 0;

            try {
                while (src.hasRemaining()) {
                    b1 = src.get();
                    int inputSize = 1;
                    switch (b1) {
                        case ISO_SO:
                            shiftout = true;
                            break;
                        case ISO_SI:
                            shiftout = false;
                            break;
                        case ISO_ESC:
                            if (src.remaining() < minDesignatorLength)
                                return CoderResult.UNDERFLOW;

                            int desig = findDesigBuf(src, SODesig);
                            if (desig != -1) {
                                curSODes = desig;
                                inputSize = SODesig[desig].length + 1;
                                break;
                            }
                            desig = findDesigBuf(src, SS2Desig);
                            if (desig != -1) {
                                curSS2Des = desig;
                                inputSize = SS2Desig[desig].length + 1;
                                break;
                            }
                            desig = findDesigBuf(src, SS3Desig);
                            if (desig != -1) {
                                curSS3Des = desig;
                                inputSize = SS3Desig[desig].length + 1;
                                break;
                            }

                            if (src.remaining() < 1)
                                return CoderResult.UNDERFLOW;
                            b1 = src.get();
                            switch(b1) {
                                case ISO_SS2_7:
                                    if (src.remaining() < 2)
                                        return CoderResult.UNDERFLOW;
                                    b2 = src.get();
                                    b3 = src.get();
                                    if (dst.remaining() < 1)
                                        return CoderResult.OVERFLOW;
                                    dst.put(decode((byte)b2,
                                                   (byte)b3,
                                                   SS2Flag));
                                    inputSize = 4;
                                    break;
                                case ISO_SS3_7:
                                    if (src.remaining() < 2)
                                        return CoderResult.UNDERFLOW;
                                    b2 = src.get();
                                    b3 = src.get();
                                    if (dst.remaining() < 1)
                                        return CoderResult.OVERFLOW;
                                    dst.put(decode((byte)b2,
                                                   (byte)b3,
                                                   SS3Flag));
                                    inputSize = 4;
                                    break;
                                default:
                                    return CoderResult.malformedForLength(2);
                            }
                            break;
                        default:
                            if (dst.remaining() < 1)
                                return CoderResult.OVERFLOW;
                            if (!shiftout) {
                                dst.put((char)(b1 & 0xff));
                            } else {
                                if (dst.remaining() < 1)
                                    return CoderResult.OVERFLOW;
                                if (src.remaining() < 1)
                                    return CoderResult.UNDERFLOW;
                                b2 = src.get() & 0xff;
                                dst.put(decode((byte)b1,
                                                      (byte)b2,
                                                        SOFlag));
                                inputSize = 2;
                            }
                            break;
                    }
                    mark += inputSize;
                }
                return CoderResult.UNDERFLOW;
            } catch (Exception e) { e.printStackTrace(); return CoderResult.OVERFLOW; }
            finally {
                src.position(mark);
            }
        }

        protected CoderResult decodeLoop(ByteBuffer src,
                                         CharBuffer dst)
        {
            if (src.hasArray() && dst.hasArray())
                return decodeArrayLoop(src, dst);
            else
                return decodeBufferLoop(src, dst);
        }
    }

    protected static class Encoder extends CharsetEncoder {
        private final Surrogate.Parser sgp = new Surrogate.Parser();
        public static final byte SS2 = (byte)0x8e;
        public static final byte PLANE2 = (byte)0xA2;
        public static final byte PLANE3 = (byte)0xA3;
        private final byte MSB = (byte)0x80;

        protected final byte maximumDesignatorLength = 4;

        protected String SODesig,
                         SS2Desig = null,
                         SS3Desig = null;

        protected CharsetEncoder ISOEncoder;

        private boolean shiftout = false;
        private boolean SODesDefined = false;
        private boolean SS2DesDefined = false;
        private boolean SS3DesDefined = false;

        private boolean newshiftout = false;
        private boolean newSODesDefined = false;
        private boolean newSS2DesDefined = false;
        private boolean newSS3DesDefined = false;

        protected Encoder(Charset cs) {
            super(cs, 4.0f, 8.0f);
        }

        public boolean canEncode(char c) {
            return (ISOEncoder.canEncode(c));
        }

        protected void implReset() {
            shiftout = false;
            SODesDefined = false;
            SS2DesDefined = false;
            SS3DesDefined = false;
        }

        private int unicodeToNative(char unicode, byte ebyte[])
        {
            int index = 0;
            byte        tmpByte[];
            char        convChar[] = {unicode};
            byte        convByte[] = new byte[4];
            int         converted;

            try{
                CharBuffer cc = CharBuffer.wrap(convChar);
                ByteBuffer bb = ByteBuffer.allocate(4);
                ISOEncoder.encode(cc, bb, true);
                bb.flip();
                converted = bb.remaining();
                bb.get(convByte,0,converted);
            } catch(Exception e) {
                return -1;
            }

            if (converted == 2) {
                if (!SODesDefined) {
                    newSODesDefined = true;
                    ebyte[0] = ISO_ESC;
                    tmpByte = SODesig.getBytes();
                    System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
                    index = tmpByte.length+1;
                }
                if (!shiftout) {
                    newshiftout = true;
                    ebyte[index++] = ISO_SO;
                }
                ebyte[index++] = (byte)(convByte[0] & 0x7f);
                ebyte[index++] = (byte)(convByte[1] & 0x7f);
            } else {
                if(convByte[0] == SS2) {
                    if (convByte[1] == PLANE2) {
                        if (!SS2DesDefined) {
                            newSS2DesDefined = true;
                            ebyte[0] = ISO_ESC;
                            tmpByte = SS2Desig.getBytes();
                            System.arraycopy(tmpByte, 0, ebyte, 1, tmpByte.length);
                            index = tmpByte.length+1;
                        }
                        ebyte[index++] = ISO_ESC;
                        ebyte[index++] = ISO_SS2_7;
                        ebyte[index++] = (byte)(convByte[2] & 0x7f);
                        ebyte[index++] = (byte)(convByte[3] & 0x7f);
                    } else if (convByte[1] == PLANE3) {
                        if(!SS3DesDefined){
                            newSS3DesDefined = true;
                            ebyte[0] = ISO_ESC;
                            tmpByte = SS3Desig.getBytes();
                            System.arraycopy(tmpByte, 0, ebyte, 1, tmpByte.length);
                            index = tmpByte.length+1;
                        }
                        ebyte[index++] = ISO_ESC;
                        ebyte[index++] = ISO_SS3_7;
                        ebyte[index++] = (byte)(convByte[2] & 0x7f);
                        ebyte[index++] = (byte)(convByte[3] & 0x7f);
                    }
                }
            }
            return index;
        }

        private CoderResult encodeArrayLoop(CharBuffer src,
                                            ByteBuffer dst)
        {
            char[] sa = src.array();
            int sp = src.arrayOffset() + src.position();
            int sl = src.arrayOffset() + src.limit();
            assert (sp <= sl);
            sp = (sp <= sl ? sp : sl);
            byte[] da = dst.array();
            int dp = dst.arrayOffset() + dst.position();
            int dl = dst.arrayOffset() + dst.limit();
            assert (dp <= dl);
            dp = (dp <= dl ? dp : dl);

            int outputSize = 0;
            byte[]  outputByte = new byte[8];
            newshiftout = shiftout;
            newSODesDefined = SODesDefined;
            newSS2DesDefined = SS2DesDefined;
            newSS3DesDefined = SS3DesDefined;

            try {
                while (sp < sl) {
                    char c = sa[sp];
                    if (Character.isSurrogate(c)) {
                        if (sgp.parse(c, sa, sp, sl) < 0)
                            return sgp.error();
                        return sgp.unmappableResult();
                    }

                    if (c < 0x80) {     // ASCII
                        if (shiftout){
                            newshiftout = false;
                            outputSize = 2;
                            outputByte[0] = ISO_SI;
                            outputByte[1] = (byte)(c & 0x7f);
                        } else {
                            outputSize = 1;
                            outputByte[0] = (byte)(c & 0x7f);
                        }
                        if(sa[sp] == '\n'){
                            newSODesDefined = false;
                            newSS2DesDefined = false;
                            newSS3DesDefined = false;
                        }
                    } else {
                        outputSize = unicodeToNative(c, outputByte);
                        if (outputSize == 0) {
                            return CoderResult.unmappableForLength(1);
                        }
                    }
                    if (dl - dp < outputSize)
                        return CoderResult.OVERFLOW;

                    for (int i = 0; i < outputSize; i++)
                        da[dp++] = outputByte[i];
                    sp++;
                    shiftout = newshiftout;
                    SODesDefined = newSODesDefined;
                    SS2DesDefined = newSS2DesDefined;
                    SS3DesDefined = newSS3DesDefined;
                }
                return CoderResult.UNDERFLOW;
             } finally {
                src.position(sp - src.arrayOffset());
                dst.position(dp - dst.arrayOffset());
             }
        }


        private CoderResult encodeBufferLoop(CharBuffer src,
                                             ByteBuffer dst)
        {
            int outputSize = 0;
            byte[]  outputByte = new byte[8];
            int     inputSize = 0;                 // Size of input
            newshiftout = shiftout;
            newSODesDefined = SODesDefined;
            newSS2DesDefined = SS2DesDefined;
            newSS3DesDefined = SS3DesDefined;
            int mark = src.position();

            try {
                while (src.hasRemaining()) {
                    char inputChar = src.get();
                    if (Character.isSurrogate(inputChar)) {
                        if (sgp.parse(inputChar, src) < 0)
                            return sgp.error();
                        return sgp.unmappableResult();
                    }
                    if (inputChar < 0x80) {     // ASCII
                        if (shiftout){
                            newshiftout = false;
                            outputSize = 2;
                            outputByte[0] = ISO_SI;
                            outputByte[1] = (byte)(inputChar & 0x7f);
                        } else {
                            outputSize = 1;
                            outputByte[0] = (byte)(inputChar & 0x7f);
                        }
                        if(inputChar == '\n'){
                            newSODesDefined = false;
                            newSS2DesDefined = false;
                            newSS3DesDefined = false;
                        }
                    } else {
                        outputSize = unicodeToNative(inputChar, outputByte);
                        if (outputSize == 0) {
                            return CoderResult.unmappableForLength(1);
                        }
                    }

                    if (dst.remaining() < outputSize)
                        return CoderResult.OVERFLOW;
                    for (int i = 0; i < outputSize; i++)
                        dst.put(outputByte[i]);
                    mark++;
                    shiftout = newshiftout;
                    SODesDefined = newSODesDefined;
                    SS2DesDefined = newSS2DesDefined;
                    SS3DesDefined = newSS3DesDefined;
                }
                return CoderResult.UNDERFLOW;
            } finally {
                src.position(mark);
            }
        }

        protected CoderResult encodeLoop(CharBuffer src,
                                         ByteBuffer dst)
        {
            if (src.hasArray() && dst.hasArray())
                return encodeArrayLoop(src, dst);
            else
                return encodeBufferLoop(src, dst);
        }
    }
}