coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
lzma.c
Go to the documentation of this file.
1 /*
2  * coreboot interface to memory-saving variant of LZMA decoder
3  *
4  * Copyright (C) 2006 Carl-Daniel Hailfinger
5  * Released under the GNU GPL v2 or later
6  *
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.
10  *
11  */
12 
13 #include <console/console.h>
14 #include <string.h>
15 #include <lib.h>
16 
17 #include "lzmadecode.h"
18 
19 size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn)
20 {
21  unsigned char properties[LZMA_PROPERTIES_SIZE];
22  const int data_offset = LZMA_PROPERTIES_SIZE + 8;
23  UInt32 outSize;
24  SizeT inProcessed;
25  SizeT outProcessed;
26  int res;
28  SizeT mallocneeds;
29  static unsigned char scratchpad[15980];
30  const unsigned char *cp;
31 
32  if (srcn < data_offset) {
33  printk(BIOS_WARNING, "lzma: Input too small.\n");
34  return 0;
35  }
36 
37  memcpy(properties, src, LZMA_PROPERTIES_SIZE);
38  /* The outSize in LZMA stream is a 64bit integer stored in little-endian
39  * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
40  * unaligned memory address and to load in correct endianness, read each
41  * byte and re-construct. */
42  cp = src + LZMA_PROPERTIES_SIZE;
43  outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0];
44  if (outSize > dstn)
45  outSize = dstn;
46  if (LzmaDecodeProperties(&state.Properties, properties,
48  printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
49  return 0;
50  }
51  mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
52  if (mallocneeds > 15980) {
53  printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
54  return 0;
55  }
56  state.Probs = (CProb *)scratchpad;
57  res = LzmaDecode(&state, src + data_offset, srcn - data_offset,
58  &inProcessed, dst, outSize, &outProcessed);
59  if (res != 0) {
60  printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
61  return 0;
62  }
63  return outProcessed;
64 }
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
#define printk(level,...)
Definition: stdlib.h:16
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn)
Definition: lzma.c:19
int LzmaDecode(CLzmaDecoderState *vs, const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed, unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed)
Definition: lzmadecode.c:186
int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size)
Definition: lzmadecode.c:157
#define LZMA_RESULT_OK
Definition: lzmadecode.h:32
#define LZMA_PROPERTIES_SIZE
Definition: lzmadecode.h:39
#define LzmaGetNumProbs(Properties)
Definition: lzmadecode.h:50
unsigned int UInt32
Definition: lzmadecode.h:27
#define CProb
Definition: lzmadecode.h:30
UInt32 SizeT
Definition: lzmadecode.h:28
state
Definition: raminit.c:1787