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 <optional>
26
27 #include "core/fxcrt/compiler_specific.h"
28 #include "core/fxcrt/fx_extension.h"
29 #include "fxbarcode/datamatrix/BC_Encoder.h"
30 #include "fxbarcode/datamatrix/BC_EncoderContext.h"
31 #include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
32 #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
33
34 namespace {
35
EncodeASCIIDigits(wchar_t digit1,wchar_t digit2)36 std::optional<wchar_t> EncodeASCIIDigits(wchar_t digit1, wchar_t digit2) {
37 if (!FXSYS_IsDecimalDigit(digit1) || !FXSYS_IsDecimalDigit(digit2)) {
38 // This could potentially return 0 as a sentinel value. Then this function
39 // can just return wchar_t instead of std::optional<wchar_t>.
40 return std::nullopt;
41 }
42 return static_cast<wchar_t>((digit1 - 48) * 10 + (digit2 - 48) + 130);
43 }
44
DetermineConsecutiveDigitCount(const WideString & msg,size_t startpos)45 size_t DetermineConsecutiveDigitCount(const WideString& msg, size_t startpos) {
46 // This is faster in debug builds and helpful for fuzzers.
47 // Access |data| with care.
48 size_t count = 0;
49 const size_t size = msg.GetLength();
50 const wchar_t* data = msg.c_str();
51
52 // SAFETY: performance-sensitive.
53 UNSAFE_BUFFERS({
54 for (size_t i = startpos; i < size; ++i) {
55 if (!FXSYS_IsDecimalDigit(data[i])) {
56 break;
57 }
58 ++count;
59 }
60 });
61
62 return count;
63 }
64
65 } // namespace
66
67 CBC_ASCIIEncoder::CBC_ASCIIEncoder() = default;
68
69 CBC_ASCIIEncoder::~CBC_ASCIIEncoder() = default;
70
GetEncodingMode()71 CBC_HighLevelEncoder::Encoding CBC_ASCIIEncoder::GetEncodingMode() {
72 return CBC_HighLevelEncoder::Encoding::ASCII;
73 }
74
Encode(CBC_EncoderContext * context)75 bool CBC_ASCIIEncoder::Encode(CBC_EncoderContext* context) {
76 size_t n = DetermineConsecutiveDigitCount(context->m_msg, context->m_pos);
77 if (n >= 2) {
78 std::optional<wchar_t> code = EncodeASCIIDigits(
79 context->m_msg[context->m_pos], context->m_msg[context->m_pos + 1]);
80 if (!code.has_value())
81 return false;
82
83 context->writeCodeword(code.value());
84 context->m_pos += 2;
85 return true;
86 }
87
88 wchar_t c = context->getCurrentChar();
89 CBC_HighLevelEncoder::Encoding newMode = CBC_HighLevelEncoder::LookAheadTest(
90 context->m_msg, context->m_pos, GetEncodingMode());
91 if (newMode != GetEncodingMode()) {
92 switch (newMode) {
93 case CBC_HighLevelEncoder::Encoding::BASE256:
94 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_BASE256);
95 break;
96 case CBC_HighLevelEncoder::Encoding::C40:
97 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_C40);
98 break;
99 case CBC_HighLevelEncoder::Encoding::X12:
100 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_ANSIX12);
101 break;
102 case CBC_HighLevelEncoder::Encoding::TEXT:
103 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_TEXT);
104 break;
105 case CBC_HighLevelEncoder::Encoding::EDIFACT:
106 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_EDIFACT);
107 break;
108 default:
109 return false;
110 }
111 context->SignalEncoderChange(newMode);
112 return true;
113 }
114
115 if (CBC_HighLevelEncoder::IsExtendedASCII(c)) {
116 context->writeCodeword(CBC_HighLevelEncoder::UPPER_SHIFT);
117 context->writeCodeword(static_cast<wchar_t>(c - 128 + 1));
118 } else {
119 context->writeCodeword(static_cast<wchar_t>(c + 1));
120 }
121 context->m_pos++;
122 return true;
123 }
124