1370b324cSopenharmony_ci// Lzma2Encoder.cpp 2370b324cSopenharmony_ci 3370b324cSopenharmony_ci#include "StdAfx.h" 4370b324cSopenharmony_ci 5370b324cSopenharmony_ci#include "../../../C/Alloc.h" 6370b324cSopenharmony_ci 7370b324cSopenharmony_ci#include "../Common/CWrappers.h" 8370b324cSopenharmony_ci#include "../Common/StreamUtils.h" 9370b324cSopenharmony_ci 10370b324cSopenharmony_ci#include "Lzma2Encoder.h" 11370b324cSopenharmony_ci 12370b324cSopenharmony_cinamespace NCompress { 13370b324cSopenharmony_ci 14370b324cSopenharmony_cinamespace NLzma { 15370b324cSopenharmony_ci 16370b324cSopenharmony_ciHRESULT SetLzmaProp(PROPID propID, const PROPVARIANT &prop, CLzmaEncProps &ep); 17370b324cSopenharmony_ci 18370b324cSopenharmony_ci} 19370b324cSopenharmony_ci 20370b324cSopenharmony_cinamespace NLzma2 { 21370b324cSopenharmony_ci 22370b324cSopenharmony_ciCEncoder::CEncoder() 23370b324cSopenharmony_ci{ 24370b324cSopenharmony_ci _encoder = NULL; 25370b324cSopenharmony_ci _encoder = Lzma2Enc_Create(&g_AlignedAlloc, &g_BigAlloc); 26370b324cSopenharmony_ci if (!_encoder) 27370b324cSopenharmony_ci throw 1; 28370b324cSopenharmony_ci} 29370b324cSopenharmony_ci 30370b324cSopenharmony_ciCEncoder::~CEncoder() 31370b324cSopenharmony_ci{ 32370b324cSopenharmony_ci if (_encoder) 33370b324cSopenharmony_ci Lzma2Enc_Destroy(_encoder); 34370b324cSopenharmony_ci} 35370b324cSopenharmony_ci 36370b324cSopenharmony_ci 37370b324cSopenharmony_ciHRESULT SetLzma2Prop(PROPID propID, const PROPVARIANT &prop, CLzma2EncProps &lzma2Props); 38370b324cSopenharmony_ciHRESULT SetLzma2Prop(PROPID propID, const PROPVARIANT &prop, CLzma2EncProps &lzma2Props) 39370b324cSopenharmony_ci{ 40370b324cSopenharmony_ci switch (propID) 41370b324cSopenharmony_ci { 42370b324cSopenharmony_ci case NCoderPropID::kBlockSize: 43370b324cSopenharmony_ci { 44370b324cSopenharmony_ci if (prop.vt == VT_UI4) 45370b324cSopenharmony_ci lzma2Props.blockSize = prop.ulVal; 46370b324cSopenharmony_ci else if (prop.vt == VT_UI8) 47370b324cSopenharmony_ci lzma2Props.blockSize = prop.uhVal.QuadPart; 48370b324cSopenharmony_ci else 49370b324cSopenharmony_ci return E_INVALIDARG; 50370b324cSopenharmony_ci break; 51370b324cSopenharmony_ci } 52370b324cSopenharmony_ci case NCoderPropID::kNumThreads: 53370b324cSopenharmony_ci if (prop.vt != VT_UI4) 54370b324cSopenharmony_ci return E_INVALIDARG; 55370b324cSopenharmony_ci lzma2Props.numTotalThreads = (int)(prop.ulVal); 56370b324cSopenharmony_ci break; 57370b324cSopenharmony_ci default: 58370b324cSopenharmony_ci RINOK(NLzma::SetLzmaProp(propID, prop, lzma2Props.lzmaProps)) 59370b324cSopenharmony_ci } 60370b324cSopenharmony_ci return S_OK; 61370b324cSopenharmony_ci} 62370b324cSopenharmony_ci 63370b324cSopenharmony_ci 64370b324cSopenharmony_ciZ7_COM7F_IMF(CEncoder::SetCoderProperties(const PROPID *propIDs, 65370b324cSopenharmony_ci const PROPVARIANT *coderProps, UInt32 numProps)) 66370b324cSopenharmony_ci{ 67370b324cSopenharmony_ci CLzma2EncProps lzma2Props; 68370b324cSopenharmony_ci Lzma2EncProps_Init(&lzma2Props); 69370b324cSopenharmony_ci 70370b324cSopenharmony_ci for (UInt32 i = 0; i < numProps; i++) 71370b324cSopenharmony_ci { 72370b324cSopenharmony_ci RINOK(SetLzma2Prop(propIDs[i], coderProps[i], lzma2Props)) 73370b324cSopenharmony_ci } 74370b324cSopenharmony_ci return SResToHRESULT(Lzma2Enc_SetProps(_encoder, &lzma2Props)); 75370b324cSopenharmony_ci} 76370b324cSopenharmony_ci 77370b324cSopenharmony_ci 78370b324cSopenharmony_ciZ7_COM7F_IMF(CEncoder::SetCoderPropertiesOpt(const PROPID *propIDs, 79370b324cSopenharmony_ci const PROPVARIANT *coderProps, UInt32 numProps)) 80370b324cSopenharmony_ci{ 81370b324cSopenharmony_ci for (UInt32 i = 0; i < numProps; i++) 82370b324cSopenharmony_ci { 83370b324cSopenharmony_ci const PROPVARIANT &prop = coderProps[i]; 84370b324cSopenharmony_ci const PROPID propID = propIDs[i]; 85370b324cSopenharmony_ci if (propID == NCoderPropID::kExpectedDataSize) 86370b324cSopenharmony_ci if (prop.vt == VT_UI8) 87370b324cSopenharmony_ci Lzma2Enc_SetDataSize(_encoder, prop.uhVal.QuadPart); 88370b324cSopenharmony_ci } 89370b324cSopenharmony_ci return S_OK; 90370b324cSopenharmony_ci} 91370b324cSopenharmony_ci 92370b324cSopenharmony_ci 93370b324cSopenharmony_ciZ7_COM7F_IMF(CEncoder::WriteCoderProperties(ISequentialOutStream *outStream)) 94370b324cSopenharmony_ci{ 95370b324cSopenharmony_ci const Byte prop = Lzma2Enc_WriteProperties(_encoder); 96370b324cSopenharmony_ci return WriteStream(outStream, &prop, 1); 97370b324cSopenharmony_ci} 98370b324cSopenharmony_ci 99370b324cSopenharmony_ci 100370b324cSopenharmony_ci#define RET_IF_WRAP_ERROR(wrapRes, sRes, sResErrorCode) \ 101370b324cSopenharmony_ci if (wrapRes != S_OK /* && (sRes == SZ_OK || sRes == sResErrorCode) */) return wrapRes; 102370b324cSopenharmony_ci 103370b324cSopenharmony_ciZ7_COM7F_IMF(CEncoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream, 104370b324cSopenharmony_ci const UInt64 * /* inSize */, const UInt64 * /* outSize */, ICompressProgressInfo *progress)) 105370b324cSopenharmony_ci{ 106370b324cSopenharmony_ci CSeqInStreamWrap inWrap; 107370b324cSopenharmony_ci CSeqOutStreamWrap outWrap; 108370b324cSopenharmony_ci CCompressProgressWrap progressWrap; 109370b324cSopenharmony_ci 110370b324cSopenharmony_ci inWrap.Init(inStream); 111370b324cSopenharmony_ci outWrap.Init(outStream); 112370b324cSopenharmony_ci progressWrap.Init(progress); 113370b324cSopenharmony_ci 114370b324cSopenharmony_ci SRes res = Lzma2Enc_Encode2(_encoder, 115370b324cSopenharmony_ci &outWrap.vt, NULL, NULL, 116370b324cSopenharmony_ci &inWrap.vt, NULL, 0, 117370b324cSopenharmony_ci progress ? &progressWrap.vt : NULL); 118370b324cSopenharmony_ci 119370b324cSopenharmony_ci RET_IF_WRAP_ERROR(inWrap.Res, res, SZ_ERROR_READ) 120370b324cSopenharmony_ci RET_IF_WRAP_ERROR(outWrap.Res, res, SZ_ERROR_WRITE) 121370b324cSopenharmony_ci RET_IF_WRAP_ERROR(progressWrap.Res, res, SZ_ERROR_PROGRESS) 122370b324cSopenharmony_ci 123370b324cSopenharmony_ci return SResToHRESULT(res); 124370b324cSopenharmony_ci} 125370b324cSopenharmony_ci 126370b324cSopenharmony_ci}} 127