blob: 7b4c69038bf781a2eba9ebea0706f6198d53fd90 [file] [log] [blame]
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +00001/*
2
Stefan Reinauerf8ee1802008-01-18 15:08:58 +00003Coreboot interface to memory-saving variant of LZMA decoder
Carl-Daniel Hailfingerfa510c72008-03-17 01:37:27 +00004
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +00005(C)opyright 2006 Carl-Daniel Hailfinger
Carl-Daniel Hailfingerfa510c72008-03-17 01:37:27 +00006Released under the GNU GPL v2 or later
7
8Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the LZMA
9SDK 4.42, which is written and distributed to public domain by Igor Pavlov.
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000010
11*/
12
13#include "lzmadecode.c"
Patrick Georgi79941642009-04-24 16:44:34 +000014#include <console/console.h>
15#include <string.h>
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000016
17
Patrick Georgi79941642009-04-24 16:44:34 +000018unsigned long ulzma(unsigned char * src, unsigned char * dst)
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000019{
20 unsigned char properties[LZMA_PROPERTIES_SIZE];
21 UInt32 outSize;
22 SizeT inProcessed;
23 SizeT outProcessed;
24 int res;
25 CLzmaDecoderState state;
26 SizeT mallocneeds;
27 unsigned char scratchpad[15980];
28
29 memcpy(properties, src, LZMA_PROPERTIES_SIZE);
30 outSize = *(UInt32 *)(src + LZMA_PROPERTIES_SIZE);
31 if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
32 printk_warning("Incorrect stream properties\n");
Ward Vandewegeec2bd532008-08-27 21:53:11 +000033 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000034 }
35 mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
36 if (mallocneeds > 15980) {
37 printk_warning("Decoder scratchpad too small!\n");
Ward Vandewegeec2bd532008-08-27 21:53:11 +000038 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000039 }
40 state.Probs = (CProb *)scratchpad;
41 res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed,
42 dst, outSize, &outProcessed);
43 if (res != 0) {
44 printk_warning("Decoding error = %d\n", res);
Ward Vandewegeec2bd532008-08-27 21:53:11 +000045 return 0;
Carl-Daniel Hailfingercba07dd2006-09-14 15:12:36 +000046 }
47 return outSize;
48}