From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id CBB008042A for ; Wed, 22 Mar 2017 19:16:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=intel; t=1490235367; x=1521771367; h=from:to:cc:subject:date:message-id:mime-version; bh=tgdBMImi89IYHXRXT/s2VzueihiRpAqew3i02fO+71w=; b=nJ3Uf0DnggKFE+y9dYXTNovXG8BuvxZWDt5WWQoD/eiHH9FyE2xMY9rW hlo23AD8kP/9yIcCcO+PL/uHjdtO7A==; Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Mar 2017 19:16:07 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.36,207,1486454400"; d="dat'59?scan'59,208,59";a="80066786" Received: from fmsmsx103.amr.corp.intel.com ([10.18.124.201]) by fmsmga006.fm.intel.com with ESMTP; 22 Mar 2017 19:16:07 -0700 Received: from fmsmsx153.amr.corp.intel.com (10.18.125.6) by FMSMSX103.amr.corp.intel.com (10.18.124.201) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 22 Mar 2017 19:16:07 -0700 Received: from shsmsx151.ccr.corp.intel.com (10.239.6.50) by FMSMSX153.amr.corp.intel.com (10.18.125.6) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 22 Mar 2017 19:16:06 -0700 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.88]) by SHSMSX151.ccr.corp.intel.com ([169.254.3.204]) with mapi id 14.03.0248.002; Thu, 23 Mar 2017 10:16:02 +0800 From: "Song, BinX" To: "edk2-devel@lists.01.org" CC: "Gao, Liming" Thread-Topic: [PATCH 2/4] MdeModulePkg: Add Brotli algorithm decompression library Thread-Index: AdKje2r2Rxx9hGz2SWSUfgyQ++2clA== Date: Thu, 23 Mar 2017 02:16:02 +0000 Message-ID: <559D2DF22BC9A3468B4FA1AA547F0EF102545F41@shsmsx102.ccr.corp.intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: <559D2DF22BC9A3468B4FA1AA547F0EF102545F41@shsmsx102.ccr.corp.intel.com> x-originating-ip: [10.239.127.40] MIME-Version: 1.0 X-Content-Filtered-By: Mailman/MimeDel 2.1.22 Subject: [PATCH 2/4] MdeModulePkg: Add Brotli algorithm decompression library X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Mar 2017 02:16:08 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable - Add Brotli algorithm decompression library support Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Bell Song --- .../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/BrotliCu= stomDecompressLib.inf create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDe= compress.c create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDe= compressLib.uni create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDe= compressLibInternal.h create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSe= ctionExtraction.c create mode 100644 MdeModulePkg/Library/BrotliCustomDecompressLib/ReadMe.t= xt diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDec= ompressLib.inf b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCusto= mDecompressLib.inf new file mode 100644 index 0000000..578f97f --- /dev/null +++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompress= Lib.inf @@ -0,0 +1,56 @@ +## @file +# BrotliCustomDecompressLib produces BROTLI custom decompression algorith= m. +# +# 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.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may b= e 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 IM= PLIED. +# +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D BrotliDecompressLib + MODULE_UNI_FILE =3D BrotliDecompressLib.uni + FILE_GUID =3D 69EC7DB2-B0DD-493A-963A-C5F330131BAA + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D NULL + CONSTRUCTOR =3D BrotliDecompressLibConstructor + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D 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 BROTL= I custom decompress algorithm. + +[LibraryClasses] + BaseLib + DebugLib + BaseMemoryLib + ExtractGuidedSectionLib diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompres= s.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.
+ This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ +#include + +/** + 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 =3D (BROTLI_BUFF *)Ptr; + + if (Private->BuffSize >=3D Size) { + Addr =3D Private->Buff; + Private->Buff =3D (VOID *) ((UINT8 *)Addr + Size); + Private->BuffSize -=3D 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 decomp= ressed + 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 dat= a. + @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 =3D FILE_BUFFER_SIZE; + Result =3D BROTLI_RESULT_ERROR; + BroState =3D BrotliCreateState(BrAlloc, BrFree, BuffInfo); + Temp =3D Destination; + + if (BroState =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + Input =3D (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE); + Output =3D (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE); + if ((Input=3D=3DNULL) || (Output=3D=3DNULL)) { + BrFree(BuffInfo, Input); + BrFree(BuffInfo, Output); + BrotliDestroyState(BroState); + return EFI_INVALID_PARAMETER; + } + NextOut =3D Output; + Result =3D BROTLI_RESULT_NEEDS_MORE_INPUT; + while (1) { + if (Result =3D=3D BROTLI_RESULT_NEEDS_MORE_INPUT) { + if (SourceSize =3D=3D 0) { + break; + } + if (SourceSize >=3D FILE_BUFFER_SIZE) { + AvailableIn =3D FILE_BUFFER_SIZE; + }else{ + AvailableIn =3D SourceSize; + } + CopyMem(Input, Source, AvailableIn); + Source =3D (VOID *)((UINT8 *)Source + AvailableIn); + SourceSize -=3D AvailableIn; + NextIn =3D Input; + } else if (Result =3D=3D BROTLI_RESULT_NEEDS_MORE_OUTPUT) { + CopyMem(Temp, Output, FILE_BUFFER_SIZE); + AvailableOut =3D FILE_BUFFER_SIZE; + Temp =3D (VOID *)((UINT8 *)Temp +FILE_BUFFER_SIZE); + NextOut =3D Output; + } else { + break; /* Error or success. */ + } + Result =3D BrotliDecompressStream( + &AvailableIn, + &NextIn, + &AvailableOut, + &NextOut, + &TotalOut, + BroState + ); + } + if (NextOut !=3D Output) { + CopyMem(Temp, Output, (size_t)(NextOut - Output)); + } + + DestSize =3D TotalOut; + + BrFree(BuffInfo, Input); + BrFree(BuffInfo, Output); + BrotliDestroyState(BroState); + return (Result =3D=3D 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 =3D 0; + for (Index =3D EndOffset - 1; Index >=3D StartOffset; Index--) + DecodedSize =3D LShiftU64(DecodedSize, 8) + EncodedData[Index]; + + return DecodedSize; +} + +/** + Given a Brotli compressed source buffer, this function retrieves the siz= e 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_SUCCE= SS is returned. + This function does not have scratch buffer available to perform a thorou= gh + checking of the validity of the source data. It just retrieves the "Orig= inal 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 uncompre= ssed buffer + that will be generated when the compressed buffe= r 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 >=3D BROTLI_SCRATCH_MAX); + + MaxOffset =3D BROTLI_DECODE_MAX; + GetSize =3D GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO= _SIZE, MaxOffset); + *DestinationSize =3D (UINT32)GetSize; + MaxOffset =3D BROTLI_SCRATCH_MAX; + GetSize =3D GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO= _SIZE, MaxOffset); + *ScratchSize =3D (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 decomp= ressed + into Destination, then RETURN_SUCCESS is returned. If the compressed so= urce 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 dat= a + @param Scratch A temporary scratch buffer that is used to perform t= he decompression. + This is an optional parameter that may be NULL if th= e + 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 =3D 0; + EFI_STATUS Status; + BROTLI_BUFF BroBuff; + UINT64 GetSize; + UINT8 MaxOffset; + + MaxOffset =3D BROTLI_SCRATCH_MAX; + GetSize =3D GetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO= _SIZE, MaxOffset); + + BroBuff.Buff =3D Scratch; + BroBuff.BuffSize =3D (UINTN)GetSize; + + Status =3D BrotliDecompress( + (VOID *)((UINT8 *)Source + BROTLI_SCRATCH_MAX), + SourceSize - BROTLI_SCRATCH_MAX, + Destination, + DestSize, + (VOID *)(&BroBuff) + ); + + return Status; +} diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompres= sLib.uni b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressL= ib.uni new file mode 100644 index 0000000..c5e13b2 --- /dev/null +++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLib.un= i @@ -0,0 +1,21 @@ +// /** @file +// BrotliCustomDecompressLib produces BROTLI custom decompression algorith= m. +// +// 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.
+// +// This program and the accompanying materials +// are licensed and made available under the terms and conditions of the B= SD License +// which accompanies this distribution. The full text of the license may b= e 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 IM= PLIED. +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "BrotliCustomDecom= pressLib produces BROTLI custom decompression algorithm" + +#string STR_MODULE_DESCRIPTION #language en-US "It is based on th= e Brotli v0.5.2. Brotli was released on the website https://github.com/goog= le/brotli." diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompres= sLibInternal.h b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecom= pressLibInternal.h new file mode 100644 index 0000000..c2d84a8 --- /dev/null +++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompressLibInt= ernal.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.
+ This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#ifndef __BROTLI_DECOMPRESS_INTERNAL_H__ +#define __BROTLI_DECOMPRESS_INTERNAL_H__ + +#include +#include +#include +#include +#include +#include +#include + +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/GuidedSectionEx= traction.c b/MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionEx= traction.c new file mode 100644 index 0000000..427adb8 --- /dev/null +++ b/MdeModulePkg/Library/BrotliCustomDecompressLib/GuidedSectionExtractio= n.c @@ -0,0 +1,196 @@ +/** @file + BROTLI Decompress GUIDed Section Extraction Library. + It wraps Brotli decompress interfaces to GUIDed Section Extraction inter= faces + and registers them into GUIDed handler table. + + Copyright (c) 2017, Intel Corporation. All rights reserved.
+ This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include + +/** + 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 GUID= ed section. + + Examines a GUIDed section specified by InputSection. + If GUID for InputSection does not match the GUID that this handler suppo= rts, + 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 suppor= ts, + then the size required to hold the decoded buffer is returned in OututBu= fferSize, + the size of an optional scratch buffer is returned in ScratchSize, and t= he Attributes field + from EFI_GUID_DEFINED_SECTION header of InputSection is returned in Sect= ionAttribute. + + 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 f= ormatted file. + @param[out] OutputBufferSize A pointer to the size, in bytes, of an ou= tput buffer required + if the buffer specified by InputSection w= ere decoded. + @param[out] ScratchBufferSize A pointer to the size, in bytes, required= as scratch space + if the buffer specified by InputSection w= ere 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 wa= s 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 !=3D NULL); + ASSERT (OutputBufferSize !=3D NULL); + ASSERT (ScratchBufferSize !=3D NULL); + ASSERT (SectionAttribute !=3D NULL); + + if (IS_SECTION2 (InputSection)) { + if (!CompareGuid ( + &gBrotliCustomDecompressGuid, + &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionG= uid))) { + return RETURN_INVALID_PARAMETER; + } + + *SectionAttribute =3D ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->At= tributes; + + return BrotliUefiDecompressGetInfo ( + (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) Input= Section)->DataOffset, + SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)= InputSection)->DataOffset, + OutputBufferSize, + ScratchBufferSize + ); + } else { + if (!CompareGuid ( + &gBrotliCustomDecompressGuid, + &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGu= id))) { + return RETURN_INVALID_PARAMETER; + } + + *SectionAttribute =3D ((EFI_GUID_DEFINED_SECTION *) InputSection)->Att= ributes; + + return BrotliUefiDecompressGetInfo ( + (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputS= ection)->DataOffset, + SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) I= nputSection)->DataOffset, + OutputBufferSize, + ScratchBufferSize + ); + } +} + +/** + Decompress a BROTLI compressed GUIDed section into a caller allocated ou= tput buffer. + + Decodes the GUIDed section specified by InputSection. + If GUID for InputSection does not match the GUID that this handler suppo= rts, then RETURN_UNSUPPORTED is returned. + If the data in InputSection can not be decoded, then RETURN_INVALID_PARA= METER is returned. + If the GUID of InputSection does match the GUID that this handler suppor= ts, then InputSection + is decoded into the buffer specified by OutputBuffer and the authenticat= ion status of this + decode operation is returned in AuthenticationStatus. If the decoded bu= ffer is identical to the + data in InputSection, then OutputBuffer is set to point at the data in I= nputSection. 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 bu= ffer, then ASSERT(). + If AuthenticationStatus is NULL, then ASSERT(). + + @param[in] InputSection A pointer to a GUIDed section of an FFS format= ted 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 oper= ation. + @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_STAT= US_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 !=3D NULL); + ASSERT (InputSection !=3D NULL); + + if (IS_SECTION2 (InputSection)) { + if (!CompareGuid ( + &gBrotliCustomDecompressGuid, + &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionG= uid))) { + return RETURN_INVALID_PARAMETER; + } + // + // Authentication is set to Zero, which may be ignored. + // + *AuthenticationStatus =3D 0; + + return BrotliUefiDecompress ( + (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) Input= Section)->DataOffset, + SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *)= InputSection)->DataOffset, + *OutputBuffer, + ScratchBuffer + ); + } else { + if (!CompareGuid ( + &gBrotliCustomDecompressGuid, + &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGu= id))) { + return RETURN_INVALID_PARAMETER; + } + // + // Authentication is set to Zero, which may be ignored. + // + *AuthenticationStatus =3D 0; + + return BrotliUefiDecompress ( + (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputS= ection)->DataOffset, + SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) I= nputSection)->DataOffset, + *OutputBuffer, + ScratchBuffer + ); + } +} + +/** + Register BrotliDecompress and BrotliDecompressGetInfo handlers with Brot= liCustomerDecompressGuid. + + @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/Md= eModulePkg/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_ =20 -#include /* for size_t */ +//#include /* for size_t */ +#ifndef _SIZE_T_DEFINED +#if !defined(_WIN64) || defined(__GNUC__) +typedef unsigned int size_t; +#endif +#endif + =20 #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 +//#include +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) */ =20 #if (!defined(_MSC_VER) || (_MSC_VER >=3D 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) { } } =20 - while ((((size_t)br->next_in) & aligned_read_mask) !=3D 0) { + while ((((size_t)(*br->next_in)) & aligned_read_mask) !=3D 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_ =20 -#include /* memcpy */ +//#include /* memcpy */ +#include =20 #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 #endif =20 -#include /* free, malloc */ -#include /* memcpy, memset */ +//#include /* free, malloc */ +//#include /* memcpy, memset */ +#include =20 #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 @@ =20 #include "./huffman.h" =20 -#include /* memcpy, memset */ +//#include /* memcpy, memset */ =20 #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 @@ =20 #include "../common/types.h" #include "./port.h" +#include =20 #if defined(__cplusplus) || defined(c_plusplus) extern "C" { diff --git a/MdeModulePkg/Library/BrotliCustomDecompressLib/dec/state.c b/M= deModulePkg/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 @@ =20 #include "./state.h" =20 -#include /* free, malloc */ +//#include /* free, malloc */ +#include =20 #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 =3D { 0x4b37fe8, 0xf6ae, 0x480b, { = 0xbd, 0xd5, 0x37, 0xd9, 0x8c, 0x5e, 0x89, 0xaa } } =20 + ## GUID indicates the BROTLI custom compress/decompress algorithm. + gBrotliCustomDecompressGuid =3D { 0x3D532050, 0x5CDA, 0x4FD0, { 0x8= 7, 0x9E, 0x0F, 0x7F, 0x63, 0x0D, 0x5A, 0xFB }} + ## GUID indicates the LZMA custom compress/decompress algorithm. # Include/Guid/LzmaDecompress.h gLzmaCustomDecompressGuid =3D { 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/CpuExceptionHandlerLibNu= ll.inf MdeModulePkg/Library/PlatformHookLibSerialPortPpi/PlatformHookLibSerialP= ortPpi.inf MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaCustomDecompressLib.inf + MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliCustomDecompressLib= .inf MdeModulePkg/Library/PeiDxeDebugLibReportStatusCode/PeiDxeDebugLibReport= StatusCode.inf MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf MdeModulePkg/Library/PlatformBootManagerLibNull/PlatformBootManagerLibNu= ll.inf --=20 2.10.2.windows.1