LzmaConnectorWriter::LzmaConnectorWriter (const CompressionSetting * const pCompressionSetting, unsigned long ulOutBufSize) : ConnectorWriter (pCompressionSetting) { if (ulOutBufSize == 0) { // Throw C++ exception here } _ulOutBufSize = ulOutBufSize; if ((_pOutputBuffer = new unsigned char [_ulOutBufSize]) == NULL) { // throw C++ exception here } resetCompStream(); if (LZMA_OK != lzma_easy_encoder (&_lzmaCompStream, getCompressionLevel(), COMPRESSION_CHECK)) { // Throw C++ exception here } }
int LzmaConnectorReader::resetConnectorReader (void) { lzma_end (&_lzmaDecompStream); resetDecompStream(); if (LZMA_OK != lzma_stream_decoder (&_lzmaDecompStream, lzma_easy_decoder_memusage (getCompressionLevel()), DECODER_FLAGS)) { return -1; } return 0; }
int LzmaConnectorWriter::writeDataAndResetWriter (const unsigned char *pSrc, unsigned int uiSrcLen, unsigned char **pDest, unsigned int &uiDestLen) { int rc; unsigned int uiOldAvail_out = 0; bool bDone = false; *pDest = NULL; uiDestLen = 0; if (!pSrc || (uiSrcLen == 0)) { lzma_end (&_lzmaCompStream); resetCompStream(); if (LZMA_OK != lzma_easy_encoder (&_lzmaCompStream, getCompressionLevel(), COMPRESSION_CHECK)) { return -1; } return 0; } _lzmaCompStream.next_in = pSrc; _lzmaCompStream.avail_in = uiSrcLen; while (!bDone) { uiOldAvail_out = _lzmaCompStream.avail_out; if (0 != (rc = lzma_code ( &_lzmaCompStream, LZMA_FINISH))) { if (rc == LZMA_STREAM_END) { bDone = true; } else if (rc != LZMA_OK) { checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError, "deflate with flag Z_FINISH returned with error code %d\n", rc); uiDestLen = 0; *pDest = NULL; return -2; } } if (_lzmaCompStream.avail_out < _ulOutBufSize) { uiDestLen += (uiOldAvail_out - _lzmaCompStream.avail_out); if (_lzmaCompStream.avail_out == 0) { _ulOutBufSize *= 2; _pOutputBuffer = (unsigned char*) realloc (_pOutputBuffer, _ulOutBufSize); if (!_pOutputBuffer) { checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError, "error trying to realloc %u (previously %u) bytes\n", _ulOutBufSize, _ulOutBufSize/2); uiDestLen = 0; *pDest = NULL; return -3; } } _lzmaCompStream.avail_out = _ulOutBufSize - uiDestLen; _lzmaCompStream.next_out = _pOutputBuffer + uiDestLen; } else if (!bDone) { // deflate was not done but did not put anything into the output buffer checkAndLogMsg ("LzmaConnectorWriter::flush", Logger::L_MildError, "lzma_code() with flag LZMA_FINISH didn't produce new output but returned code is not LZMA_STREAM_END (code: %d)\n", rc); uiDestLen = 0; *pDest = NULL; return -4; } } if (uiDestLen > 0) { *pDest = _pOutputBuffer; } else { *pDest = NULL; } _lzmaCompStream.avail_out = _ulOutBufSize; _lzmaCompStream.next_out = _pOutputBuffer; _bFlushed = true; lzma_end (&_lzmaCompStream); resetCompStream(); if (LZMA_OK != lzma_easy_encoder (&_lzmaCompStream, getCompressionLevel(), COMPRESSION_CHECK)) { return -5; } return 0; }
LzmaConnectorReader::LzmaConnectorReader (const CompressionSettings & compressionSettings, unsigned int ulInBufSize, unsigned int ulOutBufSize) : ConnectorReader{compressionSettings} { if ((ulOutBufSize == 0) || (ulInBufSize == 0) || (nullptr == (_pOutputBuffer = new unsigned char[ulOutBufSize])) || (nullptr == (_pInputBuffer = new unsigned char[ulInBufSize]))) { // throw a c++ exception here } _ulInBufSize = ulInBufSize; _ulOutBufSize = ulOutBufSize; resetDecompStream(); if (LZMA_OK != lzma_stream_decoder (&_lzmaDecompStream, lzma_easy_decoder_memusage (getCompressionLevel()), DECODER_FLAGS)) { // throw a c++ exception here } }