void CBC_ASCIIEncoder::Encode(CBC_EncoderContext& context, int32_t& e) {
    int32_t n = CBC_HighLevelEncoder::determineConsecutiveDigitCount(
                    context.m_msg, context.m_pos);
    if (n >= 2) {
        FX_WCHAR code =
            encodeASCIIDigits(context.m_msg.GetAt(context.m_pos),
                              context.m_msg.GetAt(context.m_pos + 1), e);
        if (e != BCExceptionNO) {
            return;
        }
        context.writeCodeword(code);
        context.m_pos += 2;
    } else {
        FX_WCHAR c = context.getCurrentChar();
        int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(
                              context.m_msg, context.m_pos, getEncodingMode());
        if (newMode != getEncodingMode()) {
            switch (newMode) {
            case BASE256_ENCODATION:
                context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_BASE256);
                context.signalEncoderChange(BASE256_ENCODATION);
                return;
            case C40_ENCODATION:
                context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_C40);
                context.signalEncoderChange(C40_ENCODATION);
                return;
            case X12_ENCODATION:
                context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_ANSIX12);
                context.signalEncoderChange(X12_ENCODATION);
                break;
            case TEXT_ENCODATION:
                context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_TEXT);
                context.signalEncoderChange(TEXT_ENCODATION);
                break;
            case EDIFACT_ENCODATION:
                context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_EDIFACT);
                context.signalEncoderChange(EDIFACT_ENCODATION);
                break;
            default:
                e = BCExceptionIllegalStateIllegalMode;
                return;
            }
        } else if (CBC_HighLevelEncoder::isExtendedASCII(c)) {
            context.writeCodeword(CBC_HighLevelEncoder::UPPER_SHIFT);
            context.writeCodeword((FX_WCHAR)(c - 128 + 1));
            context.m_pos++;
        } else {
            context.writeCodeword((FX_WCHAR)(c + 1));
            context.m_pos++;
        }
    }
}
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);
  }
}
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));
  }
}