uint8_t* CBC_OnedCode128Writer::EncodeImpl(const ByteString& contents, int32_t& outLength) { if (contents.GetLength() < 1 || contents.GetLength() > 80) return nullptr; std::vector<int32_t> patterns; int32_t checkSum = 0; if (m_codeFormat == BC_CODE128_B) checkSum = Encode128B(contents, &patterns); else checkSum = Encode128C(contents, &patterns); checkSum %= 103; patterns.push_back(checkSum); patterns.push_back(CODE_STOP); m_iContentLen = contents.GetLength() + 3; int32_t codeWidth = 0; for (const auto& patternIndex : patterns) { const int8_t* pattern = CODE_PATTERNS[patternIndex]; for (size_t i = 0; i < kPatternSize; ++i) codeWidth += pattern[i]; } outLength = codeWidth; std::unique_ptr<uint8_t, FxFreeDeleter> result(FX_Alloc(uint8_t, outLength)); int32_t pos = 0; for (size_t i = 0; i < patterns.size(); ++i) { const int8_t* pattern = CODE_PATTERNS[patterns[i]]; pos += AppendPattern(result.get(), pos, pattern, kPatternSize, true); } return result.release(); }
uint8_t* CBC_OnedEAN8Writer::Encode(const CFX_ByteString& contents, int32_t& outLength, int32_t& e) { if (contents.GetLength() != 8) { e = BCExceptionDigitLengthMustBe8; return nullptr; } outLength = m_codeWidth; uint8_t* result = FX_Alloc(uint8_t, m_codeWidth); int32_t pos = 0; pos += AppendPattern(result, pos, START_END_PATTERN, 3, 1, e); if (e != BCExceptionNO) { FX_Free(result); return nullptr; } int32_t i = 0; for (i = 0; i <= 3; i++) { int32_t digit = FXSYS_atoi(contents.Mid(i, 1).c_str()); pos += AppendPattern(result, pos, L_PATTERNS[digit], 4, 0, e); if (e != BCExceptionNO) { FX_Free(result); return nullptr; } } pos += AppendPattern(result, pos, MIDDLE_PATTERN, 5, 0, e); if (e != BCExceptionNO) { FX_Free(result); return nullptr; } for (i = 4; i <= 7; i++) { int32_t digit = FXSYS_atoi(contents.Mid(i, 1).c_str()); pos += AppendPattern(result, pos, L_PATTERNS[digit], 4, 1, e); if (e != BCExceptionNO) { FX_Free(result); return nullptr; } } pos += AppendPattern(result, pos, START_END_PATTERN, 3, 1, e); if (e != BCExceptionNO) { FX_Free(result); return nullptr; } return result; }
uint8_t* CBC_OnedCode128Writer::Encode(const CFX_ByteString& contents, int32_t& outLength, int32_t& e) { if (contents.GetLength() < 1 || contents.GetLength() > 80) { e = BCExceptionContentsLengthShouldBetween1and80; return NULL; } CFX_PtrArray patterns; int32_t checkSum = 0; if (m_codeFormat == BC_CODE128_B) { checkSum = Encode128B(contents, patterns); } else if (m_codeFormat == BC_CODE128_C) { checkSum = Encode128C(contents, patterns); } else { e = BCExceptionFormatException; return NULL; } checkSum %= 103; patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[checkSum]); patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[CODE_STOP]); m_iContentLen = contents.GetLength() + 3; int32_t codeWidth = 0; for (int32_t k = 0; k < patterns.GetSize(); k++) { int32_t* pattern = (int32_t*)patterns[k]; for (int32_t j = 0; j < 7; j++) { codeWidth += pattern[j]; } } outLength = codeWidth; uint8_t* result = FX_Alloc(uint8_t, outLength); int32_t pos = 0; for (int32_t j = 0; j < patterns.GetSize(); j++) { int32_t* pattern = (int32_t*)patterns[j]; pos += AppendPattern(result, pos, pattern, 7, 1, e); if (e != BCExceptionNO) { FX_Free(result); return NULL; } } return result; }