Пример #1
0
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++;
        }
    }
}
Пример #2
0
void CBC_EdifactEncoder::Encode(CBC_EncoderContext& context, int32_t& e) {
  CFX_WideString buffer;
  while (context.hasMoreCharacters()) {
    FX_WCHAR c = context.getCurrentChar();
    encodeChar(c, buffer, e);
    if (e != BCExceptionNO) {
      return;
    }
    context.m_pos++;
    int32_t count = buffer.GetLength();
    if (count >= 4) {
      context.writeCodewords(encodeToCodewords(buffer, 0, e));
      if (e != BCExceptionNO) {
        return;
      }
      buffer.Delete(0, 4);
      int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(
          context.m_msg, context.m_pos, getEncodingMode());
      if (newMode != getEncodingMode()) {
        context.signalEncoderChange(ASCII_ENCODATION);
        break;
      }
    }
  }
  buffer += (FX_WCHAR)31;
  handleEOD(context, buffer, e);
}
Пример #3
0
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);
}
Пример #4
0
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);
  }
}
Пример #5
0
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);
}
Пример #6
0
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));
  }
}
Пример #7
0
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);
}
Пример #8
0
void CBC_X12Encoder::Encode(CBC_EncoderContext& context, int32_t& e) {
  CFX_WideString buffer;
  while (context.hasMoreCharacters()) {
    FX_WCHAR c = context.getCurrentChar();
    context.m_pos++;
    encodeChar(c, buffer, e);
    if (e != BCExceptionNO) {
      return;
    }
    int32_t count = buffer.GetLength();
    if ((count % 3) == 0) {
      writeNextTriplet(context, buffer);
      int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(
          context.m_msg, context.m_pos, getEncodingMode());
      if (newMode != getEncodingMode()) {
        context.signalEncoderChange(newMode);
        break;
      }
    }
  }
  handleEOD(context, buffer, e);
}