aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/sun/io/ByteToCharISO2022.java
blob: 5fef812fd21d884cfe58ee2921d1025ada44203d (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
/*
 * Copyright (c) 1997, 2001, 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.io;

/**
 * An algorithmic conversion from ISO 2022 to Unicode
 *
 * @author Tom Zhou
 */
public abstract class ByteToCharISO2022 extends ByteToCharConverter
{
    // Value to be filled by subclass
    protected String SODesignator[];
    protected String SS2Designator[] = null;
    protected String SS3Designator[] = null;

    protected ByteToCharConverter SOConverter[];
    protected ByteToCharConverter SS2Converter[] = null;
    protected ByteToCharConverter SS3Converter[] = null;

    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 maximumDesignatorLength = 3;

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

    private ByteToCharConverter tmpConverter[];

    private int curSODes, curSS2Des, curSS3Des;
    private boolean shiftout;

    private byte remainByte[] = new byte[10];
    private int remainIndex = -1;
    private byte state, firstByte;

    public void reset()
    {
        int i = 0;

        shiftout = false;
        state = G0;
        firstByte = 0;

        curSODes = 0;
        curSS2Des = 0;
        curSS3Des = 0;

        charOff = byteOff = 0;
        remainIndex = -1;

        for(i = 0; i < remainByte.length; i++)
            remainByte[i] = 0;
    }

    public int flush(char[] output, int outStart, int outEnd)
        throws MalformedInputException
    {
        int i;
        if (state != G0) {
            badInputLength = 0;
            throw new MalformedInputException();
        }
        reset();
        return 0;
    }

    private byte[] savetyGetSrc(byte[] input, int inOff, int inEnd, int nbytes)
    {
        int i;
        byte tmp[];

        if(inOff <= (inEnd-nbytes+1))
            tmp = new byte[nbytes];
        else
            tmp = new byte[inEnd-inOff];

        for(i = 0; i < tmp.length; i++)
            tmp[i] = input[inOff+i];
        return tmp;
    }

    private char getUnicode(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;
            tmpConverter = SOConverter;
            break;
        case SS2Flag:
            tmpIndex = curSS2Des;
            tmpConverter = SS2Converter;
            break;
        case SS3Flag:
            tmpIndex = curSS3Des;
            tmpConverter = SS3Converter;
            break;
        }

        for(i = 0; i < tmpConverter.length; i++) {
            if(tmpIndex == i) {
                try {
                    tmpConverter[i].convert(tmpByte, 0, 2, tmpChar, 0, 1);
                } catch (Exception e) {}
                return tmpChar[0];
            }
        }
        return REPLACE_CHAR;
    }

    public final int convert(byte[] input, int inOff, int inEnd,
                             char[] output, int outOff, int outEnd)
                             throws ConversionBufferFullException,
                                    MalformedInputException
    {
        int i;
        int DesignatorLength = 0;
        charOff  =  outOff;
        byteOff  =  inOff;

        // Loop until we hit the end of the input
        while (byteOff < inEnd) {
            // If we don't have room for the output, throw an exception
            if (charOff >= outEnd)
                throw new ConversionBufferFullException();
            if(remainIndex < 0) {
                remainByte[0] = input[byteOff];
                remainIndex = 0;
                byteOff++;
            }
            switch (remainByte[0]) {
            case ISO_SO:
                shiftout = true;
                if(remainIndex > 0)
                    System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
                remainIndex--;
                break;
            case ISO_SI:
                shiftout = false;
                if(remainIndex > 0)
                    System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
                remainIndex--;
                break;
             case ISO_ESC:
                byte tmp[] = savetyGetSrc(input, byteOff, inEnd,
                               (maximumDesignatorLength-remainIndex));
                System.arraycopy(tmp, 0, remainByte, remainIndex+1, tmp.length);
                remainIndex += tmp.length;
                byteOff += tmp.length;
                if(tmp.length<(maximumDesignatorLength-remainIndex))
                    break;
                String tmpString = new String(remainByte, 1, remainIndex);
                for (i = 0; i < SODesignator.length; i++) {
                    if(tmpString.indexOf(SODesignator[i]) == 0) {
                        curSODes = i;
                        DesignatorLength = SODesignator[i].length();
                        break;
                    }
                }

                if (DesignatorLength == 0 ) { // Designator not recognized
                   badInputLength = tmp.length;
                   throw new MalformedInputException();
                }

                if (i == SODesignator.length) {
                    for (i = 0; i < SS2Designator.length; i++) {
                        if(tmpString.indexOf(SS2Designator[i]) == 0) {
                            curSS2Des = i;
                            DesignatorLength = SS2Designator[i].length();
                            break;
                        }
                    }
                    if(i == SS2Designator.length) {
                        for(i = 0; i < SS3Designator.length; i++) {
                            if (tmpString.indexOf(SS3Designator[i]) == 0) {
                                curSS3Des = i;
                                DesignatorLength = SS3Designator[i].length();
                                break;
                            }
                        }
                        if (i == SS3Designator.length) {
                            switch(remainByte[1]) {
                            case ISO_SS2_7:
                                output[charOff] = getUnicode(remainByte[2],
                                                          remainByte[3],
                                                          SS2Flag);
                                charOff++;
                                DesignatorLength = 3;
                                break;
                            case ISO_SS3_7:
                                output[charOff] = getUnicode(remainByte[2],
                                                          remainByte[3],
                                                          SS3Flag);
                                charOff++;
                                DesignatorLength = 3;
                                break;
                            default:
                                DesignatorLength = 0;
                            }
                        }
                    }
                }
                if (remainIndex > DesignatorLength) {
                    for(i = 0; i < remainIndex-DesignatorLength; i++)
                        remainByte[i] = remainByte[DesignatorLength+1+i];
                    remainIndex = i-1;
                } else {
                    remainIndex = -1;
                }
                break;
            default:
                if (!shiftout) {
                    output[charOff] = (char)remainByte[0];
                    charOff++;
                } else {
                    switch (state) {
                    case G0:
                        firstByte = remainByte[0];
                        state = G1;
                        break;
                    case G1:
                        output[charOff] = getUnicode(firstByte, remainByte[0],
                                                  SOFlag);
                        charOff++;
                        state = G0;
                        break;
                    }
                }
                if (remainIndex > 0)
                    System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
                remainIndex--;
            }
        }
        return charOff - outOff;
    }
}