2011-07-21 15:43:14 +02:00
|
|
|
/*
|
|
|
|
* coreboot interface to memory-saving variant of LZMA decoder
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Carl-Daniel Hailfinger
|
|
|
|
* Released under the BSD license
|
|
|
|
*
|
|
|
|
* Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the LZMA
|
|
|
|
* SDK 4.42, which is written and distributed to public domain by Igor Pavlov.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <lzma.h>
|
2016-08-06 05:43:47 +02:00
|
|
|
#include <stdlib.h>
|
2011-07-21 15:43:14 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "lzmadecode.c"
|
|
|
|
|
2013-08-10 18:35:56 +02:00
|
|
|
unsigned long ulzman(const unsigned char *src, unsigned long srcn,
|
|
|
|
unsigned char *dst, unsigned long dstn)
|
2011-07-21 15:43:14 +02:00
|
|
|
{
|
|
|
|
unsigned char properties[LZMA_PROPERTIES_SIZE];
|
2013-08-10 18:35:56 +02:00
|
|
|
const int data_offset = LZMA_PROPERTIES_SIZE + 8;
|
2011-07-21 15:43:14 +02:00
|
|
|
UInt32 outSize;
|
|
|
|
SizeT inProcessed;
|
|
|
|
SizeT outProcessed;
|
|
|
|
int res;
|
|
|
|
CLzmaDecoderState state;
|
|
|
|
SizeT mallocneeds;
|
2016-08-06 05:43:47 +02:00
|
|
|
unsigned char *scratchpad;
|
2011-07-21 15:43:14 +02:00
|
|
|
|
2020-02-21 04:55:45 +01:00
|
|
|
if (srcn < data_offset) {
|
|
|
|
printf("lzma: Input too small.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-21 15:43:14 +02:00
|
|
|
memcpy(properties, src, LZMA_PROPERTIES_SIZE);
|
2013-03-06 13:46:00 +01:00
|
|
|
memcpy(&outSize, src + LZMA_PROPERTIES_SIZE, sizeof(outSize));
|
2013-08-10 18:35:56 +02:00
|
|
|
if (outSize > dstn)
|
|
|
|
outSize = dstn;
|
|
|
|
if (LzmaDecodeProperties(&state.Properties, properties,
|
|
|
|
LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
|
2011-07-21 15:43:14 +02:00
|
|
|
printf("lzma: Incorrect stream properties.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
|
2016-08-06 05:43:47 +02:00
|
|
|
scratchpad = malloc(mallocneeds);
|
|
|
|
if (!scratchpad) {
|
|
|
|
printf("lzma: Cannot allocate %u bytes for scratchpad!\n",
|
|
|
|
mallocneeds);
|
2011-07-21 15:43:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
state.Probs = (CProb *)scratchpad;
|
2013-08-10 18:35:56 +02:00
|
|
|
res = LzmaDecode(&state, src + data_offset, srcn - data_offset,
|
|
|
|
&inProcessed, dst, outSize, &outProcessed);
|
2016-08-06 05:43:47 +02:00
|
|
|
free(scratchpad);
|
2011-07-21 15:43:14 +02:00
|
|
|
if (res != 0) {
|
|
|
|
printf("lzma: Decoding error = %d\n", res);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-26 08:22:36 +02:00
|
|
|
return outProcessed;
|
2011-07-21 15:43:14 +02:00
|
|
|
}
|
2013-08-10 18:35:56 +02:00
|
|
|
|
|
|
|
unsigned long ulzma(const unsigned char *src, unsigned char *dst)
|
|
|
|
{
|
|
|
|
return ulzman(src, (unsigned long)(-1), dst, (unsigned long)(-1));
|
|
|
|
}
|