]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/lzma/LzmaTools.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[people/ms/u-boot.git] / lib / lzma / LzmaTools.c
CommitLineData
fc9c1727 1/*
caf72ff3 2 * Usefuls routines based on the LzmaTest.c file from LZMA SDK 4.65
fc9c1727 3 *
caf72ff3 4 * Copyright (C) 2007-2009 Industrie Dial Face S.p.A.
fc9c1727
LCM
5 * Luigi 'Comio' Mantellini (luigi.mantellini@idf-hit.com)
6 *
7 * Copyright (C) 1999-2005 Igor Pavlov
8 *
1a459660 9 * SPDX-License-Identifier: GPL-2.0+
fc9c1727
LCM
10 */
11
12/*
13 * LZMA_Alone stream format:
14 *
15 * uchar Properties[5]
16 * uint64 Uncompressed size
17 * uchar data[*]
18 *
19 */
20
21#include <config.h>
22#include <common.h>
fafbb2c3 23#include <watchdog.h>
fc9c1727
LCM
24
25#ifdef CONFIG_LZMA
26
27#define LZMA_PROPERTIES_OFFSET 0
caf72ff3 28#define LZMA_SIZE_OFFSET LZMA_PROPS_SIZE
fc9c1727
LCM
29#define LZMA_DATA_OFFSET LZMA_SIZE_OFFSET+sizeof(uint64_t)
30
31#include "LzmaTools.h"
caf72ff3 32#include "LzmaDec.h"
fc9c1727
LCM
33
34#include <linux/string.h>
35#include <malloc.h>
36
caf72ff3
LCM
37static void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); }
38static void SzFree(void *p, void *address) { p = p; free(address); }
39
fc9c1727 40int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize,
caf72ff3 41 unsigned char *inStream, SizeT length)
fc9c1727 42{
caf72ff3
LCM
43 int res = SZ_ERROR_DATA;
44 int i;
45 ISzAlloc g_Alloc;
46
47 SizeT outSizeFull = 0xFFFFFFFF; /* 4GBytes limit */
caf72ff3
LCM
48 SizeT outProcessed;
49 SizeT outSize;
50 SizeT outSizeHigh;
51 ELzmaStatus state;
52 SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE);
53
dd059842
MV
54 debug ("LZMA: Image address............... 0x%p\n", inStream);
55 debug ("LZMA: Properties address.......... 0x%p\n", inStream + LZMA_PROPERTIES_OFFSET);
56 debug ("LZMA: Uncompressed size address... 0x%p\n", inStream + LZMA_SIZE_OFFSET);
57 debug ("LZMA: Compressed data address..... 0x%p\n", inStream + LZMA_DATA_OFFSET);
58 debug ("LZMA: Destination address......... 0x%p\n", outStream);
caf72ff3
LCM
59
60 memset(&state, 0, sizeof(state));
61
62 outSize = 0;
63 outSizeHigh = 0;
64 /* Read the uncompressed size */
65 for (i = 0; i < 8; i++) {
66 unsigned char b = inStream[LZMA_SIZE_OFFSET + i];
67 if (i < 4) {
68 outSize += (UInt32)(b) << (i * 8);
69 } else {
70 outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
71 }
72 }
73
74 outSizeFull = (SizeT)outSize;
75 if (sizeof(SizeT) >= 8) {
76 /*
77 * SizeT is a 64 bit uint => We can manage files larger than 4GB!
78 *
79 */
80 outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
81 } else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize) {
82 /*
83 * SizeT is a 32 bit uint => We cannot manage files larger than
f68ab43d
MF
84 * 4GB! Assume however that all 0xf values is "unknown size" and
85 * not actually a file of 2^64 bits.
caf72ff3
LCM
86 *
87 */
f68ab43d
MF
88 if (outSizeHigh != (SizeT)-1 || outSize != (SizeT)-1) {
89 debug ("LZMA: 64bit support not enabled.\n");
90 return SZ_ERROR_DATA;
91 }
caf72ff3
LCM
92 }
93
f3e6110a
MF
94 debug("LZMA: Uncompresed size............ 0x%zx\n", outSizeFull);
95 debug("LZMA: Compresed size.............. 0x%zx\n", compressedSize);
caf72ff3
LCM
96
97 g_Alloc.Alloc = SzAlloc;
98 g_Alloc.Free = SzFree;
99
100 /* Decompress */
101 outProcessed = outSizeFull;
fafbb2c3 102
103 WATCHDOG_RESET();
104
caf72ff3
LCM
105 res = LzmaDecode(
106 outStream, &outProcessed,
107 inStream + LZMA_DATA_OFFSET, &compressedSize,
108 inStream, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &state, &g_Alloc);
109 *uncompressedSize = outProcessed;
110 if (res != SZ_OK) {
111 return res;
112 }
113
114 return res;
fc9c1727
LCM
115}
116
117#endif