void CBC_EdifactEncoder::handleEOD(CBC_EncoderContext& context, CFX_WideString buffer, int32_t& e) { int32_t count = buffer.GetLength(); if (count == 0) { return; } if (count == 1) { context.updateSymbolInfo(e); if (e != BCExceptionNO) { return; } int32_t available = context.m_symbolInfo->m_dataCapacity - context.getCodewordCount(); int32_t remaining = context.getRemainingCharacters(); if (remaining == 0 && available <= 2) { return; } } if (count > 4) { e = BCExceptionIllegalStateCountMustNotExceed4; return; } int32_t restChars = count - 1; CFX_WideString encoded = encodeToCodewords(buffer, 0, e); if (e != BCExceptionNO) { return; } FX_BOOL endOfSymbolReached = !context.hasMoreCharacters(); FX_BOOL restInAscii = endOfSymbolReached && restChars <= 2; if (restChars <= 2) { context.updateSymbolInfo(context.getCodewordCount() + restChars, e); if (e != BCExceptionNO) { return; } int32_t available = context.m_symbolInfo->m_dataCapacity - context.getCodewordCount(); if (available >= 3) { restInAscii = FALSE; context.updateSymbolInfo(context.getCodewordCount() + encoded.GetLength(), e); if (e != BCExceptionNO) { return; } } } if (restInAscii) { context.resetSymbolInfo(); context.m_pos -= restChars; } else { context.writeCodewords(encoded); } context.signalEncoderChange(ASCII_ENCODATION); }
void CBC_Base256Encoder::Encode(CBC_EncoderContext& context, int32_t& e) { CFX_WideString buffer; buffer += (FX_WCHAR)'\0'; while (context.hasMoreCharacters()) { FX_WCHAR c = context.getCurrentChar(); buffer += c; context.m_pos++; int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( context.m_msg, context.m_pos, getEncodingMode()); if (newMode != getEncodingMode()) { context.signalEncoderChange(newMode); break; } } int32_t dataCount = buffer.GetLength() - 1; FX_CHAR buf[128]; FXSYS_itoa(dataCount, buf, 10); buffer.SetAt(0, FX_WCHAR(*buf) - '0'); int32_t lengthFieldSize = 1; int32_t currentSize = context.getCodewordCount() + dataCount + lengthFieldSize; context.updateSymbolInfo(currentSize, e); if (e != BCExceptionNO) { return; } FX_BOOL mustPad = (context.m_symbolInfo->m_dataCapacity - currentSize) > 0; if (context.hasMoreCharacters() || mustPad) { if (dataCount <= 249) { buffer.SetAt(0, (FX_WCHAR)dataCount); } else if (dataCount > 249 && dataCount <= 1555) { buffer.SetAt(0, (FX_WCHAR)((dataCount / 250) + 249)); buffer.Insert(1, (FX_WCHAR)(dataCount % 250)); } else { e = BCExceptionIllegalStateMessageLengthInvalid; return; } } for (int32_t i = 0, c = buffer.GetLength(); i < c; i++) { context.writeCodeword( randomize255State(buffer.GetAt(i), context.getCodewordCount() + 1)); } }
void CBC_C40Encoder::Encode(CBC_EncoderContext& context, int32_t& e) { CFX_WideString buffer; while (context.hasMoreCharacters()) { FX_WCHAR c = context.getCurrentChar(); context.m_pos++; int32_t lastCharSize = encodeChar(c, buffer, e); if (e != BCExceptionNO) { return; } int32_t unwritten = (buffer.GetLength() / 3) * 2; int32_t curCodewordCount = context.getCodewordCount() + unwritten; context.updateSymbolInfo(curCodewordCount, e); if (e != BCExceptionNO) { return; } int32_t available = context.m_symbolInfo->m_dataCapacity - curCodewordCount; if (!context.hasMoreCharacters()) { CFX_WideString removed; if ((buffer.GetLength() % 3) == 2) { if (available < 2 || available > 2) { lastCharSize = backtrackOneCharacter(context, buffer, removed, lastCharSize, e); if (e != BCExceptionNO) { return; } } } while ((buffer.GetLength() % 3) == 1 && ((lastCharSize <= 3 && available != 1) || lastCharSize > 3)) { lastCharSize = backtrackOneCharacter(context, buffer, removed, lastCharSize, e); if (e != BCExceptionNO) { return; } } break; } int32_t count = buffer.GetLength(); if ((count % 3) == 0) { int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( context.m_msg, context.m_pos, getEncodingMode()); if (newMode != getEncodingMode()) { context.signalEncoderChange(newMode); break; } } } handleEOD(context, buffer, e); }
void CBC_C40Encoder::handleEOD(CBC_EncoderContext& context, CFX_WideString& buffer, int32_t& e) { int32_t unwritten = (buffer.GetLength() / 3) * 2; int32_t rest = buffer.GetLength() % 3; int32_t curCodewordCount = context.getCodewordCount() + unwritten; context.updateSymbolInfo(curCodewordCount, e); if (e != BCExceptionNO) { return; } int32_t available = context.m_symbolInfo->m_dataCapacity - curCodewordCount; if (rest == 2) { buffer += (FX_WCHAR)'\0'; while (buffer.GetLength() >= 3) { writeNextTriplet(context, buffer); } if (context.hasMoreCharacters()) { context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); } } else if (available == 1 && rest == 1) { while (buffer.GetLength() >= 3) { writeNextTriplet(context, buffer); } if (context.hasMoreCharacters()) { context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); } context.m_pos--; } else if (rest == 0) { while (buffer.GetLength() >= 3) { writeNextTriplet(context, buffer); } if (available > 0 || context.hasMoreCharacters()) { context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); } } else { e = BCExceptionIllegalStateUnexpectedCase; return; } context.signalEncoderChange(ASCII_ENCODATION); }
void CBC_X12Encoder::handleEOD(CBC_EncoderContext& context, CFX_WideString& buffer, int32_t& e) { context.updateSymbolInfo(e); if (e != BCExceptionNO) { return; } int32_t available = context.m_symbolInfo->m_dataCapacity - context.getCodewordCount(); int32_t count = buffer.GetLength(); if (count == 2) { context.writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH); context.m_pos -= 2; context.signalEncoderChange(ASCII_ENCODATION); } else if (count == 1) { context.m_pos--; if (available > 1) { context.writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH); } context.signalEncoderChange(ASCII_ENCODATION); } }