From 669585590be62b98f6612f8d8211d9ade46a8445 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Tue, 28 Aug 2018 06:22:22 +0000 Subject: [PATCH] Added an assertion to QrCode.encodeSegmentsAdvanced() in all language versions. --- c/qrcodegen.c | 1 + cpp/QrCode.cpp | 2 ++ java/io/nayuki/qrcodegen/QrCode.java | 1 + javascript/qrcodegen.js | 2 ++ python/qrcodegen.py | 1 + rust/src/lib.rs | 1 + typescript/qrcodegen.ts | 2 ++ 7 files changed, 10 insertions(+) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index cab112f..16bb0ff 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -927,6 +927,7 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz for (int j = 0; j < seg->bitLength; j++) appendBitsToBuffer((seg->data[j >> 3] >> (7 - (j & 7))) & 1, 1, qrcode, &bitLen); } + assert(bitLen == dataUsedBits); // Add terminator and pad up to a byte if applicable int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 273056a..2d4fd3f 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -93,6 +93,8 @@ QrCode QrCode::encodeSegments(const vector &segs, Ecc ecl, bb.appendBits(seg.getNumChars(), seg.getMode().numCharCountBits(version)); bb.insert(bb.end(), seg.getData().begin(), seg.getData().end()); } + if (bb.size() != static_cast(dataUsedBits)) + throw std::logic_error("Assertion error"); // Add terminator and pad up to a byte if applicable size_t dataCapacityBits = getNumDataCodewords(version, ecl) * 8; diff --git a/java/io/nayuki/qrcodegen/QrCode.java b/java/io/nayuki/qrcodegen/QrCode.java index f558770..6da7a16 100644 --- a/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/io/nayuki/qrcodegen/QrCode.java @@ -147,6 +147,7 @@ public final class QrCode { bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version)); bb.appendData(seg.data); } + assert bb.bitLength() == dataUsedBits; // Add terminator and pad up to a byte if applicable int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index fc3d5bd..e2a9645 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -563,6 +563,8 @@ var qrcodegen = new function() { bb.push(bit); }); }); + if (bb.length != dataUsedBits) + throw "Assertion error"; // Add terminator and pad up to a byte if applicable var dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8; diff --git a/python/qrcodegen.py b/python/qrcodegen.py index d2f07c1..bd2119d 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -121,6 +121,7 @@ class QrCode(object): bb.append_bits(seg.get_mode().get_mode_bits(), 4) bb.append_bits(seg.get_num_chars(), seg.get_mode().num_char_count_bits(version)) bb.extend(seg._bitdata) + assert len(bb) == datausedbits # Add terminator and pad up to a byte if applicable datacapacitybits = QrCode._get_num_data_codewords(version, ecl) * 8 diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 78b2d2a..dc7500d 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -136,6 +136,7 @@ impl QrCode { bb.append_bits(seg.numchars as u32, seg.mode.num_char_count_bits(version)); bb.0.extend_from_slice(&seg.data); } + assert_eq!(bb.0.len(), datausedbits); // Add terminator and pad up to a byte if applicable let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8; diff --git a/typescript/qrcodegen.ts b/typescript/qrcodegen.ts index de554a3..958746f 100644 --- a/typescript/qrcodegen.ts +++ b/typescript/qrcodegen.ts @@ -108,6 +108,8 @@ namespace qrcodegen { seg.getBits().forEach( (b: bit) => bb.push(b)); }); + if (bb.length != dataUsedBits) + throw "Assertion error"; // Add terminator and pad up to a byte if applicable let dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8;