summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/netscape/security/util/ByteToCharUnicode.java
blob: 312b8a22b759581df47f003dc40ab998aa3974b6 (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
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program 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 for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package netscape.security.util;
import sun.io.ByteToCharUnicodeBig;
import sun.io.ByteToCharUnicodeLittle;
import sun.io.ConversionBufferFullException;
import sun.io.MalformedInputException;

/**
 * Convert byte arrays containing Unicode characters into arrays of actual
 * Unicode characters, sensing the byte order automatically.  To force a
 * particular byte order, use either the "UnicodeBig" or the "UnicodeLittle"
 * encoding.
 *
 * If the first character is a byte order mark, it will be interpreted and
 * discarded. Otherwise, the byte order is assumed to be BigEndian.
 * Either way, the byte order is decided by the first character. Later
 * byte order marks will be passed through as characters (if they indicate
 * the same byte order) or will cause an error (if they indicate the other
 * byte order).
 *
 * @see ByteToCharUnicodeLittle
 * @see ByteToCharUnicodeBig
 *
 * @version 	1.3, 96/11/23
 * @author	Mark Reinhold
 */

public class ByteToCharUnicode extends sun.io.ByteToCharConverter {

    static final char BYTE_ORDER_MARK = (char) 0xfeff;
    static final char REVERSED_MARK = (char) 0xfffe;

    static final int AUTO = 0;
    static final int BIG = 1;
    static final int LITTLE = 2;

    int byteOrder;

    public ByteToCharUnicode() {
	byteOrder = AUTO;
    }

    public String getCharacterEncoding() {
	switch (byteOrder) {
	case BIG:     return "UnicodeBig";
	case LITTLE:  return "UnicodeLittle";
	default:      return "Unicode";
	}
    }

    boolean started = false;
    int leftOverByte;
    boolean leftOver = false;

    public int convert(byte[] in, int inOff, int inEnd,
		       char[] out, int outOff, int outEnd)
	throws ConversionBufferFullException, MalformedInputException
    {
	byteOff = inOff;
	charOff = outOff;

	if (inOff >= inEnd)
	    return 0;

	int b1, b2;
	int bc = 0;
	int inI = inOff, outI = outOff;

	if (leftOver) {
	    b1 = leftOverByte & 0xff;
	    leftOver = false;
	}
	else
	    b1 = in[inI++] & 0xff;
	bc = 1;

	if (!started) {		/* Read possible initial byte-order mark */
	    if (inI < inEnd) {
		b2 = in[inI++] & 0xff;
		bc = 2;

		char c = (char) ((b1 << 8) | b2);
		int bo = AUTO;

		if (c == BYTE_ORDER_MARK)
		    bo = BIG;
		else if (c == REVERSED_MARK)
		    bo = LITTLE;

		if (byteOrder == AUTO) {
		    if (bo == AUTO) {
                        bo = BIG; // BigEndian by default
		    }
		    byteOrder = bo;
		    if (inI < inEnd) {
			b1 = in[inI++] & 0xff;
			bc = 1;
		    }
		}
		else if (bo == AUTO) {
		    inI--;
		    bc = 1;
		}
		else if (byteOrder == bo) {
		    if (inI < inEnd) {
			b1 = in[inI++] & 0xff;
			bc = 1;
		    }
		}
		else {
		    badInputLength = bc;
		    throw new
			MalformedInputException("Incorrect byte-order mark");
		}

		started = true;
	    }
	}

	/* Loop invariant: (b1 contains the next input byte) && (bc == 1) */
	while (inI < inEnd) {
	    b2 = in[inI++] & 0xff;
	    bc = 2;

	    char c;
	    if (byteOrder == BIG)
		c = (char) ((b1 << 8) | b2);
	    else
		c = (char) ((b2 << 8) | b1);

	    if (c == REVERSED_MARK)
		throw new
		    MalformedInputException("Reversed byte-order mark");

	    if (outI >= outEnd)
		throw new ConversionBufferFullException();
	    out[outI++] = c;
	    byteOff = inI;
	    charOff = outI;

	    if (inI < inEnd) {
		b1 = in[inI++] & 0xff;
		bc = 1;
	    }
	}

	if (bc == 1) {
	    leftOverByte = b1;
	    leftOver = true;
	}

	return outI - outOff;
    }

    public void reset() {
	leftOver = false;
	byteOff = charOff = 0;
    }

    public int flush(char buf[], int off, int len)
	throws MalformedInputException
    {
	if (leftOver) {
	    reset();
	    throw new MalformedInputException();
	}
	byteOff = charOff = 0;
	return 0;
    }

}