]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
MdePkg: Merge TianoCustomDecompress algorithm into
[mirror_edk2.git] / MdePkg / Library / BaseUefiDecompressLib / BaseUefiDecompressLib.c
index e3b2846c0986892526b7916786311064bf73e505..28b4bf9d8bcf4a36bc2779b2766bcde65368ff22 100644 (file)
@@ -1,25 +1,12 @@
 /** @file\r
   UEFI Decompress Library implementation refer to UEFI specification.\r
 \r
-  Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
   Portions copyright (c) 2008 - 2009, Apple Inc. 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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
-\r
-#include <Base.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/UefiDecompressLib.h>\r
-\r
 #include "BaseUefiDecompressLibInternals.h"\r
 \r
 /**\r
@@ -40,14 +27,14 @@ FillBuf (
   //\r
   // Left shift NumOfBits of bits in advance\r
   //\r
-  Sd->mBitBuf = (UINT32) (Sd->mBitBuf << NumOfBits);\r
+  Sd->mBitBuf = (UINT32) LShiftU64 (((UINT64)Sd->mBitBuf), NumOfBits);\r
 \r
   //\r
   // Copy data needed in bytes into mSbuBitBuf\r
   //\r
   while (NumOfBits > Sd->mBitCount) {\r
-\r
-    Sd->mBitBuf |= (UINT32) (Sd->mSubBitBuf << (NumOfBits = (UINT16) (NumOfBits - Sd->mBitCount)));\r
+    NumOfBits = (UINT16) (NumOfBits - Sd->mBitCount);\r
+    Sd->mBitBuf |= (UINT32) LShiftU64 (((UINT64)Sd->mSubBitBuf), NumOfBits);\r
 \r
     if (Sd->mCompSize > 0) {\r
       //\r
@@ -71,7 +58,7 @@ FillBuf (
   // Calculate additional bit count read to update mBitCount\r
   //\r
   Sd->mBitCount = (UINT16) (Sd->mBitCount - NumOfBits);\r
-  \r
+\r
   //\r
   // Copy NumOfBits of bits from mSubBitBuf into mBitBuf\r
   //\r
@@ -101,7 +88,7 @@ GetBits (
 \r
   //\r
   // Pop NumOfBits of Bits from Left\r
-  //  \r
+  //\r
   OutBits = (UINT32) (Sd->mBitBuf >> (BITBUFSIZ - NumOfBits));\r
 \r
   //\r
@@ -152,6 +139,7 @@ MakeTable (
   UINT16  Mask;\r
   UINT16  WordOfStart;\r
   UINT16  WordOfCount;\r
+  UINT16  MaxTableLength;\r
 \r
   //\r
   // The maximum mapping table width supported by this internal\r
@@ -164,9 +152,12 @@ MakeTable (
   }\r
 \r
   for (Index = 0; Index < NumOfChar; Index++) {\r
+    if (BitLen[Index] > 16) {\r
+      return (UINT16) BAD_TABLE;\r
+    }\r
     Count[BitLen[Index]]++;\r
   }\r
-  \r
+\r
   Start[0] = 0;\r
   Start[1] = 0;\r
 \r
@@ -182,7 +173,7 @@ MakeTable (
   }\r
 \r
   JuBits = (UINT16) (16 - TableBits);\r
-  \r
+\r
   Weight[0] = 0;\r
   for (Index = 1; Index <= TableBits; Index++) {\r
     Start[Index] >>= JuBits;\r
@@ -191,7 +182,7 @@ MakeTable (
 \r
   while (Index <= 16) {\r
     Weight[Index] = (UINT16) (1U << (16 - Index));\r
-    Index++;    \r
+    Index++;\r
   }\r
 \r
   Index = (UINT16) (Start[TableBits + 1] >> JuBits);\r
@@ -205,6 +196,7 @@ MakeTable (
 \r
   Avail = NumOfChar;\r
   Mask  = (UINT16) (1U << (15 - TableBits));\r
+  MaxTableLength = (UINT16) (1U << TableBits);\r
 \r
   for (Char = 0; Char < NumOfChar; Char++) {\r
 \r
@@ -217,6 +209,10 @@ MakeTable (
 \r
     if (Len <= TableBits) {\r
 \r
+      if (Start[Len] >= NextCode || NextCode > MaxTableLength){\r
+        return (UINT16) BAD_TABLE;\r
+      }\r
+\r
       for (Index = Start[Len]; Index < NextCode; Index++) {\r
         Table[Index] = Char;\r
       }\r
@@ -232,7 +228,7 @@ MakeTable (
           Sd->mRight[Avail] = Sd->mLeft[Avail] = 0;\r
           *Pointer = Avail++;\r
         }\r
-        \r
+\r
         if (*Pointer < (2 * NC - 1)) {\r
           if ((Index3 & Mask) != 0) {\r
             Pointer = &Sd->mRight[*Pointer];\r
@@ -335,7 +331,7 @@ ReadPTLen (
 \r
   ASSERT (nn <= NPT);\r
   //\r
-  // Read Extra Set Code Length Array size \r
+  // Read Extra Set Code Length Array size\r
   //\r
   Number = (UINT16) GetBits (Sd, nbit);\r
 \r
@@ -360,7 +356,7 @@ ReadPTLen (
 \r
     //\r
     // If a code length is less than 7, then it is encoded as a 3-bit\r
-    // value. Or it is encoded as a series of "1"s followed by a \r
+    // value. Or it is encoded as a series of "1"s followed by a\r
     // terminating "0". The number of "1"s = Code length - 4.\r
     //\r
     if (CharC == 7) {\r
@@ -370,15 +366,15 @@ ReadPTLen (
         CharC += 1;\r
       }\r
     }\r
-    \r
+\r
     FillBuf (Sd, (UINT16) ((CharC < 7) ? 3 : CharC - 3));\r
 \r
     Sd->mPTLen[Index++] = (UINT8) CharC;\r
\r
+\r
     //\r
-    // For Code&Len Set, \r
+    // For Code&Len Set,\r
     // After the third length of the code length concatenation,\r
-    // a 2-bit value is used to indicated the number of consecutive \r
+    // a 2-bit value is used to indicated the number of consecutive\r
     // zero lengths after the third length.\r
     //\r
     if (Index == Special) {\r
@@ -392,7 +388,7 @@ ReadPTLen (
   while (Index < nn && Index < NPT) {\r
     Sd->mPTLen[Index++] = 0;\r
   }\r
-  \r
+\r
   return MakeTable (Sd, nn, Sd->mPTLen, 8, Sd->mPTTable);\r
 }\r
 \r
@@ -504,7 +500,7 @@ DecodeC (
     //\r
     // Starting a new block\r
     // Read BlockSize from block header\r
-    // \r
+    //\r
     Sd->mBlockSize    = (UINT16) GetBits (Sd, 16);\r
 \r
     //\r
@@ -581,7 +577,7 @@ Decode (
   for (;;) {\r
     //\r
     // Get one code from mBitBuf\r
-    // \r
+    //\r
     CharC = DecodeC (Sd);\r
     if (Sd->mBadTableFlag != 0) {\r
       goto Done;\r
@@ -605,7 +601,7 @@ Decode (
       // Process a Pointer\r
       //\r
       CharC       = (UINT16) (CharC - (BIT8 - THRESHOLD));\r
\r
+\r
       //\r
       // Get string length\r
       //\r
@@ -620,14 +616,25 @@ Decode (
       // Write BytesRemain of bytes into mDstBase\r
       //\r
       BytesRemain--;\r
+\r
       while ((INT16) (BytesRemain) >= 0) {\r
-        Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];\r
         if (Sd->mOutBuf >= Sd->mOrigSize) {\r
           goto Done;\r
         }\r
+        if (DataIdx >= Sd->mOrigSize) {\r
+          Sd->mBadTableFlag = (UINT16) BAD_TABLE;\r
+          goto Done;\r
+        }\r
+        Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];\r
 \r
         BytesRemain--;\r
       }\r
+      //\r
+      // Once mOutBuf is fully filled, directly return\r
+      //\r
+      if (Sd->mOutBuf >= Sd->mOrigSize) {\r
+        goto Done;\r
+      }\r
     }\r
   }\r
 \r
@@ -636,18 +643,18 @@ Done:
 }\r
 \r
 /**\r
-  Given a compressed source buffer, this function retrieves the size of \r
-  the uncompressed buffer and the size of the scratch buffer required \r
+  Given a 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
+  Retrieves the size of the uncompressed buffer and the temporary scratch buffer\r
   required to decompress the buffer specified by Source and SourceSize.\r
   If the size of the uncompressed buffer or the size of the scratch buffer cannot\r
-  be determined from the compressed data specified by Source and SourceData, \r
+  be determined from the compressed data specified by Source and SourceData,\r
   then RETURN_INVALID_PARAMETER is returned.  Otherwise, the size of the uncompressed\r
   buffer is returned in DestinationSize, the size of the scratch buffer is returned\r
   in ScratchSize, and RETURN_SUCCESS is returned.\r
-  This function does not have scratch buffer available to perform a thorough \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 beginning bytes of the source data and output it as DestinationSize.\r
   And ScratchSize is specific to the decompression implementation.\r
@@ -662,16 +669,16 @@ Done:
                           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
+                          is required to decompress the compressed buffer specified\r
                           by Source and SourceSize.\r
 \r
-  @retval  RETURN_SUCCESS The size of the uncompressed data was returned \r
-                          in DestinationSize, and the size of the scratch \r
+  @retval  RETURN_SUCCESS The size of the uncompressed data was returned\r
+                          in DestinationSize, and the size of the scratch\r
                           buffer was returned in ScratchSize.\r
-  @retval  RETURN_INVALID_PARAMETER \r
-                          The size of the uncompressed data or the size of \r
-                          the scratch buffer cannot be determined from \r
-                          the compressed data specified by Source \r
+  @retval  RETURN_INVALID_PARAMETER\r
+                          The size of the uncompressed data or the size of\r
+                          the scratch buffer cannot be determined from\r
+                          the compressed data specified by Source\r
                           and SourceSize.\r
 **/\r
 RETURN_STATUS\r
@@ -694,7 +701,7 @@ UefiDecompressGetInfo (
   }\r
 \r
   CompressedSize   = ReadUnaligned32 ((UINT32 *)Source);\r
-  if (SourceSize < (CompressedSize + 8)) {\r
+  if (SourceSize < (CompressedSize + 8) || (CompressedSize + 8) < 8) {\r
     return RETURN_INVALID_PARAMETER;\r
   }\r
 \r
@@ -710,35 +717,37 @@ UefiDecompressGetInfo (
   Extracts decompressed data to its original form.\r
   This function is designed so that the decompression algorithm can be implemented\r
   without using any memory services.  As a result, this function is not allowed to\r
-  call any memory allocation services in its implementation.  It is the caller's \r
+  call any memory allocation services in its implementation.  It is the caller's\r
   responsibility to allocate and free the Destination and Scratch buffers.\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
+  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
   If Source is NULL, then ASSERT().\r
   If Destination is NULL, then ASSERT().\r
   If the required scratch buffer size > 0 and Scratch is NULL, then ASSERT().\r
+  If the Version is not 1 or 2, then ASSERT().\r
 \r
   @param  Source      The source buffer containing the compressed data.\r
   @param  Destination The destination buffer to store the decompressed data.\r
   @param  Scratch     A temporary scratch buffer that is used to perform the decompression.\r
-                      This is an optional parameter that may be NULL if the \r
+                      This is an optional parameter that may be NULL if the\r
                       required scratch buffer size is 0.\r
-                     \r
-  @retval  RETURN_SUCCESS Decompression completed successfully, and \r
+  @param  Version     1 for UEFI Decompress algoruthm, 2 for Tiano Decompess algorithm.\r
+\r
+  @retval  RETURN_SUCCESS Decompression completed successfully, and\r
                           the uncompressed buffer is returned in Destination.\r
-  @retval  RETURN_INVALID_PARAMETER \r
-                          The source buffer specified by Source is corrupted \r
+  @retval  RETURN_INVALID_PARAMETER\r
+                          The source buffer specified by Source is corrupted\r
                           (not in a valid compressed format).\r
 **/\r
 RETURN_STATUS\r
-EFIAPI\r
-UefiDecompress (\r
+UefiTianoDecompress (\r
   IN CONST VOID  *Source,\r
   IN OUT VOID    *Destination,\r
-  IN OUT VOID    *Scratch  OPTIONAL\r
+  IN OUT VOID    *Scratch,\r
+  IN UINT32      Version\r
   )\r
 {\r
   UINT32           CompSize;\r
@@ -750,6 +759,7 @@ UefiDecompress (
   ASSERT (Source != NULL);\r
   ASSERT (Destination != NULL);\r
   ASSERT (Scratch != NULL);\r
+  ASSERT (Version == 1 || Version == 2);\r
 \r
   Src     = Source;\r
   Dst     = Destination;\r
@@ -772,8 +782,18 @@ UefiDecompress (
   //\r
   // The length of the field 'Position Set Code Length Array Size' in Block Header.\r
   // For UEFI 2.0 de/compression algorithm(Version 1), mPBit = 4\r
-  //\r
-  Sd->mPBit     = 4;\r
+  // For Tiano de/compression algorithm(Version 2), mPBit = 5\r
+  //\r
+  switch (Version) {\r
+    case 1 :\r
+      Sd->mPBit = 4;\r
+      break;\r
+    case 2 :\r
+      Sd->mPBit = 5;\r
+      break;\r
+    default:\r
+      ASSERT (FALSE);\r
+  }\r
   Sd->mSrcBase  = (UINT8 *)Src;\r
   Sd->mDstBase  = Dst;\r
   //\r
@@ -801,3 +821,43 @@ UefiDecompress (
 \r
   return RETURN_SUCCESS;\r
 }\r
+\r
+/**\r
+  Decompresses a UEFI compressed source buffer.\r
+\r
+  Extracts decompressed data to its original form.\r
+  This function is designed so that the decompression algorithm can be implemented\r
+  without using any memory services.  As a result, this function is not allowed to\r
+  call any memory allocation services in its implementation.  It is the caller's\r
+  responsibility to allocate and free the Destination and Scratch buffers.\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
+  If Source is NULL, then ASSERT().\r
+  If Destination is NULL, then ASSERT().\r
+  If the required scratch buffer size > 0 and Scratch is NULL, then ASSERT().\r
+\r
+  @param  Source      The source buffer containing the compressed data.\r
+  @param  Destination The destination buffer to store the decompressed data\r
+  @param  Scratch     A temporary scratch buffer that is used to perform the decompression.\r
+                      This is an optional parameter that may be NULL if the\r
+                      required scratch buffer size is 0.\r
+\r
+  @retval  RETURN_SUCCESS Decompression completed successfully, and\r
+                          the uncompressed buffer is returned in Destination.\r
+  @retval  RETURN_INVALID_PARAMETER\r
+                          The source buffer specified by Source is corrupted\r
+                          (not in a valid compressed format).\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+UefiDecompress (\r
+  IN CONST VOID  *Source,\r
+  IN OUT VOID    *Destination,\r
+  IN OUT VOID    *Scratch  OPTIONAL\r
+  )\r
+{\r
+  return UefiTianoDecompress (Source, Destination, Scratch, 1);\r
+}\r