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 2010 ZXing authors
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/oned/BC_OnedCode128Writer.h"
24 
25 #include <cctype>
26 #include <memory>
27 
28 #include "core/fxcrt/fx_memory_wrappers.h"
29 #include "fxbarcode/BC_Writer.h"
30 #include "fxbarcode/oned/BC_OneDimWriter.h"
31 
32 namespace {
33 
34 constexpr size_t kPatternSize = 7;
35 
36 const int8_t CODE_PATTERNS[107][kPatternSize] = {
37     {2, 1, 2, 2, 2, 2, 0}, {2, 2, 2, 1, 2, 2, 0}, {2, 2, 2, 2, 2, 1, 0},
38     {1, 2, 1, 2, 2, 3, 0}, {1, 2, 1, 3, 2, 2, 0}, {1, 3, 1, 2, 2, 2, 0},
39     {1, 2, 2, 2, 1, 3, 0}, {1, 2, 2, 3, 1, 2, 0}, {1, 3, 2, 2, 1, 2, 0},
40     {2, 2, 1, 2, 1, 3, 0}, {2, 2, 1, 3, 1, 2, 0}, {2, 3, 1, 2, 1, 2, 0},
41     {1, 1, 2, 2, 3, 2, 0}, {1, 2, 2, 1, 3, 2, 0}, {1, 2, 2, 2, 3, 1, 0},
42     {1, 1, 3, 2, 2, 2, 0}, {1, 2, 3, 1, 2, 2, 0}, {1, 2, 3, 2, 2, 1, 0},
43     {2, 2, 3, 2, 1, 1, 0}, {2, 2, 1, 1, 3, 2, 0}, {2, 2, 1, 2, 3, 1, 0},
44     {2, 1, 3, 2, 1, 2, 0}, {2, 2, 3, 1, 1, 2, 0}, {3, 1, 2, 1, 3, 1, 0},
45     {3, 1, 1, 2, 2, 2, 0}, {3, 2, 1, 1, 2, 2, 0}, {3, 2, 1, 2, 2, 1, 0},
46     {3, 1, 2, 2, 1, 2, 0}, {3, 2, 2, 1, 1, 2, 0}, {3, 2, 2, 2, 1, 1, 0},
47     {2, 1, 2, 1, 2, 3, 0}, {2, 1, 2, 3, 2, 1, 0}, {2, 3, 2, 1, 2, 1, 0},
48     {1, 1, 1, 3, 2, 3, 0}, {1, 3, 1, 1, 2, 3, 0}, {1, 3, 1, 3, 2, 1, 0},
49     {1, 1, 2, 3, 1, 3, 0}, {1, 3, 2, 1, 1, 3, 0}, {1, 3, 2, 3, 1, 1, 0},
50     {2, 1, 1, 3, 1, 3, 0}, {2, 3, 1, 1, 1, 3, 0}, {2, 3, 1, 3, 1, 1, 0},
51     {1, 1, 2, 1, 3, 3, 0}, {1, 1, 2, 3, 3, 1, 0}, {1, 3, 2, 1, 3, 1, 0},
52     {1, 1, 3, 1, 2, 3, 0}, {1, 1, 3, 3, 2, 1, 0}, {1, 3, 3, 1, 2, 1, 0},
53     {3, 1, 3, 1, 2, 1, 0}, {2, 1, 1, 3, 3, 1, 0}, {2, 3, 1, 1, 3, 1, 0},
54     {2, 1, 3, 1, 1, 3, 0}, {2, 1, 3, 3, 1, 1, 0}, {2, 1, 3, 1, 3, 1, 0},
55     {3, 1, 1, 1, 2, 3, 0}, {3, 1, 1, 3, 2, 1, 0}, {3, 3, 1, 1, 2, 1, 0},
56     {3, 1, 2, 1, 1, 3, 0}, {3, 1, 2, 3, 1, 1, 0}, {3, 3, 2, 1, 1, 1, 0},
57     {3, 1, 4, 1, 1, 1, 0}, {2, 2, 1, 4, 1, 1, 0}, {4, 3, 1, 1, 1, 1, 0},
58     {1, 1, 1, 2, 2, 4, 0}, {1, 1, 1, 4, 2, 2, 0}, {1, 2, 1, 1, 2, 4, 0},
59     {1, 2, 1, 4, 2, 1, 0}, {1, 4, 1, 1, 2, 2, 0}, {1, 4, 1, 2, 2, 1, 0},
60     {1, 1, 2, 2, 1, 4, 0}, {1, 1, 2, 4, 1, 2, 0}, {1, 2, 2, 1, 1, 4, 0},
61     {1, 2, 2, 4, 1, 1, 0}, {1, 4, 2, 1, 1, 2, 0}, {1, 4, 2, 2, 1, 1, 0},
62     {2, 4, 1, 2, 1, 1, 0}, {2, 2, 1, 1, 1, 4, 0}, {4, 1, 3, 1, 1, 1, 0},
63     {2, 4, 1, 1, 1, 2, 0}, {1, 3, 4, 1, 1, 1, 0}, {1, 1, 1, 2, 4, 2, 0},
64     {1, 2, 1, 1, 4, 2, 0}, {1, 2, 1, 2, 4, 1, 0}, {1, 1, 4, 2, 1, 2, 0},
65     {1, 2, 4, 1, 1, 2, 0}, {1, 2, 4, 2, 1, 1, 0}, {4, 1, 1, 2, 1, 2, 0},
66     {4, 2, 1, 1, 1, 2, 0}, {4, 2, 1, 2, 1, 1, 0}, {2, 1, 2, 1, 4, 1, 0},
67     {2, 1, 4, 1, 2, 1, 0}, {4, 1, 2, 1, 2, 1, 0}, {1, 1, 1, 1, 4, 3, 0},
68     {1, 1, 1, 3, 4, 1, 0}, {1, 3, 1, 1, 4, 1, 0}, {1, 1, 4, 1, 1, 3, 0},
69     {1, 1, 4, 3, 1, 1, 0}, {4, 1, 1, 1, 1, 3, 0}, {4, 1, 1, 3, 1, 1, 0},
70     {1, 1, 3, 1, 4, 1, 0}, {1, 1, 4, 1, 3, 1, 0}, {3, 1, 1, 1, 4, 1, 0},
71     {4, 1, 1, 1, 3, 1, 0}, {2, 1, 1, 4, 1, 2, 0}, {2, 1, 1, 2, 1, 4, 0},
72     {2, 1, 1, 2, 3, 2, 0}, {2, 3, 3, 1, 1, 1, 2}};
73 
74 const int32_t CODE_START_B = 104;
75 const int32_t CODE_START_C = 105;
76 const int32_t CODE_STOP = 106;
77 
IsInOnedCode128Alphabet(wchar_t ch)78 bool IsInOnedCode128Alphabet(wchar_t ch) {
79   int32_t index = static_cast<int32_t>(ch);
80   return index >= 32 && index <= 126 && index != 34;
81 }
82 
83 }  // namespace
84 
CBC_OnedCode128Writer(BC_TYPE type)85 CBC_OnedCode128Writer::CBC_OnedCode128Writer(BC_TYPE type)
86     : m_codeFormat(type) {
87   ASSERT(m_codeFormat == BC_CODE128_B || m_codeFormat == BC_CODE128_C);
88 }
89 
90 CBC_OnedCode128Writer::~CBC_OnedCode128Writer() = default;
91 
CheckContentValidity(WideStringView contents)92 bool CBC_OnedCode128Writer::CheckContentValidity(WideStringView contents) {
93   return HasValidContentSize(contents) &&
94          std::all_of(contents.begin(), contents.end(), IsInOnedCode128Alphabet);
95 }
96 
FilterContents(WideStringView contents)97 WideString CBC_OnedCode128Writer::FilterContents(WideStringView contents) {
98   const wchar_t limit = m_codeFormat == BC_CODE128_B ? 126 : 106;
99 
100   WideString filtered;
101   filtered.Reserve(contents.GetLength());
102   for (size_t i = 0; i < contents.GetLength(); i++) {
103     wchar_t ch = contents[i];
104     if (ch > 175) {
105       i++;
106       continue;
107     }
108     if (ch >= 32 && ch <= limit)
109       filtered += ch;
110   }
111   return filtered;
112 }
113 
SetTextLocation(BC_TEXT_LOC location)114 bool CBC_OnedCode128Writer::SetTextLocation(BC_TEXT_LOC location) {
115   if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) {
116     return false;
117   }
118   m_locTextLoc = location;
119   return true;
120 }
121 
EncodeWithHint(const ByteString & contents,BCFORMAT format,int32_t & outWidth,int32_t & outHeight,int32_t hints)122 uint8_t* CBC_OnedCode128Writer::EncodeWithHint(const ByteString& contents,
123                                                BCFORMAT format,
124                                                int32_t& outWidth,
125                                                int32_t& outHeight,
126                                                int32_t hints) {
127   if (format != BCFORMAT_CODE_128)
128     return nullptr;
129   return CBC_OneDimWriter::EncodeWithHint(contents, format, outWidth, outHeight,
130                                           hints);
131 }
132 
EncodeImpl(const ByteString & contents,int32_t & outLength)133 uint8_t* CBC_OnedCode128Writer::EncodeImpl(const ByteString& contents,
134                                            int32_t& outLength) {
135   if (contents.GetLength() < 1 || contents.GetLength() > 80)
136     return nullptr;
137 
138   std::vector<int32_t> patterns;
139   int32_t checkSum = 0;
140   if (m_codeFormat == BC_CODE128_B)
141     checkSum = Encode128B(contents, &patterns);
142   else
143     checkSum = Encode128C(contents, &patterns);
144 
145   checkSum %= 103;
146   patterns.push_back(checkSum);
147   patterns.push_back(CODE_STOP);
148   m_iContentLen = contents.GetLength() + 3;
149   int32_t codeWidth = 0;
150   for (const auto& patternIndex : patterns) {
151     const int8_t* pattern = CODE_PATTERNS[patternIndex];
152     for (size_t i = 0; i < kPatternSize; ++i)
153       codeWidth += pattern[i];
154   }
155   outLength = codeWidth;
156   std::unique_ptr<uint8_t, FxFreeDeleter> result(FX_Alloc(uint8_t, outLength));
157   int32_t pos = 0;
158   for (size_t i = 0; i < patterns.size(); ++i) {
159     const int8_t* pattern = CODE_PATTERNS[patterns[i]];
160     pos += AppendPattern(result.get(), pos, pattern, kPatternSize, true);
161   }
162   return result.release();
163 }
164 
165 // static
Encode128B(const ByteString & contents,std::vector<int32_t> * patterns)166 int32_t CBC_OnedCode128Writer::Encode128B(const ByteString& contents,
167                                           std::vector<int32_t>* patterns) {
168   int32_t checkWeight = 1;
169   patterns->push_back(CODE_START_B);
170   int32_t checkSum = CODE_START_B * checkWeight;
171   for (size_t position = 0; position < contents.GetLength(); position++) {
172     int32_t patternIndex = contents[position] - ' ';
173     patterns->push_back(patternIndex);
174     checkSum += patternIndex * checkWeight++;
175   }
176   return checkSum;
177 }
178 
179 // static
Encode128C(const ByteString & contents,std::vector<int32_t> * patterns)180 int32_t CBC_OnedCode128Writer::Encode128C(const ByteString& contents,
181                                           std::vector<int32_t>* patterns) {
182   int32_t checkWeight = 1;
183   patterns->push_back(CODE_START_C);
184   int32_t checkSum = CODE_START_C * checkWeight;
185   size_t position = 0;
186   while (position < contents.GetLength()) {
187     int32_t patternIndex;
188     char ch = contents[position];
189     if (std::isdigit(ch)) {
190       patternIndex = FXSYS_atoi(
191           contents.Substr(position, contents.IsValidIndex(position + 1) ? 2 : 1)
192               .c_str());
193       ++position;
194       if (position < contents.GetLength() && std::isdigit(contents[position]))
195         ++position;
196     } else {
197       patternIndex = static_cast<int32_t>(ch);
198       ++position;
199     }
200     patterns->push_back(patternIndex);
201     checkSum += patternIndex * checkWeight++;
202   }
203   return checkSum;
204 }
205