libpayload: Add liblzma, libcbfs
[coreboot.git] / payloads / libpayload / liblzma / lzma.c
1 /*
2  * coreboot interface to memory-saving variant of LZMA decoder
3  *
4  * Copyright (C) 2006 Carl-Daniel Hailfinger
5  * Released under the BSD license
6  *
7  * Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the LZMA
8  * SDK 4.42, which is written and distributed to public domain by Igor Pavlov.
9  *
10  */
11
12 #include <lzma.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include "lzmadecode.c"
16
17 unsigned long ulzma(unsigned char * src, unsigned char * dst)
18 {
19         unsigned char properties[LZMA_PROPERTIES_SIZE];
20         UInt32 outSize;
21         SizeT inProcessed;
22         SizeT outProcessed;
23         int res;
24         CLzmaDecoderState state;
25         SizeT mallocneeds;
26         unsigned char scratchpad[15980];
27
28         memcpy(properties, src, LZMA_PROPERTIES_SIZE);
29         outSize = *(UInt32 *)(src + LZMA_PROPERTIES_SIZE);
30         if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
31                 printf("lzma: Incorrect stream properties.\n");
32                 return 0;
33         }
34         mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
35         if (mallocneeds > 15980) {
36                 printf("lzma: Decoder scratchpad too small!\n");
37                 return 0;
38         }
39         state.Probs = (CProb *)scratchpad;
40         res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed,
41                 dst, outSize, &outProcessed);
42         if (res != 0) {
43                 printf("lzma: Decoding error = %d\n", res);
44                 return 0;
45         }
46         return outSize;
47 }