2 * Usefuls routines based on the LzmaTest.c file from LZMA SDK 4.65
4 * Copyright (C) 2007-2009 Industrie Dial Face S.p.A.
5 * Luigi 'Comio' Mantellini (luigi.mantellini@idf-hit.com)
7 * Copyright (C) 1999-2005 Igor Pavlov
9 * SPDX-License-Identifier: GPL-2.0+
13 * LZMA_Alone stream format:
16 * uint64 Uncompressed size
27 #define LZMA_PROPERTIES_OFFSET 0
28 #define LZMA_SIZE_OFFSET LZMA_PROPS_SIZE
29 #define LZMA_DATA_OFFSET LZMA_SIZE_OFFSET+sizeof(uint64_t)
31 #include "LzmaTools.h"
34 #include <linux/string.h>
37 static void *SzAlloc(void *p, size_t size) { return malloc(size); }
38 static void SzFree(void *p, void *address) { free(address); }
40 int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize,
41 unsigned char *inStream, SizeT length)
43 int res = SZ_ERROR_DATA;
47 SizeT outSizeFull = 0xFFFFFFFF; /* 4GBytes limit */
52 SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE);
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);
60 memset(&state, 0, sizeof(state));
64 /* Read the uncompressed size */
65 for (i = 0; i < 8; i++) {
66 unsigned char b = inStream[LZMA_SIZE_OFFSET + i];
68 outSize += (UInt32)(b) << (i * 8);
70 outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
74 outSizeFull = (SizeT)outSize;
75 if (sizeof(SizeT) >= 8) {
77 * SizeT is a 64 bit uint => We can manage files larger than 4GB!
80 outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
81 } else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize) {
83 * SizeT is a 32 bit uint => We cannot manage files larger than
84 * 4GB! Assume however that all 0xf values is "unknown size" and
85 * not actually a file of 2^64 bits.
88 if (outSizeHigh != (SizeT)-1 || outSize != (SizeT)-1) {
89 debug ("LZMA: 64bit support not enabled.\n");
94 debug("LZMA: Uncompresed size............ 0x%zx\n", outSizeFull);
95 debug("LZMA: Compresed size.............. 0x%zx\n", compressedSize);
97 g_Alloc.Alloc = SzAlloc;
98 g_Alloc.Free = SzFree;
100 /* Short-circuit early if we know the buffer can't hold the results. */
101 if (outSizeFull != (SizeT)-1 && *uncompressedSize < outSizeFull)
102 return SZ_ERROR_OUTPUT_EOF;
105 outProcessed = min(outSizeFull, *uncompressedSize);
110 outStream, &outProcessed,
111 inStream + LZMA_DATA_OFFSET, &compressedSize,
112 inStream, LZMA_PROPS_SIZE, LZMA_FINISH_END, &state, &g_Alloc);
113 *uncompressedSize = outProcessed;
115 debug("LZMA: Uncompressed ............... 0x%zx\n", outProcessed);