532a2b2614589a4bd20d10202b3c34c9a5787583
[coreboot.git] / src / lib / lzma.c
1 /* 
2
3 Coreboot interface to memory-saving variant of LZMA decoder
4
5 (C)opyright 2006 Carl-Daniel Hailfinger
6 Released under the GNU GPL v2 or later
7
8 Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the LZMA
9 SDK 4.42, which is written and distributed to public domain by Igor Pavlov.
10
11 */
12
13 #include "lzmadecode.c"
14 #include <console/console.h>
15 #include <string.h>
16 #include <lib.h>
17
18 unsigned long ulzma(unsigned char * src, unsigned char * dst)
19 {
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(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
33                 return 0;
34         }
35         mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
36         if (mallocneeds > 15980) {
37                 printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
38                 return 0;
39         }
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(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
45                 return 0;
46         }
47         return outSize;
48 }