restrict TEXT to 0...127

test with CP437 and Greek chars
This commit is contained in:
ftiercelin 2024-10-22 11:12:10 +01:00
parent 488aa3368b
commit b56cb8f7f0
2 changed files with 36 additions and 14 deletions

View file

@ -176,7 +176,7 @@ final class PDF417HighLevelEncoder {
} }
if (Compaction.TEXT == compaction) { if (Compaction.TEXT == compaction) {
checkCharset(msg,255,"Consider specifying Compaction.AUTO instead of Compaction.TEXT"); checkCharset(msg,127,"Consider specifying Compaction.AUTO instead of Compaction.TEXT");
} }
if (encoding == null && !autoECI) { if (encoding == null && !autoECI) {
@ -294,7 +294,7 @@ final class PDF417HighLevelEncoder {
for (int i = 0; i < input.length(); i++) { for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) > max) { if (input.charAt(i) > max) {
throw new WriterException("Non-encodable character detected: " + input.charAt(i) + " (Unicode: " + throw new WriterException("Non-encodable character detected: " + input.charAt(i) + " (Unicode: " +
(int) input.charAt(i) + "). " + errorMessage); (int) input.charAt(i) + ") at position #" + i + " - " + errorMessage);
} }
} }
} }

View file

@ -25,6 +25,7 @@ import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter; import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException; import com.google.zxing.WriterException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.junit.Assert; import org.junit.Assert;
@ -38,21 +39,27 @@ public final class PDF417EncoderTestCase extends Assert {
@Test @Test
public void testEncodeAutoWithSpecialChars() throws Exception { public void testEncodeAutoWithSpecialChars() throws Exception {
// Just check if this does not throw an exception // Just check if this does not throw an exception
PDF417HighLevelEncoder.encodeHighLevel( checkEncodeAutoWithSpecialChars("1%§s ?aG$", Compaction.AUTO);
"1%§s ?aG$", Compaction.AUTO, StandardCharsets.UTF_8, false); checkEncodeAutoWithSpecialChars("日本語", Compaction.AUTO);
PDF417HighLevelEncoder.encodeHighLevel( checkEncodeAutoWithSpecialChars("₸ 5555", Compaction.AUTO);
"日本語", Compaction.AUTO, StandardCharsets.UTF_8, false); checkEncodeAutoWithSpecialChars("€ 123,45", Compaction.AUTO);
PDF417HighLevelEncoder.encodeHighLevel( checkEncodeAutoWithSpecialChars("€ 123,45", Compaction.BYTE);
"₸ 5555", Compaction.AUTO, StandardCharsets.UTF_8, false); checkEncodeAutoWithSpecialChars("123,45", Compaction.TEXT);
PDF417HighLevelEncoder.encodeHighLevel(
"€ 123,45", Compaction.BYTE, StandardCharsets.UTF_8, false); // Greek alphabet
PDF417HighLevelEncoder.encodeHighLevel( Charset cp437 = Charset.forName("IBM437");
"123,45", Compaction.TEXT, StandardCharsets.UTF_8, false); assertNotNull(cp437);
byte[] cp437Array = {(byte) 224,(byte) 225,(byte) 226,(byte) 227,(byte) 228}; //αßΓπΣ
String greek = new String(cp437Array, cp437);
assertEquals("αßΓπΣ", greek);
checkEncodeAutoWithSpecialChars(greek, Compaction.AUTO);
checkEncodeAutoWithSpecialChars(greek, Compaction.BYTE);
PDF417HighLevelEncoder.encodeHighLevel(greek, Compaction.AUTO, cp437, true);
PDF417HighLevelEncoder.encodeHighLevel(greek, Compaction.AUTO, cp437, false);
try { try {
// detect when a TEXT Compaction is applied to a non text input // detect when a TEXT Compaction is applied to a non text input
PDF417HighLevelEncoder.encodeHighLevel( checkEncodeAutoWithSpecialChars("€ 123,45", Compaction.TEXT);
"€ 123,45", Compaction.TEXT, StandardCharsets.UTF_8, false);
} catch (WriterException e) { } catch (WriterException e) {
assertNotNull(e.getMessage()); assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("8364")); assertTrue(e.getMessage().contains("8364"));
@ -60,6 +67,17 @@ public final class PDF417EncoderTestCase extends Assert {
assertTrue(e.getMessage().contains("Compaction.AUTO")); assertTrue(e.getMessage().contains("Compaction.AUTO"));
} }
try {
// detect when a TEXT Compaction is applied to a non text input
String input = "Hello! " + (char) 128;
checkEncodeAutoWithSpecialChars(input, Compaction.TEXT);
} catch (WriterException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("128"));
assertTrue(e.getMessage().contains("Compaction.TEXT"));
assertTrue(e.getMessage().contains("Compaction.AUTO"));
}
try { try {
// detect when a TEXT Compaction is applied to a non text input // detect when a TEXT Compaction is applied to a non text input
// https://github.com/zxing/zxing/issues/1761 // https://github.com/zxing/zxing/issues/1761
@ -80,6 +98,10 @@ public final class PDF417EncoderTestCase extends Assert {
} }
} }
public void checkEncodeAutoWithSpecialChars(String input, Compaction compaction) throws Exception {
PDF417HighLevelEncoder.encodeHighLevel(input, compaction, StandardCharsets.UTF_8, false);
}
@Test @Test
public void testCheckCharset() throws Exception { public void testCheckCharset() throws Exception {
String input = "Hello!"; String input = "Hello!";