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 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * LZMA_Alone stream format:
32 * uint64 Uncompressed size
42 #define LZMA_PROPERTIES_OFFSET 0
43 #define LZMA_SIZE_OFFSET LZMA_PROPS_SIZE
44 #define LZMA_DATA_OFFSET LZMA_SIZE_OFFSET+sizeof(uint64_t)
46 #include "LzmaTools.h"
49 #include <linux/string.h>
52 static void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); }
53 static void SzFree(void *p, void *address) { p = p; free(address); }
55 int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize,
56 unsigned char *inStream, SizeT length)
58 int res = SZ_ERROR_DATA;
62 SizeT outSizeFull = 0xFFFFFFFF; /* 4GBytes limit */
68 SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE);
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);
76 memset(&state, 0, sizeof(state));
80 /* Read the uncompressed size */
81 for (i = 0; i < 8; i++) {
82 unsigned char b = inStream[LZMA_SIZE_OFFSET + i];
84 outSize += (UInt32)(b) << (i * 8);
86 outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
90 outSizeFull = (SizeT)outSize;
91 if (sizeof(SizeT) >= 8) {
93 * SizeT is a 64 bit uint => We can manage files larger than 4GB!
96 outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
97 } else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize) {
99 * SizeT is a 32 bit uint => We cannot manage files larger than
103 debug ("LZMA: 64bit support not enabled.\n");
104 return SZ_ERROR_DATA;
107 debug ("LZMA: Uncompresed size............ 0x%lx\n", outSizeFull);
108 debug ("LZMA: Compresed size.............. 0x%lx\n", compressedSize);
110 g_Alloc.Alloc = SzAlloc;
111 g_Alloc.Free = SzFree;
114 outProcessed = outSizeFull;
116 outStream, &outProcessed,
117 inStream + LZMA_DATA_OFFSET, &compressedSize,
118 inStream, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &state, &g_Alloc);
119 *uncompressedSize = outProcessed;