public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH 2/4] MdeModulePkg: Add Brotli algorithm decompression library
@ 2017-03-23  2:16 Song, BinX
  0 siblings, 0 replies; only message in thread
From: Song, BinX @ 2017-03-23  2:16 UTC (permalink / raw)
  To: edk2-devel@lists.01.org; +Cc: Gao, Liming

- 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>
---
 .../BrotliCustomDecompressLib.inf                  |  56 ++++
 .../BrotliCustomDecompressLib/BrotliDecompress.c   | 321 +++++++++++++++++++++
 .../BrotliDecompressLib.uni                        |  21 ++
 .../BrotliDecompressLibInternal.h                  |  71 +++++
 .../GuidedSectionExtraction.c                      | 196 +++++++++++++
 .../Library/BrotliCustomDecompressLib/ReadMe.txt   |   2 +
 .../BrotliCustomDecompressLib/common/types.h       |  18 +-
 .../BrotliCustomDecompressLib/dec/bit_reader.c     |   2 +-
 .../BrotliCustomDecompressLib/dec/bit_reader.h     |   3 +-
 .../Library/BrotliCustomDecompressLib/dec/decode.c |   5 +-
 .../BrotliCustomDecompressLib/dec/huffman.c        |   2 +-
 .../BrotliCustomDecompressLib/dec/huffman.h        |   1 +
 .../Library/BrotliCustomDecompressLib/dec/state.c  |   3 +-
 MdeModulePkg/MdeModulePkg.dec                      |   3 +
 MdeModulePkg/MdeModulePkg.dsc                      |   1 +
 15 files changed, 697 insertions(+), 8 deletions(-)
 create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf
 create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c
 create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLib.uni
 create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLibInternal.h
 create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c
 create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/ReadMe.txt

diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf
new file mode 100644
index 0000000..578f97f
--- /dev/null
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf
@@ -0,0 +1,56 @@
+## @file
+#  BrotliCustomDecompressLib produces BROTLI custom decompression algorithm.
+#
+#  It is based on the Brotli v0.5.2.
+#  Brotli was released on the website https://github.com/google/brotli.
+#
+#  Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+#
+#  This program and the accompanying materials
+#  are licensed and made available under the terms and conditions of the BSD License
+#  which accompanies this distribution. The full text of the license may be found at
+#  http://opensource.org/licenses/bsd-license.php
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+#
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = BrotliDecompressLib
+  MODULE_UNI_FILE                = BrotliDecompressLib.uni
+  FILE_GUID                      = 69EC7DB2-B0DD-493A-963A-C5F330131BAA
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = NULL
+  CONSTRUCTOR                    = BrotliDecompressLibConstructor
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64
+#
+
+[Sources]
+  GuidedSectionExtraction.c
+  BrotliDecompress.c
+  BrotliDecompressLibInternal.h
+  common/dictionary.c
+  dec/bit_reader.c
+  dec/decode.c
+  dec/huffman.c
+  dec/state.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[Guids]
+  gBrotliCustomDecompressGuid  ## PRODUCES  ## UNDEFINED # specifies BROTLI custom decompress algorithm.
+
+[LibraryClasses]
+  BaseLib
+  DebugLib
+  BaseMemoryLib
+  ExtractGuidedSectionLib
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c
new file mode 100644
index 0000000..2c2648a
--- /dev/null
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c
@@ -0,0 +1,321 @@
+/** @file
+  Brotli Decompress interfaces
+
+  Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+#include <BrotliDecompressLibInternal.h>
+
+/**
+  Dummy malloc function for compiler.
+**/
+VOID *
+malloc (
+  IN size_t    Size
+  )
+{
+  ASSERT (FALSE);
+  return NULL;
+}
+
+/**
+  Dummy free function for compiler.
+**/
+VOID
+free (
+  IN VOID *    Ptr
+  )
+{
+  ASSERT (FALSE);
+}
+
+/**
+  Allocation routine used by BROTLI decompression.
+
+  @param Ptr              Pointer to the BROTLI_BUFF instance.
+  @param Size             The size in bytes to be allocated.
+
+  @return The allocated pointer address, or NULL on failure
+**/
+VOID *
+BrAlloc (
+  IN VOID *    Ptr,
+  IN size_t    Size
+  )
+{
+  VOID          *Addr;
+  BROTLI_BUFF   *Private;
+
+  Private = (BROTLI_BUFF *)Ptr;
+
+  if (Private->BuffSize >= Size) {
+    Addr = Private->Buff;
+    Private->Buff = (VOID *) ((UINT8 *)Addr + Size);
+    Private->BuffSize -= Size;
+    return Addr;
+  } else {
+    ASSERT (FALSE);
+    return NULL;
+  }
+}
+
+/**
+  Free routine used by BROTLI decompression.
+
+  @param Ptr              Pointer to the BROTLI_BUFF instance
+  @param Address          The address to be freed
+**/
+VOID
+BrFree (
+  IN VOID *    Ptr,
+  IN VOID *    Address
+  )
+{
+  //
+  // We use the 'scratch buffer' for allocations, so there is no free
+  // operation required.  The scratch buffer will be freed by the caller
+  // of the decompression code.
+  //
+}
+
+/**
+  Decompresses a Brotli compressed source buffer.
+
+  Extracts decompressed data to its original form.
+  If the compressed source data specified by Source is successfully decompressed
+  into Destination, then EFI_SUCCESS is returned. If the compressed source data
+  specified by Source is not in a valid compressed data format,
+  then EFI_INVALID_PARAMETER is returned.
+
+  @param  Source      The source buffer containing the compressed data.
+  @param  SourceSize  The size of source buffer.
+  @param  Destination The destination buffer to store the decompressed data.
+  @param  DestSize    The destination buffer size.
+  @param  BuffInfo    The pointer to the BROTLI_BUFF instance.
+
+  @retval EFI_SUCCESS Decompression completed successfully, and
+                      the uncompressed buffer is returned in Destination.
+  @retval EFI_INVALID_PARAMETER
+                      The source buffer specified by Source is corrupted
+                      (not in a valid compressed format).
+**/
+EFI_STATUS
+BrotliDecompress (
+  IN CONST VOID*  Source,
+  IN UINTN        SourceSize,
+  IN OUT VOID*    Destination,
+  IN OUT UINTN    DestSize,
+  IN VOID *       BuffInfo
+  )
+{
+  UINT8 *        Input;
+  UINT8 *        Output;
+  const UINT8 *  NextIn;
+  UINT8 *        NextOut;
+  size_t         TotalOut;
+  size_t         AvailableIn;
+  size_t         AvailableOut;
+  BrotliResult   Result;
+  BrotliState *  BroState;
+  VOID *         Temp;
+
+  AvailableOut = FILE_BUFFER_SIZE;
+  Result = BROTLI_RESULT_ERROR;
+  BroState = BrotliCreateState(BrAlloc, BrFree, BuffInfo);
+  Temp = Destination;
+
+  if (BroState == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+  Input = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);
+  Output = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);
+  if ((Input==NULL) || (Output==NULL)) {
+    BrFree(BuffInfo, Input);
+    BrFree(BuffInfo, Output);
+    BrotliDestroyState(BroState);
+    return EFI_INVALID_PARAMETER;
+  }
+  NextOut = Output;
+  Result = BROTLI_RESULT_NEEDS_MORE_INPUT;
+  while (1) {
+    if (Result == BROTLI_RESULT_NEEDS_MORE_INPUT) {
+      if (SourceSize == 0) {
+        break;
+      }
+      if (SourceSize >= FILE_BUFFER_SIZE) {
+        AvailableIn = FILE_BUFFER_SIZE;
+      }else{
+        AvailableIn = SourceSize;
+      }
+      CopyMem(Input, Source, AvailableIn);
+      Source = (VOID *)((UINT8 *)Source + AvailableIn);
+      SourceSize -= AvailableIn;
+      NextIn = Input;
+    } else if (Result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
+      CopyMem(Temp, Output, FILE_BUFFER_SIZE);
+      AvailableOut = FILE_BUFFER_SIZE;
+      Temp = (VOID *)((UINT8 *)Temp +FILE_BUFFER_SIZE);
+      NextOut = Output;
+    } else {
+      break; /* Error or success. */
+    }
+    Result = BrotliDecompressStream(
+                          &AvailableIn,
+                          &NextIn,
+                          &AvailableOut,
+                          &NextOut,
+                          &TotalOut,
+                          BroState
+                          );
+  }
+  if (NextOut != Output) {
+    CopyMem(Temp, Output, (size_t)(NextOut - Output));
+  }
+
+  DestSize = TotalOut;
+
+  BrFree(BuffInfo, Input);
+  BrFree(BuffInfo, Output);
+  BrotliDestroyState(BroState);
+  return (Result == BROTLI_RESULT_SUCCESS) ? EFI_SUCCESS : EFI_INVALID_PARAMETER;
+}
+
+/**
+  Get the size of the uncompressed buffer by parsing EncodeData header.
+
+  @param EncodedData  Pointer to the compressed data.
+  @param StartOffset  Start offset of the compressed data.
+  @param EndOffset    End offset of the compressed data.
+
+  @return The size of the uncompressed buffer.
+**/
+UINT64
+GetDecodedSizeOfBuf(
+  IN UINT8 *  EncodedData,
+  IN UINT8    StartOffset,
+  IN UINT8    EndOffset
+  )
+{
+  UINT64 DecodedSize;
+  INTN   Index;
+
+  /* Parse header */
+  DecodedSize = 0;
+  for (Index = EndOffset - 1; Index >= StartOffset; Index--)
+    DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];
+
+  return DecodedSize;
+}
+
+/**
+  Given a Brotli compressed source buffer, this function retrieves the size of
+  the uncompressed buffer and the size of the scratch buffer required
+  to decompress the compressed source buffer.
+
+  Retrieves the size of the uncompressed buffer and the temporary scratch buffer
+  required to decompress the buffer specified by Source and SourceSize.
+  The size of the uncompressed buffer is returned in DestinationSize,
+  the size of the scratch buffer is returned in ScratchSize, and EFI_SUCCESS is returned.
+  This function does not have scratch buffer available to perform a thorough
+  checking of the validity of the source data. It just retrieves the "Original Size"
+  field from the BROTLI_SCRATCH_MAX beginning bytes of the source data and output it as DestinationSize.
+  And ScratchSize is specific to the decompression implementation.
+
+  If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().
+
+  @param  Source          The source buffer containing the compressed data.
+  @param  SourceSize      The size, in bytes, of the source buffer.
+  @param  DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
+                          that will be generated when the compressed buffer specified
+                          by Source and SourceSize is decompressed.
+  @param  ScratchSize     A pointer to the size, in bytes, of the scratch buffer that
+                          is required to decompress the compressed buffer specified
+                          by Source and SourceSize.
+
+  @retval EFI_SUCCESS     The size of the uncompressed data was returned
+                          in DestinationSize and the size of the scratch
+                          buffer was returned in ScratchSize.
+**/
+EFI_STATUS
+EFIAPI
+BrotliUefiDecompressGetInfo (
+  IN  CONST VOID *  Source,
+  IN  UINT32        SourceSize,
+  OUT UINT32 *      DestinationSize,
+  OUT UINT32 *      ScratchSize
+  )
+{
+  UINT64  GetSize;
+  UINT8   MaxOffset;
+
+  ASSERT(SourceSize >= BROTLI_SCRATCH_MAX);
+
+  MaxOffset = BROTLI_DECODE_MAX;
+  GetSize = GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
+  *DestinationSize = (UINT32)GetSize;
+  MaxOffset = BROTLI_SCRATCH_MAX;
+  GetSize = GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
+  *ScratchSize = (UINT32)GetSize;
+  return EFI_SUCCESS;
+}
+
+/**
+  Decompresses a Brotli compressed source buffer.
+
+  Extracts decompressed data to its original form.
+  If the compressed source data specified by Source is successfully decompressed
+  into Destination, then RETURN_SUCCESS is returned.  If the compressed source data
+  specified by Source is not in a valid compressed data format,
+  then RETURN_INVALID_PARAMETER is returned.
+
+  @param  Source      The source buffer containing the compressed data.
+  @param  SourceSize  The size of source buffer.
+  @param  Destination The destination buffer to store the decompressed data
+  @param  Scratch     A temporary scratch buffer that is used to perform the decompression.
+                      This is an optional parameter that may be NULL if the
+                      required scratch buffer size is 0.
+
+  @retval EFI_SUCCESS Decompression completed successfully, and
+                      the uncompressed buffer is returned in Destination.
+  @retval EFI_INVALID_PARAMETER
+                      The source buffer specified by Source is corrupted
+                      (not in a valid compressed format).
+**/
+EFI_STATUS
+EFIAPI
+BrotliUefiDecompress (
+  IN CONST VOID *   Source,
+  IN UINTN          SourceSize,
+  IN OUT VOID *     Destination,
+  IN OUT VOID *     Scratch
+  )
+{
+  UINTN          DestSize = 0;
+  EFI_STATUS     Status;
+  BROTLI_BUFF    BroBuff;
+  UINT64         GetSize;
+  UINT8          MaxOffset;
+
+  MaxOffset = BROTLI_SCRATCH_MAX;
+  GetSize = GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
+
+  BroBuff.Buff     = Scratch;
+  BroBuff.BuffSize = (UINTN)GetSize;
+
+  Status = BrotliDecompress(
+            (VOID *)((UINT8 *)Source + BROTLI_SCRATCH_MAX),
+            SourceSize - BROTLI_SCRATCH_MAX,
+            Destination,
+            DestSize,
+            (VOID *)(&BroBuff)
+            );
+
+  return Status;
+}
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLib.uni b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLib.uni
new file mode 100644
index 0000000..c5e13b2
--- /dev/null
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLib.uni
@@ -0,0 +1,21 @@
+// /** @file
+// BrotliCustomDecompressLib produces BROTLI custom decompression algorithm.
+//
+// It is based on the Brotli v0.5.2.
+// Brotli was released on the website https://github.com/google/brotli.
+//
+// Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+//
+// This program and the accompanying materials
+// are licensed and made available under the terms and conditions of the BSD License
+// which accompanies this distribution. The full text of the license may be found at
+// http://opensource.org/licenses/bsd-license.php
+// THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+// WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT             #language en-US "BrotliCustomDecompressLib produces BROTLI custom decompression algorithm"
+
+#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."
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLibInternal.h b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLibInternal.h
new file mode 100644
index 0000000..c2d84a8
--- /dev/null
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLibInternal.h
@@ -0,0 +1,71 @@
+/** @file
+  BROTLI UEFI header file
+
+  Allows BROTLI code to build under UEFI (edk2) build environment
+
+  Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef __BROTLI_DECOMPRESS_INTERNAL_H__
+#define __BROTLI_DECOMPRESS_INTERNAL_H__
+
+#include <PiPei.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/ExtractGuidedSectionLib.h>
+#include <common/types.h>
+#include <dec/decode.h>
+
+typedef struct
+{
+  VOID     *Buff;
+  UINTN    BuffSize;
+} BROTLI_BUFF;
+
+#define FILE_BUFFER_SIZE     65536
+#define BROTLI_INFO_SIZE     8
+#define BROTLI_DECODE_MAX    8
+#define BROTLI_SCRATCH_MAX   16
+
+#define memcpy                      CopyMem
+#define memmove                     CopyMem
+#define memset(dest,ch,count)       SetMem(dest,(UINTN)(count),(UINT8)(ch))
+
+VOID *
+malloc (
+  IN size_t   Size
+  );
+
+VOID
+free (
+  IN VOID *   Ptr
+  );
+
+EFI_STATUS
+EFIAPI
+BrotliUefiDecompressGetInfo (
+  IN  CONST VOID  *Source,
+  IN  UINT32      SourceSize,
+  OUT UINT32      *DestinationSize,
+  OUT UINT32      *ScratchSize
+  );
+
+EFI_STATUS
+EFIAPI
+BrotliUefiDecompress (
+  IN CONST VOID  *Source,
+  IN UINTN       SourceSize,
+  IN OUT VOID    *Destination,
+  IN OUT VOID    *Scratch
+  );
+
+#endif
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c
new file mode 100644
index 0000000..427adb8
--- /dev/null
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtraction.c
@@ -0,0 +1,196 @@
+/** @file
+  BROTLI Decompress GUIDed Section Extraction Library.
+  It wraps Brotli decompress interfaces to GUIDed Section Extraction interfaces
+  and registers them into GUIDed handler table.
+
+  Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <BrotliDecompressLibInternal.h>
+
+/**
+  Examines a GUIDed section and returns the size of the decoded buffer and the
+  size of an scratch buffer required to actually decode the data in a GUIDed section.
+
+  Examines a GUIDed section specified by InputSection.
+  If GUID for InputSection does not match the GUID that this handler supports,
+  then RETURN_UNSUPPORTED is returned.
+  If the required information can not be retrieved from InputSection,
+  then RETURN_INVALID_PARAMETER is returned.
+  If the GUID of InputSection does match the GUID that this handler supports,
+  then the size required to hold the decoded buffer is returned in OututBufferSize,
+  the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field
+  from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.
+
+  If InputSection is NULL, then ASSERT().
+  If OutputBufferSize is NULL, then ASSERT().
+  If ScratchBufferSize is NULL, then ASSERT().
+  If SectionAttribute is NULL, then ASSERT().
+
+
+  @param[in]  InputSection       A pointer to a GUIDed section of an FFS formatted file.
+  @param[out] OutputBufferSize   A pointer to the size, in bytes, of an output buffer required
+                                 if the buffer specified by InputSection were decoded.
+  @param[out] ScratchBufferSize  A pointer to the size, in bytes, required as scratch space
+                                 if the buffer specified by InputSection were decoded.
+  @param[out] SectionAttribute   A pointer to the attributes of the GUIDed section. See the Attributes
+                                 field of EFI_GUID_DEFINED_SECTION in the PI Specification.
+
+  @retval  RETURN_SUCCESS            The information about InputSection was returned.
+  @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.
+  @retval  RETURN_INVALID_PARAMETER  The information can not be retrieved from the section specified by InputSection.
+
+**/
+RETURN_STATUS
+EFIAPI
+BrotliGuidedSectionGetInfo (
+  IN  CONST VOID  *InputSection,
+  OUT UINT32      *OutputBufferSize,
+  OUT UINT32      *ScratchBufferSize,
+  OUT UINT16      *SectionAttribute
+  )
+{
+  ASSERT (InputSection != NULL);
+  ASSERT (OutputBufferSize != NULL);
+  ASSERT (ScratchBufferSize != NULL);
+  ASSERT (SectionAttribute != NULL);
+
+  if (IS_SECTION2 (InputSection)) {
+    if (!CompareGuid (
+        &gBrotliCustomDecompressGuid,
+        &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
+      return RETURN_INVALID_PARAMETER;
+    }
+
+    *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
+
+    return BrotliUefiDecompressGetInfo (
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
+             SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
+             OutputBufferSize,
+             ScratchBufferSize
+             );
+  } else {
+    if (!CompareGuid (
+        &gBrotliCustomDecompressGuid,
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
+      return RETURN_INVALID_PARAMETER;
+    }
+
+    *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
+
+    return BrotliUefiDecompressGetInfo (
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
+             SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
+             OutputBufferSize,
+             ScratchBufferSize
+             );
+  }
+}
+
+/**
+  Decompress a BROTLI compressed GUIDed section into a caller allocated output buffer.
+
+  Decodes the GUIDed section specified by InputSection.
+  If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.
+  If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.
+  If the GUID of InputSection does match the GUID that this handler supports, then InputSection
+  is decoded into the buffer specified by OutputBuffer and the authentication status of this
+  decode operation is returned in AuthenticationStatus.  If the decoded buffer is identical to the
+  data in InputSection, then OutputBuffer is set to point at the data in InputSection.  Otherwise,
+  the decoded data will be placed in caller allocated buffer specified by OutputBuffer.
+
+  If InputSection is NULL, then ASSERT().
+  If OutputBuffer is NULL, then ASSERT().
+  If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
+  If AuthenticationStatus is NULL, then ASSERT().
+
+  @param[in]  InputSection  A pointer to a GUIDed section of an FFS formatted file.
+  @param[out] OutputBuffer  A pointer to a buffer that contains the result of a decode operation.
+  @param[out] ScratchBuffer A caller allocated buffer that may be required by this function
+                            as a scratch buffer to perform the decode operation.
+  @param[out] AuthenticationStatus
+                            A pointer to the authentication status of the decoded output buffer.
+                            See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
+                            section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must
+                            never be set by this handler.
+
+  @retval  RETURN_SUCCESS            The buffer specified by InputSection was decoded.
+  @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.
+  @retval  RETURN_INVALID_PARAMETER  The section specified by InputSection can not be decoded.
+
+**/
+RETURN_STATUS
+EFIAPI
+BrotliGuidedSectionExtraction (
+  IN CONST  VOID    *InputSection,
+  OUT       VOID    **OutputBuffer,
+  OUT       VOID    *ScratchBuffer,        OPTIONAL
+  OUT       UINT32  *AuthenticationStatus
+  )
+{
+  ASSERT (OutputBuffer != NULL);
+  ASSERT (InputSection != NULL);
+
+  if (IS_SECTION2 (InputSection)) {
+    if (!CompareGuid (
+        &gBrotliCustomDecompressGuid,
+        &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
+      return RETURN_INVALID_PARAMETER;
+    }
+    //
+    // Authentication is set to Zero, which may be ignored.
+    //
+    *AuthenticationStatus = 0;
+
+    return BrotliUefiDecompress (
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
+             SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
+             *OutputBuffer,
+             ScratchBuffer
+             );
+  } else {
+    if (!CompareGuid (
+        &gBrotliCustomDecompressGuid,
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
+      return RETURN_INVALID_PARAMETER;
+    }
+    //
+    // Authentication is set to Zero, which may be ignored.
+    //
+    *AuthenticationStatus = 0;
+
+    return BrotliUefiDecompress (
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
+             SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
+             *OutputBuffer,
+             ScratchBuffer
+    );
+  }
+}
+
+/**
+  Register BrotliDecompress and BrotliDecompressGetInfo handlers with BrotliCustomerDecompressGuid.
+
+  @retval  EFI_SUCCESS            Register successfully.
+  @retval  EFI_OUT_OF_RESOURCES   No enough memory to store this handler.
+**/
+EFI_STATUS
+EFIAPI
+BrotliDecompressLibConstructor (
+  )
+{
+  return ExtractGuidedSectionRegisterHandlers (
+          &gBrotliCustomDecompressGuid,
+          BrotliGuidedSectionGetInfo,
+          BrotliGuidedSectionExtraction
+          );
+}
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/ReadMe.txt b/MdeModulePkg/Library/BrotliCustomDecompressLib/ReadMe.txt
new file mode 100644
index 0000000..c19c0a1
--- /dev/null
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/ReadMe.txt
@@ -0,0 +1,2 @@
+It is based on the Brotli v0.5.2.
+Brotli was released on the website https://github.com/google/brotli.
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/common/types.h b/MdeModulePkg/Library/BrotliCustomDecompressLib/common/types.h
index 2e9135a..579617a 100644
--- a/MdeModulePkg/Library/BrotliCustomDecompressLib/common/types.h
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/common/types.h
@@ -9,7 +9,13 @@
 #ifndef BROTLI_COMMON_TYPES_H_
 #define BROTLI_COMMON_TYPES_H_
 
-#include <stddef.h>  /* for size_t */
+//#include <stddef.h>  /* for size_t */
+#ifndef _SIZE_T_DEFINED
+#if !defined(_WIN64) || defined(__GNUC__)
+typedef unsigned int size_t;
+#endif
+#endif
+
 
 #if defined(_MSC_VER) && (_MSC_VER < 1600)
 typedef __int8 int8_t;
@@ -21,7 +27,15 @@ typedef unsigned __int32 uint32_t;
 typedef unsigned __int64 uint64_t;
 typedef __int64 int64_t;
 #else
-#include <stdint.h>
+//#include <stdint.h>
+typedef INT8     int8_t;
+typedef INT16    int16_t;
+typedef INT32    int32_t;
+typedef INT64    int64_t;
+typedef UINT8    uint8_t;
+typedef UINT16   uint16_t;
+typedef UINT32   uint32_t;
+typedef UINT64   uint64_t;
 #endif  /* defined(_MSC_VER) && (_MSC_VER < 1600) */
 
 #if (!defined(_MSC_VER) || (_MSC_VER >= 1800)) && \
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.c
index 5498269..39f0d4d 100644
--- a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.c
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.c
@@ -34,7 +34,7 @@ BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) {
     }
   }
 
-  while ((((size_t)br->next_in) & aligned_read_mask) != 0) {
+  while ((((size_t)(*br->next_in)) & aligned_read_mask) != 0) {
     if (!BrotliPullByte(br)) {
       /* If we consumed all the input, we don't care about the alignment. */
       return BROTLI_TRUE;
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.h b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.h
index 22ea193..55124d2 100644
--- a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.h
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/bit_reader.h
@@ -9,7 +9,8 @@
 #ifndef BROTLI_DEC_BIT_READER_H_
 #define BROTLI_DEC_BIT_READER_H_
 
-#include <string.h>  /* memcpy */
+//#include <string.h>  /* memcpy */
+#include <BrotliDecompressLibInternal.h>
 
 #include "../common/types.h"
 #include "./port.h"
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/decode.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/decode.c
index 91e1d1b..1ebab3c 100644
--- a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/decode.c
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/decode.c
@@ -10,8 +10,9 @@
 #include <arm_neon.h>
 #endif
 
-#include <stdlib.h>  /* free, malloc */
-#include <string.h>  /* memcpy, memset */
+//#include <stdlib.h>  /* free, malloc */
+//#include <string.h>  /* memcpy, memset */
+#include <BrotliDecompressLibInternal.h>
 
 #include "../common/constants.h"
 #include "../common/dictionary.h"
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.c
index e17ee27..163334a 100644
--- a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.c
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.c
@@ -8,7 +8,7 @@
 
 #include "./huffman.h"
 
-#include <string.h>  /* memcpy, memset */
+//#include <string.h>  /* memcpy, memset */
 
 #include "../common/constants.h"
 #include "../common/types.h"
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.h b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.h
index 354f27d..b23aa8d 100644
--- a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.h
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.h
@@ -11,6 +11,7 @@
 
 #include "../common/types.h"
 #include "./port.h"
+#include <BrotliDecompressLibInternal.h>
 
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/state.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/state.c
index e6ff742..c48de44 100644
--- a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/state.c
+++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/state.c
@@ -6,7 +6,8 @@
 
 #include "./state.h"
 
-#include <stdlib.h>  /* free, malloc */
+//#include <stdlib.h>  /* free, malloc */
+#include <BrotliDecompressLibInternal.h>
 
 #include "../common/types.h"
 #include "./huffman.h"
diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec
index 7307191..efd9c64 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -344,6 +344,9 @@
   ## Include/Protocol/VarErrorFlag.h
   gEdkiiVarErrorFlagGuid               = { 0x4b37fe8, 0xf6ae, 0x480b, { 0xbd, 0xd5, 0x37, 0xd9, 0x8c, 0x5e, 0x89, 0xaa } }
 
+  ## GUID indicates the BROTLI custom compress/decompress algorithm.
+  gBrotliCustomDecompressGuid      = { 0x3D532050, 0x5CDA, 0x4FD0, { 0x87, 0x9E, 0x0F, 0x7F, 0x63, 0x0D, 0x5A, 0xFB }}
+
   ## GUID indicates the LZMA custom compress/decompress algorithm.
   #  Include/Guid/LzmaDecompress.h
   gLzmaCustomDecompressGuid      = { 0xEE4E5898, 0x3914, 0x4259, { 0x9D, 0x6E, 0xDC, 0x7B, 0xD7, 0x94, 0x03, 0xCF }}
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 5996fe5..65359b0 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -306,6 +306,7 @@
   MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLibNull.inf
   MdeModulePkg/Library/PlatformHookLibSerialPortPpi/PlatformHookLibSerialPortPpi.inf
   MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaCustomDecompressLib.inf
+  MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib.inf
   MdeModulePkg/Library/PeiDxeDebugLibReportStatusCode/PeiDxeDebugLibReportStatusCode.inf
   MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf
   MdeModulePkg/Library/PlatformBootManagerLibNull/PlatformBootManagerLibNull.inf
-- 
2.10.2.windows.1



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2017-03-23  2:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-23  2:16 [PATCH 2/4] MdeModulePkg: Add Brotli algorithm decompression library Song, BinX

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox