File: fuzzDecodeMatrix.cpp

package info (click to toggle)
zxing-cpp 2.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,832 kB
  • sloc: cpp: 32,803; ansic: 18,360; php: 1,156; python: 215; makefile: 25; sh: 3
file content (87 lines) | stat: -rw-r--r-- 1,845 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2023 Axel Waggershauser
 */
// SPDX-License-Identifier: Apache-2.0

#include "BitArray.h"
#include "ByteArray.h"
#include "DecoderResult.h"
#include "qrcode/QRErrorCorrectionLevel.h"
#include "qrcode/QRVersion.h"
#include "pdf417/PDFDecoder.h"

#include <algorithm>
#include <cstddef>
#include <cstdint>

using namespace ZXing;

namespace ZXing::Aztec {
DecoderResult Decode(const BitArray& bits);
}

namespace ZXing::DataMatrix::DecodedBitStreamParser {
DecoderResult Decode(ByteArray&& bytes, const bool isDMRE);
}

namespace ZXing::QRCode {
DecoderResult DecodeBitStream(ByteArray&& bytes, const Version& version, ErrorCorrectionLevel ecLevel);
}

void az(const uint8_t* data, size_t size)
{
	BitArray bits;
	for (size_t i = 1; i < size - 1; ++i)
		bits.appendBits(data[i], 8);

	bits.appendBits(data[size - 1], (data[0] & 0x7) + 1);

	Aztec::Decode(bits);
}

void dm(const uint8_t* data, size_t size)
{
	ByteArray ba;
	ba.insert(ba.begin(), data, data + size);
	try {
		DataMatrix::DecodedBitStreamParser::Decode(std::move(ba), false);
	} catch (...) {
	}
}

void qr(const uint8_t* data, size_t size)
{
	auto version = QRCode::Version::Model2(std::clamp(data[0] & 0x3F, 1, 40));
	auto ecLevel = QRCode::ECLevelFromBits(data[0] >> 6);

	ByteArray ba;
	ba.insert(ba.begin(), data, data + size);

	QRCode::DecodeBitStream(std::move(ba), *version, ecLevel);
}

void pd(const uint8_t* data, size_t size)
{
	auto codewords = std::vector<int>(size / 2);
	auto u16 = reinterpret_cast<const uint16_t*>(data);

	for (int i = 0; i < Size(codewords); ++i)
		codewords[i] = u16[i] % 929;

	codewords[0] = Size(codewords);

	Pdf417::Decode(codewords);
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
	if (size < 3)
		return 0;

	az(data, size);
	dm(data, size);
	qr(data, size);
	pd(data, size);

	return 0;
}