Skip to content

Commit

Permalink
Handle case where GB2312 isn't supported; don't support Hanzi mode QR…
Browse files Browse the repository at this point in the history
… codes in this case
  • Loading branch information
srowen committed Sep 20, 2022
1 parent 6fb399b commit a6273e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion core/src/main/java/com/google/zxing/common/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Map;

import com.google.zxing.DecodeHintType;
Expand All @@ -32,7 +33,17 @@ public final class StringUtils {

private static final Charset PLATFORM_DEFAULT_ENCODING = Charset.defaultCharset();
public static final Charset SHIFT_JIS_CHARSET = Charset.forName("SJIS");
public static final Charset GB2312_CHARSET = Charset.forName("GB2312");
public static final Charset GB2312_CHARSET;
static {
Charset gb2312Charset;
try {
gb2312Charset = Charset.forName("GB2312");
} catch (UnsupportedCharsetException ucee) {
// Can happen on some embedded JREs?
gb2312Charset = null;
}
GB2312_CHARSET = gb2312Charset;
}
private static final Charset EUC_JP = Charset.forName("EUC_JP");
private static final boolean ASSUME_SHIFT_JIS =
SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ static DecoderResult decode(byte[] bytes,
private static void decodeHanziSegment(BitSource bits,
StringBuilder result,
int count) throws FormatException {
if (StringUtils.GB2312_CHARSET == null) {
// Not supported without charset support
throw FormatException.getFormatInstance();
}
// Don't crash trying to read more bits than we have available.
if (count * 13 > bits.available()) {
throw FormatException.getFormatInstance();
Expand Down

0 comments on commit a6273e3

Please sign in to comment.