Rename __attribute__((packed)) --> __packed
[coreboot.git] / src / lib / lzma.c
blobf72755392c434105645a43034a4898df53675d0b
1 /*
2 * coreboot interface to memory-saving variant of LZMA decoder
4 * Copyright (C) 2006 Carl-Daniel Hailfinger
5 * Released under the GNU GPL v2 or later
7 * Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the
8 * LZMA SDK 4.42, which is written and distributed to public domain by Igor
9 * Pavlov.
13 #include <console/console.h>
14 #include <string.h>
15 #include <lib.h>
16 #include <timestamp.h>
18 #include "lzmadecode.h"
20 size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn)
22 unsigned char properties[LZMA_PROPERTIES_SIZE];
23 const int data_offset = LZMA_PROPERTIES_SIZE + 8;
24 UInt32 outSize;
25 SizeT inProcessed;
26 SizeT outProcessed;
27 int res;
28 CLzmaDecoderState state;
29 SizeT mallocneeds;
30 MAYBE_STATIC unsigned char scratchpad[15980];
31 const unsigned char *cp;
33 memcpy(properties, src, LZMA_PROPERTIES_SIZE);
34 /* The outSize in LZMA stream is a 64bit integer stored in little-endian
35 * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
36 * unaligned memory address and to load in correct endianness, read each
37 * byte and re-construct. */
38 cp = src + LZMA_PROPERTIES_SIZE;
39 outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0];
40 if (LzmaDecodeProperties(&state.Properties, properties,
41 LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
42 printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
43 return 0;
45 mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
46 if (mallocneeds > 15980) {
47 printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
48 return 0;
50 state.Probs = (CProb *)scratchpad;
51 res = LzmaDecode(&state, src + data_offset, srcn - data_offset,
52 &inProcessed, dst, outSize, &outProcessed);
53 if (res != 0) {
54 printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
55 return 0;
57 return outProcessed;