Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 1 | /* |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 2 | * Usefuls routines based on the LzmaTest.c file from LZMA SDK 4.65 |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 3 | * |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 4 | * Copyright (C) 2007-2009 Industrie Dial Face S.p.A. |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 5 | * Luigi 'Comio' Mantellini (luigi.mantellini@idf-hit.com) |
| 6 | * |
| 7 | * Copyright (C) 1999-2005 Igor Pavlov |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * LZMA_Alone stream format: |
| 30 | * |
| 31 | * uchar Properties[5] |
| 32 | * uint64 Uncompressed size |
| 33 | * uchar data[*] |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | #include <config.h> |
| 38 | #include <common.h> |
rhabarber1848@web.de | fafbb2c | 2009-07-24 08:16:30 +0200 | [diff] [blame] | 39 | #include <watchdog.h> |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 40 | |
| 41 | #ifdef CONFIG_LZMA |
| 42 | |
| 43 | #define LZMA_PROPERTIES_OFFSET 0 |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 44 | #define LZMA_SIZE_OFFSET LZMA_PROPS_SIZE |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 45 | #define LZMA_DATA_OFFSET LZMA_SIZE_OFFSET+sizeof(uint64_t) |
| 46 | |
| 47 | #include "LzmaTools.h" |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 48 | #include "LzmaDec.h" |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 49 | |
| 50 | #include <linux/string.h> |
| 51 | #include <malloc.h> |
| 52 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 53 | static void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); } |
| 54 | static void SzFree(void *p, void *address) { p = p; free(address); } |
| 55 | |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 56 | int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize, |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 57 | unsigned char *inStream, SizeT length) |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 58 | { |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 59 | int res = SZ_ERROR_DATA; |
| 60 | int i; |
| 61 | ISzAlloc g_Alloc; |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 62 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 63 | SizeT outSizeFull = 0xFFFFFFFF; /* 4GBytes limit */ |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 64 | SizeT outProcessed; |
| 65 | SizeT outSize; |
| 66 | SizeT outSizeHigh; |
| 67 | ELzmaStatus state; |
| 68 | SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE); |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 69 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 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); |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 75 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 76 | memset(&state, 0, sizeof(state)); |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 77 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 78 | outSize = 0; |
| 79 | outSizeHigh = 0; |
| 80 | /* Read the uncompressed size */ |
| 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 | } |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 89 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 90 | outSizeFull = (SizeT)outSize; |
| 91 | if (sizeof(SizeT) >= 8) { |
| 92 | /* |
| 93 | * SizeT is a 64 bit uint => We can manage files larger than 4GB! |
| 94 | * |
| 95 | */ |
| 96 | outSizeFull |= (((SizeT)outSizeHigh << 16) << 16); |
| 97 | } else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize) { |
| 98 | /* |
| 99 | * SizeT is a 32 bit uint => We cannot manage files larger than |
Mike Frysinger | f68ab43 | 2009-12-04 05:35:15 -0500 | [diff] [blame] | 100 | * 4GB! Assume however that all 0xf values is "unknown size" and |
| 101 | * not actually a file of 2^64 bits. |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 102 | * |
| 103 | */ |
Mike Frysinger | f68ab43 | 2009-12-04 05:35:15 -0500 | [diff] [blame] | 104 | if (outSizeHigh != (SizeT)-1 || outSize != (SizeT)-1) { |
| 105 | debug ("LZMA: 64bit support not enabled.\n"); |
| 106 | return SZ_ERROR_DATA; |
| 107 | } |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 108 | } |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 109 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 110 | debug ("LZMA: Uncompresed size............ 0x%lx\n", outSizeFull); |
| 111 | debug ("LZMA: Compresed size.............. 0x%lx\n", compressedSize); |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 112 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 113 | g_Alloc.Alloc = SzAlloc; |
| 114 | g_Alloc.Free = SzFree; |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 115 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 116 | /* Decompress */ |
| 117 | outProcessed = outSizeFull; |
rhabarber1848@web.de | fafbb2c | 2009-07-24 08:16:30 +0200 | [diff] [blame] | 118 | |
| 119 | WATCHDOG_RESET(); |
| 120 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 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 | } |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 129 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 130 | return res; |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | #endif |