CLzma2Dec* ZPatcher::InitLzma2Decoder(const Byte& props) { CLzma2Dec* dec = static_cast<CLzma2Dec*>(malloc(sizeof(CLzma2Dec))); Lzma2Dec_Construct(dec); SRes res = Lzma2Dec_Allocate(dec, props, &LzmaSzAlloc); assert(res == SZ_OK); return dec; }
void UnCompressWithLZMA2(std::vector<unsigned char> &outBuf, const std::vector<unsigned char> &inBuf) { CLzma2Dec dec; Lzma2Dec_Construct(&dec); UInt64 unpackSize = 0; SRes res = Lzma2Dec_Allocate(&dec, inBuf[0], &g_Alloc); assert(res == SZ_OK); Lzma2Dec_Init(&dec); for (int i = 0; i < 8; i++) unpackSize += (UInt64)inBuf[1+i] << (i * 8); outBuf.resize(unpackSize); unsigned outPos = 0, inPos = 9; ELzmaStatus status; const unsigned BUF_SIZE = 10240; while (outPos < outBuf.size()) { SizeT destLen = min(BUF_SIZE, outBuf.size() - outPos); SizeT srcLen = min(BUF_SIZE, inBuf.size() - inPos); res = Lzma2Dec_DecodeToBuf(&dec, &outBuf[outPos], &destLen, &inBuf[inPos], &srcLen, (outPos + destLen == outBuf.size()) ? LZMA_FINISH_END : LZMA_FINISH_ANY, &status); unsigned int outbufsize = outBuf.size(); assert(res == SZ_OK); inPos += srcLen; outPos += destLen; if (status == LZMA_STATUS_FINISHED_WITH_MARK) { break; } } Lzma2Dec_Free(&dec, &g_Alloc); outBuf.resize(outPos); FILE *fout = fopen("data22.dc", "wb+"); fwrite(&outBuf[0], 1, outBuf.size(), fout); fclose(fout); }
STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *prop, UInt32 size) { if (size != 1) return SZ_ERROR_UNSUPPORTED; RINOK(SResToHRESULT(Lzma2Dec_Allocate(&_state, prop[0], &g_Alloc))); if (_inBuf == 0) { _inBuf = (Byte *)MyAlloc(kInBufSize); if (_inBuf == 0) return E_OUTOFMEMORY; } return S_OK; }