1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * coreboot interface to memory-saving variant of LZMA decoder
6 * Copyright (C) 2006 Carl-Daniel Hailfinger
8 * Parts of this file are based on C/7zip/Compress/LZMA_C/LzmaTest.c from the
9 * LZMA SDK 4.42, which is written and distributed to public domain by Igor
14 #include <console/console.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;
28 CLzmaDecoderState state
;
30 static unsigned char scratchpad
[15980];
31 const unsigned char *cp
;
33 if (srcn
< data_offset
) {
34 printk(BIOS_WARNING
, "lzma: Input too small.\n");
38 memcpy(properties
, src
, LZMA_PROPERTIES_SIZE
);
39 /* The outSize in LZMA stream is a 64bit integer stored in little-endian
40 * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
41 * unaligned memory address and to load in correct endianness, read each
42 * byte and re-construct. */
43 cp
= src
+ LZMA_PROPERTIES_SIZE
;
44 outSize
= cp
[3] << 24 | cp
[2] << 16 | cp
[1] << 8 | cp
[0];
47 if (LzmaDecodeProperties(&state
.Properties
, properties
,
48 LZMA_PROPERTIES_SIZE
) != LZMA_RESULT_OK
) {
49 printk(BIOS_WARNING
, "lzma: Incorrect stream properties.\n");
52 mallocneeds
= (LzmaGetNumProbs(&state
.Properties
) * sizeof(CProb
));
53 if (mallocneeds
> 15980) {
54 printk(BIOS_WARNING
, "lzma: Decoder scratchpad too small!\n");
57 state
.Probs
= (CProb
*)scratchpad
;
58 res
= LzmaDecode(&state
, src
+ data_offset
, srcn
- data_offset
,
59 &inProcessed
, dst
, outSize
, &outProcessed
);
61 printk(BIOS_WARNING
, "lzma: Decoding error = %d\n", res
);