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