void UncompressInc( std::vector<unsigned char> &outBuf, const std::vector<unsigned char> &inBuf) { CLzmaDec dec; UInt64 unpackSize = 0; for (int i = 0; i < 8; i++) unpackSize += (UInt64)inBuf[5 + i] << (i * 8); LzmaDec_Construct(&dec); SRes res = LzmaDec_Allocate(&dec, &inBuf[0], LZMA_PROPS_SIZE, &SzAllocForLzma); assert(res == SZ_OK); LzmaDec_Init(&dec); //printf("uint64 resLen : %" PRIu64 "\n", unpackSize); //print uint64 outBuf.resize(unpackSize); unsigned outPos = 0, inPos = LZMA_PROPS_SIZE; ELzmaStatus status; const unsigned BUF_SIZE = 10240; while (outPos < outBuf.size()) { unsigned destLen = min(BUF_SIZE, outBuf.size() - outPos); unsigned srcLen = min(BUF_SIZE, inBuf.size() - 8 - inPos); res = LzmaDec_DecodeToBuf(&dec, &outBuf[outPos], &destLen, &inBuf[inPos+8], &srcLen, (outPos + destLen == outBuf.size()) ? LZMA_FINISH_END : LZMA_FINISH_ANY, &status); assert(res == SZ_OK); inPos += srcLen; outPos += destLen; if (status == LZMA_STATUS_FINISHED_WITH_MARK) break; } LzmaDec_Free(&dec, &g_Alloc); outBuf.resize(outPos); FILE *fout = fopen("data33.dc", "wb+"); fwrite(&outBuf[0], 1, outBuf.size(), fout); fclose(fout); }
static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream) { UInt64 unpackSize; int i; SRes res = 0; CLzmaDec state; /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */ unsigned char header[LZMA_PROPS_SIZE + 8]; /* Read and parse header */ RINOK(SeqInStream_Read(inStream, header, sizeof(header))); unpackSize = 0; for (i = 0; i < 8; i++) unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8); LzmaDec_Construct(&state); RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc)); res = Decode2(&state, outStream, inStream, unpackSize); LzmaDec_Free(&state, &g_Alloc); return res; }
static void pylzma_decomp_dealloc(CDecompressionObject *self) { LzmaDec_Free(&self->state, &allocator); FREE_AND_NULL(self->unconsumed_tail); self->ob_type->tp_free((PyObject*) self); }
static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream, Gate2<int64, int64> progress) { UInt64 unpackSize; int i; SRes res = 0; CLzmaDec state; /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */ unsigned char header[LZMA_PROPS_SIZE + 8]; /* Read and parse header */ IUppInStream *is = (IUppInStream *)inStream; if(is->in.Get(header, sizeof(header)) != sizeof(header)) return SZ_ERROR_READ; unpackSize = 0; for(i = 0; i < 8; i++) unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8); LzmaDec_Construct(&state); RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc)); res = Decode2(&state, outStream, inStream, unpackSize, progress); LzmaDec_Free(&state, &g_Alloc); return res; }
static void lzmafs_fileop_close(void *ref) { lzmafs_file_t *file = (lzmafs_file_t *) ref; file->lzmafs_fsctx->lzmafsctx_refcnt--; BDCLOSE(file->lzmafs_fsctx->lzmafsctx_subops,file->lzmafs_subfile); if (file->lzmafs_inbuf) KFREE(file->lzmafs_inbuf); if (file->lzmafs_outbuf) KFREE(file->lzmafs_outbuf); if (file->lzmafs_state) { LzmaDec_Free(file->lzmafs_state, &g_Alloc); KFREE(file->lzmafs_state); } if (file) KFREE(file); }
static PyObject * pylzma_decomp_reset(CDecompressionObject *self, PyObject *args, PyObject *kwargs) { int max_length = -1; // possible keywords for this function static char *kwlist[] = {"maxlength", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &max_length)) return NULL; LzmaDec_Free(&self->state, &allocator); LzmaDec_Construct(&self->state); FREE_AND_NULL(self->unconsumed_tail); self->unconsumed_length = 0; self->need_properties = 1; self->total_out = 0; self->max_length = max_length; Py_INCREF(Py_None); return Py_None; }
void lzma_dec_free(struct lzma_dec_state *state) { LzmaDec_Free(&state->decoder, &state->alloc); free(state); }
int BCMINITFN(_nvram_read)(void *buf, int idx) { uint32 *src, *dst; uint i; if (!nvram_header) return -19; /* -ENODEV */ #if defined(_CFE_) && defined(BCM_DEVINFO) if ((!devinfo_nvram_header) && (idx == 1)) { return -19; /* -ENODEV */ } src = idx == 0 ? (uint32 *) nvram_header : (uint32 *) devinfo_nvram_nvh; #else src = (uint32 *) nvram_header; #endif /* _CFE_ && BCM_DEVINFO */ dst = (uint32 *) buf; for (i = 0; i < sizeof(struct nvram_header); i += 4) *dst++ = *src++; /* Since we know what the first 3 bytes of the lzma properties * should be based on what we used to compress, check them * to see if we need to decompress (uncompressed this would show up a * a single [ and then the end of nvram marker so its invalid in an * uncompressed nvram block */ if ((((unsigned char *)src)[0] == 0x5d) && (((unsigned char *)src)[1] == 0) && (((unsigned char *)src)[2] == 0)) { unsigned int dstlen = nvram_header->len; unsigned int srclen = MAX_NVRAM_SPACE-LZMA_PROPS_SIZE-NVRAM_HEADER_SIZE; unsigned char *cp = (unsigned char *)src; CLzmaDec state; SRes res; ELzmaStatus status; LzmaDec_Construct(&state); res = LzmaDec_Allocate(&state, cp, LZMA_PROPS_SIZE, &g_Alloc); if (res != SZ_OK) { printf("Error Initializing LZMA Library\n"); return -19; } LzmaDec_Init(&state); res = LzmaDec_DecodeToBuf(&state, (unsigned char *)dst, &dstlen, &cp[LZMA_PROPS_SIZE], &srclen, LZMA_FINISH_ANY, &status); LzmaDec_Free(&state, &g_Alloc); if (res != SZ_OK) { printf("Error Decompressing eNVRAM\n"); return -19; } } else { for (; i < nvram_header->len && i < MAX_NVRAM_SPACE; i += 4) *dst++ = ltoh32(*src++); } return 0; }
PakEntry Pak::read(uint32_t tag) const { int entryIndex = findEntry(tag); if(entryIndex < 0) { return PakEntry(NULL, 0, false); } const Header *header = reinterpret_cast<const Header*>(m_pakData); const Entry *entry = reinterpret_cast<const Entry*>(header + 1) + entryIndex; size_t entrySize = (entry+1)->offset - entry->offset; if(entry->offset>m_pakDataSize || entrySize>=4*1024*1024 || entry->offset+entrySize>m_pakDataSize) { ERROR("Pak::findEntry: invalid entry offset/size (tag=0x%08x index=%u offset=%u size=%u)", tag, entryIndex, entry->offset, entrySize); return PakEntry(NULL, 0, false); } const uint8_t *entryData = cast<uint8_t>(m_pakData, entry->offset); const LzmaHeader *lzmaHeader = cast<LzmaHeader>(entryData, 0); if(memcmp(lzmaHeader->magic, "lzma", 4) != 0) { DEBUG("Pak::findEntry: found uncompressed 0x%08x at %u size %zu", tag, entry->offset, entrySize); return PakEntry(entryData, entrySize, false); } if(lzmaHeader->decompressedSize<entrySize/2 || lzmaHeader->decompressedSize>=8*1024*1024) { ERROR("Pak::findEntry: invalid uncompressed size (tag=0x%08x size=%u decompressedsize=%u)", tag, entrySize, lzmaHeader->decompressedSize); return PakEntry(NULL, 0, false); } CLzmaDec dec; LzmaDec_Construct(&dec); LzmaDec_Init(&dec); SRes res = LzmaDec_Allocate(&dec, lzmaHeader->props, LZMA_PROPS_SIZE, &lzmaAllocFuncs); if(res != SZ_OK) { ERROR("Pak::findEntry: LzmaDec_Allocate failed: %d (tag=0x%08x)", res, tag); return PakEntry(NULL, 0, false); } void *decdata = malloc(lzmaHeader->decompressedSize); if(decdata == NULL) { ERROR("Pak::findEntry: LzmaDec_Allocate failed to allocate %u bytes", lzmaHeader->decompressedSize); LzmaDec_Free(&dec, &lzmaAllocFuncs); return PakEntry(NULL, 0, false); } size_t destSize = lzmaHeader->decompressedSize; size_t srcSize = entrySize - sizeof(lzmaHeader); ELzmaStatus status; res = LzmaDec_DecodeToBuf(&dec, (Byte*)decdata, &destSize, lzmaHeader->data, &srcSize, LZMA_FINISH_END, &status); if(res != SZ_OK) { ERROR("Pak::findEntry: LzmaDec_DecodeToBuf failed: %d (tag=0x%08x)", res, tag); LzmaDec_Free(&dec, &lzmaAllocFuncs); free(decdata); return PakEntry(NULL, 0, false); } if(!(status==LZMA_STATUS_FINISHED_WITH_MARK || (status==LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK && destSize!=lzmaHeader->decompressedSize))) { ERROR("Pak::findEntry: LzmaDec_DecodeToBuf ended with unexpected status: %d (tag=0x%08x)", status, tag); LzmaDec_Free(&dec, &lzmaAllocFuncs); free(decdata); return PakEntry(NULL, 0, false); } if(destSize != lzmaHeader->decompressedSize) { ERROR("Pak::findEntry: LzmaDec_DecodeToBuf failed to decompress whole buffer: %zu!=%u (tag=0x%08x)", destSize, lzmaHeader->decompressedSize, tag); LzmaDec_Free(&dec, &lzmaAllocFuncs); free(decdata); return PakEntry(NULL, 0, false); } LzmaDec_Free(&dec, &lzmaAllocFuncs); return PakEntry(decdata, lzmaHeader->decompressedSize, true); }
static int Decode(FILE *inFile, FILE *outFile, char *rs) { UInt64 unpackSize; int thereIsSize; /* = 1, if there is uncompressed size in headers */ int i; int res = 0; CLzmaDec state; /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */ unsigned char header[LZMA_PROPS_SIZE + 8]; /* Read and parse header */ if (!MyReadFileAndCheck(inFile, header, sizeof(header))) return PrintError(rs, kCantReadMessage); unpackSize = 0; thereIsSize = 0; for (i = 0; i < 8; i++) { unsigned char b = header[LZMA_PROPS_SIZE + i]; if (b != 0xFF) thereIsSize = 1; unpackSize += (UInt64)b << (i * 8); } LzmaDec_Construct(&state); res = LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc); if (res != SZ_OK) return res; { Byte inBuf[IN_BUF_SIZE]; Byte outBuf[OUT_BUF_SIZE]; size_t inPos = 0, inSize = 0, outPos = 0; LzmaDec_Init(&state); for (;;) { if (inPos == inSize) { inSize = MyReadFile(inFile, inBuf, IN_BUF_SIZE); inPos = 0; } { SizeT inProcessed = inSize - inPos; SizeT outProcessed = OUT_BUF_SIZE - outPos; ELzmaFinishMode finishMode = LZMA_FINISH_ANY; ELzmaStatus status; if (thereIsSize && outProcessed > unpackSize) { outProcessed = (SizeT)unpackSize; finishMode = LZMA_FINISH_END; } res = LzmaDec_DecodeToBuf(&state, outBuf + outPos, &outProcessed, inBuf + inPos, &inProcessed, finishMode, &status); inPos += (UInt32)inProcessed; outPos += outProcessed; unpackSize -= outProcessed; if (outFile != 0) MyWriteFile(outFile, outBuf, outPos); outPos = 0; if (res != SZ_OK || thereIsSize && unpackSize == 0) break; if (inProcessed == 0 && outProcessed == 0) { if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK) res = SZ_ERROR_DATA; break; } } } } LzmaDec_Free(&state, &g_Alloc); return res; }
int elzma_decompress_run(elzma_decompress_handle hand, elzma_read_callback inputStream, void * inputContext, elzma_write_callback outputStream, void * outputContext, elzma_file_format format) { unsigned long long int totalRead = 0; /* total amount read from stream */ unsigned int crc32 = CRC_INIT_VAL; /* running crc32 (lzip case) */ CLzmaDec dec; unsigned int errorCode = ELZMA_E_OK; struct elzma_format_handler formatHandler; struct elzma_file_header h; struct elzma_file_footer f; /* switch between supported formats */ if (format == ELZMA_lzma) { initializeLZMAFormatHandler(&formatHandler); } else if (format == ELZMA_lzip) { CrcGenerateTable(); initializeLZIPFormatHandler(&formatHandler); } else { return ELZMA_E_BAD_PARAMS; } /* initialize footer */ f.crc32 = 0; f.uncompressedSize = 0; /* initialize decoder memory */ memset((void *) &dec, 0, sizeof(dec)); LzmaDec_Init(&dec); /* decode the header. */ { unsigned char * hdr = hand->allocStruct.Alloc(&(hand->allocStruct), formatHandler.header_size); size_t sz = formatHandler.header_size; formatHandler.init_header(&h); if (inputStream(inputContext, hdr, &sz) != 0 || sz != formatHandler.header_size) { hand->allocStruct.Free(&(hand->allocStruct), hdr); return ELZMA_E_INPUT_ERROR; } if (0 != formatHandler.parse_header(hdr, &h)) { hand->allocStruct.Free(&(hand->allocStruct), hdr); return ELZMA_E_CORRUPT_HEADER; } /* the LzmaDec_Allocate call requires 5 bytes which have * compression properties encoded in them. In the case of * lzip, the header format does not already contain what * LzmaDec_Allocate expects, so we must craft it, silly */ { unsigned char propsBuf[13]; const unsigned char * propsPtr = hdr; if (format == ELZMA_lzip) { struct elzma_format_handler lzmaHand; initializeLZMAFormatHandler(&lzmaHand); lzmaHand.serialize_header(propsBuf, &h); propsPtr = propsBuf; } /* now we're ready to allocate the decoder */ LzmaDec_Allocate(&dec, propsPtr, 5, (ISzAlloc *) &(hand->allocStruct)); } hand->allocStruct.Free(&(hand->allocStruct), hdr); } /* perform the decoding */ for (;;) { size_t dstLen = ELZMA_DECOMPRESS_OUTPUT_BUFSIZE; size_t srcLen = ELZMA_DECOMPRESS_INPUT_BUFSIZE; size_t amt = 0; size_t bufOff = 0; ELzmaStatus stat; if (0 != inputStream(inputContext, hand->inbuf, &srcLen)) { errorCode = ELZMA_E_INPUT_ERROR; goto decompressEnd; } /* handle the case where the input prematurely finishes */ if (srcLen == 0) { errorCode = ELZMA_E_INSUFFICIENT_INPUT; goto decompressEnd; } amt = srcLen; /* handle the case where a single read buffer of compressed bytes * will translate into multiple buffers of uncompressed bytes, * with this inner loop */ stat = LZMA_STATUS_NOT_SPECIFIED; while (bufOff < srcLen) { SRes r = LzmaDec_DecodeToBuf(&dec, (Byte *) hand->outbuf, &dstLen, ((Byte *) hand->inbuf + bufOff), &amt, LZMA_FINISH_ANY, &stat); /* XXX deal with result code more granularly*/ if (r != SZ_OK) { errorCode = ELZMA_E_DECOMPRESS_ERROR; goto decompressEnd; } /* write what we've read */ { size_t wt; /* if decoding lzip, update our crc32 value */ if (format == ELZMA_lzip && dstLen > 0) { crc32 = CrcUpdate(crc32, hand->outbuf, dstLen); } totalRead += dstLen; wt = outputStream(outputContext, hand->outbuf, dstLen); if (wt != dstLen) { errorCode = ELZMA_E_OUTPUT_ERROR; goto decompressEnd; } } /* do we have more data on the input buffer? */ bufOff += amt; assert( bufOff <= srcLen ); if (bufOff >= srcLen) break; amt = srcLen - bufOff; /* with lzip, we will have the footer left on the buffer! */ if (stat == LZMA_STATUS_FINISHED_WITH_MARK) { break; } } /* now check status */ if (stat == LZMA_STATUS_FINISHED_WITH_MARK) { /* read a footer if one is expected and * present */ if (formatHandler.footer_size > 0 && amt >= formatHandler.footer_size && formatHandler.parse_footer != NULL) { formatHandler.parse_footer( (unsigned char *) hand->inbuf + bufOff, &f); } break; } /* for LZMA utils, we don't always have a finished mark */ if (!h.isStreamed && totalRead >= h.uncompressedSize) { break; } } /* finish the calculated crc32 */ crc32 ^= 0xFFFFFFFF; /* if we have a footer, check that the calculated crc32 matches * the encoded crc32, and that the sizes match */ if (formatHandler.footer_size) { if (f.crc32 != crc32) { errorCode = ELZMA_E_CRC32_MISMATCH; } else if (f.uncompressedSize != totalRead) { errorCode = ELZMA_E_SIZE_MISMATCH; } } else if (!h.isStreamed) { /* if the format does not support a footer and has an uncompressed * size in the header, let's compare that with how much we actually * read */ if (h.uncompressedSize != totalRead) { errorCode = ELZMA_E_SIZE_MISMATCH; } } decompressEnd: LzmaDec_Free(&dec, (ISzAlloc *) &(hand->allocStruct)); return errorCode; }
CDecoder::~CDecoder() { LzmaDec_Free(&_state, &g_Alloc); MyFree(_inBuf); }
inline ~TImpl() throw () { LzmaDec_Free(&H_, Alloc()); }
FileReaderLZMA::~FileReaderLZMA () { LzmaDec_Free(&Streamp->Stream, &g_Alloc); delete Streamp; }
FileReaderLZMA::~FileReaderLZMA () { LzmaDec_Free(&Stream, &g_Alloc); }
int _tmain(int argc, _TCHAR* argv[]) { HANDLE hFileIn = ::CreateFile(_T("C:\\Users\\Christoph\\Desktop\\S.csv.lzma"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE hFileOut = ::CreateFile(_T("C:\\Users\\Christoph\\Desktop\\S.csv.decd"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); BYTE header[LZMA_PROPS_SIZE + 8]; DWORD dwBytesRead = 0; DWORD dwBytesWritten = 0; ::ReadFile(hFileIn, header, sizeof(header), &dwBytesRead, NULL); ELzmaStatus status; CLzmaDec state; LzmaDec_Construct(&state); LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc); LzmaDec_Init(&state); __int64 nSize = *reinterpret_cast<__int64*>(header + LZMA_PROPS_SIZE); _tprintf(_T("Extracted size: %d\n"), nSize); BYTE *pDataIn = new BYTE[BUFFER_SIZE]; BYTE *pDataOut = new BYTE[BUFFER_SIZE]; size_t nTotalSize = (size_t)nSize; size_t nInSize = BUFFER_SIZE, nInPos = 0; size_t nOutSize = BUFFER_SIZE; while (nTotalSize) { if (!::ReadFile(hFileIn, pDataIn, BUFFER_SIZE, &dwBytesRead, NULL)) break; if (dwBytesRead == 0) break; nInSize = dwBytesRead; nInPos = 0; _tprintf(_T(".")); while (nInPos < nInSize) { size_t nInProcessed = nInSize - nInPos; size_t nOutProcessed = BUFFER_SIZE; LzmaDec_DecodeToBuf(&state, pDataOut, &nOutProcessed, pDataIn + nInPos, &nInProcessed, (nOutProcessed > nTotalSize)? LZMA_FINISH_END : LZMA_FINISH_ANY, &status); ::WriteFile(hFileOut, pDataOut, nOutProcessed, &dwBytesRead, NULL); nInPos += nInProcessed; nTotalSize -= nOutProcessed; if (nInProcessed == 0 && nOutProcessed == 0) { nInPos = nInSize = 0; nTotalSize = 0; } } } delete[] pDataOut; delete[] pDataIn; LzmaDec_Free(&state, &g_Alloc); ::CloseHandle(hFileOut); ::CloseHandle(hFileIn); #ifdef _DEBUG _tsystem(_T("pause")); #endif return 0; }
static int lzmafs_fileop_open(void **ref,void *fsctx_arg,char *filename,int mode) { lzmafs_fsctx_t *fsctx; lzmafs_file_t *file; int err = 0, i; /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */ unsigned char header[LZMA_PROPS_SIZE + 8]; if (mode != FILE_MODE_READ) return CFE_ERR_UNSUPPORTED; fsctx = (lzmafs_fsctx_t *) fsctx_arg; file = KMALLOC(sizeof(lzmafs_file_t), 0); if (!file) { return CFE_ERR_NOMEM; } file->lzmafs_fileoffset = 0; file->lzmafs_fsctx = fsctx; file->lzmafs_inlen = 0; file->lzmafs_eofseen = 0; file->lzmafs_state = KMALLOC(sizeof(CLzmaDec), 0); if (!file->lzmafs_state) goto error2; file->lzmafs_outlen = 0; /* Open the raw file system */ err = BDOPEN(fsctx->lzmafsctx_subops, &(file->lzmafs_subfile), fsctx->lzmafsctx_subfsctx, filename); if (err != 0) goto error2; err = BDREAD(file->lzmafs_fsctx->lzmafsctx_subops, file->lzmafs_subfile, header, sizeof(header)); if (err < 0) goto error; file->lzmafs_unpackSize = 0; /* uncompressed size */ for (i = 0; i < 8; i++) file->lzmafs_unpackSize += header[LZMA_PROPS_SIZE + i] << (i * 8); LzmaDec_Construct(file->lzmafs_state); RINOK(LzmaDec_Allocate(file->lzmafs_state, header, LZMA_PROPS_SIZE, &g_Alloc)); file->lzmafs_inbuf = KMALLOC(LZMAFS_INBUFSIZE, 0); file->lzmafs_outbuf = KMALLOC(LZMAFS_OUTBUFSIZE, 0); if (!file->lzmafs_inbuf || !file->lzmafs_outbuf) { err = CFE_ERR_NOMEM; goto error; } file->lzmafs_outptr = file->lzmafs_outbuf; LzmaDec_Init(file->lzmafs_state); fsctx->lzmafsctx_refcnt++; *ref = file; return 0; error: BDCLOSE(file->lzmafs_fsctx->lzmafsctx_subops, file->lzmafs_subfile); error2: if (file->lzmafs_inbuf) KFREE(file->lzmafs_inbuf); if (file->lzmafs_outbuf) KFREE(file->lzmafs_outbuf); if (file->lzmafs_state) { LzmaDec_Free(file->lzmafs_state, &g_Alloc); KFREE(file->lzmafs_state); } if (file) KFREE(file); return err; }
void cli_LzmaShutdown(struct CLI_LZMA *L) { if(L->freeme) LzmaDec_Free(&L->state, &g_Alloc); return; }
~DecompressorLZMA () { LzmaDec_Free(&Stream, &g_Alloc); }