• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EdifactEncoder.h"
24 
25 #include <algorithm>
26 
27 #include "fxbarcode/datamatrix/BC_Encoder.h"
28 #include "fxbarcode/datamatrix/BC_EncoderContext.h"
29 #include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
30 #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
31 
32 namespace {
33 
EncodeToEdifactCodewords(const WideString & sb)34 WideString EncodeToEdifactCodewords(const WideString& sb) {
35   size_t len = sb.GetLength();
36   if (len == 0)
37     return WideString();
38 
39   wchar_t c1 = sb[0];
40   wchar_t c2 = len >= 2 ? sb[1] : 0;
41   wchar_t c3 = len >= 3 ? sb[2] : 0;
42   wchar_t c4 = len >= 4 ? sb[3] : 0;
43   int32_t v = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4;
44   constexpr size_t kBuflen = 3;
45   wchar_t cw[kBuflen];
46   cw[0] = static_cast<wchar_t>((v >> 16) & 255);
47   cw[1] = static_cast<wchar_t>((v >> 8) & 255);
48   cw[2] = static_cast<wchar_t>(v & 255);
49   // TODO(tsepez): stop putting binary data in strings.
50   return WideString(
51       WideStringView(pdfium::make_span(cw).first(std::min(len, kBuflen))));
52 }
53 
HandleEOD(CBC_EncoderContext * context,const WideString & buffer)54 bool HandleEOD(CBC_EncoderContext* context, const WideString& buffer) {
55   size_t count = buffer.GetLength();
56   if (count == 0)
57     return true;
58   if (count > 4)
59     return false;
60 
61   if (count == 1) {
62     if (!context->UpdateSymbolInfo())
63       return false;
64 
65     int32_t available =
66         context->m_symbolInfo->data_capacity() - context->getCodewordCount();
67     int32_t remaining = context->getRemainingCharacters();
68     if (remaining == 0 && available <= 2)
69       return true;
70   }
71 
72   int32_t restChars = count - 1;
73   WideString encoded = EncodeToEdifactCodewords(buffer);
74   if (encoded.IsEmpty())
75     return false;
76 
77   bool endOfSymbolReached = !context->hasMoreCharacters();
78   bool restInAscii = endOfSymbolReached && restChars <= 2;
79   if (restChars <= 2) {
80     if (!context->UpdateSymbolInfo(context->getCodewordCount() + restChars))
81       return false;
82 
83     int32_t available =
84         context->m_symbolInfo->data_capacity() - context->getCodewordCount();
85     if (available >= 3) {
86       restInAscii = false;
87       if (!context->UpdateSymbolInfo(context->getCodewordCount() +
88                                      encoded.GetLength())) {
89         return false;
90       }
91     }
92   }
93 
94   if (restInAscii) {
95     context->resetSymbolInfo();
96     context->m_pos -= restChars;
97   } else {
98     context->writeCodewords(encoded);
99   }
100   context->SignalEncoderChange(CBC_HighLevelEncoder::Encoding::ASCII);
101   return true;
102 }
103 
AppendEncodedChar(wchar_t c,WideString * sb)104 bool AppendEncodedChar(wchar_t c, WideString* sb) {
105   if (c >= ' ' && c <= '?') {
106     *sb += c;
107     return true;
108   }
109 
110   if (c >= '@' && c <= '^') {
111     *sb += (c - 64);
112     return true;
113   }
114 
115   return false;
116 }
117 
118 }  // namespace
119 
120 CBC_EdifactEncoder::CBC_EdifactEncoder() = default;
121 
122 CBC_EdifactEncoder::~CBC_EdifactEncoder() = default;
123 
GetEncodingMode()124 CBC_HighLevelEncoder::Encoding CBC_EdifactEncoder::GetEncodingMode() {
125   return CBC_HighLevelEncoder::Encoding::EDIFACT;
126 }
127 
Encode(CBC_EncoderContext * context)128 bool CBC_EdifactEncoder::Encode(CBC_EncoderContext* context) {
129   WideString buffer;
130   while (context->hasMoreCharacters()) {
131     wchar_t c = context->getCurrentChar();
132     if (!AppendEncodedChar(c, &buffer))
133       return false;
134 
135     context->m_pos++;
136     size_t count = buffer.GetLength();
137     if (count >= 4) {
138       WideString encoded = EncodeToEdifactCodewords(buffer);
139       if (encoded.IsEmpty())
140         return false;
141 
142       context->writeCodewords(encoded);
143       buffer.Delete(0, 4);
144       CBC_HighLevelEncoder::Encoding newMode =
145           CBC_HighLevelEncoder::LookAheadTest(context->m_msg, context->m_pos,
146                                               GetEncodingMode());
147       if (newMode != GetEncodingMode()) {
148         context->SignalEncoderChange(CBC_HighLevelEncoder::Encoding::ASCII);
149         break;
150       }
151     }
152   }
153   buffer += static_cast<wchar_t>(31);
154   return HandleEOD(context, buffer);
155 }
156