From a268c93ec5a722ada9d92ec847e8d727f9fcf762 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Tue, 28 Aug 2018 07:18:27 +0000 Subject: [PATCH] Clarified some assertions and code. --- src/io/nayuki/fastqrcodegen/QrCode.java | 6 ++++-- src/io/nayuki/fastqrcodegen/QrTemplate.java | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/io/nayuki/fastqrcodegen/QrCode.java b/src/io/nayuki/fastqrcodegen/QrCode.java index 7c00e75..c54d718 100644 --- a/src/io/nayuki/fastqrcodegen/QrCode.java +++ b/src/io/nayuki/fastqrcodegen/QrCode.java @@ -79,22 +79,24 @@ public final class QrCode { } // Concatenate all segments to create the data bit string - int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; BitBuffer bb = new BitBuffer(); for (QrSegment seg : segs) { bb.appendBits(seg.mode.modeBits, 4); bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version)); bb.appendBits(seg.data, seg.bitLength); } + assert bb.bitLength == dataUsedBits; // Add terminator and pad up to a byte if applicable + int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; + assert bb.bitLength <= dataCapacityBits; bb.appendBits(0, Math.min(4, dataCapacityBits - bb.bitLength)); bb.appendBits(0, (8 - bb.bitLength % 8) % 8); + assert bb.bitLength % 8 == 0; // Pad with alternating bytes until data capacity is reached for (int padByte = 0xEC; bb.bitLength < dataCapacityBits; padByte ^= 0xEC ^ 0x11) bb.appendBits(padByte, 8); - assert bb.bitLength % 8 == 0; // Create the QR Code symbol return new QrCode(version, ecl, bb.getBytes(), mask); diff --git a/src/io/nayuki/fastqrcodegen/QrTemplate.java b/src/io/nayuki/fastqrcodegen/QrTemplate.java index 6d4024e..4087df4 100644 --- a/src/io/nayuki/fastqrcodegen/QrTemplate.java +++ b/src/io/nayuki/fastqrcodegen/QrTemplate.java @@ -308,7 +308,7 @@ final class QrTemplate { int numAlign = ver / 7 + 2; result -= (25 * numAlign - 10) * numAlign - 55; if (ver >= 7) - result -= 18 * 2; + result -= 36; } return result; }