]> git.karo-electronics.de Git - karo-tx-uboot.git/blobdiff - lib/lzma/LzmaTools.c
fdtdec: Add compatible id and string for Intel Quark MRC
[karo-tx-uboot.git] / lib / lzma / LzmaTools.c
index 8860bfbf3da0dd019104668491868d46f5a8be11..f88629b74f34f2e976d8497aa49a54944d87b67d 100644 (file)
@@ -6,23 +6,7 @@
  *
  * Copyright (C) 1999-2005 Igor Pavlov
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+ 
  */
 
 /*
@@ -50,8 +34,8 @@
 #include <linux/string.h>
 #include <malloc.h>
 
-static void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); }
-static void SzFree(void *p, void *address) { p = p; free(address); }
+static void *SzAlloc(void *p, size_t size) { return malloc(size); }
+static void SzFree(void *p, void *address) { free(address); }
 
 int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize,
                   unsigned char *inStream,  SizeT  length)
@@ -67,11 +51,11 @@ int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize,
     ELzmaStatus state;
     SizeT compressedSize = (SizeT)(length - LZMA_PROPS_SIZE);
 
-    debug ("LZMA: Image address............... 0x%lx\n", inStream);
-    debug ("LZMA: Properties address.......... 0x%lx\n", inStream + LZMA_PROPERTIES_OFFSET);
-    debug ("LZMA: Uncompressed size address... 0x%lx\n", inStream + LZMA_SIZE_OFFSET);
-    debug ("LZMA: Compressed data address..... 0x%lx\n", inStream + LZMA_DATA_OFFSET);
-    debug ("LZMA: Destination address......... 0x%lx\n", outStream);
+    debug ("LZMA: Image address............... 0x%p\n", inStream);
+    debug ("LZMA: Properties address.......... 0x%p\n", inStream + LZMA_PROPERTIES_OFFSET);
+    debug ("LZMA: Uncompressed size address... 0x%p\n", inStream + LZMA_SIZE_OFFSET);
+    debug ("LZMA: Compressed data address..... 0x%p\n", inStream + LZMA_DATA_OFFSET);
+    debug ("LZMA: Destination address......... 0x%p\n", outStream);
 
     memset(&state, 0, sizeof(state));
 
@@ -107,22 +91,29 @@ int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize,
         }
     }
 
-    debug ("LZMA: Uncompresed size............ 0x%lx\n", outSizeFull);
-    debug ("LZMA: Compresed size.............. 0x%lx\n", compressedSize);
+    debug("LZMA: Uncompresed size............ 0x%zx\n", outSizeFull);
+    debug("LZMA: Compresed size.............. 0x%zx\n", compressedSize);
 
     g_Alloc.Alloc = SzAlloc;
     g_Alloc.Free = SzFree;
 
+    /* Short-circuit early if we know the buffer can't hold the results. */
+    if (outSizeFull != (SizeT)-1 && *uncompressedSize < outSizeFull)
+        return SZ_ERROR_OUTPUT_EOF;
+
     /* Decompress */
-    outProcessed = outSizeFull;
+    outProcessed = min(outSizeFull, *uncompressedSize);
 
     WATCHDOG_RESET();
 
     res = LzmaDecode(
         outStream, &outProcessed,
         inStream + LZMA_DATA_OFFSET, &compressedSize,
-        inStream, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &state, &g_Alloc);
+        inStream, LZMA_PROPS_SIZE, LZMA_FINISH_END, &state, &g_Alloc);
     *uncompressedSize = outProcessed;
+
+    debug("LZMA: Uncompressed ............... 0x%zx\n", outProcessed);
+
     if (res != SZ_OK)  {
         return res;
     }