]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
Minor code enhancement.
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
index f1729825c746a8534ca3579c7929b67994f043e8..97ca8e68f7d3ac508859171fb67da53a44e7d9e6 100644 (file)
@@ -1,11 +1,7 @@
 /** @file\r
-  LZMA Decompress routines for edk2\r
+  LZMA Decompress interfaces\r
 \r
-  Portions based on LZMA SDK 4.65:\r
-    LzmaUtil.c -- Test application for LZMA compression\r
-    2008-11-23 : Igor Pavlov : Public domain\r
-\r
-  Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
+  Copyright (c) 2009 - 2010, Intel Corporation<BR>\r
   All rights reserved. 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
 \r
 **/\r
 \r
-#include <Uefi.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/UefiDecompressLib.h>\r
-#include <Library/ExtractGuidedSectionLib.h>\r
-#include <Guid/LzmaDecompress.h>\r
-\r
+#include "LzmaDecompressLibInternal.h"\r
 #include "Sdk/C/Types.h"\r
 #include "Sdk/C/7zVersion.h"\r
 #include "Sdk/C/LzmaDec.h"\r
 \r
-extern ISzAlloc g_Alloc;\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
+  @param P                Pointer to the ISzAlloc instance\r
+  @param Size             The size in bytes to be allocated\r
+\r
+  @return The allocated pointer address, or NULL on failure\r
+**/\r
+VOID *\r
+SzAlloc (\r
+  VOID *P,\r
+  size_t Size\r
+  )\r
+{\r
+  VOID *Addr;\r
+  ISzAllocWithData *Private;\r
+\r
+  Private = (ISzAllocWithData*) P;\r
+\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
+    return NULL;\r
+  }\r
+}\r
+\r
+/**\r
+  Free routine used by LZMA decompression.\r
+\r
+  @param P                Pointer to the ISzAlloc instance\r
+  @param Address          The address to be freed\r
+**/\r
+VOID\r
+SzFree (\r
+  VOID *P,\r
+  VOID *Address\r
+  )\r
+{\r
+  //\r
+  // We use the 'scratch buffer' for allocations, so there is no free\r
+  // operation required.  The scratch buffer will be freed by the caller\r
+  // of the decompression code.\r
+  //\r
+}\r
 \r
 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
 \r
-STATIC\r
+/**\r
+  Get the size of the uncompressed buffer by parsing EncodeData header.\r
+\r
+  @param EncodedData  Pointer to the compressed data.\r
+\r
+  @return The size of the uncompressed buffer.\r
+**/\r
 UINT64\r
 GetDecodedSizeOfBuf(\r
-  UINT8 *encodedData\r
+  UINT8 *EncodedData\r
   )\r
 {\r
   UINT64 DecodedSize;\r
@@ -45,29 +95,44 @@ GetDecodedSizeOfBuf(
   /* Parse header */\r
   DecodedSize = 0;\r
   for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)\r
-    DecodedSize = LShiftU64(DecodedSize, 8) + encodedData[Index];\r
+    DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];\r
 \r
   return DecodedSize;\r
 }\r
 \r
 //\r
-// LZMA functions and data as defined in local LzmaDecompress.h\r
+// LZMA functions and data as defined in local LzmaDecompressLibInternal.h\r
 //\r
 \r
-STATIC CONST VOID  *mSourceLastUsedWithGetInfo;\r
-STATIC UINT32      mSizeOfLastSource;\r
-STATIC UINT32      mDecompressedSizeForLastSource;\r
-\r
 /**\r
-  The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().\r
-  \r
-  @param Source           The source buffer containing the compressed data.\r
-  @param SourceSize       The size of source buffer\r
-  @param DestinationSize  The size of destination buffer.\r
-  @param ScratchSize      The size of scratch buffer.\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
+  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 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
+  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
+\r
+  If SourceSize is less than LZMA_HEADER_SIZE, then ASSERT().\r
+\r
+  @param  Source          The source buffer containing the compressed data.\r
+  @param  SourceSize      The size, in bytes, of the source buffer.\r
+  @param  DestinationSize A pointer to the size, in bytes, of the uncompressed buffer\r
+                          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
+                          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
+                          buffer was returned in ScratchSize.\r
 \r
-  @retval RETURN_SUCCESS           - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
-  @retval RETURN_INVALID_PARAMETER - The source data is corrupted\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
@@ -84,58 +149,69 @@ LzmaUefiDecompressGetInfo (
 \r
   DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
 \r
-  mSourceLastUsedWithGetInfo = Source;\r
-  mSizeOfLastSource = SourceSize;\r
-  mDecompressedSizeForLastSource = (UInt32)DecodedSize;\r
-  *DestinationSize = mDecompressedSizeForLastSource;\r
-  *ScratchSize = 0x10;\r
+  *DestinationSize = (UINT32)DecodedSize;\r
+  *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
   return RETURN_SUCCESS;\r
 }\r
 \r
-\r
 /**\r
-  The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r
-  \r
-  @param Source          - The source buffer containing the compressed data.\r
-  @param Destination     - The destination buffer to store the decompressed data\r
-  @param Scratch         - The buffer used internally by the decompress routine. This  buffer is needed to store intermediate data.\r
-\r
-  @retval RETURN_SUCCESS           - Decompression is successfull\r
-  @retval RETURN_INVALID_PARAMETER - The source data is corrupted  \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
+  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
+                      required scratch buffer size is 0.\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
+                          (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
-\r
-  decodedBufSize = (SizeT)mDecompressedSizeForLastSource;\r
-  encodedDataSize = (SizeT)(mSizeOfLastSource - LZMA_HEADER_SIZE);\r
+  SRes              LzmaResult;\r
+  ELzmaStatus       Status;\r
+  SizeT             DecodedBufSize;\r
+  SizeT             EncodedDataSize;\r
+  ISzAllocWithData  AllocFuncs;\r
+\r
+  AllocFuncs.Functions.Alloc  = SzAlloc;\r
+  AllocFuncs.Functions.Free   = SzFree;\r
+  AllocFuncs.Buffer           = Scratch;\r
+  AllocFuncs.BufferSize       = SCRATCH_BUFFER_REQUEST_SIZE;\r
+  \r
+  DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);\r
+  EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);\r
 \r
-  lzmaResult = LzmaDecode(\r
+  LzmaResult = LzmaDecode(\r
     Destination,\r
-    &decodedBufSize,\r
+    &DecodedBufSize,\r
     (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
-    &encodedDataSize,\r
+    &EncodedDataSize,\r
     Source,\r
     LZMA_PROPS_SIZE,\r
     LZMA_FINISH_END,\r
-    &status,\r
-    &g_Alloc\r
+    &Status,\r
+    &(AllocFuncs.Functions)\r
     );\r
 \r
-  if (lzmaResult == SZ_OK) {\r
+  if (LzmaResult == SZ_OK) {\r
     return RETURN_SUCCESS;\r
   } else {\r
     return RETURN_INVALID_PARAMETER;\r