diff --git a/core/src/main/java/com/google/zxing/oned/CodaBarReader.java b/core/src/main/java/com/google/zxing/oned/CodaBarReader.java index ed8ea76f7..a5e230d28 100644 --- a/core/src/main/java/com/google/zxing/oned/CodaBarReader.java +++ b/core/src/main/java/com/google/zxing/oned/CodaBarReader.java @@ -174,7 +174,7 @@ public final class CodaBarReader extends OneDReader { // We break out of this loop in the middle, in order to handle // inter-character spaces properly. int pos = start; - for (int i = 0; true; i++) { + for (int i = 0; i <= end; i++) { int pattern = CHARACTER_ENCODINGS[decodeRowResult.charAt(i)]; for (int j = 6; j >= 0; j--) { // Even j = bars, while odd j = spaces. Categories 2 and 3 are for @@ -184,9 +184,6 @@ public final class CodaBarReader extends OneDReader { counts[category]++; pattern >>= 1; } - if (i >= end) { - break; - } // We ignore the inter-character space - it could be of any size. pos += 8; } @@ -206,7 +203,7 @@ public final class CodaBarReader extends OneDReader { // Now verify that all of the stripes are within the thresholds. pos = start; - for (int i = 0; true; i++) { + for (int i = 0; i <= end; i++) { int pattern = CHARACTER_ENCODINGS[decodeRowResult.charAt(i)]; for (int j = 6; j >= 0; j--) { // Even j = bars, while odd j = spaces. Categories 2 and 3 are for @@ -218,9 +215,6 @@ public final class CodaBarReader extends OneDReader { } pattern >>= 1; } - if (i >= end) { - break; - } pos += 8; } } diff --git a/core/src/main/java/com/google/zxing/oned/rss/expanded/ExpandedRow.java b/core/src/main/java/com/google/zxing/oned/rss/expanded/ExpandedRow.java index b826c0e45..38048f4ba 100644 --- a/core/src/main/java/com/google/zxing/oned/rss/expanded/ExpandedRow.java +++ b/core/src/main/java/com/google/zxing/oned/rss/expanded/ExpandedRow.java @@ -26,13 +26,10 @@ final class ExpandedRow { private final List pairs; private final int rowNumber; - /** Did this row of the image have to be reversed (mirrored) to recognize the pairs? */ - private final boolean wasReversed; - ExpandedRow(List pairs, int rowNumber, boolean wasReversed) { + ExpandedRow(List pairs, int rowNumber) { this.pairs = new ArrayList<>(pairs); this.rowNumber = rowNumber; - this.wasReversed = wasReversed; } List getPairs() { @@ -61,12 +58,12 @@ final class ExpandedRow { return false; } ExpandedRow that = (ExpandedRow) o; - return this.pairs.equals(that.pairs) && wasReversed == that.wasReversed; + return this.pairs.equals(that.pairs); } @Override public int hashCode() { - return pairs.hashCode() ^ Boolean.valueOf(wasReversed).hashCode(); + return pairs.hashCode(); } } diff --git a/core/src/main/java/com/google/zxing/oned/rss/expanded/RSSExpandedReader.java b/core/src/main/java/com/google/zxing/oned/rss/expanded/RSSExpandedReader.java index 9b9970449..f065dd909 100644 --- a/core/src/main/java/com/google/zxing/oned/rss/expanded/RSSExpandedReader.java +++ b/core/src/main/java/com/google/zxing/oned/rss/expanded/RSSExpandedReader.java @@ -291,7 +291,7 @@ public final class RSSExpandedReader extends AbstractRSSReader { return; } - this.rows.add(insertPos, new ExpandedRow(this.pairs, rowNumber, false)); + this.rows.add(insertPos, new ExpandedRow(this.pairs, rowNumber)); removePartialRows(this.pairs, this.rows); } diff --git a/core/src/main/java/com/google/zxing/pdf417/decoder/DecodedBitStreamParser.java b/core/src/main/java/com/google/zxing/pdf417/decoder/DecodedBitStreamParser.java index d1bb23bd5..86b5be618 100644 --- a/core/src/main/java/com/google/zxing/pdf417/decoder/DecodedBitStreamParser.java +++ b/core/src/main/java/com/google/zxing/pdf417/decoder/DecodedBitStreamParser.java @@ -182,16 +182,16 @@ final class DecodedBitStreamParser { // Decoding the fileId codewords as 0-899 numbers, each 0-filled to width 3. This follows the spec // (See ISO/IEC 15438:2015 Annex H.6) and preserves all info, but some generators (e.g. TEC-IT) write // the fileId using text compaction, so in those cases the fileId will appear mangled. - String fileId = ""; + StringBuilder fileId = new StringBuilder(); for (int i = 0; codeIndex < codewords[0] && codewords[codeIndex] != MACRO_PDF417_TERMINATOR && codewords[codeIndex] != BEGIN_MACRO_PDF417_OPTIONAL_FIELD; i++, codeIndex++) { - fileId += String.format("%03d", codewords[codeIndex]); + fileId.append(String.format("%03d", codewords[codeIndex])); } if (fileId.length() == 0) { // at least one fileId codeword is required (Annex H.2) throw FormatException.getFormatInstance(); } - resultMetadata.setFileId(fileId); + resultMetadata.setFileId(fileId.toString()); int optionalFieldsStart = -1; if (codewords[codeIndex] == BEGIN_MACRO_PDF417_OPTIONAL_FIELD) { diff --git a/core/src/test/java/com/google/zxing/aztec/encoder/EncoderTest.java b/core/src/test/java/com/google/zxing/aztec/encoder/EncoderTest.java index 4678e5bd1..adf809772 100644 --- a/core/src/test/java/com/google/zxing/aztec/encoder/EncoderTest.java +++ b/core/src/test/java/com/google/zxing/aztec/encoder/EncoderTest.java @@ -425,8 +425,17 @@ public final class EncoderTest extends Assert { "...X. XXXXX ..X.. X....... ..X.XXX. ..X..... X......."); } - @Test + @Test(expected = IllegalArgumentException.class) public void testUserSpecifiedLayers() { + doTestUserSpecifiedLayers(33); + } + + @Test(expected = IllegalArgumentException.class) + public void testUserSpecifiedLayers2() { + doTestUserSpecifiedLayers(-1); + } + + private void doTestUserSpecifiedLayers(int userSpecifiedLayers) { String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; AztecCode aztec = Encoder.encode(alphabet, 25, -2); assertEquals(2, aztec.getLayers()); @@ -436,19 +445,17 @@ public final class EncoderTest extends Assert { assertEquals(32, aztec.getLayers()); assertFalse(aztec.isCompact()); - try { - Encoder.encode(alphabet, 25, 33); - fail("Encode should have failed. No such thing as 33 layers"); - } catch (IllegalArgumentException expected) { - // continue - } + Encoder.encode(alphabet, 25, userSpecifiedLayers); + } - try { - Encoder.encode(alphabet, 25, -1); - fail("Encode should have failed. Text can't fit in 1-layer compact"); - } catch (IllegalArgumentException expected) { - // continue - } + @Test(expected = IllegalArgumentException.class) + public void testBorderCompact4CaseFailed() { + // Compact(4) con hold 608 bits of information, but at most 504 can be data. Rest must + // be error correction + String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + // encodes as 26 * 5 * 4 = 520 bits of data + String alphabet4 = alphabet + alphabet + alphabet + alphabet; + Encoder.encode(alphabet4, 0, -4); } @Test @@ -458,12 +465,6 @@ public final class EncoderTest extends Assert { String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // encodes as 26 * 5 * 4 = 520 bits of data String alphabet4 = alphabet + alphabet + alphabet + alphabet; - try { - Encoder.encode(alphabet4, 0, -4); - fail("Encode should have failed. Text can't fit in 1-layer compact"); - } catch (IllegalArgumentException expected) { - // continue - } // If we just try to encode it normally, it will go to a non-compact 4 layer AztecCode aztecCode = Encoder.encode(alphabet4, 0, Encoder.DEFAULT_AZTEC_LAYERS); diff --git a/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java b/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java index 6494a9a24..50cc9e48f 100644 --- a/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java +++ b/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java @@ -155,40 +155,24 @@ public class PDF417DecoderTestCase extends Assert { assertNull(resultMetadata.getOptionalData()); } - @Test + @Test(expected = FormatException.class) public void testSampleWithBadSequenceIndexMacro() throws FormatException { int[] sampleCodes = {3, 928, 222, 0}; PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata(); - - try { - DecodedBitStreamParser.decodeMacroBlock(sampleCodes, 2, resultMetadata); - } catch (FormatException expected) { - // continue - } + DecodedBitStreamParser.decodeMacroBlock(sampleCodes, 2, resultMetadata); } - @Test + @Test(expected = FormatException.class) public void testSampleWithNoFileIdMacro() throws FormatException { int[] sampleCodes = {4, 928, 222, 198, 0}; PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata(); - - try { - DecodedBitStreamParser.decodeMacroBlock(sampleCodes, 2, resultMetadata); - } catch (FormatException expected) { - // continue - } + DecodedBitStreamParser.decodeMacroBlock(sampleCodes, 2, resultMetadata); } - @Test + @Test(expected = FormatException.class) public void testSampleWithNoDataNoMacro() throws FormatException { int[] sampleCodes = {3, 899, 899, 0}; - - try { - DecodedBitStreamParser.decode(sampleCodes, "0"); - } catch (FormatException expected) { - // continue - } - + DecodedBitStreamParser.decode(sampleCodes, "0"); } }