03ca16399a0014e0eae66c83c34e92ff6c297cfa
[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
17 unsigned long ulzma(unsigned char * src, unsigned char * dst);
18
19 unsigned long ulzma(unsigned char * src, unsigned char * dst)
20 {
21         unsigned char properties[LZMA_PROPERTIES_SIZE];
22         UInt32 outSize;
23         SizeT inProcessed;
24         SizeT outProcessed;
25         int res;
26         CLzmaDecoderState state;
27         SizeT mallocneeds;
28         unsigned char scratchpad[15980];
29
30         memcpy(properties, src, LZMA_PROPERTIES_SIZE);
31         outSize = *(UInt32 *)(src + LZMA_PROPERTIES_SIZE);
32         if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
33                 printk_warning("lzma: Incorrect stream properties.\n");
34                 return 0;
35         }
36         mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
37         if (mallocneeds > 15980) {
38                 printk_warning("lzma: Decoder scratchpad too small!\n");
39                 return 0;
40         }
41         state.Probs = (CProb *)scratchpad;
42         res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed,
43                 dst, outSize, &outProcessed);
44         if (res != 0) {
45                 printk_warning("lzma: Decoding error = %d\n", res);
46                 return 0;
47         }
48         return outSize;
49 }