Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
BC_ASCIIEncoder.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6// Original code is licensed as follows:
7/*
8 * Copyright 2006-2007 Jeremias Maerki.
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include "fxbarcode/datamatrix/BC_ASCIIEncoder.h"
24
25#include "core/fxcrt/fx_extension.h"
26#include "fxbarcode/datamatrix/BC_Encoder.h"
27#include "fxbarcode/datamatrix/BC_EncoderContext.h"
28#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
29#include "fxbarcode/datamatrix/BC_SymbolInfo.h"
30#include "third_party/abseil-cpp/absl/types/optional.h"
31
32namespace {
33
34absl::optional<wchar_t> EncodeASCIIDigits(wchar_t digit1, wchar_t digit2) {
36 // This could potentially return 0 as a sentinel value. Then this function
37 // can just return wchar_t instead of absl::optional<wchar_t>.
38 return absl::nullopt;
39 }
40 return static_cast<wchar_t>((digit1 - 48) * 10 + (digit2 - 48) + 130);
41}
42
43size_t DetermineConsecutiveDigitCount(const WideString& msg, size_t startpos) {
44 // This is faster in debug builds and helpful for fuzzers.
45 // Access |data| with care.
46 size_t count = 0;
47 const size_t size = msg.GetLength();
48 const wchar_t* data = msg.c_str();
49 for (size_t i = startpos; i < size; ++i) {
50 if (!FXSYS_IsDecimalDigit(data[i]))
51 break;
52 ++count;
53 }
54 return count;
55}
56
57} // namespace
58
59CBC_ASCIIEncoder::CBC_ASCIIEncoder() = default;
60
61CBC_ASCIIEncoder::~CBC_ASCIIEncoder() = default;
62
66
67bool CBC_ASCIIEncoder::Encode(CBC_EncoderContext* context) {
68 size_t n = DetermineConsecutiveDigitCount(context->m_msg, context->m_pos);
69 if (n >= 2) {
70 absl::optional<wchar_t> code = EncodeASCIIDigits(
71 context->m_msg[context->m_pos], context->m_msg[context->m_pos + 1]);
72 if (!code.has_value())
73 return false;
74
75 context->writeCodeword(code.value());
76 context->m_pos += 2;
77 return true;
78 }
79
80 wchar_t c = context->getCurrentChar();
81 CBC_HighLevelEncoder::Encoding newMode = CBC_HighLevelEncoder::LookAheadTest(
82 context->m_msg, context->m_pos, GetEncodingMode());
83 if (newMode != GetEncodingMode()) {
84 switch (newMode) {
87 break;
90 break;
93 break;
96 break;
99 break;
100 default:
101 return false;
102 }
103 context->SignalEncoderChange(newMode);
104 return true;
105 }
106
109 context->writeCodeword(static_cast<wchar_t>(c - 128 + 1));
110 } else {
111 context->writeCodeword(static_cast<wchar_t>(c + 1));
112 }
113 context->m_pos++;
114 return true;
115}
bool Encode(CBC_EncoderContext *context) override
~CBC_ASCIIEncoder() override
CBC_HighLevelEncoder::Encoding GetEncodingMode() override
void writeCodeword(wchar_t codeword)
void SignalEncoderChange(CBC_HighLevelEncoder::Encoding encoding)
static const wchar_t LATCH_TO_TEXT
static const wchar_t LATCH_TO_ANSIX12
static const wchar_t LATCH_TO_BASE256
static bool IsExtendedASCII(wchar_t ch)
static const wchar_t LATCH_TO_C40
static const wchar_t UPPER_SHIFT
static const wchar_t LATCH_TO_EDIFACT
const wchar_t * c_str() const
Definition widestring.h:81
bool FXSYS_IsDecimalDigit(wchar_t c)