Improvement to Shift_JIS encoding detector to avoid detecting some UTF-8 strings...
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / DecodedBitStreamParser.java
index 8734faf..c3a440b 100644 (file)
@@ -70,19 +70,26 @@ final class DecodedBitStreamParser {
         // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
         mode = Mode.TERMINATOR;
       } else {
-        mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
+        try {
+          mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
+        } catch (IllegalArgumentException iae) {
+          throw ReaderException.getInstance();
+        }
       }
       if (!mode.equals(Mode.TERMINATOR)) {
         if (mode.equals(Mode.FNC1_FIRST_POSITION) || mode.equals(Mode.FNC1_SECOND_POSITION)) {
           // We do little with FNC1 except alter the parsed result a bit according to the spec
           fc1InEffect = true;
+        } else if (mode.equals(Mode.STRUCTURED_APPEND)) {
+          // not really supported; all we do is ignore it
+          // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
+          bits.readBits(16);
         } else if (mode.equals(Mode.ECI)) {
           // Count doesn't apply to ECI
           int value = parseECIValue(bits);
-          try {
-            currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
-          } catch (IllegalArgumentException iae) {
-            // unsupported... just continue?
+          currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
+          if (currentCharacterSetECI == null) {
+            throw ReaderException.getInstance();
           }
         } else {
           // How many characters will follow, encoded in this mode?
@@ -96,7 +103,7 @@ final class DecodedBitStreamParser {
           } else if (mode.equals(Mode.KANJI)) {
             decodeKanjiSegment(bits, result, count);
           } else {
-            throw new ReaderException("Unsupported mode indicator");
+            throw ReaderException.getInstance();
           }
         }
       }
@@ -132,7 +139,7 @@ final class DecodedBitStreamParser {
     try {
       result.append(new String(buffer, SHIFT_JIS));
     } catch (UnsupportedEncodingException uee) {
-      throw new ReaderException(SHIFT_JIS + " encoding is not supported on this device");
+      throw ReaderException.getInstance();
     }
   }
 
@@ -143,7 +150,7 @@ final class DecodedBitStreamParser {
                                         Vector byteSegments) throws ReaderException {
     byte[] readBytes = new byte[count];
     if (count << 3 > bits.available()) {
-      throw new ReaderException("Count too large: " + count);
+      throw ReaderException.getInstance();
     }
     for (int i = 0; i < count; i++) {
       readBytes[i] = (byte) bits.readBits(8);
@@ -162,7 +169,7 @@ final class DecodedBitStreamParser {
     try {
       result.append(new String(readBytes, encoding));
     } catch (UnsupportedEncodingException uce) {
-      throw new ReaderException(uce.toString());
+      throw ReaderException.getInstance();
     }
     byteSegments.addElement(readBytes);
   }
@@ -208,7 +215,7 @@ final class DecodedBitStreamParser {
       // Each 10 bits encodes three digits
       int threeDigitsBits = bits.readBits(10);
       if (threeDigitsBits >= 1000) {
-        throw new ReaderException("Illegal value for 3-digit unit: " + threeDigitsBits);
+        throw ReaderException.getInstance();
       }
       result.append(ALPHANUMERIC_CHARS[threeDigitsBits / 100]);
       result.append(ALPHANUMERIC_CHARS[(threeDigitsBits / 10) % 10]);
@@ -219,7 +226,7 @@ final class DecodedBitStreamParser {
       // Two digits left over to read, encoded in 7 bits
       int twoDigitsBits = bits.readBits(7);
       if (twoDigitsBits >= 100) {
-        throw new ReaderException("Illegal value for 2-digit unit: " + twoDigitsBits);
+        throw ReaderException.getInstance();
       }
       result.append(ALPHANUMERIC_CHARS[twoDigitsBits / 10]);
       result.append(ALPHANUMERIC_CHARS[twoDigitsBits % 10]);
@@ -227,7 +234,7 @@ final class DecodedBitStreamParser {
       // One digit left over to read
       int digitBits = bits.readBits(4);
       if (digitBits >= 10) {
-        throw new ReaderException("Illegal value for digit unit: " + digitBits);
+        throw ReaderException.getInstance();
       }
       result.append(ALPHANUMERIC_CHARS[digitBits]);
     }
@@ -278,10 +285,9 @@ final class DecodedBitStreamParser {
       if (!lastWasPossibleDoubleByteStart && ((value >= 0xF0 && value <= 0xFF) || value == 0x80 || value == 0xA0)) {
         canBeShiftJIS = false;
       }
-      if (((value >= 0x81 && value <= 0x9F) || (value >= 0xE0 && value <= 0xEF)) && i < length - 1) {
+      if (((value >= 0x81 && value <= 0x9F) || (value >= 0xE0 && value <= 0xEF))) {
         // These start double-byte characters in Shift_JIS. Let's see if it's followed by a valid
         // second byte.
-        sawDoubleByteStart = true;
         if (lastWasPossibleDoubleByteStart) {
           // If we just checked this and the last byte for being a valid double-byte
           // char, don't check starting on this byte. If this and the last byte
@@ -292,12 +298,18 @@ final class DecodedBitStreamParser {
           // ... otherwise do check to see if this plus the next byte form a valid
           // double byte pair encoding a character.
           lastWasPossibleDoubleByteStart = true;
-          int nextValue = bytes[i + 1] & 0xFF;
-          if (nextValue < 0x40 || nextValue > 0xFC) {
+          if (i >= bytes.length - 1) {
             canBeShiftJIS = false;
+          } else {
+            int nextValue = bytes[i + 1] & 0xFF;
+            if (nextValue < 0x40 || nextValue > 0xFC) {
+              canBeShiftJIS = false;
+            } else {
+              sawDoubleByteStart = true;
+            }
+            // There is some conflicting information out there about which bytes can follow which in
+            // double-byte Shift_JIS characters. The rule above seems to be the one that matches practice.
           }
-          // There is some conflicting information out there about which bytes can follow which in
-          // double-byte Shift_JIS characters. The rule above seems to be the one that matches practice.
         }
       } else {
         lastWasPossibleDoubleByteStart = false;