static int decompress_data(CLzmaDecoderState *vs, unsigned char *outStream, SizeT outSize) { SizeT ip, op; return LzmaDecode(vs, data, datalen, &ip, outStream, outSize, &op); }
unsigned long ulzma(u8 *src, u8 *dst) { unsigned char properties[LZMA_PROPERTIES_SIZE]; UInt32 outSize; SizeT inProcessed; SizeT outProcessed; int res; CLzmaDecoderState state; SizeT mallocneeds; unsigned char scratchpad[15980]; memcpy(properties, src, LZMA_PROPERTIES_SIZE); outSize = *(UInt32 *)(src + LZMA_PROPERTIES_SIZE); if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { printf("Incorrect stream properties\n"); } mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); if (mallocneeds > 15980) { printf("Decoder scratchpad too small!\n"); } state.Probs = (CProb *)scratchpad; res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed, dst, outSize, &outProcessed); if (res != 0) { printf("Decoding error = %d\n", res); } return outSize; }
bool CCCrypto::uncompressData( char* pInData, unsigned int nInSize, unsigned char** pOutData, SizeT& nOutSize ) { if (nInSize<sizeof(stHead)) return false; stHead* phead = (stHead*)pInData; if ( phead->m_sign!='@Fml' ) return false; nOutSize = phead->m_OriginalSize; nOutSize ^= 1002; *pOutData = (unsigned char*)malloc(nOutSize+1); (*pOutData)[nOutSize] = 0; SizeT srcLen = nInSize - sizeof(stHead); ELzmaStatus status; Byte prop[LZMA_PROPS_SIZE]; for ( int i = 0; i < LZMA_PROPS_SIZE; i ++ ) prop[i] = phead->m_szPorp[i]; prop[3] ^= 5; for ( unsigned i = 0; i < 16 && i < nOutSize; i ++ ) { pInData[sizeof(stHead)+i] ^= 6636; } SRes res = LzmaDecode( *pOutData, (SizeT*)&nOutSize, (const Byte*)&pInData[sizeof(stHead)], (SizeT*)&srcLen, prop, LZMA_PROPS_SIZE, LZMA_FINISH_END, &status, &SzAllocForLzma ); // LZMA_FINISH_ANY if ( res == SZ_OK ) return true; free( *pOutData ); *pOutData = NULL; nOutSize = 0; return false; }
SRes Lzma86_Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen) { SRes res; int useFilter; SizeT inSizePure; ELzmaStatus status; if (*srcLen < LZMA86_HEADER_SIZE) return SZ_ERROR_INPUT_EOF; useFilter = src[0]; if (useFilter > 1) { *destLen = 0; return SZ_ERROR_UNSUPPORTED; } inSizePure = *srcLen - LZMA86_HEADER_SIZE; res = LzmaDecode(dest, destLen, src + LZMA86_HEADER_SIZE, &inSizePure, src + 1, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &g_Alloc); *srcLen = inSizePure + LZMA86_HEADER_SIZE; if (res != SZ_OK) return res; if (useFilter == 1) { UInt32 x86State; x86_Convert_Init(x86State); x86_Convert(dest, *destLen, 0, &x86State, 0); } return SZ_OK; }
int squashfs_uncompress_block (void *dst, int dstlen, void *src, int srclen) { int err; #ifdef CONFIG_SQUASHFS_LZMA SizeT InProcessed; int bytes; if((err = LzmaDecode(&state, src, srclen, &InProcessed, dst, dstlen, &bytes)) != LZMA_RESULT_OK) { printf("lzma_fs returned unexpected result 0x%x\n", err); return 0; } return bytes; #else inflateReset (&stream); stream.next_in = src; stream.avail_in = srclen; stream.next_out = dst; stream.avail_out = dstlen; err = inflate (&stream, Z_FINISH); if ((err==Z_OK)||(err==Z_STREAM_END)) return dstlen-stream.avail_out; else return 0; #endif }
void lzmaUncompress( unsigned char* destBuf, int destSize, unsigned char *srcBuf, int srcSize ) { SizeT lzmaDestSize = (SizeT)destSize; SizeT lzmaSrcSize = (SizeT)srcSize - LZMA_PROPS_SIZE; cout << "DECOMPRESSING " << srcSize << endl; ELzmaStatus finishStatus; int res = LzmaDecode( destBuf, &lzmaDestSize, srcBuf+LZMA_PROPS_SIZE, &lzmaSrcSize, srcBuf, LZMA_PROPS_SIZE, LZMA_FINISH_END, &finishStatus, &SzAllocForLzma); cout << "DECOMPRESSED " << srcSize << " BYTES DOWN TO " << lzmaDestSize << endl; if(res != SZ_OK || finishStatus != LZMA_STATUS_FINISHED_WITH_MARK) { cout << "ERROR DECOMPRESSING DATA\n"; cout << res << ',' << finishStatus << endl; exit(1); } }
/* Decompresses a Lzma compressed source buffer. Extracts decompressed data to its original form. If the compressed source data specified by Source is successfully decompressed into Destination, then RETURN_SUCCESS is returned. If the compressed source data specified by Source is not a valid compressed data format, then RETURN_INVALID_PARAMETER is returned. @param Source The source buffer containing the compressed data. @param SourceSize The size of source buffer. @param Destination The destination buffer to store the decompressed data @retval EFI_SUCCESS Decompression completed successfully, and the uncompressed buffer is returned Destination. @retval EFI_INVALID_PARAMETER The source buffer specified by Source is corrupted (not a valid compressed format). */ INT32 EFIAPI LzmaDecompress( CONST VOID *Source, UINT32 SourceSize, VOID *Destination ) { SRes LzmaResult; ELzmaStatus Status; SizeT DecodedBufSize; SizeT EncodedDataSize; DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source); EncodedDataSize = (SizeT)(SourceSize - LZMA_HEADER_SIZE); LzmaResult = LzmaDecode( (Byte*)Destination, &DecodedBufSize, (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE), &EncodedDataSize, (CONST Byte*) Source, LZMA_PROPS_SIZE, LZMA_FINISH_END, &Status, &SzAllocForLzma ); if (LzmaResult == SZ_OK) { return ERR_SUCCESS; } else { return ERR_INVALID_PARAMETER; } }
int cli_LzmaDecode(CLI_LZMA **Lp, struct stream_state* state) { int res; SizeT processed_in, processed_out; CLI_LZMA* L = *Lp; if(L) { L->avail_in = state->avail_in; L->next_in = state->next_in; L->avail_out = state->avail_out; L->next_out = state->next_out; } if (!L || !L->initted) { if(cli_LzmaInit(Lp, 0) != LZMA_RESULT_OK) return LZMA_RESULT_DATA_ERROR; L = *Lp; } res = LzmaDecode(&L->state, L->next_in, L->avail_in, &processed_in, L->next_out, L->avail_out, &processed_out, (L->avail_in==0)); L->next_in += processed_in; L->avail_in -= processed_in; L->next_out += processed_out; L->avail_out -= processed_out; state->avail_in = L->avail_in; state->next_in = L->next_in; state->avail_out = L->avail_out; state->next_out = L->next_out; return res; }
int lzma_inflate( unsigned char *source, int s_len, unsigned char *dest, int *d_len) { /* Get the properties */ unsigned int check_internal_size; unsigned char properties[5]; unsigned char prop0; int lc, lp, pb; int res, i; unsigned int dictionary_size = 0; unsigned int encoded_size; if(s_len<5){ /* Can't even get properities, just exit */ return LZMA_ERROR; } /* Get the size of the uncompressed buffer */ encoded_size = source[0] | (source[1] << 8) | (source[2] << 16) | (source[3] << 24); source+=4; /* We use this to check that the size of internal data structures are big enough. If it isn't, then we flag it. */ memcpy(properties, source, sizeof(properties)); prop0 = properties[0]; if (prop0 >= (9*5*5)) return LZMA_ERROR; for (pb = 0; prop0 >= (9 * 5); pb++, prop0 -= (9 * 5)); for (lp = 0; prop0 >= 9; lp++, prop0 -= 9); lc = prop0; source += 5; check_internal_size = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb); for (i = 0; i < 4; i++) dictionary_size += (UInt32)(properties[1 + i]) << (i * 8); if(check_internal_size > internal_size){ printk("internal_size = %d, header size = %d\n", internal_size, check_internal_size); printk("lc = %d, lp=%d, pb=%d\n", lc, lp, pb); printk("byte=%x, dictionary size = %8.8x\n", prop0, dictionary_size); return LZMA_TOO_BIG; } res = LzmaDecode(internal_data, internal_size, lc, lp, pb, (unsigned char *)source, s_len, (unsigned char *)dest, encoded_size, d_len); return res; }
/* should be the first function */ void entry(unsigned long icache_size, unsigned long icache_lsize, unsigned long dcache_size, unsigned long dcache_lsize) { /* clear the bss */ long *bssp; /* clear the bss */ for (bssp = bss_start; bssp != bss_end; bssp++) *bssp = 0; unsigned int i; /* temp value */ unsigned int osize; /* uncompressed size */ ILzmaInCallback callback; CLzmaDecoderState vs; callback.Read = read_byte; puts("Atheros WiSOC DD-WRT LZMA Kernel Loader ("); puts(__DATE__); puts(")\n"); puts("decompressing"); data = lzma_start; /* lzma args */ i = get_byte(); vs.Properties.lc = i % 9, i = i / 9; vs.Properties.lp = i % 5, vs.Properties.pb = i / 5; vs.Probs = (CProb *)buffer; /* skip rest of the LZMA coder property */ for (i = 0; i < 4; i++) get_byte(); /* read the lower half of uncompressed size in the header */ osize = ((unsigned int)get_byte()) + ((unsigned int)get_byte() << 8) + ((unsigned int)get_byte() << 16) + ((unsigned int)get_byte() << 24); /* skip rest of the header (upper half of uncompressed size) */ for (i = 0; i < 4; i++) get_byte(); /* decompress kernel */ if ((i = LzmaDecode(&vs, &callback, (unsigned char*)KERNEL_ENTRY, osize, &osize)) == LZMA_RESULT_OK) { puts("\ndone.\njump to kernel...\n"); blast_dcache(dcache_size, dcache_lsize); blast_icache(icache_size, icache_lsize); /* Jump to load address */ ((void (*)(unsigned long, unsigned long, unsigned long)) KERNEL_ENTRY) (linux_args[0], linux_args[1], linux_args[2]); } puts("Fatal error while decompressing!\n"); }
int hc_lzma1_decompress (const unsigned char *in, SizeT *in_len, unsigned char *out, SizeT *out_len, const char *props) { ISzAlloc hc_lzma_mem_alloc = {hc_lzma_alloc, hc_lzma_free}; ELzmaStatus status; // parameters to LzmaDecode (): unsigned char *dest, size_t *destLen, const unsigned char *src, // size_t *srcLen, const unsigned char *props, size_t propsSize, ELzmaFinishMode finishMode, ELzmaStatus status, ISzAlloc *alloc return LzmaDecode (out, out_len, in, in_len, (const Byte *) props, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &hc_lzma_mem_alloc); }
/* should be the first function */ void entry(unsigned long icache_size, unsigned long icache_lsize, unsigned long dcache_size, unsigned long dcache_lsize) { unsigned int i; /* temp value */ unsigned int lc; /* literal context bits */ unsigned int lp; /* literal pos state bits */ unsigned int pb; /* pos state bits */ unsigned int osize; /* uncompressed size */ ILzmaInCallback callback; callback.Read = read_byte; /* look for trx header, 32-bit data access */ for (data = ((unsigned char *) KSEG1ADDR(BCM4710_FLASH)); ((struct trx_header *)data)->magic != TRX_MAGIC; data += 65536); /* compressed kernel is in the partition 0 or 1 */ if (((struct trx_header *)data)->offsets[1] > 65536) data += ((struct trx_header *)data)->offsets[0]; else data += ((struct trx_header *)data)->offsets[1]; offset = 0; /* lzma args */ i = get_byte(); lc = i % 9, i = i / 9; lp = i % 5, pb = i / 5; /* skip rest of the LZMA coder property */ for (i = 0; i < 4; i++) get_byte(); /* read the lower half of uncompressed size in the header */ osize = ((unsigned int)get_byte()) + ((unsigned int)get_byte() << 8) + ((unsigned int)get_byte() << 16) + ((unsigned int)get_byte() << 24); /* skip rest of the header (upper half of uncompressed size) */ for (i = 0; i < 4; i++) get_byte(); /* decompress kernel */ if (LzmaDecode(workspace, ~0, lc, lp, pb, &callback, (unsigned char*)LOADADDR, osize, &i) == LZMA_RESULT_OK) { blast_dcache(dcache_size, dcache_lsize); blast_icache(icache_size, icache_lsize); /* Jump to load address */ ((void (*)(void)) LOADADDR)(); } }
static int decompress_data(CLzmaDecoderState *vs, unsigned char *outStream, SizeT outSize) { SizeT op; #if 0 vs->Buffer = data; vs->BufferLim = datalen; #endif return LzmaDecode(vs, &lzma_callback, outStream, outSize, &op); }
/* should be the first function */ void entry(unsigned long icache_size, unsigned long icache_lsize, unsigned long dcache_size, unsigned long dcache_lsize, unsigned long fw_arg0, unsigned long fw_arg1, unsigned long fw_arg2, unsigned long fw_arg3) { unsigned int i; /* temp value */ unsigned int osize; /* uncompressed size */ CLzmaDecoderState vs; ILzmaInCallback callback; callback.Read = read_byte; /* look for trx header, 32-bit data access */ for (data = ((unsigned char *) KSEG1ADDR(BCM4710_FLASH)); ((struct trx_header *)data)->magic != TRX_MAGIC; data += 65536); /* compressed kernel is in the partition 1 */ data += ((struct trx_header *)data)->offsets[1]; offset = 0; /* lzma args */ i = get_byte(); vs.Properties.lc = i % 9, i = i / 9; vs.Properties.lp = i % 5, vs.Properties.pb = i / 5; vs.Probs = (CProb *)workspace; /* skip rest of the LZMA coder property */ for (i = 0; i < 4; i++) get_byte(); /* read the lower half of uncompressed size in the header */ osize = ((unsigned int)get_byte()) + ((unsigned int)get_byte() << 8) + ((unsigned int)get_byte() << 16) + ((unsigned int)get_byte() << 24); /* skip rest of the header (upper half of uncompressed size) */ for (i = 0; i < 4; i++) get_byte(); /* decompress kernel */ if (LzmaDecode(&vs, &callback, (unsigned char*)LOADADDR, osize, &i) == LZMA_RESULT_OK) { blast_dcache(dcache_size, dcache_lsize); blast_icache(icache_size, icache_lsize); /* Jump to load address */ ((void (*)(unsigned long, unsigned long, unsigned long, unsigned long)) LOADADDR)(fw_arg0, fw_arg1, fw_arg2, fw_arg3); } }
size32_t expand(const void* input, size32_t inlength, void* output, size32_t maxout) { SizeT reslen = maxout; SizeT propsize = *(size32_t *)input; SizeT inlen = inlength -= sizeof(size32_t)+propsize; ELzmaStatus status; SRes res = LzmaDecode((byte *)output, &reslen, (const byte *)input+sizeof(size32_t)+propsize, &inlen, (byte *)input+sizeof(size32_t), propsize, LZMA_FINISH_END, &status, &g_Alloc); if (res!=SZ_OK) throw MakeStringException(-1,"LzmaDecode failed(%d)",(int)res); return reslen; }
int LzmaRamDecompress( unsigned char *inBuffer, size_t inSize, unsigned char *outBuffer, size_t outSize, size_t *outSizeProcessed, void * (*allocFunc)(size_t size), void (*freeFunc)(void *)) { int lc, lp, pb; size_t lzmaInternalSize; void *lzmaInternalData; int result; UInt32 outSizeProcessedLoc; int useFilter = inBuffer[0]; *outSizeProcessed = 0; if (useFilter > 1) return 1; if (inSize < LZMA_PROPS_SIZE) return 1; lc = inBuffer[1]; if (lc >= (9 * 5 * 5)) return 1; for (pb = 0; lc >= (9 * 5); pb++, lc -= (9 * 5)); for (lp = 0; lc >= 9; lp++, lc -= 9); lzmaInternalSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp))) * sizeof(CProb); lzmaInternalData = allocFunc(lzmaInternalSize); if (lzmaInternalData == 0) return SZE_OUTOFMEMORY; result = LzmaDecode((unsigned char *)lzmaInternalData, (UInt32)lzmaInternalSize, lc, lp, pb, inBuffer + LZMA_PROPS_SIZE, (UInt32)inSize - LZMA_PROPS_SIZE, outBuffer, (UInt32)outSize, &outSizeProcessedLoc); freeFunc(lzmaInternalData); if (result != LZMA_RESULT_OK) return 1; *outSizeProcessed = (size_t)outSizeProcessedLoc; if (useFilter == 1) { UInt32 _prevMask; UInt32 _prevPos; x86_Convert_Init(_prevMask, _prevPos); x86_Convert(outBuffer, (UInt32)outSizeProcessedLoc, 0, &_prevMask, &_prevPos, 0); } return 0; }
int64_t lzbench_lzma_decompress(char *inbuf, size_t insize, char *outbuf, size_t outsize, size_t, size_t, char*) { int res; SizeT out_len = outsize; SizeT src_len = insize - LZMA_PROPS_SIZE; ELzmaStatus status; // SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc) res = LzmaDecode((uint8_t*)outbuf, &out_len, (uint8_t*)inbuf+LZMA_PROPS_SIZE, &src_len, (uint8_t*)inbuf, LZMA_PROPS_SIZE, LZMA_FINISH_END, &status, &g_Alloc); if (res != SZ_OK) return 0; // printf("out_len=%u\n", (int)(out_len + LZMA_PROPS_SIZE)); return out_len; }
int main() { uint32_t i; int r; uint32_t src_len = sizeof(c_data); uint32_t dst_len = 131072ul; uint32_t src_out = 0; uint32_t dst_out = 0; const bytep src; bytep dst; CLzmaDecoderState *s; printf("Decompress %lu %lu\n", (long)src_len, (long)dst_len); s = (CLzmaDecoderState *) malloc(32768u); src = c_data; dst = (bytep) acc_halloc(dst_len); if (!s || !dst) { printf("ERROR: Out of memory!\n"); return 1; } for (i = 0; i != dst_len; ++i) dst[i] = 255; s->Properties.lc = 3; s->Properties.lp = 0; s->Properties.pb = 2; r = LzmaDecode(s, src, src_len, &src_out, dst, dst_len, &dst_out); if (r != 0 || src_out != src_len || dst_out != dst_len) { printf("ERROR: Decompression error %d %lu %lu\n", r, (long)src_out, (long)dst_out); return 1; } i = 0; if (dst[i] != 1) goto data_error; while (++i != dst_len - 1) if (dst[i] != 0) goto data_error; if (dst[i] != 2) goto data_error; printf("Decompression test passed. All done.\n"); return 0; data_error: printf("ERROR: Decompression data error at offset %lu.\n", (long)i); return 1; }
STATIC int jffs2_lzma_decompress(unsigned char *data_in, unsigned char *cpage_out, uint32_t srclen, uint32_t destlen) { int ret; SizeT dl = (SizeT)destlen; SizeT sl = (SizeT)srclen; ELzmaStatus status; ret = LzmaDecode(cpage_out, &dl, data_in, &sl, propsEncoded, propsSize, LZMA_FINISH_ANY, &status, &lzma_alloc); if (ret != SZ_OK || status == LZMA_STATUS_NOT_FINISHED || dl != (SizeT)destlen) return -1; return 0; }
bool zmodifyer::uncompress( unsigned char * dest, unsigned int & destLen, const unsigned char *src, size_t srcLen) { if( !src || srcLen < 10 || !dest ) return false; if( destLen < *(unsigned int*)(src+6) ) return false; unsigned char sizeProp = *(src+5); srcLen -= 10; ELzmaStatus status; int result = LzmaDecode(dest, &destLen, (src+10), &srcLen, src, sizeProp, LZMA_FINISH_ANY, &status, &alloctator); return (result == SZ_OK || result == SZ_ERROR_INPUT_EOF) ? true : false; }
int LzmaRamDecompress( const unsigned char *inBuffer, size_t inSize, unsigned char *outBuffer, size_t outSize, size_t *outSizeProcessed, void * (*allocFunc)(size_t size), void (*freeFunc)(void *)) { CLzmaDecoderState state; /* it's about 24 bytes structure, if int is 32-bit */ int result; SizeT outSizeProcessedLoc; SizeT inProcessed; int useFilter; if (inSize < LZMA_PROPS_SIZE) return 1; useFilter = inBuffer[0]; *outSizeProcessed = 0; if (useFilter > 1) return 1; if (LzmaDecodeProperties(&state.Properties, inBuffer + 1, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) return 1; state.Probs = (CProb *)allocFunc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); if (state.Probs == 0) return SZE_OUTOFMEMORY; result = LzmaDecode(&state, inBuffer + LZMA_PROPS_SIZE, (SizeT)inSize - LZMA_PROPS_SIZE, &inProcessed, outBuffer, (SizeT)outSize, &outSizeProcessedLoc); freeFunc(state.Probs); if (result != LZMA_RESULT_OK) return 1; *outSizeProcessed = (size_t)outSizeProcessedLoc; if (useFilter == 1) { UInt32 _prevMask; UInt32 _prevPos; x86_Convert_Init(_prevMask, _prevPos); x86_Convert(outBuffer, (UInt32)outSizeProcessedLoc, 0, &_prevMask, &_prevPos, 0); } return 0; }
errno_t plain_lzma_decode( void *dest, size_t *dest_len, void *src, size_t *src_len, int logLevel ) { Byte propData[5]; ELzmaStatus status; SRes rc = LzmaDecode( dest, dest_len, src, src_len, propData, sizeof(propData), LZMA_FINISH_END, &status, &alloc); switch(rc) { case SZ_OK: break; case SZ_ERROR_DATA: SHOW_ERROR0( logLevel, "Broken data" ); return EFTYPE; case SZ_ERROR_MEM: SHOW_ERROR0( logLevel, "Out of mem" ); return ENOMEM; case SZ_ERROR_UNSUPPORTED: SHOW_ERROR0( logLevel, "Unsupported props" ); return EINVAL; case SZ_ERROR_INPUT_EOF: SHOW_ERROR0( logLevel, "Premature data end" ); return ENOSPC; } switch(status) { case LZMA_STATUS_FINISHED_WITH_MARK: break; case LZMA_STATUS_NOT_SPECIFIED: // impossible case LZMA_STATUS_NEEDS_MORE_INPUT: case LZMA_STATUS_NOT_FINISHED: case LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK: SHOW_ERROR0( logLevel, "Premature data end" ); return ENOSPC; } return 0; }
char* Decompress(const char *compressed, size_t compressedSize, size_t* uncompressedSizeOut, Allocator *allocator) { ISzAllocatorAlloc lzmaAlloc; lzmaAlloc.Alloc = LzmaAllocatorAlloc; lzmaAlloc.Free = LzmaAllocatorFree; lzmaAlloc.allocator = allocator; if (compressedSize < LZMA86_HEADER_SIZE) return NULL; uint8_t usesX86Filter = (uint8_t)compressed[0]; if (usesX86Filter > 1) return NULL; const uint8_t* compressed2 = (const uint8_t*)compressed; SizeT uncompressedSize = Read4(compressed2 + LZMA86_SIZE_OFFSET); uint8_t* uncompressed = (uint8_t*)Allocator::Alloc(allocator, uncompressedSize); if (!uncompressed) return NULL; SizeT compressedSizeTmp = compressedSize - LZMA86_HEADER_SIZE; SizeT uncompressedSizeTmp = uncompressedSize; ELzmaStatus status; // Note: would be nice to understand why status returns LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK and // not LZMA_STATUS_FINISHED_WITH_MARK. It seems to work, though. int res = LzmaDecode(uncompressed, &uncompressedSizeTmp, compressed2 + LZMA86_HEADER_SIZE, &compressedSizeTmp, compressed2 + 1, LZMA_PROPS_SIZE, LZMA_FINISH_END, &status, &lzmaAlloc); if (SZ_OK != res) { Allocator::Free(allocator, uncompressed); return NULL; } if (usesX86Filter) { UInt32 x86State; x86_Convert_Init(x86State); x86_Convert(uncompressed, uncompressedSizeTmp, 0, &x86State, 0); } *uncompressedSizeOut = uncompressedSize; return (char*)uncompressed; }
/** Decompresses a Lzma compressed source buffer. Extracts decompressed data to its original form. If the compressed source data specified by Source is successfully decompressed into Destination, then RETURN_SUCCESS is returned. If the compressed source data specified by Source is not in a valid compressed data format, then RETURN_INVALID_PARAMETER is returned. @param Source The source buffer containing the compressed data. @param SourceSize The size of source buffer. @param Destination The destination buffer to store the decompressed data @param Scratch A temporary scratch buffer that is used to perform the decompression. This is an optional parameter that may be NULL if the required scratch buffer size is 0. @retval RETURN_SUCCESS Decompression completed successfully, and the uncompressed buffer is returned in Destination. @retval RETURN_INVALID_PARAMETER The source buffer specified by Source is corrupted (not in a valid compressed format). **/ RETURN_STATUS EFIAPI LzmaUefiDecompress ( IN CONST VOID *Source, IN UINTN SourceSize, IN OUT VOID *Destination, IN OUT VOID *Scratch ) { SRes LzmaResult; ELzmaStatus Status; SizeT DecodedBufSize; SizeT EncodedDataSize; ISzAllocWithData AllocFuncs; AllocFuncs.Functions.Alloc = SzAlloc; AllocFuncs.Functions.Free = SzFree; AllocFuncs.Buffer = Scratch; AllocFuncs.BufferSize = SCRATCH_BUFFER_REQUEST_SIZE; DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source); EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE); // PrintString ("DecodedBufSize = %x EncodedDataSize = %x\n", DecodedBufSize, EncodedDataSize); LzmaResult = LzmaDecode( Destination, &DecodedBufSize, (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE), &EncodedDataSize, Source, LZMA_PROPS_SIZE, LZMA_FINISH_END, &Status, &(AllocFuncs.Functions) ); // PrintString ("LzmaDecode OK\n"); if (LzmaResult == SZ_OK) { return RETURN_SUCCESS; } else { return RETURN_INVALID_PARAMETER; } }
unsigned long ulzma(unsigned char * src, unsigned char * dst) { unsigned char properties[LZMA_PROPERTIES_SIZE]; UInt32 outSize; SizeT inProcessed; SizeT outProcessed; int res; CLzmaDecoderState state; SizeT mallocneeds; #if !defined(__PRE_RAM__) /* in ramstage, this can go in BSS */ static #endif /* in pre-ram, it must go on the stack */ unsigned char scratchpad[15980]; unsigned char *cp; memcpy(properties, src, LZMA_PROPERTIES_SIZE); /* The outSize in LZMA stream is a 64bit integer stored in little-endian * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by * unaligned memory address and to load in correct endianess, read each * byte and re-costruct. */ cp = src + LZMA_PROPERTIES_SIZE; outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0]; if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n"); return 0; } mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); if (mallocneeds > 15980) { printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n"); return 0; } state.Probs = (CProb *)scratchpad; res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed, dst, outSize, &outProcessed); if (res != 0) { printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res); return 0; } return outSize; }
size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn) { unsigned char properties[LZMA_PROPERTIES_SIZE]; const int data_offset = LZMA_PROPERTIES_SIZE + 8; UInt32 outSize; SizeT inProcessed; SizeT outProcessed; int res; CLzmaDecoderState state; SizeT mallocneeds; MAYBE_STATIC unsigned char scratchpad[15980]; const unsigned char *cp; /* Note: these timestamps aren't useful for memory-mapped media (x86) */ timestamp_add_now(TS_START_ULZMA); memcpy(properties, src, LZMA_PROPERTIES_SIZE); /* The outSize in LZMA stream is a 64bit integer stored in little-endian * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by * unaligned memory address and to load in correct endianness, read each * byte and re-construct. */ cp = src + LZMA_PROPERTIES_SIZE; outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0]; if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n"); return 0; } mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); if (mallocneeds > 15980) { printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n"); return 0; } state.Probs = (CProb *)scratchpad; res = LzmaDecode(&state, src + data_offset, srcn - data_offset, &inProcessed, dst, outSize, &outProcessed); if (res != 0) { printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res); return 0; } timestamp_add_now(TS_END_ULZMA); return outProcessed; }
uint8_t* decodeLZMA(uint8_t* in, unsigned inSize, unsigned* uncompressedSizeOut) { const unsigned PropHeaderSize = 5; const unsigned HeaderSize = 13; int32_t outSize = read4(in + PropHeaderSize); SizeT outSizeT = outSize; uint8_t* out = static_cast<uint8_t*>(malloc(outSize)); SizeT inSizeT = inSize; ELzmaStatus status; int result = LzmaDecode(out, &outSizeT, in + HeaderSize, &inSizeT, in, PropHeaderSize, LZMA_FINISH_END, &status, &g_Alloc); //expect(s, status == LZMA_STATUS_FINISHED_WITH_MARK); *uncompressedSizeOut = outSize; return out; }
static int Decompress_LZMA_MPK(void * pvOutBuffer, int * pcbOutBuffer, void * pvInBuffer, int cbInBuffer) { ELzmaStatus LzmaStatus; ISzAlloc SzAlloc; Byte * destBuffer = (Byte *)pvOutBuffer; Byte * srcBuffer = (Byte *)pvInBuffer; SizeT destLen = *pcbOutBuffer; SizeT srcLen = cbInBuffer; SRes nResult; BYTE LZMA_Props[] = {0x5D, 0x00, 0x00, 0x00, 0x01}; // There must be at least 0x0E bytes in the buffer if(srcLen <= sizeof(LZMA_Props)) return 0; // Verify the props header if(memcmp(pvInBuffer, LZMA_Props, sizeof(LZMA_Props))) return 0; // Fill the callbacks in structures SzAlloc.Alloc = LZMA_Callback_Alloc; SzAlloc.Free = LZMA_Callback_Free; // Perform compression srcLen = cbInBuffer - sizeof(LZMA_Props); nResult = LzmaDecode(destBuffer, &destLen, srcBuffer + sizeof(LZMA_Props), &srcLen, srcBuffer, sizeof(LZMA_Props), LZMA_FINISH_END, &LzmaStatus, &SzAlloc); if(nResult != SZ_OK) return 0; *pcbOutBuffer = (unsigned int)destLen; return 1; }
long lzma_read(struct LZMAFile *lzmaFile, unsigned char *buffer, unsigned len, _fsize64_t* bytesRead) { SizeT outProcessed; SizeT outAvail = len; int res; if (!lzmaFile->waitEOS && lzmaFile->outSizeHigh == 0 && outAvail > lzmaFile->outSize) outAvail = (SizeT)(lzmaFile->outSize); res = LzmaDecode(&(lzmaFile->state), &(lzmaFile->InCallback), buffer, outAvail, &outProcessed); *bytesRead = g_totalRead; if (res != 0) { PrintMessage("lzma_read: Decoding error (%d)", res); return -1; } if (lzmaFile->outSize < outProcessed) lzmaFile->outSizeHigh--; lzmaFile->outSize -= (UInt32)outProcessed; lzmaFile->outSize &= 0xFFFFFFFF; if (outProcessed == 0) { if (!lzmaFile->waitEOS && (lzmaFile->outSize != 0 || lzmaFile->outSizeHigh != 0)) { PrintMessage("lzma_read: Unexpected EOS"); return -1; } } return outProcessed; }
// the first compressed byte indicates whether compression is LZMA (0), LZMA+BJC (1) or none (-1) static bool Decompress(const char *compressed, size_t compressedSize, char *uncompressed, size_t uncompressedSize, Allocator *allocator) { if (compressedSize < 1) return false; uint8_t usesX86Filter = compressed[0]; // handle stored data if (usesX86Filter == (uint8_t)-1) { if (uncompressedSize != compressedSize - 1) return false; memcpy(uncompressed, compressed + 1, compressedSize - 1); return true; } if (compressedSize < LZMA_HEADER_SIZE || usesX86Filter > 1) return false; SizeT uncompressedSizeCmp = uncompressedSize; SizeT compressedSizeTmp = compressedSize - LZMA_HEADER_SIZE; ISzAllocatorAlloc lzmaAlloc(allocator); ELzmaStatus status; int res = LzmaDecode((Byte *)uncompressed, &uncompressedSizeCmp, (Byte *)compressed + LZMA_HEADER_SIZE, &compressedSizeTmp, (Byte *)compressed + 1, LZMA_PROPS_SIZE, LZMA_FINISH_END, &status, &lzmaAlloc); if (SZ_OK != res || status != LZMA_STATUS_FINISHED_WITH_MARK) return false; if (uncompressedSizeCmp != uncompressedSize) return false; if (usesX86Filter) { UInt32 x86State; x86_Convert_Init(x86State); x86_Convert((Byte *)uncompressed, uncompressedSize, 0, &x86State, 0); } return true; }