]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
MdeModulePkg: Add two library instances
[mirror_edk2.git] / MdeModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
diff --git a/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c b/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
new file mode 100644 (file)
index 0000000..e32b6a3
--- /dev/null
@@ -0,0 +1,220 @@
+/** @file\r
+  LZMA Decompress interfaces\r
+\r
+  Copyright (c) 2009 - 2010, 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
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\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
+#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
+/**\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
+  )\r
+{\r
+  UINT64 DecodedSize;\r
+  INTN   Index;\r
+\r
+  /* 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
+\r
+  return DecodedSize;\r
+}\r
+\r
+//\r
+// LZMA functions and data as defined in local LzmaDecompressLibInternal.h\r
+//\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
+  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
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+LzmaUefiDecompressGetInfo (\r
+  IN  CONST VOID  *Source,\r
+  IN  UINT32      SourceSize,\r
+  OUT UINT32      *DestinationSize,\r
+  OUT UINT32      *ScratchSize\r
+  )\r
+{\r
+  UInt64  DecodedSize;\r
+\r
+  ASSERT(SourceSize >= LZMA_HEADER_SIZE);\r
+\r
+  DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
+\r
+  *DestinationSize = (UINT32)DecodedSize;\r
+  *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
+  return RETURN_SUCCESS;\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
+  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
+  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
+    Destination,\r
+    &DecodedBufSize,\r
+    (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
+    &EncodedDataSize,\r
+    Source,\r
+    LZMA_PROPS_SIZE,\r
+    LZMA_FINISH_END,\r
+    &Status,\r
+    &(AllocFuncs.Functions)\r
+    );\r
+\r
+  if (LzmaResult == SZ_OK) {\r
+    return RETURN_SUCCESS;\r
+  } else {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+}\r
+\r