Skip to content

Commit

Permalink
Support for platforms where SJIS is unavailable (#1747)
Browse files Browse the repository at this point in the history
* Add support for embedded JREs where SJIS isn't available

(cherry picked from commit 67896f9)

* Add support for embedded JREs where EUC_JP isn't available

(cherry picked from commit 979d3b6)
  • Loading branch information
blakepuhak authored Jan 17, 2024
1 parent 956bbca commit 1cf9eaa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
30 changes: 25 additions & 5 deletions core/src/main/java/com/google/zxing/common/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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 SHIFT_JIS_CHARSET;
static {
Charset sjisCharset;
try {
sjisCharset = Charset.forName("SJIS");
} catch (UnsupportedCharsetException ucee) {
// Can happen on JREs without lib/charsets.jar
sjisCharset = null;
}
SHIFT_JIS_CHARSET = sjisCharset;
}
public static final Charset GB2312_CHARSET;
static {
Charset gb2312Charset;
Expand All @@ -44,10 +54,20 @@ public final class StringUtils {
}
GB2312_CHARSET = gb2312Charset;
}
private static final Charset EUC_JP = Charset.forName("EUC_JP");
private static final Charset EUC_JP;
static {
Charset eucJpCharset;
try {
eucJpCharset = Charset.forName("EUC_JP");
} catch (UnsupportedCharsetException ucee) {
// Can happen on JREs without lib/charsets.jar
eucJpCharset = null;
}
EUC_JP = eucJpCharset;
}
private static final boolean ASSUME_SHIFT_JIS =
SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING) ||
EUC_JP.equals(PLATFORM_DEFAULT_ENCODING);
(SHIFT_JIS_CHARSET != null && SHIFT_JIS_CHARSET.equals(PLATFORM_DEFAULT_ENCODING)) ||
(EUC_JP != null && EUC_JP.equals(PLATFORM_DEFAULT_ENCODING));

// Retained for ABI compatibility with earlier versions
public static final String SHIFT_JIS = "SJIS";
Expand Down Expand Up @@ -101,7 +121,7 @@ public static Charset guessCharset(byte[] bytes, Map<DecodeHintType,?> hints) {
// which should be by far the most common encodings.
int length = bytes.length;
boolean canBeISO88591 = true;
boolean canBeShiftJIS = true;
boolean canBeShiftJIS = SHIFT_JIS_CHARSET != null;
boolean canBeUTF8 = true;
int utf8BytesLeft = 0;
int utf2BytesChars = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ private static void decodeHanziSegment(BitSource bits,
private static void decodeKanjiSegment(BitSource bits,
StringBuilder result,
int count) throws FormatException {
if (StringUtils.SHIFT_JIS_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
31 changes: 21 additions & 10 deletions core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -91,7 +92,11 @@ public static QRCode encode(String content,
Charset encoding = DEFAULT_BYTE_MODE_ENCODING;
boolean hasEncodingHint = hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET);
if (hasEncodingHint) {
encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
try {
encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
} catch (UnsupportedCharsetException ignore) {
//ignore the encodingHint and use the DEFAULT_BYTE_MODE_ENCODING
}
}

if (hasCompactionHint) {
Expand All @@ -105,37 +110,37 @@ public static QRCode encode(String content,
version = rn.getVersion();

} else {

// Pick an encoding mode appropriate for the content. Note that this will not attempt to use
// multiple modes / segments even if that were more efficient.
mode = chooseMode(content, encoding);

// This will store the header information, like mode and
// length, as well as "header" segments like an ECI segment.
BitArray headerBits = new BitArray();

// Append ECI segment if applicable
if (mode == Mode.BYTE && hasEncodingHint) {
CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(encoding);
if (eci != null) {
appendECI(eci, headerBits);
}
}

// Append the FNC1 mode header for GS1 formatted data if applicable
if (hasGS1FormatHint) {
// GS1 formatted codes are prefixed with a FNC1 in first position mode header
appendModeInfo(Mode.FNC1_FIRST_POSITION, headerBits);
}

// (With ECI in place,) Write the mode marker
appendModeInfo(mode, headerBits);

// Collect data within the main segment, separately, to count its size if needed. Don't add it to
// main payload yet.
BitArray dataBits = new BitArray();
appendBytes(content, mode, dataBits, encoding);

if (hints != null && hints.containsKey(EncodeHintType.QR_VERSION)) {
int versionNumber = Integer.parseInt(hints.get(EncodeHintType.QR_VERSION).toString());
version = Version.getVersionForNumber(versionNumber);
Expand All @@ -146,7 +151,7 @@ public static QRCode encode(String content,
} else {
version = recommendVersion(ecLevel, mode, headerBits, dataBits);
}

headerAndDataBits = new BitArray();
headerAndDataBits.appendBitArray(headerBits);
// Find "length" of main segment and write it
Expand Down Expand Up @@ -244,7 +249,9 @@ public static Mode chooseMode(String content) {
* if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}.
*/
private static Mode chooseMode(String content, Charset encoding) {
if (StringUtils.SHIFT_JIS_CHARSET.equals(encoding) && isOnlyDoubleByteKanji(content)) {
if (StringUtils.SHIFT_JIS_CHARSET != null &&
StringUtils.SHIFT_JIS_CHARSET.equals(encoding) &&
isOnlyDoubleByteKanji(content)) {
// Choose Kanji mode if all input are double-byte characters
return Mode.KANJI;
}
Expand Down Expand Up @@ -605,6 +612,10 @@ static void append8BitBytes(String content, BitArray bits, Charset encoding) {
}

static void appendKanjiBytes(String content, BitArray bits) throws WriterException {
if (StringUtils.SHIFT_JIS_CHARSET == null) {
// Not supported without charset support
throw new WriterException("SJIS Charset not supported on this platform");
}
byte[] bytes = content.getBytes(StringUtils.SHIFT_JIS_CHARSET);
if (bytes.length % 2 != 0) {
throw new WriterException("Kanji byte size not even");
Expand Down

0 comments on commit 1cf9eaa

Please sign in to comment.