Merge pull request #3 from ftiercelin/ISS1761

Throw a more explicit exception when trying to PDF417/TEXT encode something outside of 0...255
This commit is contained in:
François Tiercelin 2024-10-21 18:43:07 +01:00 committed by GitHub
commit d25307d166
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 54 additions and 7 deletions

View file

@ -174,6 +174,16 @@ final class PDF417HighLevelEncoder {
if (msg.isEmpty()) {
throw new WriterException("Empty message not allowed");
}
if (Compaction.TEXT == compaction) {
for (int i = 0; i < msg.length(); i++) {
if (msg.charAt(i) > 255) {
throw new WriterException("Non-encodable character detected: " + msg.charAt(i) + " (Unicode: " +
(int) msg.charAt(i) +
"). Consider specifying Compaction.AUTO instead of Compaction.TEXT");
}
}
}
if (encoding == null && !autoECI) {
for (int i = 0; i < msg.length(); i++) {

View file

@ -16,6 +16,12 @@
package com.google.zxing.pdf417.encoder;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import java.nio.charset.StandardCharsets;
@ -28,18 +34,49 @@ import org.junit.Test;
*/
public final class PDF417EncoderTestCase extends Assert {
@Test
public void testEncodeAuto() throws Exception {
String encoded = PDF417HighLevelEncoder.encodeHighLevel(
"ABCD", Compaction.AUTO, StandardCharsets.UTF_8, false);
assertEquals("\u039f\u001A\u0385ABCD", encoded);
}
@Test
public void testEncodeAutoWithSpecialChars() throws Exception {
// Just check if this does not throw an exception
PDF417HighLevelEncoder.encodeHighLevel(
"1%§s ?aG$", Compaction.AUTO, StandardCharsets.UTF_8, false);
PDF417HighLevelEncoder.encodeHighLevel(
"日本語", Compaction.AUTO, StandardCharsets.UTF_8, false);
PDF417HighLevelEncoder.encodeHighLevel(
"₸ 5555", Compaction.AUTO, StandardCharsets.UTF_8, false);
PDF417HighLevelEncoder.encodeHighLevel(
"€ 123,45", Compaction.BYTE, StandardCharsets.UTF_8, false);
PDF417HighLevelEncoder.encodeHighLevel(
"123,45", Compaction.TEXT, StandardCharsets.UTF_8, false);
try {
// detect when a TEXT Compaction is applied to a non text input
PDF417HighLevelEncoder.encodeHighLevel(
"€ 123,45", Compaction.TEXT, StandardCharsets.UTF_8, false);
} catch (WriterException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("8364"));
assertTrue(e.getMessage().contains("Compaction.TEXT"));
assertTrue(e.getMessage().contains("Compaction.AUTO"));
}
try {
// detect when a TEXT Compaction is applied to a non text input
// https://github.com/zxing/zxing/issues/1761
String content = "€ 123,45";
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, 4);
hints.put(EncodeHintType.PDF417_DIMENSIONS, new com.google.zxing.pdf417.encoder.Dimensions(7, 7, 1, 300));
hints.put(EncodeHintType.MARGIN, 0);
hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-15");
hints.put(EncodeHintType.PDF417_COMPACTION, com.google.zxing.pdf417.encoder.Compaction.TEXT);
(new MultiFormatWriter()).encode(content, BarcodeFormat.PDF_417, 200, 100, hints);
} catch (WriterException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("8364"));
assertTrue(e.getMessage().contains("Compaction.TEXT"));
assertTrue(e.getMessage().contains("Compaction.AUTO"));
}
}
@Test