]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/BaseUefiTianoCustomDecompressLib/BaseUefiTianoCustomDecompressLib.c
Update the copyright notice format
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / BaseUefiTianoCustomDecompressLib / BaseUefiTianoCustomDecompressLib.c
index acde4d503e3eb8f4619975bfcda72364643374cc..6a0e5eca45e8192754ce9d1bbb44b0484fe4d0fd 100644 (file)
@@ -1,8 +1,9 @@
-/**@file\r
-  UEFI and Custom Decompress Library \r
+/** @file\r
+  UEFI and Tiano Custom Decompress Library \r
+  Tt will do Tiano or UEFI decompress with different verison parameter.\r
   \r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+Copyright (c) 2006 - 2009, Intel Corporation. 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
@@ -14,28 +15,26 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "BaseUefiTianoCustomDecompressLibInternals.h"\r
 \r
+/**\r
+  Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.\r
+  \r
+  @param Sd         The global scratch data\r
+  @param NumOfBits  The number of bits to shift and read.  \r
+**/\r
 VOID\r
 FillBuf (\r
   IN  SCRATCH_DATA  *Sd,\r
   IN  UINT16        NumOfBits\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.\r
-\r
-Arguments:\r
-\r
-  Sd        - The global scratch data\r
-  NumOfBits  - The number of bits to shift and read.\r
-\r
-Returns: (VOID)\r
-\r
---*/\r
 {\r
+  //\r
+  // Left shift NumOfBits of bits in advance\r
+  //\r
   Sd->mBitBuf = (UINT32) (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
@@ -59,43 +58,67 @@ Returns: (VOID)
     }\r
   }\r
 \r
+  //\r
+  // Caculate additional bit count read to update mBitCount\r
+  //\r
   Sd->mBitCount = (UINT16) (Sd->mBitCount - NumOfBits);\r
+  \r
+  //\r
+  // Copy NumOfBits of bits from mSubBitBuf into mBitBuf\r
+  //\r
   Sd->mBitBuf |= Sd->mSubBitBuf >> Sd->mBitCount;\r
 }\r
 \r
-UINT32\r
-GetBits (\r
-  IN  SCRATCH_DATA  *Sd,\r
-  IN  UINT16        NumOfBits\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\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
   popped out.\r
 \r
-Arguments:\r
-\r
-  Sd            - The global scratch data.\r
-  NumOfBits     - The number of bits to pop and read.\r
+  @param  Sd        The global scratch data.\r
+  @param  NumOfBits The number of bits to pop and read.\r
 \r
-Returns:\r
+  @return The bits that are popped out.\r
 \r
-  The bits that are popped out.\r
-\r
---*/\r
+**/\r
+UINT32\r
+GetBits (\r
+  IN  SCRATCH_DATA  *Sd,\r
+  IN  UINT16        NumOfBits\r
+  )\r
 {\r
   UINT32  OutBits;\r
 \r
+  //\r
+  // Pop NumOfBits of Bits from Left\r
+  //  \r
   OutBits = (UINT32) (Sd->mBitBuf >> (BITBUFSIZ - NumOfBits));\r
 \r
+  //\r
+  // Fill up mBitBuf from source\r
+  //\r
   FillBuf (Sd, NumOfBits);\r
 \r
   return OutBits;\r
 }\r
 \r
+/**\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
+  and Position Set according to code length array.\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 to be created.\r
+\r
+  @retval  0 OK.\r
+  @retval  BAD_TABLE The table is corrupted.\r
+\r
+**/\r
 UINT16\r
 MakeTable (\r
   IN  SCRATCH_DATA  *Sd,\r
@@ -104,33 +127,13 @@ MakeTable (
   IN  UINT16        TableBits,\r
   OUT UINT16        *Table\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Creates Huffman Code mapping table according to code length array.\r
-\r
-Arguments:\r
-\r
-  Sd        - The global scratch data\r
-  NumOfChar - Number of symbols in the symbol set\r
-  BitLen    - Code length array\r
-  TableBits - The width of the mapping table\r
-  Table     - The table\r
-  \r
-Returns:\r
-  \r
-  0         - OK.\r
-  BAD_TABLE - The table is corrupted.\r
-\r
---*/\r
 {\r
   UINT16  Count[17];\r
   UINT16  Weight[17];\r
   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
@@ -140,14 +143,15 @@ Returns:
   UINT16  WordOfStart;\r
   UINT16  WordOfCount;\r
 \r
-  for (Index = 1; Index <= 16; Index++) {\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
@@ -162,7 +166,8 @@ Returns:
   }\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
@@ -170,15 +175,15 @@ Returns:
 \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
 \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
@@ -188,7 +193,7 @@ Returns:
   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
@@ -207,15 +212,17 @@ Returns:
       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
@@ -234,25 +241,19 @@ Returns:
   return 0;\r
 }\r
 \r
+/**\r
+  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
+  @return The position value decoded.\r
+**/\r
 UINT32\r
 DecodeP (\r
   IN  SCRATCH_DATA  *Sd\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Decodes a position value.\r
-\r
-Arguments:\r
-\r
-  Sd      - the global scratch data\r
-\r
-Returns:\r
-\r
-  The position value decoded.\r
-\r
---*/\r
 {\r
   UINT16  Val;\r
   UINT32  Mask;\r
@@ -265,7 +266,7 @@ Returns:
 \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
@@ -287,6 +288,21 @@ Returns:
   return Pos;\r
 }\r
 \r
+/**\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
+  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  Special The special symbol that needs to be taken care of.\r
+\r
+  @retval  0 OK.\r
+  @retval  BAD_TABLE Table is corrupted.\r
+\r
+**/\r
 UINT16\r
 ReadPTLen (\r
   IN  SCRATCH_DATA  *Sd,\r
@@ -294,53 +310,43 @@ ReadPTLen (
   IN  UINT16        nbit,\r
   IN  UINT16        Special\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Reads code lengths for the Extra Set or the Position Set\r
-\r
-Arguments:\r
-\r
-  Sd        - The global scratch data\r
-  nn        - Number of symbols\r
-  nbit      - Number of bits needed to represent nn\r
-  Special   - The special symbol that needs to be taken care of \r
-\r
-Returns:\r
-\r
-  0         - OK.\r
-  BAD_TABLE - Table is corrupted.\r
-\r
---*/\r
 {\r
   UINT16  Number;\r
   UINT16  CharC;\r
-  volatile UINT16  Index;\r
+  UINT16  Index;\r
   UINT32  Mask;\r
 \r
+  //\r
+  // Read Extra Set Code Length Array size \r
+  //\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
 \r
     for (Index = 0; Index < 256; Index++) {\r
       Sd->mPTTable[Index] = CharC;\r
     }\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
+    //\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
+    // terminating "0". The number of "1"s = Code length - 4.\r
+    //\r
     if (CharC == 7) {\r
       Mask = 1U << (BITBUFSIZ - 1 - 3);\r
       while (Mask & Sd->mBitBuf) {\r
@@ -348,57 +354,60 @@ Returns:
         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
+    // After the third length of the code length concatenation,\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) {\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
+  \r
   return MakeTable (Sd, nn, Sd->mPTLen, 8, Sd->mPTTable);\r
 }\r
 \r
+/**\r
+  Reads code lengths for Char&Len Set.\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
+\r
+**/\r
 VOID\r
 ReadCLen (\r
   SCRATCH_DATA  *Sd\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Reads code lengths for Char&Len Set.\r
-\r
-Arguments:\r
-\r
-  Sd    - the global scratch data\r
-\r
-Returns: (VOID)\r
-\r
---*/\r
 {\r
   UINT16  Number;\r
   UINT16  CharC;\r
-  volatile UINT16  Index;\r
+  UINT16  Index;\r
   UINT32  Mask;\r
 \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
 \r
-    for (Index = 0; Index < NC; Index++) {\r
-      Sd->mCLen[Index] = 0;\r
-    }\r
+    SetMem (Sd->mCLen, NC, 0);\r
 \r
     for (Index = 0; Index < 4096; Index++) {\r
       Sd->mCTable[Index] = CharC;\r
@@ -408,8 +417,7 @@ Returns: (VOID)
   }\r
 \r
   Index = 0;\r
-  while (Index < Number) {\r
-\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
@@ -441,7 +449,7 @@ Returns: (VOID)
         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
@@ -452,34 +460,29 @@ Returns: (VOID)
     }\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
   return ;\r
 }\r
 \r
-UINT16\r
-DecodeC (\r
-  SCRATCH_DATA  *Sd\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
   Decode a character/length value.\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
 \r
-Arguments:\r
-\r
-  Sd    - The global scratch data.\r
-\r
-Returns:\r
+  @param  Sd The global scratch data.\r
 \r
-  The value decoded.\r
+  @return The value decoded.\r
 \r
---*/\r
+**/\r
+UINT16\r
+DecodeC (\r
+  SCRATCH_DATA  *Sd\r
+  )\r
 {\r
   UINT16  Index2;\r
   UINT32  Mask;\r
@@ -487,21 +490,38 @@ Returns:
   if (Sd->mBlockSize == 0) {\r
     //\r
     // Starting a new block\r
-    //\r
+    // Read BlockSize from block header\r
+    // \r
     Sd->mBlockSize    = (UINT16) GetBits (Sd, 16);\r
+\r
+    //\r
+    // Read in the Extra Set Code Length Arrary,\r
+    // Generate the Huffman code mapping table for Extra Set.\r
+    //\r
     Sd->mBadTableFlag = ReadPTLen (Sd, NT, TBIT, 3);\r
     if (Sd->mBadTableFlag != 0) {\r
       return 0;\r
     }\r
 \r
+    //\r
+    // Read in and decode the Char&Len Set Code Length Arrary,\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
+    // Generate the Huffman code mapping table for the Position Set.\r
+    //\r
     Sd->mBadTableFlag = ReadPTLen (Sd, MAXNP, Sd->mPBit, (UINT16) (-1));\r
     if (Sd->mBadTableFlag != 0) {\r
       return 0;\r
     }\r
   }\r
 \r
+  //\r
+  // Get one code according to Code&Set Huffman Table\r
+  //\r
   Sd->mBlockSize--;\r
   Index2 = Sd->mCTable[Sd->mBitBuf >> (BITBUFSIZ - 12)];\r
 \r
@@ -509,7 +529,7 @@ Returns:
     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
@@ -526,23 +546,15 @@ Returns:
   return Index2;\r
 }\r
 \r
+/**\r
+  Decode the source data and put the resulting data into the destination buffer.\r
+  \r
+  @param  Sd The global scratch data\r
+**/\r
 VOID\r
 Decode (\r
   SCRATCH_DATA  *Sd\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Decode the source data and put the resulting data into the destination buffer.\r
-\r
-Arguments:\r
-\r
-  Sd            - The global scratch data\r
-\r
-Returns: (VOID)\r
-\r
- --*/\r
 {\r
   UINT16  BytesRemain;\r
   UINT32  DataIdx;\r
@@ -553,9 +565,12 @@ Returns: (VOID)
   DataIdx     = 0;\r
 \r
   for (;;) {\r
+    //\r
+    // Get one code from mBitBuf\r
+    // \r
     CharC = DecodeC (Sd);\r
     if (Sd->mBadTableFlag != 0) {\r
-      goto Done ;\r
+      goto Done;\r
     }\r
 \r
     if (CharC < 256) {\r
@@ -563,8 +578,11 @@ Returns: (VOID)
       // Process an Original character\r
       //\r
       if (Sd->mOutBuf >= Sd->mOrigSize) {\r
-        goto Done ;\r
+        goto Done;\r
       } else {\r
+        //\r
+        // Write orignal character into mDstBase\r
+        //\r
         Sd->mDstBase[Sd->mOutBuf++] = (UINT8) CharC;\r
       }\r
 \r
@@ -572,12 +590,21 @@ Returns: (VOID)
       //\r
       // Process a Pointer\r
       //\r
-      CharC       = (UINT16) (CharC - (UINT8_MAX + 1 - THRESHOLD));\r
-\r
+      CharC       = (UINT16) (CharC - (BIT8 - THRESHOLD));\r
\r
+      //\r
+      // Get string length\r
+      //\r
       BytesRemain = CharC;\r
 \r
+      //\r
+      // Locate string position\r
+      //\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
@@ -594,6 +621,45 @@ Done:
   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
+  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
+  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
+  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
+  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
+\r
+  If Source is NULL, then ASSERT().\r
+  If DestinationSize is NULL, then ASSERT().\r
+  If ScratchSize is NULL, then ASSERT().\r
+\r
+  @param  Source          The source buffer containing the compressed data.\r
+  @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
+  @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 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
 UefiDecompressGetInfo (\r
@@ -602,25 +668,6 @@ UefiDecompressGetInfo (
   OUT UINT32      *DestinationSize,\r
   OUT UINT32      *ScratchSize\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().\r
-\r
-Arguments:\r
-\r
-  Source          - The source buffer containing the compressed data.\r
-  SourceSize      - The size of source buffer\r
-  DestinationSize - The size of destination buffer.\r
-  ScratchSize     - The size of scratch buffer.\r
-\r
-Returns:\r
-\r
-  RETURN_SUCCESS           - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
-  RETURN_INVALID_PARAMETER - The source data is corrupted\r
-\r
---*/\r
 {\r
   UINT32  CompressedSize;\r
 \r
@@ -628,22 +675,51 @@ Returns:
   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 by EFI or Tiano algorithm.\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
+  @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
+                          (not in a valid compressed format).\r
+**/\r
 RETURN_STATUS\r
 EFIAPI\r
 UefiTianoDecompress (\r
@@ -652,27 +728,7 @@ UefiTianoDecompress (
   IN OUT VOID    *Scratch,\r
   IN UINT32      Version\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r
-\r
-Arguments:\r
-\r
-  Source          - The source buffer containing the compressed data.\r
-  Destination     - The destination buffer to store the decompressed data\r
-  Scratch         - The buffer used internally by the decompress routine. This  buffer is needed to store intermediate data.\r
-  Version         - 1 for UEFI Decompress algoruthm, 2 for Tiano Decompess algorithm\r
-\r
-Returns:\r
-\r
-  RETURN_SUCCESS           - Decompression is successfull\r
-  RETURN_INVALID_PARAMETER - The source data is corrupted\r
-\r
---*/\r
 {\r
-  volatile UINT32  Index;\r
   UINT32           CompSize;\r
   UINT32           OrigSize;\r
   SCRATCH_DATA     *Sd;\r
@@ -700,12 +756,11 @@ Returns:
 \r
   Src = Src + 8;\r
 \r
-  for (Index = 0; Index < sizeof (SCRATCH_DATA); Index++) {\r
-    ((UINT8 *) Sd)[Index] = 0;\r
-  }\r
+  SetMem (Sd, sizeof (SCRATCH_DATA), 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 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
@@ -720,6 +775,9 @@ Returns:
   }\r
   Sd->mSrcBase  = (UINT8 *)Src;\r
   Sd->mDstBase  = Dst;\r
+  //\r
+  // CompSize and OrigSize are caculated in bytes\r
+  //\r
   Sd->mCompSize = CompSize;\r
   Sd->mOrigSize = OrigSize;\r
 \r
@@ -743,91 +801,197 @@ Returns:
   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\r
+  IN OUT VOID    *Scratch  OPTIONAL\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r
+{\r
+  return UefiTianoDecompress (Source, Destination, Scratch, 1);\r
+}\r
 \r
-Arguments:\r
+/**\r
+  Examines a GUIDed section and returns the size of the decoded buffer and the\r
+  size of an optional scratch buffer required to actually decode the data in a GUIDed section.\r
+\r
+  Examines a GUIDed section specified by InputSection.  \r
+  If GUID for InputSection does not match the GUID that this handler supports,\r
+  then RETURN_UNSUPPORTED is returned.  \r
+  If the required information can not be retrieved from InputSection,\r
+  then RETURN_INVALID_PARAMETER is returned.\r
+  If the GUID of InputSection does match the GUID that this handler supports,\r
+  then the size required to hold the decoded buffer is returned in OututBufferSize,\r
+  the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field\r
+  from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.\r
+  \r
+  If InputSection is NULL, then ASSERT().\r
+  If OutputBufferSize is NULL, then ASSERT().\r
+  If ScratchBufferSize is NULL, then ASSERT().\r
+  If SectionAttribute is NULL, then ASSERT().\r
 \r
-  Source          - The source buffer containing the compressed data.\r
-  Destination     - The destination buffer to store the decompressed data\r
-  Scratch         - The buffer used internally by the decompress routine. This  buffer is needed to store intermediate data.\r
 \r
-Returns:\r
+  @param[in]  InputSection       A pointer to a GUIDed section of an FFS formatted file.\r
+  @param[out] OutputBufferSize   A pointer to the size, in bytes, of an output buffer required\r
+                                 if the buffer specified by InputSection were decoded.\r
+  @param[out] ScratchBufferSize  A pointer to the size, in bytes, required as scratch space\r
+                                 if the buffer specified by InputSection were decoded.\r
+  @param[out] SectionAttribute   A pointer to the attributes of the GUIDed section. See the Attributes\r
+                                 field of EFI_GUID_DEFINED_SECTION in the PI Specification.\r
 \r
-  RETURN_SUCCESS           - Decompression is successfull\r
-  RETURN_INVALID_PARAMETER - The source data is corrupted\r
-\r
---*/\r
-{\r
-  return UefiTianoDecompress (Source, Destination, Scratch, 1);\r
-}\r
+  @retval  RETURN_SUCCESS            The information about InputSection was returned.\r
+  @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.\r
+  @retval  RETURN_INVALID_PARAMETER  The information can not be retrieved from the section specified by InputSection.\r
 \r
+**/\r
 RETURN_STATUS\r
 EFIAPI\r
-CustomDecompressGetInfo (\r
-  IN  CONST VOID  *Source,\r
-  IN  UINT32      SourceSize,\r
-  OUT UINT32      *DestinationSize,\r
-  OUT UINT32      *ScratchSize\r
+TianoDecompressGetInfo (\r
+  IN  CONST VOID  *InputSection,\r
+  OUT UINT32      *OutputBufferSize,\r
+  OUT UINT32      *ScratchBufferSize,\r
+  OUT UINT16      *SectionAttribute\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().\r
-\r
-Arguments:\r
 \r
-  Source          - The source buffer containing the compressed data.\r
-  SourceSize      - The size of source buffer\r
-  DestinationSize - The size of destination buffer.\r
-  ScratchSize     - The size of scratch buffer.\r
+{\r
+  ASSERT (SectionAttribute != NULL);\r
 \r
-Returns:\r
+  if (InputSection == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
 \r
-  RETURN_SUCCESS           - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
-  RETURN_INVALID_PARAMETER - The source data is corrupted\r
+  if (!CompareGuid (\r
+        &gTianoCustomDecompressGuid, \r
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Get guid attribute of guid section. \r
+  //\r
+  *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
 \r
---*/\r
-{\r
-  return UefiDecompressGetInfo (Source, SourceSize, DestinationSize, ScratchSize);\r
+  //\r
+  // Call Tiano GetInfo to get the required size info.\r
+  //\r
+  return UefiDecompressGetInfo (\r
+          (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+          (*(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+          OutputBufferSize,\r
+          ScratchBufferSize\r
+         );\r
 }\r
 \r
+/**\r
+  Decompress a Tiano compressed GUIDed section into a caller allocated output buffer.\r
+  \r
+  Decodes the GUIDed section specified by InputSection.  \r
+  If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.  \r
+  If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.\r
+  If the GUID of InputSection does match the GUID that this handler supports, then InputSection\r
+  is decoded into the buffer specified by OutputBuffer and the authentication status of this\r
+  decode operation is returned in AuthenticationStatus.  If the decoded buffer is identical to the\r
+  data in InputSection, then OutputBuffer is set to point at the data in InputSection.  Otherwise,\r
+  the decoded data will be placed in caller allocated buffer specified by OutputBuffer.\r
+  \r
+  If InputSection is NULL, then ASSERT().\r
+  If OutputBuffer is NULL, then ASSERT().\r
+  If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().\r
+  If AuthenticationStatus is NULL, then ASSERT().\r
+\r
+\r
+  @param[in]  InputSection  A pointer to a GUIDed section of an FFS formatted file.\r
+  @param[out] OutputBuffer  A pointer to a buffer that contains the result of a decode operation. \r
+  @param[in] ScratchBuffer  A caller allocated buffer that may be required by this function\r
+                            as a scratch buffer to perform the decode operation. \r
+  @param[out] AuthenticationStatus \r
+                            A pointer to the authentication status of the decoded output buffer.\r
+                            See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI\r
+                            section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must\r
+                            never be set by this handler.\r
+\r
+  @retval  RETURN_SUCCESS            The buffer specified by InputSection was decoded.\r
+  @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.\r
+  @retval  RETURN_INVALID_PARAMETER  The section specified by InputSection can not be decoded.\r
+\r
+**/\r
 RETURN_STATUS\r
 EFIAPI\r
-CustomDecompress (\r
-  IN CONST VOID  *Source,\r
-  IN OUT VOID    *Destination,\r
-  IN OUT VOID    *Scratch\r
+TianoDecompress (\r
+  IN CONST  VOID    *InputSection,\r
+  OUT       VOID    **OutputBuffer,\r
+  IN        VOID    *ScratchBuffer,        OPTIONAL\r
+  OUT       UINT32  *AuthenticationStatus\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r
-\r
-Arguments:\r
+{\r
+  ASSERT (OutputBuffer != NULL);\r
+  ASSERT (InputSection != NULL);\r
 \r
-  Source          - The source buffer containing the compressed data.\r
-  Destination     - The destination buffer to store the decompressed data\r
-  Scratch         - The buffer used internally by the decompress routine. This  buffer is needed to store intermediate data.\r
+  if (!CompareGuid (\r
+        &gTianoCustomDecompressGuid, \r
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
 \r
-Returns:\r
+  //\r
+  // Set Authentication to Zero.\r
+  //\r
+  *AuthenticationStatus = 0;\r
+  \r
+  //\r
+  // Call Tiano Decompress to get the raw data\r
+  //\r
+  return UefiTianoDecompress (\r
+          (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+          *OutputBuffer,\r
+          ScratchBuffer,\r
+          2\r
+         );\r
+}\r
 \r
-  RETURN_SUCCESS           - Decompression is successfull\r
-  RETURN_INVALID_PARAMETER - The source data is corrupted\r
+/**\r
+  Registers TianoDecompress and TianoDecompressGetInfo handlers with TianoCustomerDecompressGuid\r
 \r
---*/\r
+  @retval  RETURN_SUCCESS            Register successfully.\r
+  @retval  RETURN_OUT_OF_RESOURCES   No enough memory to store this handler.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+TianoDecompressLibConstructor (\r
+)\r
 {\r
-  return UefiTianoDecompress (Source, Destination, Scratch, 2);\r
+  return ExtractGuidedSectionRegisterHandlers (\r
+          &gTianoCustomDecompressGuid,\r
+          TianoDecompressGetInfo,\r
+          TianoDecompress\r
+          );      \r
 }\r