8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#include "fxbarcode/qrcode/BC_QRCoderEncoder.h"
32#include "core/fxcrt/data_vector.h"
33#include "core/fxcrt/fx_string.h"
34#include "core/fxcrt/span_util.h"
35#include "fxbarcode/common/BC_CommonByteMatrix.h"
36#include "fxbarcode/common/reedsolomon/BC_ReedSolomon.h"
37#include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
38#include "fxbarcode/qrcode/BC_QRCoder.h"
39#include "fxbarcode/qrcode/BC_QRCoderBitVector.h"
40#include "fxbarcode/qrcode/BC_QRCoderECBlocks.h"
41#include "fxbarcode/qrcode/BC_QRCoderMaskUtil.h"
42#include "fxbarcode/qrcode/BC_QRCoderMatrixUtil.h"
43#include "fxbarcode/qrcode/BC_QRCoderMode.h"
44#include "fxbarcode/qrcode/BC_QRCoderVersion.h"
45#include "third_party/abseil-cpp/absl/types/optional.h"
46#include "third_party/base/check.h"
47#include "third_party/base/check_op.h"
49using ModeStringPair =
std::pair<CBC_QRCoderMode*, ByteString>;
55struct QRCoderBlockPair {
56 DataVector<uint8_t> data;
57 DataVector<uint8_t> ecc;
61const int8_t kAlphaNumericTable[] = {
62 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
63 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
64 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
65 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35};
67int32_t GetAlphaNumericCode(int32_t code) {
70 size_t code_index =
static_cast<size_t>(code - 32);
71 if (code_index >=
std::size(kAlphaNumericTable))
73 return kAlphaNumericTable[code_index];
77 size_t length = content.GetLength();
80 int32_t num1 = content[i] -
'0';
82 int32_t num2 = content[i + 1] -
'0';
83 int32_t num3 = content[i + 2] -
'0';
86 }
else if (i + 1 < length) {
87 int32_t num2 = content[i + 1] -
'0';
98bool AppendAlphaNumericBytes(
const ByteString& content,
100 size_t length = content.GetLength();
103 int32_t code1 = GetAlphaNumericCode(content[i]);
107 if (i + 1 < length) {
108 int32_t code2 = GetAlphaNumericCode(content[i + 1]);
123 for (
char c : content)
132bool AppendLengthInfo(int32_t numLetters,
134 CBC_QRCoderMode* mode,
142 if (numBits > ((1 << numBits) - 1))
149bool AppendBytes(
const ByteString& content,
150 CBC_QRCoderMode* mode,
153 return AppendNumericBytes(content, bits);
155 return AppendAlphaNumericBytes(content, bits);
156 if (mode == CBC_QRCoderMode
::sBYTE)
157 return Append8BitBytes(content, bits);
161bool InitQRCode(int32_t numInputBytes,
163 CBC_QRCoder* qrCode) {
171 int32_t numDataBytes = numBytes - numEcBytes;
172 if (numDataBytes >= numInputBytes + 3) {
185DataVector<uint8_t> GenerateECBytes(pdfium::span<
const uint8_t> dataBytes,
186 size_t numEcBytesInBlock) {
188 DCHECK(numEcBytesInBlock > 0);
189 std::vector<int32_t> toEncode(dataBytes.size() + numEcBytesInBlock);
190 std::copy(dataBytes.begin(), dataBytes.end(), toEncode.begin());
192 DataVector<uint8_t> ecBytes;
194 if (encoder.Encode(&toEncode, numEcBytesInBlock)) {
195 ecBytes = DataVector<uint8_t>(toEncode.begin() + dataBytes.size(),
197 DCHECK_EQ(ecBytes.size(),
static_cast<size_t>(numEcBytesInBlock));
202int32_t CalculateMaskPenalty(CBC_CommonByteMatrix* matrix) {
209absl::optional<int32_t> ChooseMaskPattern(
213 CBC_CommonByteMatrix* matrix) {
214 int32_t minPenalty = 65535;
215 int32_t bestMaskPattern = -1;
220 return absl::nullopt;
222 int32_t penalty = CalculateMaskPenalty(matrix);
223 if (penalty < minPenalty) {
224 minPenalty = penalty;
225 bestMaskPattern = maskPattern;
228 return bestMaskPattern;
231void GetNumDataBytesAndNumECBytesForBlockID(int32_t numTotalBytes,
232 int32_t numDataBytes,
235 int32_t* numDataBytesInBlock,
236 int32_t* numECBytesInBlock) {
237 if (blockID >= numRSBlocks)
240 int32_t numRsBlocksInGroup2 = numTotalBytes % numRSBlocks;
241 int32_t numRsBlocksInGroup1 = numRSBlocks - numRsBlocksInGroup2;
242 int32_t numTotalBytesInGroup1 = numTotalBytes / numRSBlocks;
243 int32_t numTotalBytesInGroup2 = numTotalBytesInGroup1 + 1;
244 int32_t numDataBytesInGroup1 = numDataBytes / numRSBlocks;
245 int32_t numDataBytesInGroup2 = numDataBytesInGroup1 + 1;
246 int32_t numEcBytesInGroup1 = numTotalBytesInGroup1 - numDataBytesInGroup1;
247 int32_t numEcBytesInGroup2 = numTotalBytesInGroup2 - numDataBytesInGroup2;
248 if (blockID < numRsBlocksInGroup1) {
249 *numDataBytesInBlock = numDataBytesInGroup1;
250 *numECBytesInBlock = numEcBytesInGroup1;
252 *numDataBytesInBlock = numDataBytesInGroup2;
253 *numECBytesInBlock = numEcBytesInGroup2;
258 size_t capacity = numDataBytes << 3;
259 if (bits->Size() > capacity)
262 for (int32_t i = 0; i < 4 && bits->Size() < capacity; ++i)
265 int32_t numBitsInLastByte = bits->Size() % 8;
266 if (numBitsInLastByte > 0) {
267 int32_t numPaddingBits = 8 - numBitsInLastByte;
268 for (int32_t j = 0; j < numPaddingBits; ++j)
272 if (bits->Size() % 8 != 0)
275 int32_t numPaddingBytes = numDataBytes - bits->sizeInBytes();
276 for (int32_t k = 0; k < numPaddingBytes; ++k)
278 return bits->Size() == capacity;
281CBC_QRCoderMode* ChooseMode(
const ByteString& content) {
282 bool hasNumeric =
false;
283 bool hasAlphaNumeric =
false;
284 for (size_t i = 0; i < content.GetLength(); i++) {
285 if (isdigit(content[i])) {
287 }
else if (GetAlphaNumericCode(content[i]) != -1) {
288 hasAlphaNumeric =
true;
301 int32_t numTotalBytes,
302 int32_t numDataBytes,
305 DCHECK(numTotalBytes >= 0);
306 DCHECK(numDataBytes >= 0);
307 if (bits->sizeInBytes() !=
static_cast<size_t>(numDataBytes))
310 int32_t dataBytesOffset = 0;
311 size_t maxNumDataBytes = 0;
312 size_t maxNumEcBytes = 0;
313 std::vector<QRCoderBlockPair> blocks(numRSBlocks);
314 for (int32_t i = 0; i < numRSBlocks; i++) {
315 int32_t numDataBytesInBlock;
316 int32_t numEcBytesInBlock;
317 GetNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes,
318 numRSBlocks, i, &numDataBytesInBlock,
320 if (numDataBytesInBlock < 0 || numEcBytesInBlock <= 0)
323 DataVector<uint8_t> dataBytes(numDataBytesInBlock);
325 pdfium::make_span(dataBytes),
326 bits->GetArray().subspan(dataBytesOffset, numDataBytesInBlock));
327 DataVector<uint8_t> ecBytes = GenerateECBytes(dataBytes, numEcBytesInBlock);
331 maxNumDataBytes = std::max(maxNumDataBytes, dataBytes.size());
332 maxNumEcBytes =
std::max(maxNumEcBytes, ecBytes.size());
333 blocks[i].data = std::move(dataBytes);
334 blocks[i].ecc =
std::move(ecBytes);
335 dataBytesOffset += numDataBytesInBlock;
337 if (numDataBytes != dataBytesOffset)
340 for (size_t x = 0; x < maxNumDataBytes; x++) {
341 for (size_t j = 0; j < blocks.size(); j++) {
342 const DataVector<uint8_t>& dataBytes = blocks[j].data;
343 if (x < dataBytes.size())
347 for (size_t y = 0; y < maxNumEcBytes; y++) {
348 for (size_t l = 0; l < blocks.size(); l++) {
349 const DataVector<uint8_t>& ecBytes = blocks[l].ecc;
350 if (y < ecBytes.size())
354 return static_cast<size_t>(numTotalBytes) == result->sizeInBytes();
367 delete g_QRCodeField;
368 g_QRCodeField =
nullptr;
374 CBC_QRCoder* qrCode) {
375 ByteString utf8Data = FX_UTF8Encode(content);
376 CBC_QRCoderMode* mode = ChooseMode(utf8Data);
378 if (!AppendBytes(utf8Data, mode, &dataBits))
380 int32_t numInputBytes = dataBits.sizeInBytes();
381 if (!InitQRCode(numInputBytes, ecLevel, qrCode))
384 AppendModeInfo(mode, &headerAndDataBits);
385 int32_t numLetters = mode == CBC_QRCoderMode
::sBYTE ? dataBits.sizeInBytes()
386 : content.GetLength();
388 &headerAndDataBits)) {
401 auto matrix =
std::make_unique<CBC_CommonByteMatrix>(
403 absl::optional<int32_t> maskPattern = ChooseMaskPattern(
404 &finalBits, qrCode->GetECLevel(), qrCode->GetVersion(), matrix.get());
405 if (!maskPattern.has_value())
408 qrCode->SetMaskPattern(maskPattern.value());
415 qrCode->SetMatrix(
std::move(matrix));
void AppendBits(int32_t value, int32_t numBits)
void AppendBitVector(const CBC_QRCoderBitVector *bits)
void AppendBit(int32_t bit)
int32_t GetTotalECCodeWords() const
int32_t GetNumBlocks() const
static bool Encode(WideStringView content, const CBC_QRCoderErrorCorrectionLevel *ecLevel, CBC_QRCoder *qrCode)
static int32_t ApplyMaskPenaltyRule1(CBC_CommonByteMatrix *matrix)
static int32_t ApplyMaskPenaltyRule2(CBC_CommonByteMatrix *matrix)
static int32_t ApplyMaskPenaltyRule3(CBC_CommonByteMatrix *matrix)
static int32_t ApplyMaskPenaltyRule4(CBC_CommonByteMatrix *matrix)
static bool BuildMatrix(CBC_QRCoderBitVector *dataBits, const CBC_QRCoderErrorCorrectionLevel *ecLevel, int32_t version, int32_t maskPattern, CBC_CommonByteMatrix *matrix)
static CBC_QRCoderMode * sNUMERIC
int32_t GetCharacterCountBits(int32_t number) const
static CBC_QRCoderMode * sBYTE
static CBC_QRCoderMode * sALPHANUMERIC
int32_t GetDimensionForVersion() const
int32_t GetVersionNumber() const
static const CBC_QRCoderVersion * GetVersionForNumber(int32_t versionNumber)
static constexpr int32_t kMaxVersion
int32_t GetTotalCodeWords() const
const CBC_QRCoderECBlocks * GetECBlocksForLevel(const CBC_QRCoderErrorCorrectionLevel &ecLevel) const
void SetMatrixWidth(int32_t width)
int32_t GetNumRSBlocks() const
int32_t GetNumTotalBytes() const
void SetNumECBytes(int32_t value)
void SetECLevel(const CBC_QRCoderErrorCorrectionLevel *ecLevel)
void SetNumRSBlocks(int32_t block)
const CBC_QRCoderErrorCorrectionLevel * GetECLevel() const
int32_t GetMatrixWidth() const
int32_t GetNumDataBytes() const
static constexpr int32_t kNumMaskPatterns
int32_t GetMaskPattern() const
void SetNumDataBytes(int32_t bytes)
void SetNumTotalBytes(int32_t value)
int32_t GetVersion() const
void SetVersion(int32_t version)
CBC_ReedSolomonEncoder(CBC_ReedSolomonGF256 *field)
CBC_ReedSolomonGF256(int32_t primitive)