diff --git a/autotests/aztecbarcodetest.cpp b/autotests/aztecbarcodetest.cpp --- a/autotests/aztecbarcodetest.cpp +++ b/autotests/aztecbarcodetest.cpp @@ -123,9 +123,12 @@ BitVector in, out; QTest::newRow("emtpy") << in << out << 4; - in.appendMSB(0x3, 2); out.appendMSB(0xf, 4); + in.appendMSB(0x2, 2); out.appendMSB(0xB, 4); QTest::newRow("pad only") << in << out << 4; in.clear(); out.clear(); + in.appendMSB(0x3, 2); out.appendMSB(0xE, 4); + QTest::newRow("pad only inverted") << in << out << 4; + in.clear(); out.clear(); in.appendMSB(0xe0, 8); out.appendMSB(0xe13, 12); QTest::newRow("stuff and pad") << in << out << 4; in.clear(); out.clear(); diff --git a/src/lib/aztecbarcode.cpp b/src/lib/aztecbarcode.cpp --- a/src/lib/aztecbarcode.cpp +++ b/src/lib/aztecbarcode.cpp @@ -576,9 +576,24 @@ res.appendBit(input.at(i++)); } - // pad to nearest code word boundary + // check if we are code word aligned already + const auto trailingBits = res.size() % codeWordSize; + if (!trailingBits) { // nothing to pad + return res; + } + + // pad with ones to nearest code word boundary + // last bit has to be zero if we'd otherwise would have all ones though + bool allOnes = true; + for (int i = res.size() - trailingBits; i < res.size(); ++i) { + allOnes &= res.at(i); + } while (res.size() % codeWordSize) { - res.appendBit(true); + if ((res.size() % codeWordSize) == (codeWordSize - 1)) { + res.appendBit(allOnes ? false : true); + } else { + res.appendBit(true); + } } return res;