]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
MdePkg: Fix Xcode 9 Beta treating 32-bit left shift as undefined
[mirror_edk2.git] / MdePkg / Library / BaseUefiDecompressLib / BaseUefiDecompressLib.c
index a33641776085adfac13368b46aee2cecc6008a56..e818543d925530ee9a9f66245c423c15b22bc2a6 100644 (file)
@@ -1,79 +1,33 @@
 /** @file\r
-  UEFI Decompress Library.\r
+  UEFI Decompress Library implementation refer to UEFI specification.\r
 \r
-  Copyright (c) 2006, Intel Corporation\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2006 - 2015, 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
+  http://opensource.org/licenses/bsd-license.php.\r
 \r
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-  Module Name:  UefiDecompressLib.c\r
-\r
 **/\r
 \r
-//\r
-// Decompression algorithm begins here\r
-//\r
-#define BITBUFSIZ 32\r
-#define MAXMATCH  256\r
-#define THRESHOLD 3\r
-#define CODE_BIT  16\r
-#define BAD_TABLE - 1\r
-\r
-//\r
-// C: Char&Len Set; P: Position Set; T: exTra Set\r
-//\r
-#define NC      (0xff + MAXMATCH + 2 - THRESHOLD)\r
-#define CBIT    9\r
-#define MAXPBIT 5\r
-#define TBIT    5\r
-#define MAXNP   ((1U << MAXPBIT) - 1)\r
-#define NT      (CODE_BIT + 3)\r
-#if NT > MAXNP\r
-#define NPT NT\r
-#else\r
-#define NPT MAXNP\r
-#endif\r
-\r
-typedef struct {\r
-  UINT8   *mSrcBase;  ///< Starting address of compressed data\r
-  UINT8   *mDstBase;  ///< Starting address of decompressed data\r
-  UINT32  mOutBuf;\r
-  UINT32  mInBuf;\r
-\r
-  UINT16  mBitCount;\r
-  UINT32  mBitBuf;\r
-  UINT32  mSubBitBuf;\r
-  UINT16  mBlockSize;\r
-  UINT32  mCompSize;\r
-  UINT32  mOrigSize;\r
-\r
-  UINT16  mBadTableFlag;\r
-\r
-  UINT16  mLeft[2 * NC - 1];\r
-  UINT16  mRight[2 * NC - 1];\r
-  UINT8   mCLen[NC];\r
-  UINT8   mPTLen[NPT];\r
-  UINT16  mCTable[4096];\r
-  UINT16  mPTTable[256];\r
-\r
-  ///\r
-  /// The length of the field 'Position Set Code Length Array Size' in Block Header.\r
-  /// For EFI 1.1 de/compression algorithm, mPBit = 4\r
-  /// For Tiano de/compression algorithm, mPBit = 5\r
-  ///\r
-  UINT8   mPBit;\r
-} SCRATCH_DATA;\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
-  Read NumOfBit of bits from source into mBitBuf\r
+  Read NumOfBit of bits from source into mBitBuf.\r
 \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
@@ -86,14 +40,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
@@ -114,7 +68,7 @@ FillBuf (
   }\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
@@ -125,10 +79,10 @@ FillBuf (
 }\r
 \r
 /**\r
-  Get NumOfBits of bits out from mBitBuf\r
+  Get NumOfBits of bits out from mBitBuf.\r
 \r
-  Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent \r
-  NumOfBits of bits from source. Returns NumOfBits of bits that are \r
+  Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent\r
+  NumOfBits of bits from source. Returns NumOfBits of bits that are\r
   popped out.\r
 \r
   @param  Sd        The global scratch data.\r
@@ -161,14 +115,15 @@ GetBits (
 /**\r
   Creates Huffman Code mapping table according to code length array.\r
 \r
-  Creates Huffman Code mapping table for Extra Set, Char&Len Set \r
+  Creates Huffman Code mapping table for Extra Set, Char&Len Set\r
   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  Table     The 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
   @retval  BAD_TABLE The table is corrupted.\r
@@ -188,26 +143,37 @@ MakeTable (
   UINT16  Start[18];\r
   UINT16  *Pointer;\r
   UINT16  Index3;\r
-  volatile UINT16  Index;\r
+  UINT16  Index;\r
   UINT16  Len;\r
   UINT16  Char;\r
   UINT16  JuBits;\r
   UINT16  Avail;\r
   UINT16  NextCode;\r
   UINT16  Mask;\r
+  UINT16  WordOfStart;\r
+  UINT16  WordOfCount;\r
 \r
-  for (Index = 1; Index <= 16; Index++) {\r
+  //\r
+  // The maximum mapping table width supported by this internal\r
+  // working function is 16.\r
+  //\r
+  ASSERT (TableBits <= 16);\r
+\r
+  for (Index = 0; Index <= 16; Index++) {\r
     Count[Index] = 0;\r
   }\r
 \r
   for (Index = 0; Index < NumOfChar; Index++) {\r
     Count[BitLen[Index]]++;\r
   }\r
-\r
+  \r
+  Start[0] = 0;\r
   Start[1] = 0;\r
 \r
   for (Index = 1; Index <= 16; Index++) {\r
-    Start[Index + 1] = (UINT16) (Start[Index] + (Count[Index] << (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
@@ -216,7 +182,8 @@ 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
     Weight[Index] = (UINT16) (1U << (TableBits - Index));\r
@@ -231,8 +198,8 @@ MakeTable (
 \r
   if (Index != 0) {\r
     Index3 = (UINT16) (1U << TableBits);\r
-    while (Index != Index3) {\r
-      Table[Index++] = 0;\r
+    if (Index < Index3) {\r
+      SetMem16 (Table + Index, (Index3 - Index) * sizeof (*Table), 0);\r
     }\r
   }\r
 \r
@@ -242,7 +209,7 @@ MakeTable (
   for (Char = 0; Char < NumOfChar; Char++) {\r
 \r
     Len = BitLen[Char];\r
-    if (Len == 0) {\r
+    if (Len == 0 || Len >= 17) {\r
       continue;\r
     }\r
 \r
@@ -261,15 +228,17 @@ MakeTable (
       Index   = (UINT16) (Len - TableBits);\r
 \r
       while (Index != 0) {\r
-        if (*Pointer == 0) {\r
-          Sd->mRight[Avail]                     = Sd->mLeft[Avail] = 0;\r
+        if (*Pointer == 0 && Avail < (2 * NC - 1)) {\r
+          Sd->mRight[Avail] = Sd->mLeft[Avail] = 0;\r
           *Pointer = Avail++;\r
         }\r
-\r
-        if (Index3 & Mask) {\r
-          Pointer = &Sd->mRight[*Pointer];\r
-        } else {\r
-          Pointer = &Sd->mLeft[*Pointer];\r
+        \r
+        if (*Pointer < (2 * NC - 1)) {\r
+          if ((Index3 & Mask) != 0) {\r
+            Pointer = &Sd->mRight[*Pointer];\r
+          } else {\r
+            Pointer = &Sd->mLeft[*Pointer];\r
+          }\r
         }\r
 \r
         Index3 <<= 1;\r
@@ -292,8 +261,8 @@ MakeTable (
   Decodes a position value.\r
 \r
   Get a position value according to Position Huffman Table.\r
-  \r
-  @param  Sd the global scratch data\r
+\r
+  @param  Sd The global scratch data.\r
 \r
   @return The position value decoded.\r
 \r
@@ -314,7 +283,7 @@ DecodeP (
 \r
     do {\r
 \r
-      if (Sd->mBitBuf & Mask) {\r
+      if ((Sd->mBitBuf & Mask) != 0) {\r
         Val = Sd->mRight[Val];\r
       } else {\r
         Val = Sd->mLeft[Val];\r
@@ -339,12 +308,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
@@ -361,9 +330,10 @@ ReadPTLen (
 {\r
   UINT16  Number;\r
   UINT16  CharC;\r
-  volatile UINT16  Index;\r
+  UINT16  Index;\r
   UINT32  Mask;\r
 \r
+  ASSERT (nn <= NPT);\r
   //\r
   // Read Extra Set Code Length Array size \r
   //\r
@@ -375,20 +345,16 @@ ReadPTLen (
     //\r
     CharC = (UINT16) GetBits (Sd, nbit);\r
 \r
-    for (Index = 0; Index < 256; Index++) {\r
-      Sd->mPTTable[Index] = CharC;\r
-    }\r
+    SetMem16 (&Sd->mPTTable[0] , sizeof (Sd->mPTTable), CharC);\r
 \r
-    for (Index = 0; Index < nn; Index++) {\r
-      Sd->mPTLen[Index] = 0;\r
-    }\r
+    SetMem (Sd->mPTLen, nn, 0);\r
 \r
     return 0;\r
   }\r
 \r
   Index = 0;\r
 \r
-  while (Index < Number) {\r
+  while (Index < Number && Index < NPT) {\r
 \r
     CharC = (UINT16) (Sd->mBitBuf >> (BITBUFSIZ - 3));\r
 \r
@@ -417,13 +383,13 @@ ReadPTLen (
     //\r
     if (Index == Special) {\r
       CharC = (UINT16) GetBits (Sd, 2);\r
-      while ((INT16) (--CharC) >= 0) {\r
+      while ((INT16) (--CharC) >= 0 && Index < NPT) {\r
         Sd->mPTLen[Index++] = 0;\r
       }\r
     }\r
   }\r
 \r
-  while (Index < nn) {\r
+  while (Index < nn && Index < NPT) {\r
     Sd->mPTLen[Index++] = 0;\r
   }\r
   \r
@@ -432,11 +398,11 @@ ReadPTLen (
 \r
 /**\r
   Reads code lengths for Char&Len Set.\r
-  \r
+\r
   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
@@ -446,7 +412,7 @@ ReadCLen (
 {\r
   UINT16           Number;\r
   UINT16           CharC;\r
-  volatile UINT16  Index;\r
+  UINT16           Index;\r
   UINT32           Mask;\r
 \r
   Number = (UINT16) GetBits (Sd, CBIT);\r
@@ -457,19 +423,14 @@ ReadCLen (
     //\r
     CharC = (UINT16) GetBits (Sd, CBIT);\r
 \r
-    for (Index = 0; Index < NC; Index++) {\r
-      Sd->mCLen[Index] = 0;\r
-    }\r
-\r
-    for (Index = 0; Index < 4096; Index++) {\r
-      Sd->mCTable[Index] = CharC;\r
-    }\r
+    SetMem (Sd->mCLen, NC, 0);\r
+    SetMem16 (&Sd->mCTable[0], sizeof (Sd->mCTable), CharC);\r
 \r
     return ;\r
   }\r
 \r
   Index = 0;\r
-  while (Index < Number) {\r
+  while (Index < Number && Index < NC) {\r
     CharC = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)];\r
     if (CharC >= NT) {\r
       Mask = 1U << (BITBUFSIZ - 1 - 8);\r
@@ -501,7 +462,7 @@ ReadCLen (
         CharC = (UINT16) (GetBits (Sd, CBIT) + 20);\r
       }\r
 \r
-      while ((INT16) (--CharC) >= 0) {\r
+      while ((INT16) (--CharC) >= 0 && Index < NC) {\r
         Sd->mCLen[Index++] = 0;\r
       }\r
 \r
@@ -512,9 +473,7 @@ ReadCLen (
     }\r
   }\r
 \r
-  while (Index < NC) {\r
-    Sd->mCLen[Index++] = 0;\r
-  }\r
+  SetMem (Sd->mCLen + Index, NC - Index, 0);\r
 \r
   MakeTable (Sd, NC, Sd->mCLen, 12, Sd->mCTable);\r
 \r
@@ -523,7 +482,7 @@ ReadCLen (
 \r
 /**\r
   Decode a character/length value.\r
-  \r
+\r
   Read one value from mBitBuf, Get one code from mBitBuf. If it is at block boundary, generates\r
   Huffman code mapping table for Extra Set, Code&Len Set and\r
   Position Set.\r
@@ -549,7 +508,7 @@ DecodeC (
     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
@@ -558,13 +517,13 @@ 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
@@ -583,7 +542,7 @@ DecodeC (
     Mask = 1U << (BITBUFSIZ - 1 - 12);\r
 \r
     do {\r
-      if (Sd->mBitBuf & Mask) {\r
+      if ((Sd->mBitBuf & Mask) != 0) {\r
         Index2 = Sd->mRight[Index2];\r
       } else {\r
         Index2 = Sd->mLeft[Index2];\r
@@ -603,9 +562,7 @@ DecodeC (
 /**\r
   Decode the source data and put the resulting data into the destination buffer.\r
 \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
@@ -627,7 +584,7 @@ Decode (
     // \r
     CharC = DecodeC (Sd);\r
     if (Sd->mBadTableFlag != 0) {\r
-      return ;\r
+      goto Done;\r
     }\r
 \r
     if (CharC < 256) {\r
@@ -635,7 +592,7 @@ Decode (
       // Process an Original character\r
       //\r
       if (Sd->mOutBuf >= Sd->mOrigSize) {\r
-        return ;\r
+        goto Done;\r
       } else {\r
         //\r
         // Write orignal character into mDstBase\r
@@ -647,7 +604,7 @@ Decode (
       //\r
       // Process a Pointer\r
       //\r
-      CharC       = (UINT16) (CharC - (UINT8_MAX + 1 - THRESHOLD));\r
+      CharC       = (UINT16) (CharC - (BIT8 - THRESHOLD));\r
  \r
       //\r
       // Get string length\r
@@ -666,7 +623,7 @@ Decode (
       while ((INT16) (BytesRemain) >= 0) {\r
         Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];\r
         if (Sd->mOutBuf >= Sd->mOrigSize) {\r
-          return ;\r
+          goto Done;\r
         }\r
 \r
         BytesRemain--;\r
@@ -674,11 +631,14 @@ Decode (
     }\r
   }\r
 \r
+Done:\r
   return ;\r
 }\r
 \r
 /**\r
-  Retrieves the size of the uncompressed buffer and the size of the scratch buffer.\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
   required to decompress the buffer specified by Source and SourceSize.\r
@@ -700,15 +660,19 @@ Decode (
   @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
                           by Source and SourceSize.\r
 \r
-  @retval  RETURN_SUCCESS The size of destination buffer and the size of scratch \r
-                          buffer are successull retrieved.\r
-  @retval  RETURN_INVALID_PARAMETER The source data is corrupted\r
-\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
+                          and SourceSize.\r
 **/\r
 RETURN_STATUS\r
 EFIAPI\r
@@ -725,30 +689,30 @@ UefiDecompressGetInfo (
   ASSERT (DestinationSize != NULL);\r
   ASSERT (ScratchSize != NULL);\r
 \r
-  *ScratchSize  = sizeof (SCRATCH_DATA);\r
-\r
   if (SourceSize < 8) {\r
     return RETURN_INVALID_PARAMETER;\r
   }\r
 \r
-  CopyMem (&CompressedSize, Source, sizeof (UINT32));\r
-  CopyMem (DestinationSize, (VOID *)((UINT8 *)Source + 4), sizeof (UINT32));\r
-\r
+  CompressedSize   = ReadUnaligned32 ((UINT32 *)Source);\r
   if (SourceSize < (CompressedSize + 8)) {\r
     return RETURN_INVALID_PARAMETER;\r
   }\r
 \r
+  *ScratchSize  = sizeof (SCRATCH_DATA);\r
+  *DestinationSize = ReadUnaligned32 ((UINT32 *)Source + 1);\r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
 /**\r
   Decompresses a 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\r
-  esponsibility to allocate and free the Destination and Scratch buffers.\r
-  If the compressed source data specified by Source is sucessfully decompressed \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
@@ -758,24 +722,25 @@ UefiDecompressGetInfo (
   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  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 is successfull\r
-  @retval  RETURN_INVALID_PARAMETER The source data is corrupted\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\r
+  IN OUT VOID    *Scratch  OPTIONAL\r
   )\r
 {\r
-  volatile UINT32  Index;\r
   UINT32           CompSize;\r
   UINT32           OrigSize;\r
   SCRATCH_DATA     *Sd;\r
@@ -802,20 +767,17 @@ UefiDecompress (
   }\r
 \r
   Src = Src + 8;\r
+  SetMem (Sd, sizeof (SCRATCH_DATA), 0);\r
 \r
-  for (Index = 0; Index < sizeof (SCRATCH_DATA); Index++) {\r
-    ((UINT8 *) Sd)[Index] = 0;\r
-  }\r
   //\r
   // The length of the field 'Position Set Code Length Array Size' in Block Header.\r
-  // For EFI 1.1 de/compression algorithm(Version 1), mPBit = 4\r
-  // For Tiano de/compression algorithm(Version 2), mPBit = 5\r
+  // For UEFI 2.0 de/compression algorithm(Version 1), mPBit = 4\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