Skip to content

Commit

Permalink
QRDecoder: return some content even in the presence of a checksum error
Browse files Browse the repository at this point in the history
This is related to zxing-cpp#828. As an example, with this change, the first image
in zxing-cpp#454 gets decoded as
    "httPs*//ímbizurl.cn/S/fPwhXQqeH"
instead of an empty string, while the correct version is
    "https://mmbizurl.cn/s/fPwhXQqeH"
  • Loading branch information
axxel committed Sep 9, 2024
1 parent 432df38 commit 39ebe72
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions core/src/qrcode/QRDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,23 @@ DecoderResult Decode(const BitMatrix& bits)
auto resultIterator = resultBytes.begin();

// Error-correct and copy data blocks together into a stream of bytes
Error error;
for (auto& dataBlock : dataBlocks)
{
ByteArray& codewordBytes = dataBlock.codewords();
int numDataCodewords = dataBlock.numDataCodewords();

if (!CorrectErrors(codewordBytes, numDataCodewords))
return ChecksumError();
error = ChecksumError();

resultIterator = std::copy_n(codewordBytes.begin(), numDataCodewords, resultIterator);
}

// Decode the contents of that stream of bytes
return DecodeBitStream(std::move(resultBytes), version, formatInfo.ecLevel).setIsMirrored(formatInfo.isMirrored);
auto ret = DecodeBitStream(std::move(resultBytes), version, formatInfo.ecLevel).setIsMirrored(formatInfo.isMirrored);
if (error)
ret.setError(error);
return ret;
}

} // namespace ZXing::QRCode
2 changes: 1 addition & 1 deletion test/unit/qrcode/MQRDecoderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ TEST(MQRDecoderTest, MQRCodeM1Error4Bits)
88, false);
const auto result = Decode(bitMatrix);
EXPECT_EQ(Error::Checksum, result.error());
EXPECT_TRUE(result.text().empty());
EXPECT_EQ(result.text(), L"6350");
}

TEST(MQRDecoderTest, MQRCodeM4)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/qrcode/RMQRDecoderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ TEST(RMQRDecoderTest, RMQRCodeR7x43MError6Bits)

const auto result = Decode(bitMatrix);
EXPECT_EQ(Error::Checksum, result.error());
EXPECT_TRUE(result.text().empty());
EXPECT_TRUE(result.content().text(TextMode::Plain).empty());
EXPECT_EQ(result.text(), L"LSZ2EFJ");
EXPECT_EQ(result.content().text(TextMode::Plain), "LSZ2EFJ");
}

TEST(RMQRDecoderTest, RMQRCodeR7x139H)
Expand Down

0 comments on commit 39ebe72

Please sign in to comment.