History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/misc/UCDecoder.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/misc/UCDecoder.java+233−0
@@ -0,0 +1,233 @@
1+/*
2+ * Copyright (c) 1995, 2000, 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+package sun.misc;
26+
27+import java.io.OutputStream;
28+import java.io.ByteArrayOutputStream;
29+import java.io.PushbackInputStream;
30+import java.io.PrintStream;
31+import java.io.IOException;
32+
33+/**
34+ * This class implements a robust character decoder. The decoder will
35+ * converted encoded text into binary data.
36+ *
37+ * The basic encoding unit is a 3 character atom. It encodes two bytes
38+ * of data. Bytes are encoded into a 64 character set, the characters
39+ * were chosen specifically because they appear in all codesets.
40+ * We don't care what their numerical equivalent is because
41+ * we use a character array to map them. This is like UUencoding
42+ * with the dependency on ASCII removed.
43+ *
44+ * The three chars that make up an atom are encoded as follows:
45+ * <pre>
46+ * 00xxxyyy 00axxxxx 00byyyyy
47+ * 00 = leading zeros, all values are 0 - 63
48+ * xxxyyy - Top 3 bits of X, Top 3 bits of Y
49+ * axxxxx - a = X parity bit, xxxxx lower 5 bits of X
50+ * byyyyy - b = Y parity bit, yyyyy lower 5 bits of Y
51+ * </pre>
52+ *
53+ * The atoms are arranged into lines suitable for inclusion into an
54+ * email message or text file. The number of bytes that are encoded
55+ * per line is 48 which keeps the total line length under 80 chars)
56+ *
57+ * Each line has the form(
58+ * <pre>
59+ * *(LLSS)(DDDD)(DDDD)(DDDD)...(CRC)
60+ * Where each (xxx) represents a three character atom.
61+ * (LLSS) - 8 bit length (high byte), and sequence number
62+ * modulo 256;
63+ * (DDDD) - Data byte atoms, if length is odd, last data
64+ * atom has (DD00) (high byte data, low byte 0)
65+ * (CRC) - 16 bit CRC for the line, includes length,
66+ * sequence, and all data bytes. If there is a
67+ * zero pad byte (odd length) it is _NOT_
68+ * included in the CRC.
69+ * </pre>
70+ *
71+ * If an error is encountered during decoding this class throws a
72+ * CEFormatException. The specific detail messages are:
73+ *
74+ * <pre>
75+ * "UCDecoder: High byte parity error."
76+ * "UCDecoder: Low byte parity error."
77+ * "UCDecoder: Out of sequence line."
78+ * "UCDecoder: CRC check failed."
79+ * </pre>
80+ *
81+ * @author Chuck McManis
82+ * @see CharacterEncoder
83+ * @see UCEncoder
84+ */
85+public class UCDecoder extends CharacterDecoder {
86+
87+ /** This class encodes two bytes per atom. */
88+ protected int bytesPerAtom() {
89+ return (2);
90+ }
91+
92+ /** this class encodes 48 bytes per line */
93+ protected int bytesPerLine() {
94+ return (48);
95+ }
96+
97+ /* this is the UCE mapping of 0-63 to characters .. */
98+ private final static byte map_array[] = {
99+ // 0 1 2 3 4 5 6 7
100+ (byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7', // 0
101+ (byte)'8',(byte)'9',(byte)'A',(byte)'B',(byte)'C',(byte)'D',(byte)'E',(byte)'F', // 1
102+ (byte)'G',(byte)'H',(byte)'I',(byte)'J',(byte)'K',(byte)'L',(byte)'M',(byte)'N', // 2
103+ (byte)'O',(byte)'P',(byte)'Q',(byte)'R',(byte)'S',(byte)'T',(byte)'U',(byte)'V', // 3
104+ (byte)'W',(byte)'X',(byte)'Y',(byte)'Z',(byte)'a',(byte)'b',(byte)'c',(byte)'d', // 4
105+ (byte)'e',(byte)'f',(byte)'g',(byte)'h',(byte)'i',(byte)'j',(byte)'k',(byte)'l', // 5
106+ (byte)'m',(byte)'n',(byte)'o',(byte)'p',(byte)'q',(byte)'r',(byte)'s',(byte)'t', // 6
107+ (byte)'u',(byte)'v',(byte)'w',(byte)'x',(byte)'y',(byte)'z',(byte)'(',(byte)')' // 7
108+ };
109+
110+ private int sequence;
111+ private byte tmp[] = new byte[2];
112+ private CRC16 crc = new CRC16();
113+
114+ /**
115+ * Decode one atom - reads the characters from the input stream, decodes
116+ * them, and checks for valid parity.
117+ */
118+ protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l) throws IOException {
119+ int i, p1, p2, np1, np2;
120+ byte a = -1, b = -1, c = -1;
121+ byte high_byte, low_byte;
122+ byte tmp[] = new byte[3];
123+
124+ i = inStream.read(tmp);
125+ if (i != 3) {
126+ throw new CEStreamExhausted();
127+ }
128+ for (i = 0; (i < 64) && ((a == -1) || (b == -1) || (c == -1)); i++) {
129+ if (tmp[0] == map_array[i]) {
130+ a = (byte) i;
131+ }
132+ if (tmp[1] == map_array[i]) {
133+ b = (byte) i;
134+ }
135+ if (tmp[2] == map_array[i]) {
136+ c = (byte) i;
137+ }
138+ }
139+ high_byte = (byte) (((a & 0x38) << 2) + (b & 0x1f));
140+ low_byte = (byte) (((a & 0x7) << 5) + (c & 0x1f));
141+ p1 = 0;
142+ p2 = 0;
143+ for (i = 1; i < 256; i = i * 2) {
144+ if ((high_byte & i) != 0)
145+ p1++;
146+ if ((low_byte & i) != 0)
147+ p2++;
148+ }
149+ np1 = (b & 32) / 32;
150+ np2 = (c & 32) / 32;
151+ if ((p1 & 1) != np1) {
152+ throw new CEFormatException("UCDecoder: High byte parity error.");
153+ }
154+ if ((p2 & 1) != np2) {
155+ throw new CEFormatException("UCDecoder: Low byte parity error.");
156+ }
157+ outStream.write(high_byte);
158+ crc.update(high_byte);
159+ if (l == 2) {
160+ outStream.write(low_byte);
161+ crc.update(low_byte);
162+ }
163+ }
164+
165+ private ByteArrayOutputStream lineAndSeq = new ByteArrayOutputStream(2);
166+
167+ /**
168+ * decodeBufferPrefix initializes the sequence number to zero.
169+ */
170+ protected void decodeBufferPrefix(PushbackInputStream inStream, OutputStream outStream) {
171+ sequence = 0;
172+ }
173+
174+ /**
175+ * decodeLinePrefix reads the sequence number and the number of
176+ * encoded bytes from the line. If the sequence number is not the
177+ * previous sequence number + 1 then an exception is thrown.
178+ * UCE lines are line terminator immune, they all start with *
179+ * so the other thing this method does is scan for the next line
180+ * by looking for the * character.
181+ *
182+ * @exception CEFormatException out of sequence lines detected.
183+ */
184+ protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
185+ int i;
186+ int nLen, nSeq;
187+ byte xtmp[];
188+ int c;
189+
190+ crc.value = 0;
191+ while (true) {
192+ c = inStream.read(tmp, 0, 1);
193+ if (c == -1) {
194+ throw new CEStreamExhausted();
195+ }
196+ if (tmp[0] == '*') {
197+ break;
198+ }
199+ }
200+ lineAndSeq.reset();
201+ decodeAtom(inStream, lineAndSeq, 2);
202+ xtmp = lineAndSeq.toByteArray();
203+ nLen = xtmp[0] & 0xff;
204+ nSeq = xtmp[1] & 0xff;
205+ if (nSeq != sequence) {
206+ throw new CEFormatException("UCDecoder: Out of sequence line.");
207+ }
208+ sequence = (sequence + 1) & 0xff;
209+ return (nLen);
210+ }
211+
212+
213+ /**
214+ * this method reads the CRC that is at the end of every line and
215+ * verifies that it matches the computed CRC.
216+ *
217+ * @exception CEFormatException if CRC check fails.
218+ */
219+ protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
220+ int i;
221+ int lineCRC = crc.value;
222+ int readCRC;
223+ byte tmp[];
224+
225+ lineAndSeq.reset();
226+ decodeAtom(inStream, lineAndSeq, 2);
227+ tmp = lineAndSeq.toByteArray();
228+ readCRC = ((tmp[0] << 8) & 0xFF00) + (tmp[1] & 0xff);
229+ if (readCRC != lineCRC) {
230+ throw new CEFormatException("UCDecoder: CRC check failed.");
231+ }
232+ }
233+}