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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 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> |
rhabarber1848@web.de | fafbb2c | 2009-07-24 08:16:30 +0200 | [diff] [blame] | 23 | #include <watchdog.h> |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 24 | |
| 25 | #ifdef CONFIG_LZMA |
| 26 | |
| 27 | #define LZMA_PROPERTIES_OFFSET 0 |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 28 | #define LZMA_SIZE_OFFSET LZMA_PROPS_SIZE |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 29 | #define LZMA_DATA_OFFSET LZMA_SIZE_OFFSET+sizeof(uint64_t) |
| 30 | |
| 31 | #include "LzmaTools.h" |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 32 | #include "LzmaDec.h" |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 33 | |
| 34 | #include <linux/string.h> |
| 35 | #include <malloc.h> |
| 36 | |
Jeroen Hofstee | 867abda | 2014-06-10 23:37:23 +0200 | [diff] [blame] | 37 | static void *SzAlloc(void *p, size_t size) { return malloc(size); } |
| 38 | static void SzFree(void *p, void *address) { free(address); } |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 39 | |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 40 | int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize, |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 41 | unsigned char *inStream, SizeT length) |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 42 | { |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 43 | int res = SZ_ERROR_DATA; |
| 44 | int i; |
| 45 | ISzAlloc g_Alloc; |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 46 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 47 | SizeT outSizeFull = 0xFFFFFFFF; /* 4GBytes limit */ |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 48 | SizeT outProcessed; |
| 49 | SizeT outSize; |
| 50 | SizeT outSizeHigh; |
| 51 | ELzmaStatus state; |
| 52 | SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE); |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 53 | |
Marek Vasut | dd05984 | 2011-10-24 23:41:36 +0000 | [diff] [blame] | 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); |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 59 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 60 | memset(&state, 0, sizeof(state)); |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 61 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 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 | } |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 73 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 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 |
Mike Frysinger | f68ab43 | 2009-12-04 05:35:15 -0500 | [diff] [blame] | 84 | * 4GB! Assume however that all 0xf values is "unknown size" and |
| 85 | * not actually a file of 2^64 bits. |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 86 | * |
| 87 | */ |
Mike Frysinger | f68ab43 | 2009-12-04 05:35:15 -0500 | [diff] [blame] | 88 | if (outSizeHigh != (SizeT)-1 || outSize != (SizeT)-1) { |
| 89 | debug ("LZMA: 64bit support not enabled.\n"); |
| 90 | return SZ_ERROR_DATA; |
| 91 | } |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 92 | } |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 93 | |
Mike Frysinger | f3e6110a | 2012-01-07 23:14:35 +0000 | [diff] [blame] | 94 | debug("LZMA: Uncompresed size............ 0x%zx\n", outSizeFull); |
| 95 | debug("LZMA: Compresed size.............. 0x%zx\n", compressedSize); |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 96 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 97 | g_Alloc.Alloc = SzAlloc; |
| 98 | g_Alloc.Free = SzFree; |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 99 | |
Kees Cook | afca294 | 2013-08-16 07:59:14 -0700 | [diff] [blame] | 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; |
| 103 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 104 | /* Decompress */ |
Antonios Vamporakis | 4d3b8a0 | 2013-12-31 02:57:01 +0100 | [diff] [blame] | 105 | outProcessed = outSizeFull; |
rhabarber1848@web.de | fafbb2c | 2009-07-24 08:16:30 +0200 | [diff] [blame] | 106 | |
| 107 | WATCHDOG_RESET(); |
| 108 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 109 | res = LzmaDecode( |
| 110 | outStream, &outProcessed, |
| 111 | inStream + LZMA_DATA_OFFSET, &compressedSize, |
Kees Cook | afca294 | 2013-08-16 07:59:14 -0700 | [diff] [blame] | 112 | inStream, LZMA_PROPS_SIZE, LZMA_FINISH_END, &state, &g_Alloc); |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 113 | *uncompressedSize = outProcessed; |
Antonios Vamporakis | 4d3b8a0 | 2013-12-31 02:57:01 +0100 | [diff] [blame] | 114 | |
| 115 | debug("LZMA: Uncompresed ................ 0x%zx\n", outProcessed); |
| 116 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 117 | if (res != SZ_OK) { |
| 118 | return res; |
| 119 | } |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 120 | |
Luigi 'Comio' Mantellini | caf72ff | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 121 | return res; |
Luigi 'Comio' Mantellini | fc9c172 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | #endif |