1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <config.h>
38#include <common.h>
39#include <watchdog.h>
40
41#ifdef CONFIG_LZMA
42
43#define LZMA_PROPERTIES_OFFSET 0
44#define LZMA_SIZE_OFFSET LZMA_PROPS_SIZE
45#define LZMA_DATA_OFFSET LZMA_SIZE_OFFSET+sizeof(uint64_t)
46
47#include "LzmaTools.h"
48#include "LzmaDec.h"
49
50#include <linux/string.h>
51#include <malloc.h>
52
53static void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); }
54static void SzFree(void *p, void *address) { p = p; free(address); }
55
56int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize,
57 unsigned char *inStream, SizeT length)
58{
59 int res = SZ_ERROR_DATA;
60 int i;
61 ISzAlloc g_Alloc;
62
63 SizeT outSizeFull = 0xFFFFFFFF;
64 SizeT outProcessed;
65 SizeT outSize;
66 SizeT outSizeHigh;
67 ELzmaStatus state;
68 SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE);
69
70 debug ("LZMA: Image address............... 0x%lx\n", inStream);
71 debug ("LZMA: Properties address.......... 0x%lx\n", inStream + LZMA_PROPERTIES_OFFSET);
72 debug ("LZMA: Uncompressed size address... 0x%lx\n", inStream + LZMA_SIZE_OFFSET);
73 debug ("LZMA: Compressed data address..... 0x%lx\n", inStream + LZMA_DATA_OFFSET);
74 debug ("LZMA: Destination address......... 0x%lx\n", outStream);
75
76 memset(&state, 0, sizeof(state));
77
78 outSize = 0;
79 outSizeHigh = 0;
80
81 for (i = 0; i < 8; i++) {
82 unsigned char b = inStream[LZMA_SIZE_OFFSET + i];
83 if (i < 4) {
84 outSize += (UInt32)(b) << (i * 8);
85 } else {
86 outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
87 }
88 }
89
90 outSizeFull = (SizeT)outSize;
91 if (sizeof(SizeT) >= 8) {
92
93
94
95
96 outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
97 } else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize) {
98
99
100
101
102
103
104 if (outSizeHigh != (SizeT)-1 || outSize != (SizeT)-1) {
105 debug ("LZMA: 64bit support not enabled.\n");
106 return SZ_ERROR_DATA;
107 }
108 }
109
110 debug ("LZMA: Uncompresed size............ 0x%lx\n", outSizeFull);
111 debug ("LZMA: Compresed size.............. 0x%lx\n", compressedSize);
112
113 g_Alloc.Alloc = SzAlloc;
114 g_Alloc.Free = SzFree;
115
116
117 outProcessed = outSizeFull;
118
119 WATCHDOG_RESET();
120
121 res = LzmaDecode(
122 outStream, &outProcessed,
123 inStream + LZMA_DATA_OFFSET, &compressedSize,
124 inStream, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &state, &g_Alloc);
125 *uncompressedSize = outProcessed;
126 if (res != SZ_OK) {
127 return res;
128 }
129
130 return res;
131}
132
133#endif
134