diff options
| author | François Tiercelin <francois.tiercelin@hpe.com> | 2024-10-22 16:24:36 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-22 16:24:36 +0300 |
| commit | 3b189fc700c9c7c210f4b78fc66b01f4c13ae5d2 (patch) | |
| tree | 105c216a21c3bea48722830f8bdba1d505d7c29b /core/src/main/java | |
| parent | e750aca9e53d80eb7c2d7ffc3ccdb4e7cf6a7aca (diff) | |
| download | zxing-3b189fc700c9c7c210f4b78fc66b01f4c13ae5d2.tar.xz | |
PDF417: Check that input is made of 0...127 chars when using Compaction.TEXT, throw an explicit exception if not the case (#1878)
* throw a more explicit exception when trying to PDF417/TEXT encode something outside of 0...255
* refactor PDF417HighLevelEncoder to avoid code duplication
extend UT to new method PDF417HighLevelEncoder#checkCharset
* fix javadoc typo
make UT more stringent on PDF417HighLevelEncoder#checkCharset
* restrict TEXT to 0...127
test with CP437 and Greek chars
* reinstate testEncodeAuto UT
* refactor testEncodeAuto UT
* address codacy findings
* formatting
Diffstat (limited to 'core/src/main/java')
| -rw-r--r-- | core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java b/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java index 564d7c968..8ac202b9d 100644 --- a/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java +++ b/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java @@ -174,16 +174,15 @@ final class PDF417HighLevelEncoder { if (msg.isEmpty()) { throw new WriterException("Empty message not allowed"); } + + if (Compaction.TEXT == compaction) { + checkCharset(msg, 127, "Consider specifying Compaction.AUTO instead of Compaction.TEXT"); + } if (encoding == null && !autoECI) { - 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 EncodeHintType.PDF417_AUTO_ECI and/or EncodeTypeHint.CHARACTER_SET."); - } - } + checkCharset(msg, 255, "Consider specifying EncodeHintType.PDF417_AUTO_ECI and/or EncodeTypeHint.CHARACTER_SET"); } + //the codewords 0..928 are encoded as Unicode characters StringBuilder sb = new StringBuilder(msg.length()); @@ -283,6 +282,22 @@ final class PDF417HighLevelEncoder { return sb.toString(); } + + /** + * Check if input is only made of characters between 0 and the upper limit + * @param input the input + * @param max the upper limit for charset + * @param errorMessage the message to explain the error + * @throws WriterException exception highlighting the offending character and a suggestion to avoid the error + */ + protected static void checkCharset(String input, int max, String errorMessage) throws WriterException { + for (int i = 0; i < input.length(); i++) { + if (input.charAt(i) > max) { + throw new WriterException("Non-encodable character detected: " + input.charAt(i) + " (Unicode: " + + (int) input.charAt(i) + ") at position #" + i + " - " + errorMessage); + } + } + } /** * Encode parts of the message using Text Compaction as described in ISO/IEC 15438:2001(E), |
