Added an assertion to QrCode.encodeSegmentsAdvanced() in all language versions.

pull/39/merge
Project Nayuki 6 years ago
parent 87cab22ffc
commit 669585590b

@ -927,6 +927,7 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
for (int j = 0; j < seg->bitLength; j++) for (int j = 0; j < seg->bitLength; j++)
appendBitsToBuffer((seg->data[j >> 3] >> (7 - (j & 7))) & 1, 1, qrcode, &bitLen); 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 // Add terminator and pad up to a byte if applicable
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; int dataCapacityBits = getNumDataCodewords(version, ecl) * 8;

@ -93,6 +93,8 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,
bb.appendBits(seg.getNumChars(), seg.getMode().numCharCountBits(version)); bb.appendBits(seg.getNumChars(), seg.getMode().numCharCountBits(version));
bb.insert(bb.end(), seg.getData().begin(), seg.getData().end()); bb.insert(bb.end(), seg.getData().begin(), seg.getData().end());
} }
if (bb.size() != static_cast<size_t>(dataUsedBits))
throw std::logic_error("Assertion error");
// Add terminator and pad up to a byte if applicable // Add terminator and pad up to a byte if applicable
size_t dataCapacityBits = getNumDataCodewords(version, ecl) * 8; size_t dataCapacityBits = getNumDataCodewords(version, ecl) * 8;

@ -147,6 +147,7 @@ public final class QrCode {
bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version)); bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));
bb.appendData(seg.data); bb.appendData(seg.data);
} }
assert bb.bitLength() == dataUsedBits;
// Add terminator and pad up to a byte if applicable // Add terminator and pad up to a byte if applicable
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; int dataCapacityBits = getNumDataCodewords(version, ecl) * 8;

@ -563,6 +563,8 @@ var qrcodegen = new function() {
bb.push(bit); bb.push(bit);
}); });
}); });
if (bb.length != dataUsedBits)
throw "Assertion error";
// Add terminator and pad up to a byte if applicable // Add terminator and pad up to a byte if applicable
var dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8; var dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8;

@ -121,6 +121,7 @@ class QrCode(object):
bb.append_bits(seg.get_mode().get_mode_bits(), 4) 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.append_bits(seg.get_num_chars(), seg.get_mode().num_char_count_bits(version))
bb.extend(seg._bitdata) bb.extend(seg._bitdata)
assert len(bb) == datausedbits
# Add terminator and pad up to a byte if applicable # Add terminator and pad up to a byte if applicable
datacapacitybits = QrCode._get_num_data_codewords(version, ecl) * 8 datacapacitybits = QrCode._get_num_data_codewords(version, ecl) * 8

@ -136,6 +136,7 @@ impl QrCode {
bb.append_bits(seg.numchars as u32, seg.mode.num_char_count_bits(version)); bb.append_bits(seg.numchars as u32, seg.mode.num_char_count_bits(version));
bb.0.extend_from_slice(&seg.data); bb.0.extend_from_slice(&seg.data);
} }
assert_eq!(bb.0.len(), datausedbits);
// Add terminator and pad up to a byte if applicable // Add terminator and pad up to a byte if applicable
let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8; let datacapacitybits: usize = QrCode::get_num_data_codewords(version, ecl) * 8;

@ -108,6 +108,8 @@ namespace qrcodegen {
seg.getBits().forEach( seg.getBits().forEach(
(b: bit) => bb.push(b)); (b: bit) => bb.push(b));
}); });
if (bb.length != dataUsedBits)
throw "Assertion error";
// Add terminator and pad up to a byte if applicable // Add terminator and pad up to a byte if applicable
let dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8; let dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8;

Loading…
Cancel
Save