From c021a6305fabb7861712842ddca5534eb9a8ba89 Mon Sep 17 00:00:00 2001 From: Ito Kazumitsu Date: Sun, 13 Nov 2005 22:18:23 +0000 Subject: 2005-11-13 Ito Kazumitsu Fixes bug #23008 * gnu/java/nio/charset/UTF_16Decoder.java MAYBE_BIG_ENDIAN, MAYBE_LITTLE_ENDIAN: New constants representing such endianness which is similar to UNKNOWN_ENDIAN but defaults to big/little endian without a byte order mark. (decodeLoop): Handle MAYBE_BIG_ENDIAN and MAYBE_LITTLE_ENDIAN. * gnu/java/nio/charset/UnicodeLittle.java (newDecoder): Set the endianness to MAYBE_LITTLE_ENDIAN. --- gnu/java/nio/charset/UTF_16Decoder.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'gnu/java/nio/charset/UTF_16Decoder.java') diff --git a/gnu/java/nio/charset/UTF_16Decoder.java b/gnu/java/nio/charset/UTF_16Decoder.java index e3927d994..fa1dbc412 100644 --- a/gnu/java/nio/charset/UTF_16Decoder.java +++ b/gnu/java/nio/charset/UTF_16Decoder.java @@ -54,6 +54,8 @@ final class UTF_16Decoder extends CharsetDecoder static final int BIG_ENDIAN = 0; static final int LITTLE_ENDIAN = 1; static final int UNKNOWN_ENDIAN = 2; + static final int MAYBE_BIG_ENDIAN = 3; + static final int MAYBE_LITTLE_ENDIAN = 4; private static final char BYTE_ORDER_MARK = 0xFEFF; private static final char REVERSED_BYTE_ORDER_MARK = 0xFFFE; @@ -81,26 +83,37 @@ final class UTF_16Decoder extends CharsetDecoder byte b2 = in.get (); // handle byte order mark - if (byteOrder == UNKNOWN_ENDIAN) + if (byteOrder == UNKNOWN_ENDIAN || + byteOrder == MAYBE_BIG_ENDIAN || + byteOrder == MAYBE_LITTLE_ENDIAN) { char c = (char) (((b1 & 0xFF) << 8) | (b2 & 0xFF)); if (c == BYTE_ORDER_MARK) { + if (byteOrder == MAYBE_LITTLE_ENDIAN) + { + return CoderResult.malformedForLength (2); + } byteOrder = BIG_ENDIAN; inPos += 2; continue; } else if (c == REVERSED_BYTE_ORDER_MARK) { + if (byteOrder == MAYBE_BIG_ENDIAN) + { + return CoderResult.malformedForLength (2); + } byteOrder = LITTLE_ENDIAN; inPos += 2; continue; } else { - // assume big endian, do not consume bytes, + // assume big or little endian, do not consume bytes, // continue with normal processing - byteOrder = BIG_ENDIAN; + byteOrder = (byteOrder == MAYBE_LITTLE_ENDIAN ? + LITTLE_ENDIAN : BIG_ENDIAN); } } -- cgit v1.2.1