xref: /aosp_15_r20/external/pdfium/fxbarcode/datamatrix/BC_Base256Encoder.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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_Base256Encoder.h"
24 
25 #include "fxbarcode/datamatrix/BC_Encoder.h"
26 #include "fxbarcode/datamatrix/BC_EncoderContext.h"
27 #include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
28 #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
29 
30 namespace {
31 
Randomize255State(wchar_t ch,int32_t position)32 wchar_t Randomize255State(wchar_t ch, int32_t position) {
33   int32_t pseudoRandom = ((149 * position) % 255) + 1;
34   int32_t tempVariable = ch + pseudoRandom;
35   if (tempVariable <= 255)
36     return static_cast<wchar_t>(tempVariable);
37   return static_cast<wchar_t>(tempVariable - 256);
38 }
39 
40 }  // namespace
41 
42 CBC_Base256Encoder::CBC_Base256Encoder() = default;
43 
44 CBC_Base256Encoder::~CBC_Base256Encoder() = default;
45 
GetEncodingMode()46 CBC_HighLevelEncoder::Encoding CBC_Base256Encoder::GetEncodingMode() {
47   return CBC_HighLevelEncoder::Encoding::BASE256;
48 }
49 
Encode(CBC_EncoderContext * context)50 bool CBC_Base256Encoder::Encode(CBC_EncoderContext* context) {
51   WideString buffer;
52   buffer.Reserve(context->getRemainingCharacters() + 1);
53   buffer += L'\0';
54   while (context->hasMoreCharacters()) {
55     wchar_t c = context->getCurrentChar();
56     buffer += c;
57     context->m_pos++;
58     CBC_HighLevelEncoder::Encoding newMode =
59         CBC_HighLevelEncoder::LookAheadTest(context->m_msg, context->m_pos,
60                                             GetEncodingMode());
61     if (newMode != GetEncodingMode()) {
62       context->SignalEncoderChange(newMode);
63       break;
64     }
65   }
66   size_t dataCount = buffer.GetLength() - 1;
67   char buf[128];
68   FXSYS_itoa(dataCount, buf, 10);
69   buffer.SetAt(0, static_cast<wchar_t>(*buf) - '0');
70   int32_t lengthFieldSize = 1;
71   int32_t currentSize =
72       context->getCodewordCount() + dataCount + lengthFieldSize;
73   if (!context->UpdateSymbolInfo(currentSize))
74     return false;
75 
76   bool mustPad = (context->m_symbolInfo->data_capacity() - currentSize) > 0;
77   if (context->hasMoreCharacters() || mustPad) {
78     if (dataCount <= 249) {
79       buffer.SetAt(0, static_cast<wchar_t>(dataCount));
80     } else if (dataCount > 249 && dataCount <= 1555) {
81       buffer.SetAt(0, static_cast<wchar_t>((dataCount / 250) + 249));
82       buffer.Insert(1, static_cast<wchar_t>(dataCount % 250));
83     } else {
84       return false;
85     }
86   }
87   for (const auto& c : buffer) {
88     context->writeCodeword(
89         Randomize255State(c, context->getCodewordCount() + 1));
90   }
91   return true;
92 }
93