]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Add Brotli algorithm decompression library
authorSong, BinX <binx.song@intel.com>
Thu, 23 Mar 2017 02:16:02 +0000 (10:16 +0800)
committerLiming Gao <liming.gao@intel.com>
Wed, 29 Mar 2017 04:14:41 +0000 (12:14 +0800)
- Add Brotli algorithm decompression library support

Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Bell Song <binx.song@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
15 files changed:
MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf [new file with mode: 0644]
MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c [new file with mode: 0644]
MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLib.uni [new file with mode: 0644]
MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLibInternal.h [new file with mode: 0644]
MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c [new file with mode: 0644]
MdeModulePkg/Library/BrotliCustomDecompressLib/ReadMe.txt [new file with mode: 0644]
MdeModulePkg/Library/BrotliCustomDecompressLib/common/types.h
MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.c
MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.h
MdeModulePkg/Library/BrotliCustomDecompressLib/dec/decode.c
MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.c
MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.h
MdeModulePkg/Library/BrotliCustomDecompressLib/dec/state.c
MdeModulePkg/MdeModulePkg.dec
MdeModulePkg/MdeModulePkg.dsc

diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf
new file mode 100644 (file)
index 0000000..578f97f
--- /dev/null
@@ -0,0 +1,56 @@
+## @file\r
+#  BrotliCustomDecompressLib produces BROTLI custom decompression algorithm.\r
+#\r
+#  It is based on the Brotli v0.5.2.\r
+#  Brotli was released on the website https://github.com/google/brotli.\r
+#\r
+#  Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
+#\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
+#  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
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = BrotliDecompressLib\r
+  MODULE_UNI_FILE                = BrotliDecompressLib.uni\r
+  FILE_GUID                      = 69EC7DB2-B0DD-493A-963A-C5F330131BAA\r
+  MODULE_TYPE                    = BASE\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = NULL\r
+  CONSTRUCTOR                    = BrotliDecompressLibConstructor\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  GuidedSectionExtraction.c\r
+  BrotliDecompress.c\r
+  BrotliDecompressLibInternal.h\r
+  common/dictionary.c\r
+  dec/bit_reader.c\r
+  dec/decode.c\r
+  dec/huffman.c\r
+  dec/state.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+\r
+[Guids]\r
+  gBrotliCustomDecompressGuid  ## PRODUCES  ## UNDEFINED # specifies BROTLI custom decompress algorithm.\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  DebugLib\r
+  BaseMemoryLib\r
+  ExtractGuidedSectionLib\r
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c
new file mode 100644 (file)
index 0000000..2c2648a
--- /dev/null
@@ -0,0 +1,321 @@
+/** @file\r
+  Brotli Decompress interfaces\r
+\r
+  Copyright (c) 2017, 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
+#include <BrotliDecompressLibInternal.h>\r
+\r
+/**\r
+  Dummy malloc function for compiler.\r
+**/\r
+VOID *\r
+malloc (\r
+  IN size_t    Size\r
+  )\r
+{\r
+  ASSERT (FALSE);\r
+  return NULL;\r
+}\r
+\r
+/**\r
+  Dummy free function for compiler.\r
+**/\r
+VOID\r
+free (\r
+  IN VOID *    Ptr\r
+  )\r
+{\r
+  ASSERT (FALSE);\r
+}\r
+\r
+/**\r
+  Allocation routine used by BROTLI decompression.\r
+\r
+  @param Ptr              Pointer to the BROTLI_BUFF 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
+BrAlloc (\r
+  IN VOID *    Ptr,\r
+  IN size_t    Size\r
+  )\r
+{\r
+  VOID          *Addr;\r
+  BROTLI_BUFF   *Private;\r
+\r
+  Private = (BROTLI_BUFF *)Ptr;\r
+\r
+  if (Private->BuffSize >= Size) {\r
+    Addr = Private->Buff;\r
+    Private->Buff = (VOID *) ((UINT8 *)Addr + Size);\r
+    Private->BuffSize -= Size;\r
+    return Addr;\r
+  } else {\r
+    ASSERT (FALSE);\r
+    return NULL;\r
+  }\r
+}\r
+\r
+/**\r
+  Free routine used by BROTLI decompression.\r
+\r
+  @param Ptr              Pointer to the BROTLI_BUFF instance\r
+  @param Address          The address to be freed\r
+**/\r
+VOID\r
+BrFree (\r
+  IN VOID *    Ptr,\r
+  IN 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
+/**\r
+  Decompresses a Brotli 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 EFI_SUCCESS is returned. If the compressed source data\r
+  specified by Source is not in a valid compressed data format,\r
+  then EFI_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  DestSize    The destination buffer size.\r
+  @param  BuffInfo    The pointer to the BROTLI_BUFF instance.\r
+\r
+  @retval EFI_SUCCESS Decompression completed successfully, and\r
+                      the uncompressed buffer is returned in Destination.\r
+  @retval EFI_INVALID_PARAMETER\r
+                      The source buffer specified by Source is corrupted\r
+                      (not in a valid compressed format).\r
+**/\r
+EFI_STATUS\r
+BrotliDecompress (\r
+  IN CONST VOID*  Source,\r
+  IN UINTN        SourceSize,\r
+  IN OUT VOID*    Destination,\r
+  IN OUT UINTN    DestSize,\r
+  IN VOID *       BuffInfo\r
+  )\r
+{\r
+  UINT8 *        Input;\r
+  UINT8 *        Output;\r
+  const UINT8 *  NextIn;\r
+  UINT8 *        NextOut;\r
+  size_t         TotalOut;\r
+  size_t         AvailableIn;\r
+  size_t         AvailableOut;\r
+  BrotliResult   Result;\r
+  BrotliState *  BroState;\r
+  VOID *         Temp;\r
+\r
+  AvailableOut = FILE_BUFFER_SIZE;\r
+  Result = BROTLI_RESULT_ERROR;\r
+  BroState = BrotliCreateState(BrAlloc, BrFree, BuffInfo);\r
+  Temp = Destination;\r
+\r
+  if (BroState == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  Input = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);\r
+  Output = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);\r
+  if ((Input==NULL) || (Output==NULL)) {\r
+    BrFree(BuffInfo, Input);\r
+    BrFree(BuffInfo, Output);\r
+    BrotliDestroyState(BroState);\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  NextOut = Output;\r
+  Result = BROTLI_RESULT_NEEDS_MORE_INPUT;\r
+  while (1) {\r
+    if (Result == BROTLI_RESULT_NEEDS_MORE_INPUT) {\r
+      if (SourceSize == 0) {\r
+        break;\r
+      }\r
+      if (SourceSize >= FILE_BUFFER_SIZE) {\r
+        AvailableIn = FILE_BUFFER_SIZE;\r
+      }else{\r
+        AvailableIn = SourceSize;\r
+      }\r
+      CopyMem(Input, Source, AvailableIn);\r
+      Source = (VOID *)((UINT8 *)Source + AvailableIn);\r
+      SourceSize -= AvailableIn;\r
+      NextIn = Input;\r
+    } else if (Result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {\r
+      CopyMem(Temp, Output, FILE_BUFFER_SIZE);\r
+      AvailableOut = FILE_BUFFER_SIZE;\r
+      Temp = (VOID *)((UINT8 *)Temp +FILE_BUFFER_SIZE);\r
+      NextOut = Output;\r
+    } else {\r
+      break; /* Error or success. */\r
+    }\r
+    Result = BrotliDecompressStream(\r
+                          &AvailableIn,\r
+                          &NextIn,\r
+                          &AvailableOut,\r
+                          &NextOut,\r
+                          &TotalOut,\r
+                          BroState\r
+                          );\r
+  }\r
+  if (NextOut != Output) {\r
+    CopyMem(Temp, Output, (size_t)(NextOut - Output));\r
+  }\r
+\r
+  DestSize = TotalOut;\r
+\r
+  BrFree(BuffInfo, Input);\r
+  BrFree(BuffInfo, Output);\r
+  BrotliDestroyState(BroState);\r
+  return (Result == BROTLI_RESULT_SUCCESS) ? EFI_SUCCESS : EFI_INVALID_PARAMETER;\r
+}\r
+\r
+/**\r
+  Get the size of the uncompressed buffer by parsing EncodeData header.\r
+\r
+  @param EncodedData  Pointer to the compressed data.\r
+  @param StartOffset  Start offset of the compressed data.\r
+  @param EndOffset    End offset of the compressed data.\r
+\r
+  @return The size of the uncompressed buffer.\r
+**/\r
+UINT64\r
+GetDecodedSizeOfBuf(\r
+  IN UINT8 *  EncodedData,\r
+  IN UINT8    StartOffset,\r
+  IN UINT8    EndOffset\r
+  )\r
+{\r
+  UINT64 DecodedSize;\r
+  INTN   Index;\r
+\r
+  /* Parse header */\r
+  DecodedSize = 0;\r
+  for (Index = EndOffset - 1; Index >= StartOffset; Index--)\r
+    DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];\r
+\r
+  return DecodedSize;\r
+}\r
+\r
+/**\r
+  Given a Brotli 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 EFI_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 BROTLI_SCRATCH_MAX 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 BROTLI_SCRATCH_MAX, 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 EFI_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
+EFI_STATUS\r
+EFIAPI\r
+BrotliUefiDecompressGetInfo (\r
+  IN  CONST VOID *  Source,\r
+  IN  UINT32        SourceSize,\r
+  OUT UINT32 *      DestinationSize,\r
+  OUT UINT32 *      ScratchSize\r
+  )\r
+{\r
+  UINT64  GetSize;\r
+  UINT8   MaxOffset;\r
+\r
+  ASSERT(SourceSize >= BROTLI_SCRATCH_MAX);\r
+\r
+  MaxOffset = BROTLI_DECODE_MAX;\r
+  GetSize = GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);\r
+  *DestinationSize = (UINT32)GetSize;\r
+  MaxOffset = BROTLI_SCRATCH_MAX;\r
+  GetSize = GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);\r
+  *ScratchSize = (UINT32)GetSize;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Decompresses a Brotli 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 EFI_SUCCESS Decompression completed successfully, and\r
+                      the uncompressed buffer is returned in Destination.\r
+  @retval EFI_INVALID_PARAMETER\r
+                      The source buffer specified by Source is corrupted\r
+                      (not in a valid compressed format).\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BrotliUefiDecompress (\r
+  IN CONST VOID *   Source,\r
+  IN UINTN          SourceSize,\r
+  IN OUT VOID *     Destination,\r
+  IN OUT VOID *     Scratch\r
+  )\r
+{\r
+  UINTN          DestSize = 0;\r
+  EFI_STATUS     Status;\r
+  BROTLI_BUFF    BroBuff;\r
+  UINT64         GetSize;\r
+  UINT8          MaxOffset;\r
+\r
+  MaxOffset = BROTLI_SCRATCH_MAX;\r
+  GetSize = GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);\r
+\r
+  BroBuff.Buff     = Scratch;\r
+  BroBuff.BuffSize = (UINTN)GetSize;\r
+\r
+  Status = BrotliDecompress(\r
+            (VOID *)((UINT8 *)Source + BROTLI_SCRATCH_MAX),\r
+            SourceSize - BROTLI_SCRATCH_MAX,\r
+            Destination,\r
+            DestSize,\r
+            (VOID *)(&BroBuff)\r
+            );\r
+\r
+  return Status;\r
+}\r
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLib.uni b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLib.uni
new file mode 100644 (file)
index 0000000..c5e13b2
--- /dev/null
@@ -0,0 +1,21 @@
+// /** @file\r
+// BrotliCustomDecompressLib produces BROTLI custom decompression algorithm.\r
+//\r
+// It is based on the Brotli v0.5.2.\r
+// Brotli was released on the website https://github.com/google/brotli.\r
+//\r
+// Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
+//\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
+// 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
+\r
+#string STR_MODULE_ABSTRACT             #language en-US "BrotliCustomDecompressLib produces BROTLI custom decompression algorithm"\r
+\r
+#string STR_MODULE_DESCRIPTION          #language en-US "It is based on the Brotli v0.5.2. Brotli was released on the website https://github.com/google/brotli."\r
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLibInternal.h b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLibInternal.h
new file mode 100644 (file)
index 0000000..c2d84a8
--- /dev/null
@@ -0,0 +1,71 @@
+/** @file\r
+  BROTLI UEFI header file\r
+\r
+  Allows BROTLI code to build under UEFI (edk2) build environment\r
+\r
+  Copyright (c) 2017, 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
+#ifndef __BROTLI_DECOMPRESS_INTERNAL_H__\r
+#define __BROTLI_DECOMPRESS_INTERNAL_H__\r
+\r
+#include <PiPei.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/ExtractGuidedSectionLib.h>\r
+#include <common/types.h>\r
+#include <dec/decode.h>\r
+\r
+typedef struct\r
+{\r
+  VOID     *Buff;\r
+  UINTN    BuffSize;\r
+} BROTLI_BUFF;\r
+\r
+#define FILE_BUFFER_SIZE     65536\r
+#define BROTLI_INFO_SIZE     8\r
+#define BROTLI_DECODE_MAX    8\r
+#define BROTLI_SCRATCH_MAX   16\r
+\r
+#define memcpy                      CopyMem\r
+#define memmove                     CopyMem\r
+#define memset(dest,ch,count)       SetMem(dest,(UINTN)(count),(UINT8)(ch))\r
+\r
+VOID *\r
+malloc (\r
+  IN size_t   Size\r
+  );\r
+\r
+VOID\r
+free (\r
+  IN VOID *   Ptr\r
+  );\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+BrotliUefiDecompressGetInfo (\r
+  IN  CONST VOID  *Source,\r
+  IN  UINT32      SourceSize,\r
+  OUT UINT32      *DestinationSize,\r
+  OUT UINT32      *ScratchSize\r
+  );\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+BrotliUefiDecompress (\r
+  IN CONST VOID  *Source,\r
+  IN UINTN       SourceSize,\r
+  IN OUT VOID    *Destination,\r
+  IN OUT VOID    *Scratch\r
+  );\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c
new file mode 100644 (file)
index 0000000..427adb8
--- /dev/null
@@ -0,0 +1,196 @@
+/** @file\r
+  BROTLI Decompress GUIDed Section Extraction Library.\r
+  It wraps Brotli decompress interfaces to GUIDed Section Extraction interfaces\r
+  and registers them into GUIDed handler table.\r
+\r
+  Copyright (c) 2017, 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 <BrotliDecompressLibInternal.h>\r
+\r
+/**\r
+  Examines a GUIDed section and returns the size of the decoded buffer and the\r
+  size of an scratch buffer required to actually decode the data in a GUIDed section.\r
+\r
+  Examines a GUIDed section specified by InputSection.\r
+  If GUID for InputSection does not match the GUID that this handler supports,\r
+  then RETURN_UNSUPPORTED is returned.\r
+  If the required information can not be retrieved from InputSection,\r
+  then RETURN_INVALID_PARAMETER is returned.\r
+  If the GUID of InputSection does match the GUID that this handler supports,\r
+  then the size required to hold the decoded buffer is returned in OututBufferSize,\r
+  the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field\r
+  from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.\r
+\r
+  If InputSection is NULL, then ASSERT().\r
+  If OutputBufferSize is NULL, then ASSERT().\r
+  If ScratchBufferSize is NULL, then ASSERT().\r
+  If SectionAttribute is NULL, then ASSERT().\r
+\r
+\r
+  @param[in]  InputSection       A pointer to a GUIDed section of an FFS formatted file.\r
+  @param[out] OutputBufferSize   A pointer to the size, in bytes, of an output buffer required\r
+                                 if the buffer specified by InputSection were decoded.\r
+  @param[out] ScratchBufferSize  A pointer to the size, in bytes, required as scratch space\r
+                                 if the buffer specified by InputSection were decoded.\r
+  @param[out] SectionAttribute   A pointer to the attributes of the GUIDed section. See the Attributes\r
+                                 field of EFI_GUID_DEFINED_SECTION in the PI Specification.\r
+\r
+  @retval  RETURN_SUCCESS            The information about InputSection was returned.\r
+  @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.\r
+  @retval  RETURN_INVALID_PARAMETER  The information can not be retrieved from the section specified by InputSection.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+BrotliGuidedSectionGetInfo (\r
+  IN  CONST VOID  *InputSection,\r
+  OUT UINT32      *OutputBufferSize,\r
+  OUT UINT32      *ScratchBufferSize,\r
+  OUT UINT16      *SectionAttribute\r
+  )\r
+{\r
+  ASSERT (InputSection != NULL);\r
+  ASSERT (OutputBufferSize != NULL);\r
+  ASSERT (ScratchBufferSize != NULL);\r
+  ASSERT (SectionAttribute != NULL);\r
+\r
+  if (IS_SECTION2 (InputSection)) {\r
+    if (!CompareGuid (\r
+        &gBrotliCustomDecompressGuid,\r
+        &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+\r
+    *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;\r
+\r
+    return BrotliUefiDecompressGetInfo (\r
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,\r
+             SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,\r
+             OutputBufferSize,\r
+             ScratchBufferSize\r
+             );\r
+  } else {\r
+    if (!CompareGuid (\r
+        &gBrotliCustomDecompressGuid,\r
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+\r
+    *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
+\r
+    return BrotliUefiDecompressGetInfo (\r
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+             SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+             OutputBufferSize,\r
+             ScratchBufferSize\r
+             );\r
+  }\r
+}\r
+\r
+/**\r
+  Decompress a BROTLI compressed GUIDed section into a caller allocated output buffer.\r
+\r
+  Decodes the GUIDed section specified by InputSection.\r
+  If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.\r
+  If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.\r
+  If the GUID of InputSection does match the GUID that this handler supports, then InputSection\r
+  is decoded into the buffer specified by OutputBuffer and the authentication status of this\r
+  decode operation is returned in AuthenticationStatus.  If the decoded buffer is identical to the\r
+  data in InputSection, then OutputBuffer is set to point at the data in InputSection.  Otherwise,\r
+  the decoded data will be placed in caller allocated buffer specified by OutputBuffer.\r
+\r
+  If InputSection is NULL, then ASSERT().\r
+  If OutputBuffer is NULL, then ASSERT().\r
+  If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().\r
+  If AuthenticationStatus is NULL, then ASSERT().\r
+\r
+  @param[in]  InputSection  A pointer to a GUIDed section of an FFS formatted file.\r
+  @param[out] OutputBuffer  A pointer to a buffer that contains the result of a decode operation.\r
+  @param[out] ScratchBuffer A caller allocated buffer that may be required by this function\r
+                            as a scratch buffer to perform the decode operation.\r
+  @param[out] AuthenticationStatus\r
+                            A pointer to the authentication status of the decoded output buffer.\r
+                            See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI\r
+                            section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must\r
+                            never be set by this handler.\r
+\r
+  @retval  RETURN_SUCCESS            The buffer specified by InputSection was decoded.\r
+  @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.\r
+  @retval  RETURN_INVALID_PARAMETER  The section specified by InputSection can not be decoded.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+BrotliGuidedSectionExtraction (\r
+  IN CONST  VOID    *InputSection,\r
+  OUT       VOID    **OutputBuffer,\r
+  OUT       VOID    *ScratchBuffer,        OPTIONAL\r
+  OUT       UINT32  *AuthenticationStatus\r
+  )\r
+{\r
+  ASSERT (OutputBuffer != NULL);\r
+  ASSERT (InputSection != NULL);\r
+\r
+  if (IS_SECTION2 (InputSection)) {\r
+    if (!CompareGuid (\r
+        &gBrotliCustomDecompressGuid,\r
+        &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Authentication is set to Zero, which may be ignored.\r
+    //\r
+    *AuthenticationStatus = 0;\r
+\r
+    return BrotliUefiDecompress (\r
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,\r
+             SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,\r
+             *OutputBuffer,\r
+             ScratchBuffer\r
+             );\r
+  } else {\r
+    if (!CompareGuid (\r
+        &gBrotliCustomDecompressGuid,\r
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Authentication is set to Zero, which may be ignored.\r
+    //\r
+    *AuthenticationStatus = 0;\r
+\r
+    return BrotliUefiDecompress (\r
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+             SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+             *OutputBuffer,\r
+             ScratchBuffer\r
+    );\r
+  }\r
+}\r
+\r
+/**\r
+  Register BrotliDecompress and BrotliDecompressGetInfo handlers with BrotliCustomerDecompressGuid.\r
+\r
+  @retval  EFI_SUCCESS            Register successfully.\r
+  @retval  EFI_OUT_OF_RESOURCES   No enough memory to store this handler.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+BrotliDecompressLibConstructor (\r
+  )\r
+{\r
+  return ExtractGuidedSectionRegisterHandlers (\r
+          &gBrotliCustomDecompressGuid,\r
+          BrotliGuidedSectionGetInfo,\r
+          BrotliGuidedSectionExtraction\r
+          );\r
+}\r
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/ReadMe.txt b/MdeModulePkg/Library/BrotliCustomDecompressLib/ReadMe.txt
new file mode 100644 (file)
index 0000000..c19c0a1
--- /dev/null
@@ -0,0 +1,2 @@
+It is based on the Brotli v0.5.2.\r
+Brotli was released on the website https://github.com/google/brotli.\r
index c6a37a6aac9944216d90c8eeee8239119067b30a..de25359e8a8f22cf5ee1fcd7be853517f492243c 100644 (file)
@@ -9,7 +9,13 @@
 #ifndef BROTLI_COMMON_TYPES_H_\r
 #define BROTLI_COMMON_TYPES_H_\r
 \r
-#include <stddef.h>  /* for size_t */\r
+//#include <stddef.h>  /* for size_t */\r
+#ifndef _SIZE_T_DEFINED\r
+#if !defined(_WIN64) || defined(__GNUC__)\r
+typedef unsigned int size_t;\r
+#endif\r
+#endif\r
+\r
 \r
 #if defined(_MSC_VER) && (_MSC_VER < 1600)\r
 typedef __int8 int8_t;\r
@@ -21,7 +27,15 @@ typedef unsigned __int32 uint32_t;
 typedef unsigned __int64 uint64_t;\r
 typedef __int64 int64_t;\r
 #else\r
-#include <stdint.h>\r
+//#include <stdint.h>\r
+typedef INT8     int8_t;\r
+typedef INT16    int16_t;\r
+typedef INT32    int32_t;\r
+typedef INT64    int64_t;\r
+typedef UINT8    uint8_t;\r
+typedef UINT16   uint16_t;\r
+typedef UINT32   uint32_t;\r
+typedef UINT64   uint64_t;\r
 #endif  /* defined(_MSC_VER) && (_MSC_VER < 1600) */\r
 \r
 #if (!defined(_MSC_VER) || (_MSC_VER >= 1800)) && \\r
index 83dcc36b46ff41bff1f9a0385fb317462dc62210..21845ce83c53fe2f2a79da41468486ee2d47b99c 100644 (file)
@@ -34,7 +34,7 @@ BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) {
     }\r
   }\r
 \r
-  while ((((size_t)br->next_in) & aligned_read_mask) != 0) {\r
+  while ((((size_t)(*br->next_in)) & aligned_read_mask) != 0) {\r
     if (!BrotliPullByte(br)) {\r
       /* If we consumed all the input, we don't care about the alignment. */\r
       return BROTLI_TRUE;\r
index c0f6d1bf6efb71a8a7bdaea8134bd4f037e73cf6..9f65b78a8589d56d0be42c446a440199013e22b7 100644 (file)
@@ -9,7 +9,8 @@
 #ifndef BROTLI_DEC_BIT_READER_H_\r
 #define BROTLI_DEC_BIT_READER_H_\r
 \r
-#include <string.h>  /* memcpy */\r
+//#include <string.h>  /* memcpy */\r
+#include <BrotliDecompressLibInternal.h>\r
 \r
 #include "../common/types.h"\r
 #include "./port.h"\r
index 0b3eca3644c850613f1addf7bc0268a553d92c82..7ba1d0512e251f806d56aff5b7c6c2791b8bf465 100644 (file)
@@ -10,8 +10,9 @@
 #include <arm_neon.h>\r
 #endif\r
 \r
-#include <stdlib.h>  /* free, malloc */\r
-#include <string.h>  /* memcpy, memset */\r
+//#include <stdlib.h>  /* free, malloc */\r
+//#include <string.h>  /* memcpy, memset */\r
+#include <BrotliDecompressLibInternal.h>\r
 \r
 #include "../common/constants.h"\r
 #include "../common/dictionary.h"\r
index 09a3c30d95ae40748708c4edf12793672e2ef487..6b99cfea4b88b6723379cb2261ffba5b1c939c2b 100644 (file)
@@ -8,7 +8,7 @@
 \r
 #include "./huffman.h"\r
 \r
-#include <string.h>  /* memcpy, memset */\r
+//#include <string.h>  /* memcpy, memset */\r
 \r
 #include "../common/constants.h"\r
 #include "../common/types.h"\r
index ead502dd7fceceec477b1f0ac88be40acd87941a..268008918611fd3aa85c87d129f5d0342738f663 100644 (file)
@@ -11,6 +11,7 @@
 \r
 #include "../common/types.h"\r
 #include "./port.h"\r
+#include <BrotliDecompressLibInternal.h>\r
 \r
 #if defined(__cplusplus) || defined(c_plusplus)\r
 extern "C" {\r
index 940abaaddb4741c6b1b79524fe6ec526ba77191e..e7e5e3cc72f6d0ae99e6f97abbf752ba5b00014d 100644 (file)
@@ -6,7 +6,8 @@
 \r
 #include "./state.h"\r
 \r
-#include <stdlib.h>  /* free, malloc */\r
+//#include <stdlib.h>  /* free, malloc */\r
+#include <BrotliDecompressLibInternal.h>\r
 \r
 #include "../common/types.h"\r
 #include "./huffman.h"\r
index 626e479f5e45b2f32d2f44d90abe5fd7d7b57aed..4f69c24c4d1d8be9049d26084667acba42d517ea 100644 (file)
   ## Include/Protocol/VarErrorFlag.h\r
   gEdkiiVarErrorFlagGuid               = { 0x4b37fe8, 0xf6ae, 0x480b, { 0xbd, 0xd5, 0x37, 0xd9, 0x8c, 0x5e, 0x89, 0xaa } }\r
 \r
+  ## GUID indicates the BROTLI custom compress/decompress algorithm.\r
+  gBrotliCustomDecompressGuid      = { 0x3D532050, 0x5CDA, 0x4FD0, { 0x87, 0x9E, 0x0F, 0x7F, 0x63, 0x0D, 0x5A, 0xFB }}\r
+\r
   ## GUID indicates the LZMA custom compress/decompress algorithm.\r
   #  Include/Guid/LzmaDecompress.h\r
   gLzmaCustomDecompressGuid      = { 0xEE4E5898, 0x3914, 0x4259, { 0x9D, 0x6E, 0xDC, 0x7B, 0xD7, 0x94, 0x03, 0xCF }}\r
index 1bb361a5dd938f23c8f92a4c72d79764607054da..6ceec92111216f3b1c704817b493be73f98f0768 100644 (file)
   MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.inf\r
   MdeModulePkg/Library/PlatformHookLibSerialPortPpi/PlatformHookLibSerialPortPpi.inf\r
   MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaCustomDecompressLib.inf\r
+  MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf\r
   MdeModulePkg/Library/PeiDxeDebugLibReportStatusCode/PeiDxeDebugLibReportStatusCode.inf\r
   MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf\r
   MdeModulePkg/Library/PlatformBootManagerLibNull/PlatformBootManagerLibNull.inf\r