1245N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
1245N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1245N/A *
1245N/A * This code is free software; you can redistribute it and/or modify it
1245N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1245N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1245N/A *
1245N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1245N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1245N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1245N/A * version 2 for more details (a copy is included in the LICENSE file that
1245N/A * accompanied this code).
1245N/A *
1245N/A * You should have received a copy of the GNU General Public License version
1245N/A * 2 along with this work; if not, write to the Free Software Foundation,
1245N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1245N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1245N/A */
1245N/A
1245N/A/*
1245N/A */
1245N/A
1245N/A/**
1245N/A * Simple EUC-like decoder used by IBM01383 and IBM970
1245N/A * supports G1 - no support for G2 or G3
1245N/A */
1245N/A
1245N/A
1245N/Aimport java.nio.ByteBuffer;
1245N/Aimport java.nio.CharBuffer;
1245N/Aimport java.nio.charset.Charset;
1245N/Aimport java.nio.charset.CharsetDecoder;
1245N/Aimport java.nio.charset.CoderResult;
1245N/A
1245N/Aabstract class SimpleEUCDecoder
1245N/A extends CharsetDecoder
1245N/A{
1245N/A private final int SS2 = 0x8E;
1245N/A private final int SS3 = 0x8F;
1245N/A
1245N/A protected static String mappingTableG1;
1245N/A protected static String byteToCharTable;
1245N/A
1245N/A protected SimpleEUCDecoder(Charset cs) {
1245N/A super(cs, 0.5f, 1.0f);
1245N/A }
1245N/A
1245N/A private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
1245N/A byte[] sa = src.array();
1245N/A int sp = src.arrayOffset() + src.position();
1245N/A int sl = src.arrayOffset() + src.limit();
1245N/A assert (sp <= sl);
1245N/A sp = (sp <= sl ? sp : sl);
1245N/A char[] da = dst.array();
1245N/A int dp = dst.arrayOffset() + dst.position();
1245N/A int dl = dst.arrayOffset() + dst.limit();
1245N/A assert (dp <= dl);
1245N/A dp = (dp <= dl ? dp : dl);
1245N/A
1245N/A try {
1245N/A while (sp < sl) {
1245N/A int byte1, byte2;
1245N/A int inputSize = 1;
1245N/A char outputChar = '\uFFFD';
1245N/A
1245N/A byte1 = sa[sp] & 0xff;
1245N/A
1245N/A if ( byte1 <= 0x9f ) { // < 0x9f has its own table (G0)
1245N/A if (byte1 == SS2 || byte1 == SS3 ) {
1245N/A // No support provided for G2/G3 at this time.
1245N/A return CoderResult.malformedForLength(1);
1245N/A }
1245N/A outputChar = byteToCharTable.charAt(byte1);
1245N/A } else if (byte1 < 0xa1 || byte1 > 0xfe) { // invalid range?
1245N/A return CoderResult.malformedForLength(1);
1245N/A } else { // (G1)
1245N/A if (sl - sp < 2) {
1245N/A return CoderResult.UNDERFLOW;
1245N/A }
1245N/A byte2 = sa[sp + 1] & 0xff;
1245N/A inputSize++;
1245N/A if ( byte2 < 0xa1 || byte2 > 0xfe) {
1245N/A return CoderResult.malformedForLength(2);
1245N/A }
1245N/A outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);
1245N/A }
1245N/A if (outputChar == '\uFFFD') {
1245N/A return CoderResult.unmappableForLength(inputSize);
1245N/A }
1245N/A if (dl - dp < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A da[dp++] = outputChar;
1245N/A sp += inputSize;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(sp - src.arrayOffset());
1245N/A dst.position(dp - dst.arrayOffset());
1245N/A }
1245N/A }
1245N/A
1245N/A private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
1245N/A int mark = src.position();
1245N/A
1245N/A try {
1245N/A while (src.hasRemaining()) {
1245N/A char outputChar = '\uFFFD';
1245N/A int inputSize = 1;
1245N/A int byte1, byte2;
1245N/A
1245N/A byte1 = src.get() & 0xff;
1245N/A if ( byte1 <= 0x9f ) {
1245N/A if (byte1 == SS2 || byte1 == SS3 ) {
1245N/A return CoderResult.malformedForLength(1);
1245N/A }
1245N/A outputChar = byteToCharTable.charAt(byte1);
1245N/A } else if (byte1 < 0xa1 || byte1 > 0xfe) {
1245N/A return CoderResult.malformedForLength(1);
1245N/A } else {
1245N/A if (!src.hasRemaining()) {
1245N/A return CoderResult.UNDERFLOW;
1245N/A }
1245N/A byte2 = src.get() & 0xff;
1245N/A inputSize++;
1245N/A if ( byte2 < 0xa1 || byte2 > 0xfe) {
1245N/A return CoderResult.malformedForLength(2);
1245N/A }
1245N/A outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);
1245N/A }
1245N/A if (outputChar == '\uFFFD') {
1245N/A return CoderResult.unmappableForLength(inputSize);
1245N/A }
1245N/A if (!dst.hasRemaining())
1245N/A return CoderResult.OVERFLOW;
1245N/A dst.put(outputChar);
1245N/A mark += inputSize;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(mark);
1245N/A }
1245N/A }
1245N/A
1245N/A protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
1245N/A if (src.hasArray() && dst.hasArray())
1245N/A return decodeArrayLoop(src, dst);
1245N/A else
1245N/A return decodeBufferLoop(src, dst);
1245N/A }
1245N/A}