1 /* 2 * Usefuls routines based on the LzmaTest.c file from LZMA SDK 4.65 3 * 4 * Copyright (C) 2007-2009 Industrie Dial Face S.p.A. 5 * Luigi 'Comio' Mantellini (luigi.mantellini@idf-hit.com) 6 * 7 * Copyright (C) 1999-2005 Igor Pavlov 8 * 9 * SPDX-License-Identifier: GPL-2.0+ 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> 23 #include <watchdog.h> 24 25 #if CONFIG_IS_ENABLED(LZMA) 26 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) 30 31 #include "LzmaTools.h" 32 #include "LzmaDec.h" 33 34 #include <linux/string.h> 35 #include <malloc.h> 36 37 static void *SzAlloc(void *p, size_t size) { return malloc(size); } 38 static void SzFree(void *p, void *address) { free(address); } 39 40 int lzma_is_valid(const unsigned char *buf) 41 { 42 if (buf[0] != 0x5d || buf[1] || buf[2]) 43 return 0; 44 if (buf[12] && buf[12] != 0xff) 45 return 0; 46 47 return 1; 48 } 49 50 int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize, 51 unsigned char *inStream, SizeT length) 52 { 53 int res = SZ_ERROR_DATA; 54 int i; 55 ISzAlloc g_Alloc; 56 57 SizeT outSizeFull = 0xFFFFFFFF; /* 4GBytes limit */ 58 SizeT outProcessed; 59 SizeT outSize; 60 SizeT outSizeHigh; 61 ELzmaStatus state; 62 SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE); 63 64 debug ("LZMA: Image address............... 0x%p\n", inStream); 65 debug ("LZMA: Properties address.......... 0x%p\n", inStream + LZMA_PROPERTIES_OFFSET); 66 debug ("LZMA: Uncompressed size address... 0x%p\n", inStream + LZMA_SIZE_OFFSET); 67 debug ("LZMA: Compressed data address..... 0x%p\n", inStream + LZMA_DATA_OFFSET); 68 debug ("LZMA: Destination address......... 0x%p\n", outStream); 69 70 memset(&state, 0, sizeof(state)); 71 72 outSize = 0; 73 outSizeHigh = 0; 74 /* Read the uncompressed size */ 75 for (i = 0; i < 8; i++) { 76 unsigned char b = inStream[LZMA_SIZE_OFFSET + i]; 77 if (i < 4) { 78 outSize += (UInt32)(b) << (i * 8); 79 } else { 80 outSizeHigh += (UInt32)(b) << ((i - 4) * 8); 81 } 82 } 83 84 outSizeFull = (SizeT)outSize; 85 if (sizeof(SizeT) >= 8) { 86 /* 87 * SizeT is a 64 bit uint => We can manage files larger than 4GB! 88 * 89 */ 90 outSizeFull |= (((SizeT)outSizeHigh << 16) << 16); 91 } else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize) { 92 /* 93 * SizeT is a 32 bit uint => We cannot manage files larger than 94 * 4GB! Assume however that all 0xf values is "unknown size" and 95 * not actually a file of 2^64 bits. 96 * 97 */ 98 if (outSizeHigh != (SizeT)-1 || outSize != (SizeT)-1) { 99 debug ("LZMA: 64bit support not enabled.\n"); 100 return SZ_ERROR_DATA; 101 } 102 } 103 104 debug("LZMA: Uncompresed size............ 0x%zx\n", outSizeFull); 105 debug("LZMA: Compresed size.............. 0x%zx\n", compressedSize); 106 107 g_Alloc.Alloc = SzAlloc; 108 g_Alloc.Free = SzFree; 109 110 /* Short-circuit early if we know the buffer can't hold the results. */ 111 if (outSizeFull != (SizeT)-1 && *uncompressedSize < outSizeFull) 112 return SZ_ERROR_OUTPUT_EOF; 113 114 /* Decompress */ 115 outProcessed = min(outSizeFull, *uncompressedSize); 116 117 WATCHDOG_RESET(); 118 119 res = LzmaDecode( 120 outStream, &outProcessed, 121 inStream + LZMA_DATA_OFFSET, &compressedSize, 122 inStream, LZMA_PROPS_SIZE, LZMA_FINISH_END, &state, &g_Alloc); 123 *uncompressedSize = outProcessed; 124 125 debug("LZMA: Uncompressed ............... 0x%zx\n", outProcessed); 126 127 if (res != SZ_OK) { 128 return res; 129 } 130 131 return res; 132 } 133 134 #endif 135