]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
IntelFrameworkModulePkg: Clean up source files
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
index b8523f4b4fe2a227a7cabaafe7df39bc5f98146f..501a15d99d1f0c2118f9d8d92abe45fcc44a5293 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   LZMA Decompress interfaces\r
 \r
-  Copyright (c) 2009, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
   http://opensource.org/licenses/bsd-license.php\r
 **/\r
 \r
 #include "LzmaDecompressLibInternal.h"\r
-#include "Sdk/C/Types.h"\r
+#include "Sdk/C/7zTypes.h"\r
 #include "Sdk/C/7zVersion.h"\r
 #include "Sdk/C/LzmaDec.h"\r
 \r
-//\r
-// Global data\r
-//\r
-\r
-CONST VOID  *mSourceLastUsedWithGetInfo;\r
-UINT32      mSizeOfLastSource;\r
-UINT32      mDecompressedSizeForLastSource;\r
-VOID        *mScratchBuffer;\r
-UINTN       mScratchBufferSize;\r
-\r
 #define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB\r
 \r
+typedef struct\r
+{\r
+  ISzAlloc Functions;\r
+  VOID     *Buffer;\r
+  UINTN    BufferSize;\r
+} ISzAllocWithData;\r
+\r
 /**\r
   Allocation routine used by LZMA decompression.\r
 \r
@@ -44,11 +41,14 @@ SzAlloc (
   )\r
 {\r
   VOID *Addr;\r
+  ISzAllocWithData *Private;\r
+\r
+  Private = (ISzAllocWithData*) P;\r
 \r
-  if (mScratchBufferSize >= Size) {\r
-    Addr = mScratchBuffer;\r
-    mScratchBuffer = (VOID*) ((UINT8*)Addr + Size);\r
-    mScratchBufferSize -= Size;\r
+  if (Private->BufferSize >= Size) {\r
+    Addr = Private->Buffer;\r
+    Private->Buffer = (VOID*) ((UINT8*)Addr + Size);\r
+    Private->BufferSize -= Size;\r
     return Addr;\r
   } else {\r
     ASSERT (FALSE);\r
@@ -75,8 +75,6 @@ SzFree (
   //\r
 }\r
 \r
-STATIC ISzAlloc g_Alloc = { SzAlloc, SzFree };\r
-\r
 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
 \r
 /**\r
@@ -107,15 +105,15 @@ GetDecodedSizeOfBuf(
 //\r
 \r
 /**\r
-  Given a Lzma compressed source buffer, this function retrieves the size of \r
-  the uncompressed buffer and the size of the scratch buffer required \r
+  Given a Lzma compressed source buffer, this function retrieves the size of\r
+  the uncompressed buffer and the size of the scratch buffer required\r
   to decompress the compressed source buffer.\r
 \r
-  Retrieves the size of the uncompressed buffer and the temporary scratch buffer \r
+  Retrieves the size of the uncompressed buffer and the temporary scratch buffer\r
   required to decompress the buffer specified by Source and SourceSize.\r
-  The size of the uncompressed buffer is returned in DestinationSize, \r
+  The size of the uncompressed buffer is returned in DestinationSize,\r
   the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.\r
-  This function does not have scratch buffer available to perform a thorough \r
+  This function does not have scratch buffer available to perform a thorough\r
   checking of the validity of the source data. It just retrieves the "Original Size"\r
   field from the LZMA_HEADER_SIZE beginning bytes of the source data and output it as DestinationSize.\r
   And ScratchSize is specific to the decompression implementation.\r
@@ -128,11 +126,11 @@ GetDecodedSizeOfBuf(
                           that will be generated when the compressed buffer specified\r
                           by Source and SourceSize is decompressed.\r
   @param  ScratchSize     A pointer to the size, in bytes, of the scratch buffer that\r
-                          is required to decompress the compressed buffer specified \r
+                          is required to decompress the compressed buffer specified\r
                           by Source and SourceSize.\r
 \r
-  @retval  RETURN_SUCCESS The size of the uncompressed data was returned \r
-                          in DestinationSize and the size of the scratch \r
+  @retval  RETURN_SUCCESS The size of the uncompressed data was returned\r
+                          in DestinationSize and the size of the scratch\r
                           buffer was returned in ScratchSize.\r
 \r
 **/\r
@@ -151,58 +149,55 @@ LzmaUefiDecompressGetInfo (
 \r
   DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
 \r
-  mSourceLastUsedWithGetInfo = Source;\r
-  mSizeOfLastSource = SourceSize;\r
-  mDecompressedSizeForLastSource = (UInt32)DecodedSize;\r
-  *DestinationSize = mDecompressedSizeForLastSource;\r
+  *DestinationSize = (UINT32)DecodedSize;\r
   *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
   return RETURN_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
   Decompresses a Lzma compressed source buffer.\r
 \r
   Extracts decompressed data to its original form.\r
-  If the compressed source data specified by Source is successfully decompressed \r
-  into Destination, then RETURN_SUCCESS is returned.  If the compressed source data \r
+  If the compressed source data specified by Source is successfully decompressed\r
+  into Destination, then RETURN_SUCCESS is returned.  If the compressed source data\r
   specified by Source is not in a valid compressed data format,\r
   then RETURN_INVALID_PARAMETER is returned.\r
 \r
   @param  Source      The source buffer containing the compressed data.\r
+  @param  SourceSize  The size of source buffer.\r
   @param  Destination The destination buffer to store the decompressed data\r
   @param  Scratch     A temporary scratch buffer that is used to perform the decompression.\r
-                      This is an optional parameter that may be NULL if the \r
+                      This is an optional parameter that may be NULL if the\r
                       required scratch buffer size is 0.\r
-                     \r
-  @retval  RETURN_SUCCESS Decompression completed successfully, and \r
+\r
+  @retval  RETURN_SUCCESS Decompression completed successfully, and\r
                           the uncompressed buffer is returned in Destination.\r
-  @retval  RETURN_INVALID_PARAMETER \r
-                          The source buffer specified by Source is corrupted \r
+  @retval  RETURN_INVALID_PARAMETER\r
+                          The source buffer specified by Source is corrupted\r
                           (not in a valid compressed format).\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
 LzmaUefiDecompress (\r
   IN CONST VOID  *Source,\r
+  IN UINTN       SourceSize,\r
   IN OUT VOID    *Destination,\r
   IN OUT VOID    *Scratch\r
   )\r
 {\r
-  SRes        LzmaResult;\r
-  ELzmaStatus Status;\r
-  SizeT       DecodedBufSize;\r
-  SizeT       EncodedDataSize;\r
-\r
-  if (Source != mSourceLastUsedWithGetInfo) {\r
-    return RETURN_INVALID_PARAMETER;\r
-  }\r
+  SRes              LzmaResult;\r
+  ELzmaStatus       Status;\r
+  SizeT             DecodedBufSize;\r
+  SizeT             EncodedDataSize;\r
+  ISzAllocWithData  AllocFuncs;\r
 \r
-  DecodedBufSize = (SizeT)mDecompressedSizeForLastSource;\r
-  EncodedDataSize = (SizeT)(mSizeOfLastSource - LZMA_HEADER_SIZE);\r
+  AllocFuncs.Functions.Alloc  = SzAlloc;\r
+  AllocFuncs.Functions.Free   = SzFree;\r
+  AllocFuncs.Buffer           = Scratch;\r
+  AllocFuncs.BufferSize       = SCRATCH_BUFFER_REQUEST_SIZE;\r
 \r
-  mScratchBuffer = Scratch;\r
-  mScratchBufferSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
+  DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);\r
+  EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);\r
 \r
   LzmaResult = LzmaDecode(\r
     Destination,\r
@@ -213,7 +208,7 @@ LzmaUefiDecompress (
     LZMA_PROPS_SIZE,\r
     LZMA_FINISH_END,\r
     &Status,\r
-    &g_Alloc\r
+    &(AllocFuncs.Functions)\r
     );\r
 \r
   if (LzmaResult == SZ_OK) {\r