1 // Copyright 2014 PDFium Authors. All rights reserved.
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/base/optional.h"
31
32 namespace {
33
EncodeASCIIDigits(wchar_t digit1,wchar_t digit2)34 Optional<wchar_t> EncodeASCIIDigits(wchar_t digit1, wchar_t digit2) {
35 if (!FXSYS_IsDecimalDigit(digit1) || !FXSYS_IsDecimalDigit(digit2)) {
36 // This could potentially return 0 as a sentinel value. Then this function
37 // can just return wchar_t instead of Optional<wchar_t>.
38 return {};
39 }
40 return static_cast<wchar_t>((digit1 - 48) * 10 + (digit2 - 48) + 130);
41 }
42
DetermineConsecutiveDigitCount(const WideString & msg,size_t startpos)43 size_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
59 CBC_ASCIIEncoder::CBC_ASCIIEncoder() = default;
60
61 CBC_ASCIIEncoder::~CBC_ASCIIEncoder() = default;
62
GetEncodingMode()63 CBC_HighLevelEncoder::Encoding CBC_ASCIIEncoder::GetEncodingMode() {
64 return CBC_HighLevelEncoder::Encoding::ASCII;
65 }
66
Encode(CBC_EncoderContext * context)67 bool CBC_ASCIIEncoder::Encode(CBC_EncoderContext* context) {
68 size_t n = DetermineConsecutiveDigitCount(context->m_msg, context->m_pos);
69 if (n >= 2) {
70 Optional<wchar_t> code = EncodeASCIIDigits(
71 context->m_msg[context->m_pos], context->m_msg[context->m_pos + 1]);
72 if (!code)
73 return false;
74
75 context->writeCodeword(*code);
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) {
85 case CBC_HighLevelEncoder::Encoding::BASE256:
86 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_BASE256);
87 break;
88 case CBC_HighLevelEncoder::Encoding::C40:
89 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_C40);
90 break;
91 case CBC_HighLevelEncoder::Encoding::X12:
92 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_ANSIX12);
93 break;
94 case CBC_HighLevelEncoder::Encoding::TEXT:
95 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_TEXT);
96 break;
97 case CBC_HighLevelEncoder::Encoding::EDIFACT:
98 context->writeCodeword(CBC_HighLevelEncoder::LATCH_TO_EDIFACT);
99 break;
100 default:
101 return false;
102 }
103 context->SignalEncoderChange(newMode);
104 return true;
105 }
106
107 if (CBC_HighLevelEncoder::IsExtendedASCII(c)) {
108 context->writeCodeword(CBC_HighLevelEncoder::UPPER_SHIFT);
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 }
116