124 static constexpr size_t kMaxNumericInputLength = 3116;
127 if (msg.GetLength() > kMaxNumericInputLength)
134 if (msg
.Back() == kMacroTrailer) {
135 WideString left = msg.First(6);
136 if (left
== kMacro05Header) {
140 }
else if (left
== kMacro06Header) {
147 std::vector<std::unique_ptr<CBC_Encoder>> encoders;
148 encoders.push_back(
std::make_unique<CBC_ASCIIEncoder>());
150 encoders.push_back(
std::make_unique<CBC_TextEncoder>());
151 encoders.push_back(
std::make_unique<CBC_X12Encoder>());
152 encoders.push_back(
std::make_unique<CBC_EdifactEncoder>());
153 encoders.push_back(
std::make_unique<CBC_Base256Encoder>());
156 if (!encoders[EncoderIndex(encodingMode)]->Encode(&context))
164 size_t len = context.m_codewords.GetLength();
168 size_t capacity = context.m_symbolInfo->data_capacity();
169 if (len < capacity) {
173 WideString codewords = context.m_codewords;
174 if (codewords.GetLength() < capacity)
177 while (codewords.GetLength() < capacity)
178 codewords += Randomize253State(kPad, codewords.GetLength() + 1);
180 DCHECK(!codewords.IsEmpty());
186 const WideString& msg,
189 if (startpos >= msg.GetLength())
192 std::array<
float, kEncoderCount> charCounts;
194 charCounts = {0, 1, 1, 1, 1, 1.25f};
196 charCounts = {1, 2, 2, 2, 2, 2.25f};
197 charCounts[EncoderIndex(currentMode)] = 0;
200 size_t charsProcessed = 0;
202 if ((startpos + charsProcessed) == msg.GetLength()) {
203 std::array<int32_t, kEncoderCount> intCharCounts;
204 std::array<uint8_t, kEncoderCount> mins;
205 int32_t min = FindMinimums(charCounts, &intCharCounts, &mins);
206 if (intCharCounts[EncoderIndex(Encoding::ASCII)] == min)
208 const int32_t minCount = GetMinimumCount(mins);
210 if (mins[EncoderIndex(Encoding::BASE256)] > 0)
212 if (mins[EncoderIndex(Encoding::EDIFACT)] > 0)
214 if (mins[EncoderIndex(Encoding::TEXT)] > 0)
216 if (mins[EncoderIndex(Encoding::X12)] > 0)
222 wchar_t c = msg[startpos + charsProcessed];
225 auto& count = charCounts[EncoderIndex(Encoding::ASCII)];
229 count = ceilf(count) + 2;
231 count = ceilf(count) + 1;
235 auto& count = charCounts[EncoderIndex(Encoding::C40)];
237 count += 2.0f / 3.0f;
239 count += 8.0f / 3.0f;
241 count += 4.0f / 3.0f;
245 auto& count = charCounts[EncoderIndex(Encoding::TEXT)];
247 count += 2.0f / 3.0f;
249 count += 8.0f / 3.0f;
251 count += 4.0f / 3.0f;
255 auto& count = charCounts[EncoderIndex(Encoding::X12)];
257 count += 2.0f / 3.0f;
259 count += 13.0f / 3.0f;
261 count += 10.0f / 3.0f;
265 auto& count = charCounts[EncoderIndex(Encoding::EDIFACT)];
266 if (IsNativeEDIFACT(c))
267 count += 3.0f / 4.0f;
269 count += 17.0f / 4.0f;
271 count += 13.0f / 4.0f;
274 charCounts[EncoderIndex(Encoding::BASE256)]++;
275 if (charsProcessed < 4)
278 std::array<int32_t, kEncoderCount> intCharCounts;
279 std::array<uint8_t, kEncoderCount> mins;
280 FindMinimums(charCounts, &intCharCounts, &mins);
281 int32_t minCount = GetMinimumCount(mins);
282 int32_t ascii_count = intCharCounts[EncoderIndex(Encoding::ASCII)];
283 int32_t c40_count = intCharCounts[EncoderIndex(Encoding::C40)];
284 int32_t text_count = intCharCounts[EncoderIndex(Encoding::TEXT)];
285 int32_t x12_count = intCharCounts[EncoderIndex(Encoding::X12)];
286 int32_t editfact_count = intCharCounts[EncoderIndex(Encoding::EDIFACT)];
287 int32_t base256_count = intCharCounts[EncoderIndex(Encoding::BASE256)];
288 int32_t bet_min =
std::min({base256_count, editfact_count, text_count});
289 if (ascii_count < bet_min && ascii_count < c40_count &&
290 ascii_count < x12_count) {
293 if (base256_count < ascii_count ||
294 (mins[EncoderIndex(Encoding::C40)] +
295 mins[EncoderIndex(Encoding::TEXT)] +
296 mins[EncoderIndex(Encoding::X12)] +
297 mins[EncoderIndex(Encoding::EDIFACT)]) == 0) {
301 if (mins[EncoderIndex(Encoding::EDIFACT)] > 0)
303 if (mins[EncoderIndex(Encoding::TEXT)] > 0)
305 if (mins[EncoderIndex(Encoding::X12)] > 0)
308 if (c40_count + 1 < ascii_count && c40_count + 1 < bet_min) {
309 if (c40_count < x12_count)
311 if (c40_count == x12_count) {
312 size_t p = startpos + charsProcessed + 1;
313 while (p < msg.GetLength()) {
315 if (IsX12TermSep(tc))
317 if (!IsNativeX12(tc))
void writeCodeword(wchar_t codeword)
CBC_HighLevelEncoder::Encoding m_newEncoding
void setSkipAtEnd(int32_t count)
bool HasCharactersOutsideISO88591Encoding() const
CBC_EncoderContext(const WideString &msg)