fuzzDecodeMatrix.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2023 Axel Waggershauser
  3. */
  4. // SPDX-License-Identifier: Apache-2.0
  5. #include "BitArray.h"
  6. #include "ByteArray.h"
  7. #include "DecoderResult.h"
  8. #include "qrcode/QRErrorCorrectionLevel.h"
  9. #include "qrcode/QRVersion.h"
  10. #include "pdf417/PDFDecoder.h"
  11. #include <algorithm>
  12. #include <cstddef>
  13. #include <cstdint>
  14. using namespace ZXing;
  15. namespace ZXing::Aztec {
  16. DecoderResult Decode(const BitArray& bits);
  17. }
  18. namespace ZXing::DataMatrix::DecodedBitStreamParser {
  19. DecoderResult Decode(ByteArray&& bytes, const bool isDMRE);
  20. }
  21. namespace ZXing::QRCode {
  22. DecoderResult DecodeBitStream(ByteArray&& bytes, const Version& version, ErrorCorrectionLevel ecLevel);
  23. }
  24. void az(const uint8_t* data, size_t size)
  25. {
  26. BitArray bits;
  27. for (size_t i = 1; i < size - 1; ++i)
  28. bits.appendBits(data[i], 8);
  29. bits.appendBits(data[size - 1], (data[0] & 0x7) + 1);
  30. Aztec::Decode(bits);
  31. }
  32. void dm(const uint8_t* data, size_t size)
  33. {
  34. ByteArray ba;
  35. ba.insert(ba.begin(), data, data + size);
  36. try {
  37. DataMatrix::DecodedBitStreamParser::Decode(std::move(ba), false);
  38. } catch (...) {
  39. }
  40. }
  41. void qr(const uint8_t* data, size_t size)
  42. {
  43. auto version = QRCode::Version::Model2(std::clamp(data[0] & 0x3F, 1, 40));
  44. auto ecLevel = QRCode::ECLevelFromBits(data[0] >> 6);
  45. ByteArray ba;
  46. ba.insert(ba.begin(), data, data + size);
  47. QRCode::DecodeBitStream(std::move(ba), *version, ecLevel);
  48. }
  49. void pd(const uint8_t* data, size_t size)
  50. {
  51. auto codewords = std::vector<int>(size / 2);
  52. auto u16 = reinterpret_cast<const uint16_t*>(data);
  53. for (int i = 0; i < Size(codewords); ++i)
  54. codewords[i] = u16[i] % 929;
  55. codewords[0] = Size(codewords);
  56. Pdf417::Decode(codewords);
  57. }
  58. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
  59. {
  60. if (size < 3)
  61. return 0;
  62. az(data, size);
  63. dm(data, size);
  64. qr(data, size);
  65. pd(data, size);
  66. return 0;
  67. }