Avoid some rare IllegalArgumentException and ArrayIndexOutOfBoundsException on bad input

git-svn-id: https://zxing.googlecode.com/svn/trunk@2184 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-02-14 09:51:59 +00:00
parent de25d10e9d
commit 0cf9ec342f
2 changed files with 9 additions and 4 deletions

View file

@ -16,6 +16,8 @@
package com.google.zxing.common; package com.google.zxing.common;
import com.google.zxing.FormatException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -97,9 +99,9 @@ public enum CharacterSetECI {
* unsupported * unsupported
* @throws IllegalArgumentException if ECI value is invalid * @throws IllegalArgumentException if ECI value is invalid
*/ */
public static CharacterSetECI getCharacterSetECIByValue(int value) { public static CharacterSetECI getCharacterSetECIByValue(int value) throws FormatException {
if (value < 0 || value >= 900) { if (value < 0 || value >= 900) {
throw new IllegalArgumentException("Bad ECI value: " + value); throw FormatException.getFormatInstance();
} }
return VALUE_TO_ECI.get(value); return VALUE_TO_ECI.get(value);
} }

View file

@ -248,6 +248,9 @@ final class DecodedBitStreamParser {
// Read two characters at a time // Read two characters at a time
int start = result.length(); int start = result.length();
while (count > 1) { while (count > 1) {
if (bits.available() < 11) {
throw FormatException.getFormatInstance();
}
int nextTwoCharsBits = bits.readBits(11); int nextTwoCharsBits = bits.readBits(11);
result.append(toAlphaNumericChar(nextTwoCharsBits / 45)); result.append(toAlphaNumericChar(nextTwoCharsBits / 45));
result.append(toAlphaNumericChar(nextTwoCharsBits % 45)); result.append(toAlphaNumericChar(nextTwoCharsBits % 45));
@ -316,7 +319,7 @@ final class DecodedBitStreamParser {
} }
} }
private static int parseECIValue(BitSource bits) { private static int parseECIValue(BitSource bits) throws FormatException {
int firstByte = bits.readBits(8); int firstByte = bits.readBits(8);
if ((firstByte & 0x80) == 0) { if ((firstByte & 0x80) == 0) {
// just one byte // just one byte
@ -332,7 +335,7 @@ final class DecodedBitStreamParser {
int secondThirdBytes = bits.readBits(16); int secondThirdBytes = bits.readBits(16);
return ((firstByte & 0x1F) << 16) | secondThirdBytes; return ((firstByte & 0x1F) << 16) | secondThirdBytes;
} }
throw new IllegalArgumentException("Bad ECI bits starting with byte " + firstByte); throw FormatException.getFormatInstance();
} }
} }