2010-04-27 08:56:47 +02:00
|
|
|
/*
|
2010-09-23 20:29:40 +02:00
|
|
|
* coreboot interface to memory-saving variant of LZMA decoder
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Carl-Daniel Hailfinger
|
|
|
|
* Released under the GNU GPL v2 or later
|
|
|
|
*
|
2017-03-11 00:23:24 +01:00
|
|
|
* 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.
|
2010-09-23 20:29:40 +02:00
|
|
|
*
|
|
|
|
*/
|
2006-09-14 17:12:36 +02:00
|
|
|
|
2009-04-24 18:44:34 +02:00
|
|
|
#include <console/console.h>
|
|
|
|
#include <string.h>
|
2009-10-28 17:13:28 +01:00
|
|
|
#include <lib.h>
|
2006-09-14 17:12:36 +02:00
|
|
|
|
2014-11-11 02:27:06 +01:00
|
|
|
#include "lzmadecode.h"
|
|
|
|
|
2016-02-08 20:46:22 +01:00
|
|
|
size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn)
|
2006-09-14 17:12:36 +02:00
|
|
|
{
|
|
|
|
unsigned char properties[LZMA_PROPERTIES_SIZE];
|
2016-02-08 20:46:22 +01:00
|
|
|
const int data_offset = LZMA_PROPERTIES_SIZE + 8;
|
2006-09-14 17:12:36 +02:00
|
|
|
UInt32 outSize;
|
|
|
|
SizeT inProcessed;
|
|
|
|
SizeT outProcessed;
|
|
|
|
int res;
|
|
|
|
CLzmaDecoderState state;
|
|
|
|
SizeT mallocneeds;
|
2019-08-20 05:01:57 +02:00
|
|
|
MAYBE_STATIC_BSS unsigned char scratchpad[15980];
|
2016-02-08 20:46:22 +01:00
|
|
|
const unsigned char *cp;
|
2006-09-14 17:12:36 +02:00
|
|
|
|
2020-02-23 00:13:39 +01:00
|
|
|
if (srcn < data_offset) {
|
|
|
|
printk(BIOS_WARNING, "lzma: Input too small.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-14 17:12:36 +02:00
|
|
|
memcpy(properties, src, LZMA_PROPERTIES_SIZE);
|
2013-01-31 05:14:46 +01:00
|
|
|
/* The outSize in LZMA stream is a 64bit integer stored in little-endian
|
|
|
|
* (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
|
2013-07-10 05:51:14 +02:00
|
|
|
* unaligned memory address and to load in correct endianness, read each
|
|
|
|
* byte and re-construct. */
|
2013-01-31 05:14:46 +01:00
|
|
|
cp = src + LZMA_PROPERTIES_SIZE;
|
|
|
|
outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0];
|
2018-03-16 12:25:28 +01:00
|
|
|
if (outSize > dstn)
|
|
|
|
outSize = dstn;
|
2016-02-08 20:46:22 +01:00
|
|
|
if (LzmaDecodeProperties(&state.Properties, properties,
|
|
|
|
LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
|
2008-08-27 23:53:11 +02:00
|
|
|
return 0;
|
2006-09-14 17:12:36 +02:00
|
|
|
}
|
|
|
|
mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
|
|
|
|
if (mallocneeds > 15980) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
|
2008-08-27 23:53:11 +02:00
|
|
|
return 0;
|
2006-09-14 17:12:36 +02:00
|
|
|
}
|
|
|
|
state.Probs = (CProb *)scratchpad;
|
2016-02-08 20:46:22 +01:00
|
|
|
res = LzmaDecode(&state, src + data_offset, srcn - data_offset,
|
|
|
|
&inProcessed, dst, outSize, &outProcessed);
|
2006-09-14 17:12:36 +02:00
|
|
|
if (res != 0) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
|
2008-08-27 23:53:11 +02:00
|
|
|
return 0;
|
2006-09-14 17:12:36 +02:00
|
|
|
}
|
2015-06-26 08:22:36 +02:00
|
|
|
return outProcessed;
|
2006-09-14 17:12:36 +02:00
|
|
|
}
|