]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseUefiDecompressLib / BaseUefiDecompressLib.c
index f6b1fe005d1cde1185a4cd8fca06a9b6d6384604..2f0a0c8c4507ecc96ccc1c3ca18e9cdd1c79d0c8 100644 (file)
@@ -1,25 +1,12 @@
 /** @file\r
   UEFI Decompress Library implementation refer to UEFI specification.\r
 \r
-  Copyright (c) 2006 - 2010, 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
@@ -27,7 +14,7 @@
 \r
   Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.\r
 \r
-  @param  Sd        The global scratch data\r
+  @param  Sd        The global scratch data.\r
   @param  NumOfBits The number of bits to shift and read.\r
 \r
 **/\r
@@ -40,38 +27,36 @@ 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
       // Get 1 byte into SubBitBuf\r
       //\r
       Sd->mCompSize--;\r
-      Sd->mSubBitBuf  = Sd->mSrcBase[Sd->mInBuf++];\r
-      Sd->mBitCount   = 8;\r
-\r
+      Sd->mSubBitBuf = Sd->mSrcBase[Sd->mInBuf++];\r
+      Sd->mBitCount  = 8;\r
     } else {\r
       //\r
       // No more bits from the source, just pad zero bit.\r
       //\r
-      Sd->mSubBitBuf  = 0;\r
-      Sd->mBitCount   = 8;\r
-\r
+      Sd->mSubBitBuf = 0;\r
+      Sd->mBitCount  = 8;\r
     }\r
   }\r
 \r
   //\r
-  // Caculate additional bit count read to update mBitCount\r
+  // Calculate additional bit count read to update mBitCount\r
   //\r
-  Sd->mBitCount = (UINT16) (Sd->mBitCount - NumOfBits);\r
-  \r
+  Sd->mBitCount = (UINT16)(Sd->mBitCount - NumOfBits);\r
+\r
   //\r
   // Copy NumOfBits of bits from mSubBitBuf into mBitBuf\r
   //\r
@@ -101,8 +86,8 @@ GetBits (
 \r
   //\r
   // Pop NumOfBits of Bits from Left\r
-  //  \r
-  OutBits = (UINT32) (Sd->mBitBuf >> (BITBUFSIZ - NumOfBits));\r
+  //\r
+  OutBits = (UINT32)(Sd->mBitBuf >> (BITBUFSIZ - NumOfBits));\r
 \r
   //\r
   // Fill up mBitBuf from source\r
@@ -119,10 +104,10 @@ GetBits (
   and Position Set according to code length array.\r
   If TableBits > 16, then ASSERT ().\r
 \r
-  @param  Sd        The global scratch data\r
-  @param  NumOfChar Number of symbols in the symbol set\r
-  @param  BitLen    Code length array\r
-  @param  TableBits The width of the mapping table\r
+  @param  Sd        The global scratch data.\r
+  @param  NumOfChar The number of symbols in the symbol set.\r
+  @param  BitLen    Code length array.\r
+  @param  TableBits The width of the mapping table.\r
   @param  Table     The table to be created.\r
 \r
   @retval  0 OK.\r
@@ -152,6 +137,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,75 +150,80 @@ MakeTable (
   }\r
 \r
   for (Index = 0; Index < NumOfChar; Index++) {\r
+    if (BitLen[Index] > 16) {\r
+      return (UINT16)BAD_TABLE;\r
+    }\r
+\r
     Count[BitLen[Index]]++;\r
   }\r
-  \r
+\r
   Start[0] = 0;\r
   Start[1] = 0;\r
 \r
   for (Index = 1; Index <= 16; Index++) {\r
-    WordOfStart = Start[Index];\r
-    WordOfCount = Count[Index];\r
-    Start[Index + 1] = (UINT16) (WordOfStart + (WordOfCount << (16 - Index)));\r
+    WordOfStart      = Start[Index];\r
+    WordOfCount      = Count[Index];\r
+    Start[Index + 1] = (UINT16)(WordOfStart + (WordOfCount << (16 - Index)));\r
   }\r
 \r
   if (Start[17] != 0) {\r
     /*(1U << 16)*/\r
-    return (UINT16) BAD_TABLE;\r
+    return (UINT16)BAD_TABLE;\r
   }\r
 \r
-  JuBits = (UINT16) (16 - TableBits);\r
-  \r
+  JuBits = (UINT16)(16 - TableBits);\r
+\r
   Weight[0] = 0;\r
   for (Index = 1; Index <= TableBits; Index++) {\r
     Start[Index] >>= JuBits;\r
-    Weight[Index] = (UINT16) (1U << (TableBits - Index));\r
+    Weight[Index]  = (UINT16)(1U << (TableBits - Index));\r
   }\r
 \r
   while (Index <= 16) {\r
-    Weight[Index] = (UINT16) (1U << (16 - Index));\r
-    Index++;    \r
+    Weight[Index] = (UINT16)(1U << (16 - Index));\r
+    Index++;\r
   }\r
 \r
-  Index = (UINT16) (Start[TableBits + 1] >> JuBits);\r
+  Index = (UINT16)(Start[TableBits + 1] >> JuBits);\r
 \r
   if (Index != 0) {\r
-    Index3 = (UINT16) (1U << TableBits);\r
+    Index3 = (UINT16)(1U << TableBits);\r
     if (Index < Index3) {\r
       SetMem16 (Table + Index, (Index3 - Index) * sizeof (*Table), 0);\r
     }\r
   }\r
 \r
-  Avail = NumOfChar;\r
-  Mask  = (UINT16) (1U << (15 - TableBits));\r
+  Avail          = NumOfChar;\r
+  Mask           = (UINT16)(1U << (15 - TableBits));\r
+  MaxTableLength = (UINT16)(1U << TableBits);\r
 \r
   for (Char = 0; Char < NumOfChar; Char++) {\r
-\r
     Len = BitLen[Char];\r
-    if (Len == 0 || Len >= 17) {\r
+    if ((Len == 0) || (Len >= 17)) {\r
       continue;\r
     }\r
 \r
-    NextCode = (UINT16) (Start[Len] + Weight[Len]);\r
+    NextCode = (UINT16)(Start[Len] + Weight[Len]);\r
 \r
     if (Len <= TableBits) {\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
-\r
     } else {\r
-\r
       Index3  = Start[Len];\r
       Pointer = &Table[Index3 >> JuBits];\r
-      Index   = (UINT16) (Len - TableBits);\r
+      Index   = (UINT16)(Len - TableBits);\r
 \r
       while (Index != 0) {\r
-        if (*Pointer == 0 && Avail < (2 * NC - 1)) {\r
+        if ((*Pointer == 0) && (Avail < (2 * NC - 1))) {\r
           Sd->mRight[Avail] = Sd->mLeft[Avail] = 0;\r
-          *Pointer = Avail++;\r
+          *Pointer          = Avail++;\r
         }\r
-        \r
+\r
         if (*Pointer < (2 * NC - 1)) {\r
           if ((Index3 & Mask) != 0) {\r
             Pointer = &Sd->mRight[*Pointer];\r
@@ -246,11 +237,11 @@ MakeTable (
       }\r
 \r
       *Pointer = Char;\r
-\r
     }\r
 \r
     Start[Len] = NextCode;\r
   }\r
+\r
   //\r
   // Succeeds\r
   //\r
@@ -262,7 +253,7 @@ MakeTable (
 \r
   Get a position value according to Position Huffman Table.\r
 \r
-  @param  Sd the global scratch data\r
+  @param  Sd The global scratch data.\r
 \r
   @return The position value decoded.\r
 \r
@@ -282,7 +273,6 @@ DecodeP (
     Mask = 1U << (BITBUFSIZ - 1 - 8);\r
 \r
     do {\r
-\r
       if ((Sd->mBitBuf & Mask) != 0) {\r
         Val = Sd->mRight[Val];\r
       } else {\r
@@ -292,6 +282,7 @@ DecodeP (
       Mask >>= 1;\r
     } while (Val >= MAXNP);\r
   }\r
+\r
   //\r
   // Advance what we have read\r
   //\r
@@ -299,7 +290,7 @@ DecodeP (
 \r
   Pos = Val;\r
   if (Val > 1) {\r
-    Pos = (UINT32) ((1U << (Val - 1)) + GetBits (Sd, (UINT16) (Val - 1)));\r
+    Pos = (UINT32)((1U << (Val - 1)) + GetBits (Sd, (UINT16)(Val - 1)));\r
   }\r
 \r
   return Pos;\r
@@ -308,12 +299,12 @@ DecodeP (
 /**\r
   Reads code lengths for the Extra Set or the Position Set.\r
 \r
-  Read in the Extra Set or Pointion Set Length Arrary, then\r
+  Read in the Extra Set or Position Set Length Array, then\r
   generate the Huffman code mapping for them.\r
 \r
   @param  Sd      The global scratch data.\r
-  @param  nn      Number of symbols.\r
-  @param  nbit    Number of bits needed to represent nn.\r
+  @param  nn      The number of symbols.\r
+  @param  nbit    The number of bits needed to represent nn.\r
   @param  Special The special symbol that needs to be taken care of.\r
 \r
   @retval  0 OK.\r
@@ -333,18 +324,19 @@ ReadPTLen (
   UINT16  Index;\r
   UINT32  Mask;\r
 \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
+  Number = (UINT16)GetBits (Sd, nbit);\r
 \r
   if (Number == 0) {\r
     //\r
     // This represents only Huffman code used\r
     //\r
-    CharC = (UINT16) GetBits (Sd, nbit);\r
+    CharC = (UINT16)GetBits (Sd, nbit);\r
 \r
-    SetMem16 (&Sd->mPTTable[0] , sizeof (Sd->mPTTable), CharC);\r
+    SetMem16 (&Sd->mPTTable[0], sizeof (Sd->mPTTable), CharC);\r
 \r
     SetMem (Sd->mPTLen, nn, 0);\r
 \r
@@ -354,12 +346,11 @@ ReadPTLen (
   Index = 0;\r
 \r
   while (Index < Number && Index < NPT) {\r
-\r
-    CharC = (UINT16) (Sd->mBitBuf >> (BITBUFSIZ - 3));\r
+    CharC = (UINT16)(Sd->mBitBuf >> (BITBUFSIZ - 3));\r
 \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
@@ -369,20 +360,20 @@ ReadPTLen (
         CharC += 1;\r
       }\r
     }\r
-    \r
-    FillBuf (Sd, (UINT16) ((CharC < 7) ? 3 : CharC - 3));\r
 \r
-    Sd->mPTLen[Index++] = (UINT8) CharC;\r
\r
+    FillBuf (Sd, (UINT16)((CharC < 7) ? 3 : CharC - 3));\r
+\r
+    Sd->mPTLen[Index++] = (UINT8)CharC;\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
-      CharC = (UINT16) GetBits (Sd, 2);\r
-      while ((INT16) (--CharC) >= 0 && Index < NPT) {\r
+      CharC = (UINT16)GetBits (Sd, 2);\r
+      while ((INT16)(--CharC) >= 0 && Index < NPT) {\r
         Sd->mPTLen[Index++] = 0;\r
       }\r
     }\r
@@ -391,7 +382,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
@@ -401,7 +392,7 @@ ReadPTLen (
   Read in and decode the Char&Len Set Code Length Array, then\r
   generate the Huffman Code mapping table for the Char&Len Set.\r
 \r
-  @param  Sd the global scratch data\r
+  @param  Sd The global scratch data.\r
 \r
 **/\r
 VOID\r
@@ -409,23 +400,23 @@ ReadCLen (
   SCRATCH_DATA  *Sd\r
   )\r
 {\r
-  UINT16           Number;\r
-  UINT16           CharC;\r
-  UINT16           Index;\r
-  UINT32           Mask;\r
+  UINT16  Number;\r
+  UINT16  CharC;\r
+  UINT16  Index;\r
+  UINT32  Mask;\r
 \r
-  Number = (UINT16) GetBits (Sd, CBIT);\r
+  Number = (UINT16)GetBits (Sd, CBIT);\r
 \r
   if (Number == 0) {\r
     //\r
     // This represents only Huffman code used\r
     //\r
-    CharC = (UINT16) GetBits (Sd, CBIT);\r
+    CharC = (UINT16)GetBits (Sd, CBIT);\r
 \r
     SetMem (Sd->mCLen, NC, 0);\r
     SetMem16 (&Sd->mCTable[0], sizeof (Sd->mCTable), CharC);\r
 \r
-    return ;\r
+    return;\r
   }\r
 \r
   Index = 0;\r
@@ -435,7 +426,6 @@ ReadCLen (
       Mask = 1U << (BITBUFSIZ - 1 - 8);\r
 \r
       do {\r
-\r
         if (Mask & Sd->mBitBuf) {\r
           CharC = Sd->mRight[CharC];\r
         } else {\r
@@ -443,32 +433,28 @@ ReadCLen (
         }\r
 \r
         Mask >>= 1;\r
-\r
       } while (CharC >= NT);\r
     }\r
+\r
     //\r
     // Advance what we have read\r
     //\r
     FillBuf (Sd, Sd->mPTLen[CharC]);\r
 \r
     if (CharC <= 2) {\r
-\r
       if (CharC == 0) {\r
         CharC = 1;\r
       } else if (CharC == 1) {\r
-        CharC = (UINT16) (GetBits (Sd, 4) + 3);\r
+        CharC = (UINT16)(GetBits (Sd, 4) + 3);\r
       } else if (CharC == 2) {\r
-        CharC = (UINT16) (GetBits (Sd, CBIT) + 20);\r
+        CharC = (UINT16)(GetBits (Sd, CBIT) + 20);\r
       }\r
 \r
-      while ((INT16) (--CharC) >= 0 && Index < NC) {\r
+      while ((INT16)(--CharC) >= 0 && Index < NC) {\r
         Sd->mCLen[Index++] = 0;\r
       }\r
-\r
     } else {\r
-\r
-      Sd->mCLen[Index++] = (UINT8) (CharC - 2);\r
-\r
+      Sd->mCLen[Index++] = (UINT8)(CharC - 2);\r
     }\r
   }\r
 \r
@@ -476,7 +462,7 @@ ReadCLen (
 \r
   MakeTable (Sd, NC, Sd->mCLen, 12, Sd->mCTable);\r
 \r
-  return ;\r
+  return;\r
 }\r
 \r
 /**\r
@@ -503,11 +489,11 @@ DecodeC (
     //\r
     // Starting a new block\r
     // Read BlockSize from block header\r
-    // \r
-    Sd->mBlockSize    = (UINT16) GetBits (Sd, 16);\r
+    //\r
+    Sd->mBlockSize = (UINT16)GetBits (Sd, 16);\r
 \r
     //\r
-    // Read in the Extra Set Code Length Arrary,\r
+    // Read in the Extra Set Code Length Array,\r
     // Generate the Huffman code mapping table for Extra Set.\r
     //\r
     Sd->mBadTableFlag = ReadPTLen (Sd, NT, TBIT, 3);\r
@@ -516,16 +502,16 @@ DecodeC (
     }\r
 \r
     //\r
-    // Read in and decode the Char&Len Set Code Length Arrary,\r
+    // Read in and decode the Char&Len Set Code Length Array,\r
     // Generate the Huffman code mapping table for Char&Len Set.\r
     //\r
     ReadCLen (Sd);\r
 \r
     //\r
-    // Read in the Position Set Code Length Arrary, \r
+    // Read in the Position Set Code Length Array,\r
     // Generate the Huffman code mapping table for the Position Set.\r
     //\r
-    Sd->mBadTableFlag = ReadPTLen (Sd, MAXNP, Sd->mPBit, (UINT16) (-1));\r
+    Sd->mBadTableFlag = ReadPTLen (Sd, MAXNP, Sd->mPBit, (UINT16)(-1));\r
     if (Sd->mBadTableFlag != 0) {\r
       return 0;\r
     }\r
@@ -550,6 +536,7 @@ DecodeC (
       Mask >>= 1;\r
     } while (Index2 >= NC);\r
   }\r
+\r
   //\r
   // Advance what we have read\r
   //\r
@@ -561,7 +548,7 @@ DecodeC (
 /**\r
   Decode the source data and put the resulting data into the destination buffer.\r
 \r
-  @param  Sd The global scratch data\r
+  @param  Sd The global scratch data.\r
 \r
 **/\r
 VOID\r
@@ -573,14 +560,14 @@ Decode (
   UINT32  DataIdx;\r
   UINT16  CharC;\r
 \r
-  BytesRemain = (UINT16) (-1);\r
+  BytesRemain = (UINT16)(-1);\r
 \r
-  DataIdx     = 0;\r
+  DataIdx = 0;\r
 \r
-  for (;;) {\r
+  for ( ; ;) {\r
     //\r
     // Get one code from mBitBuf\r
-    // \r
+    //\r
     CharC = DecodeC (Sd);\r
     if (Sd->mBadTableFlag != 0) {\r
       goto Done;\r
@@ -596,15 +583,14 @@ Decode (
         //\r
         // Write orignal character into mDstBase\r
         //\r
-        Sd->mDstBase[Sd->mOutBuf++] = (UINT8) CharC;\r
+        Sd->mDstBase[Sd->mOutBuf++] = (UINT8)CharC;\r
       }\r
-\r
     } else {\r
       //\r
       // Process a Pointer\r
       //\r
-      CharC       = (UINT16) (CharC - (BIT8 - THRESHOLD));\r
\r
+      CharC = (UINT16)(CharC - (BIT8 - THRESHOLD));\r
+\r
       //\r
       // Get string length\r
       //\r
@@ -613,40 +599,54 @@ Decode (
       //\r
       // Locate string position\r
       //\r
-      DataIdx     = Sd->mOutBuf - DecodeP (Sd) - 1;\r
+      DataIdx = Sd->mOutBuf - DecodeP (Sd) - 1;\r
 \r
       //\r
       // Write BytesRemain of bytes into mDstBase\r
       //\r
       BytesRemain--;\r
-      while ((INT16) (BytesRemain) >= 0) {\r
-        Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];\r
+\r
+      while ((INT16)(BytesRemain) >= 0) {\r
         if (Sd->mOutBuf >= Sd->mOrigSize) {\r
           goto Done;\r
         }\r
 \r
+        if (DataIdx >= Sd->mOrigSize) {\r
+          Sd->mBadTableFlag = (UINT16)BAD_TABLE;\r
+          goto Done;\r
+        }\r
+\r
+        Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];\r
+\r
         BytesRemain--;\r
       }\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
 Done:\r
-  return ;\r
+  return;\r
 }\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
@@ -659,18 +659,18 @@ Done:
   @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
+                          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
@@ -692,12 +692,12 @@ UefiDecompressGetInfo (
     return RETURN_INVALID_PARAMETER;\r
   }\r
 \r
-  CompressedSize   = ReadUnaligned32 ((UINT32 *)Source);\r
-  if (SourceSize < (CompressedSize + 8)) {\r
+  CompressedSize = ReadUnaligned32 ((UINT32 *)Source);\r
+  if ((SourceSize < (CompressedSize + 8)) || ((CompressedSize + 8) < 8)) {\r
     return RETURN_INVALID_PARAMETER;\r
   }\r
 \r
-  *ScratchSize  = sizeof (SCRATCH_DATA);\r
+  *ScratchSize     = sizeof (SCRATCH_DATA);\r
   *DestinationSize = ReadUnaligned32 ((UINT32 *)Source + 1);\r
 \r
   return RETURN_SUCCESS;\r
@@ -709,54 +709,57 @@ 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  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
-  UINT32           OrigSize;\r
-  SCRATCH_DATA     *Sd;\r
-  CONST UINT8      *Src;\r
-  UINT8            *Dst;\r
+  UINT32        CompSize;\r
+  UINT32        OrigSize;\r
+  SCRATCH_DATA  *Sd;\r
+  CONST UINT8   *Src;\r
+  UINT8         *Dst;\r
 \r
   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
+  Src = Source;\r
+  Dst = Destination;\r
 \r
-  Sd = (SCRATCH_DATA *) Scratch;\r
+  Sd = (SCRATCH_DATA *)Scratch;\r
 \r
-  CompSize  = Src[0] + (Src[1] << 8) + (Src[2] << 16) + (Src[3] << 24);\r
-  OrigSize  = Src[4] + (Src[5] << 8) + (Src[6] << 16) + (Src[7] << 24);\r
+  CompSize = Src[0] + (Src[1] << 8) + (Src[2] << 16) + (Src[3] << 24);\r
+  OrigSize = Src[4] + (Src[5] << 8) + (Src[6] << 16) + (Src[7] << 24);\r
 \r
   //\r
   // If compressed file size is 0, return\r
@@ -771,12 +774,23 @@ 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
+  // 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
+\r
+  Sd->mSrcBase = (UINT8 *)Src;\r
+  Sd->mDstBase = Dst;\r
   //\r
-  Sd->mPBit     = 4;\r
-  Sd->mSrcBase  = (UINT8 *)Src;\r
-  Sd->mDstBase  = Dst;\r
-  //\r
-  // CompSize and OrigSize are caculated in bytes\r
+  // CompSize and OrigSize are calculated in bytes\r
   //\r
   Sd->mCompSize = CompSize;\r
   Sd->mOrigSize = OrigSize;\r
@@ -800,3 +814,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