/** @file\r
UEFI Decompress Library implementation refer to UEFI specification.\r
\r
- Copyright (c) 2006 - 2008, Intel Corporation\r
- All rights reserved. This program and the accompanying materials\r
+ Copyright (c) 2006 - 2008, 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
\r
#include <Base.h>\r
-\r
-\r
#include <Library/BaseLib.h>\r
-#include <Library/UefiDecompressLib.h>\r
#include <Library/DebugLib.h>\r
#include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiDecompressLib.h>\r
\r
#include "BaseUefiDecompressLibInternals.h"\r
\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
+ 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
/**\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
\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
+ @param Table The table to be created.\r
\r
@retval 0 OK.\r
@retval BAD_TABLE The table is corrupted.\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
UINT16 WordOfCount;\r
\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
}\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
\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
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
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) != 0) {\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
Decodes a position value.\r
\r
Get a position value according to Position Huffman Table.\r
- \r
+\r
@param Sd the global scratch data\r
\r
@return The position value decoded.\r
{\r
UINT16 Number;\r
UINT16 CharC;\r
- volatile UINT16 Index;\r
+ UINT16 Index;\r
UINT32 Mask;\r
\r
//\r
//\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
//\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
/**\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
{\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
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
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
}\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
\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
\r
/**\r
Decode the source data and put the resulting data into the destination buffer.\r
- \r
+\r
@param Sd The global scratch data\r
\r
**/\r
//\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
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
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
}\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 UEFI 2.0 de/compression algorithm(Version 1), mPBit = 4\r