Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 2 | /* |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 3 | * Usefuls routines based on the LzmaTest.c file from LZMA SDK 4.65 |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 4 | * |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 5 | * Copyright (C) 2007-2009 Industrie Dial Face S.p.A. |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 6 | * Luigi 'Comio' Mantellini (luigi.mantellini@idf-hit.com) |
| 7 | * |
| 8 | * Copyright (C) 1999-2005 Igor Pavlov |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * LZMA_Alone stream format: |
| 13 | * |
| 14 | * uchar Properties[5] |
| 15 | * uint64 Uncompressed size |
| 16 | * uchar data[*] |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <config.h> |
| 21 | #include <common.h> |
rhabarber1848@web.de | be335ff | 2009-07-24 08:16:30 +0200 | [diff] [blame] | 22 | #include <watchdog.h> |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 23 | |
| 24 | #ifdef CONFIG_LZMA |
| 25 | |
| 26 | #define LZMA_PROPERTIES_OFFSET 0 |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 27 | #define LZMA_SIZE_OFFSET LZMA_PROPS_SIZE |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 28 | #define LZMA_DATA_OFFSET LZMA_SIZE_OFFSET+sizeof(uint64_t) |
| 29 | |
| 30 | #include "LzmaTools.h" |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 31 | #include "LzmaDec.h" |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 32 | |
| 33 | #include <linux/string.h> |
| 34 | #include <malloc.h> |
| 35 | |
Jeroen Hofstee | 0d23e8a | 2014-06-10 23:37:23 +0200 | [diff] [blame] | 36 | static void *SzAlloc(void *p, size_t size) { return malloc(size); } |
| 37 | static void SzFree(void *p, void *address) { free(address); } |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 38 | |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 39 | int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize, |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 40 | unsigned char *inStream, SizeT length) |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 41 | { |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 42 | int res = SZ_ERROR_DATA; |
| 43 | int i; |
| 44 | ISzAlloc g_Alloc; |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 45 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 46 | SizeT outSizeFull = 0xFFFFFFFF; /* 4GBytes limit */ |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 47 | SizeT outProcessed; |
| 48 | SizeT outSize; |
| 49 | SizeT outSizeHigh; |
| 50 | ELzmaStatus state; |
| 51 | SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE); |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 52 | |
Marek Vasut | 92585c9 | 2011-10-24 23:41:36 +0000 | [diff] [blame] | 53 | debug ("LZMA: Image address............... 0x%p\n", inStream); |
| 54 | debug ("LZMA: Properties address.......... 0x%p\n", inStream + LZMA_PROPERTIES_OFFSET); |
| 55 | debug ("LZMA: Uncompressed size address... 0x%p\n", inStream + LZMA_SIZE_OFFSET); |
| 56 | debug ("LZMA: Compressed data address..... 0x%p\n", inStream + LZMA_DATA_OFFSET); |
| 57 | debug ("LZMA: Destination address......... 0x%p\n", outStream); |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 58 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 59 | memset(&state, 0, sizeof(state)); |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 60 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 61 | outSize = 0; |
| 62 | outSizeHigh = 0; |
| 63 | /* Read the uncompressed size */ |
| 64 | for (i = 0; i < 8; i++) { |
| 65 | unsigned char b = inStream[LZMA_SIZE_OFFSET + i]; |
| 66 | if (i < 4) { |
| 67 | outSize += (UInt32)(b) << (i * 8); |
| 68 | } else { |
| 69 | outSizeHigh += (UInt32)(b) << ((i - 4) * 8); |
| 70 | } |
| 71 | } |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 72 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 73 | outSizeFull = (SizeT)outSize; |
| 74 | if (sizeof(SizeT) >= 8) { |
| 75 | /* |
| 76 | * SizeT is a 64 bit uint => We can manage files larger than 4GB! |
| 77 | * |
| 78 | */ |
| 79 | outSizeFull |= (((SizeT)outSizeHigh << 16) << 16); |
| 80 | } else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize) { |
| 81 | /* |
| 82 | * SizeT is a 32 bit uint => We cannot manage files larger than |
Mike Frysinger | a502df9 | 2009-12-04 05:35:15 -0500 | [diff] [blame] | 83 | * 4GB! Assume however that all 0xf values is "unknown size" and |
| 84 | * not actually a file of 2^64 bits. |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 85 | * |
| 86 | */ |
Mike Frysinger | a502df9 | 2009-12-04 05:35:15 -0500 | [diff] [blame] | 87 | if (outSizeHigh != (SizeT)-1 || outSize != (SizeT)-1) { |
| 88 | debug ("LZMA: 64bit support not enabled.\n"); |
| 89 | return SZ_ERROR_DATA; |
| 90 | } |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 91 | } |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 92 | |
Mike Frysinger | 6001915 | 2012-01-07 23:14:35 +0000 | [diff] [blame] | 93 | debug("LZMA: Uncompresed size............ 0x%zx\n", outSizeFull); |
| 94 | debug("LZMA: Compresed size.............. 0x%zx\n", compressedSize); |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 95 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 96 | g_Alloc.Alloc = SzAlloc; |
| 97 | g_Alloc.Free = SzFree; |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 98 | |
Kees Cook | 60e50cd | 2013-08-16 07:59:14 -0700 | [diff] [blame] | 99 | /* Short-circuit early if we know the buffer can't hold the results. */ |
| 100 | if (outSizeFull != (SizeT)-1 && *uncompressedSize < outSizeFull) |
| 101 | return SZ_ERROR_OUTPUT_EOF; |
| 102 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 103 | /* Decompress */ |
Simon Glass | 47f204a | 2014-12-02 13:17:29 -0700 | [diff] [blame] | 104 | outProcessed = min(outSizeFull, *uncompressedSize); |
rhabarber1848@web.de | be335ff | 2009-07-24 08:16:30 +0200 | [diff] [blame] | 105 | |
| 106 | WATCHDOG_RESET(); |
| 107 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 108 | res = LzmaDecode( |
| 109 | outStream, &outProcessed, |
| 110 | inStream + LZMA_DATA_OFFSET, &compressedSize, |
Kees Cook | 60e50cd | 2013-08-16 07:59:14 -0700 | [diff] [blame] | 111 | inStream, LZMA_PROPS_SIZE, LZMA_FINISH_END, &state, &g_Alloc); |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 112 | *uncompressedSize = outProcessed; |
Antonios Vamporakis | 1c4f629 | 2013-12-31 02:57:01 +0100 | [diff] [blame] | 113 | |
Simon Glass | 47f204a | 2014-12-02 13:17:29 -0700 | [diff] [blame] | 114 | debug("LZMA: Uncompressed ............... 0x%zx\n", outProcessed); |
Antonios Vamporakis | 1c4f629 | 2013-12-31 02:57:01 +0100 | [diff] [blame] | 115 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 116 | if (res != SZ_OK) { |
| 117 | return res; |
| 118 | } |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 119 | |
Luigi 'Comio' Mantellini | d02bd74 | 2009-07-21 10:45:49 +0200 | [diff] [blame] | 120 | return res; |
Luigi 'Comio' Mantellini | 35afc06 | 2008-09-08 02:46:13 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | #endif |