X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FBaseUefiDecompressLib%2FBaseUefiDecompressLib.c;h=4b86f5076297bd166992c55277e0d9258f346353;hb=4f953ed72109eeb87cbb11bd534c069f31e8b344;hp=21d35e29da3e71cea487e937f991f6af27297036;hpb=daa6553a39c528210f7afc84f437716b60514c8b;p=mirror_edk2.git diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c index 21d35e29da..4b86f50762 100644 --- a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c +++ b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c @@ -1,11 +1,12 @@ /** @file UEFI Decompress Library implementation refer to UEFI specification. - Copyright (c) 2006 - 2008, Intel Corporation - All rights reserved. This program and the accompanying materials + Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.
+ Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
+ This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at - http://opensource.org/licenses/bsd-license.php + http://opensource.org/licenses/bsd-license.php. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. @@ -14,11 +15,10 @@ #include - - #include -#include #include +#include +#include #include "BaseUefiDecompressLibInternals.h" @@ -27,7 +27,7 @@ Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source. - @param Sd The global scratch data + @param Sd The global scratch data. @param NumOfBits The number of bits to shift and read. **/ @@ -68,7 +68,7 @@ FillBuf ( } // - // Caculate additional bit count read to update mBitCount + // Calculate additional bit count read to update mBitCount // Sd->mBitCount = (UINT16) (Sd->mBitCount - NumOfBits); @@ -81,8 +81,8 @@ FillBuf ( /** Get NumOfBits of bits out from mBitBuf. - Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent - NumOfBits of bits from source. Returns NumOfBits of bits that are + Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent + NumOfBits of bits from source. Returns NumOfBits of bits that are popped out. @param Sd The global scratch data. @@ -115,14 +115,15 @@ GetBits ( /** Creates Huffman Code mapping table according to code length array. - Creates Huffman Code mapping table for Extra Set, Char&Len Set + Creates Huffman Code mapping table for Extra Set, Char&Len Set and Position Set according to code length array. + If TableBits > 16, then ASSERT (). - @param Sd The global scratch data - @param NumOfChar Number of symbols in the symbol set - @param BitLen Code length array - @param TableBits The width of the mapping table - @param Table The table to be created + @param Sd The global scratch data. + @param NumOfChar The number of symbols in the symbol set. + @param BitLen Code length array. + @param TableBits The width of the mapping table. + @param Table The table to be created. @retval 0 OK. @retval BAD_TABLE The table is corrupted. @@ -142,7 +143,7 @@ MakeTable ( UINT16 Start[18]; UINT16 *Pointer; UINT16 Index3; - volatile UINT16 Index; + UINT16 Index; UINT16 Len; UINT16 Char; UINT16 JuBits; @@ -152,15 +153,21 @@ MakeTable ( UINT16 WordOfStart; UINT16 WordOfCount; + // + // The maximum mapping table width supported by this internal + // working function is 16. + // + ASSERT (TableBits <= 16); - for (Index = 1; Index <= 16; Index++) { + for (Index = 0; Index <= 16; Index++) { Count[Index] = 0; } for (Index = 0; Index < NumOfChar; Index++) { Count[BitLen[Index]]++; } - + + Start[0] = 0; Start[1] = 0; for (Index = 1; Index <= 16; Index++) { @@ -175,7 +182,8 @@ MakeTable ( } JuBits = (UINT16) (16 - TableBits); - + + Weight[0] = 0; for (Index = 1; Index <= TableBits; Index++) { Start[Index] >>= JuBits; Weight[Index] = (UINT16) (1U << (TableBits - Index)); @@ -190,8 +198,8 @@ MakeTable ( if (Index != 0) { Index3 = (UINT16) (1U << TableBits); - while (Index != Index3) { - Table[Index++] = 0; + if (Index < Index3) { + SetMem16 (Table + Index, (Index3 - Index) * sizeof (*Table), 0); } } @@ -201,7 +209,7 @@ MakeTable ( for (Char = 0; Char < NumOfChar; Char++) { Len = BitLen[Char]; - if (Len == 0) { + if (Len == 0 || Len >= 17) { continue; } @@ -220,15 +228,17 @@ MakeTable ( Index = (UINT16) (Len - TableBits); while (Index != 0) { - if (*Pointer == 0) { - Sd->mRight[Avail] = Sd->mLeft[Avail] = 0; + if (*Pointer == 0 && Avail < (2 * NC - 1)) { + Sd->mRight[Avail] = Sd->mLeft[Avail] = 0; *Pointer = Avail++; } - - if ((Index3 & Mask) != 0) { - Pointer = &Sd->mRight[*Pointer]; - } else { - Pointer = &Sd->mLeft[*Pointer]; + + if (*Pointer < (2 * NC - 1)) { + if ((Index3 & Mask) != 0) { + Pointer = &Sd->mRight[*Pointer]; + } else { + Pointer = &Sd->mLeft[*Pointer]; + } } Index3 <<= 1; @@ -251,8 +261,8 @@ MakeTable ( Decodes a position value. Get a position value according to Position Huffman Table. - - @param Sd the global scratch data + + @param Sd The global scratch data. @return The position value decoded. @@ -302,8 +312,8 @@ DecodeP ( generate the Huffman code mapping for them. @param Sd The global scratch data. - @param nn Number of symbols. - @param nbit Number of bits needed to represent nn. + @param nn The number of symbols. + @param nbit The number of bits needed to represent nn. @param Special The special symbol that needs to be taken care of. @retval 0 OK. @@ -320,9 +330,10 @@ ReadPTLen ( { UINT16 Number; UINT16 CharC; - volatile UINT16 Index; + UINT16 Index; UINT32 Mask; + ASSERT (nn <= NPT); // // Read Extra Set Code Length Array size // @@ -334,20 +345,16 @@ ReadPTLen ( // CharC = (UINT16) GetBits (Sd, nbit); - for (Index = 0; Index < 256; Index++) { - Sd->mPTTable[Index] = CharC; - } + SetMem16 (&Sd->mPTTable[0] , sizeof (Sd->mPTTable), CharC); - for (Index = 0; Index < nn; Index++) { - Sd->mPTLen[Index] = 0; - } + SetMem (Sd->mPTLen, nn, 0); return 0; } Index = 0; - while (Index < Number) { + while (Index < Number && Index < NPT) { CharC = (UINT16) (Sd->mBitBuf >> (BITBUFSIZ - 3)); @@ -376,13 +383,13 @@ ReadPTLen ( // if (Index == Special) { CharC = (UINT16) GetBits (Sd, 2); - while ((INT16) (--CharC) >= 0) { + while ((INT16) (--CharC) >= 0 && Index < NPT) { Sd->mPTLen[Index++] = 0; } } } - while (Index < nn) { + while (Index < nn && Index < NPT) { Sd->mPTLen[Index++] = 0; } @@ -391,11 +398,11 @@ ReadPTLen ( /** Reads code lengths for Char&Len Set. - + Read in and decode the Char&Len Set Code Length Array, then generate the Huffman Code mapping table for the Char&Len Set. - @param Sd the global scratch data + @param Sd The global scratch data. **/ VOID @@ -405,7 +412,7 @@ ReadCLen ( { UINT16 Number; UINT16 CharC; - volatile UINT16 Index; + UINT16 Index; UINT32 Mask; Number = (UINT16) GetBits (Sd, CBIT); @@ -416,19 +423,14 @@ ReadCLen ( // CharC = (UINT16) GetBits (Sd, CBIT); - for (Index = 0; Index < NC; Index++) { - Sd->mCLen[Index] = 0; - } - - for (Index = 0; Index < 4096; Index++) { - Sd->mCTable[Index] = CharC; - } + SetMem (Sd->mCLen, NC, 0); + SetMem16 (&Sd->mCTable[0], sizeof (Sd->mCTable), CharC); return ; } Index = 0; - while (Index < Number) { + while (Index < Number && Index < NC) { CharC = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)]; if (CharC >= NT) { Mask = 1U << (BITBUFSIZ - 1 - 8); @@ -460,7 +462,7 @@ ReadCLen ( CharC = (UINT16) (GetBits (Sd, CBIT) + 20); } - while ((INT16) (--CharC) >= 0) { + while ((INT16) (--CharC) >= 0 && Index < NC) { Sd->mCLen[Index++] = 0; } @@ -471,9 +473,7 @@ ReadCLen ( } } - while (Index < NC) { - Sd->mCLen[Index++] = 0; - } + SetMem (Sd->mCLen + Index, NC - Index, 0); MakeTable (Sd, NC, Sd->mCLen, 12, Sd->mCTable); @@ -482,7 +482,7 @@ ReadCLen ( /** Decode a character/length value. - + Read one value from mBitBuf, Get one code from mBitBuf. If it is at block boundary, generates Huffman code mapping table for Extra Set, Code&Len Set and Position Set. @@ -561,8 +561,8 @@ DecodeC ( /** Decode the source data and put the resulting data into the destination buffer. - - @param Sd The global scratch data + + @param Sd The global scratch data. **/ VOID @@ -660,13 +660,13 @@ Done: @param SourceSize The size, in bytes, of the source buffer. @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer that will be generated when the compressed buffer specified - by Source and SourceSize is decompressed.. + by Source and SourceSize is decompressed. @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that is required to decompress the compressed buffer specified by Source and SourceSize. @retval RETURN_SUCCESS The size of the uncompressed data was returned - in DestinationSize and the size of the scratch + in DestinationSize, and the size of the scratch buffer was returned in ScratchSize. @retval RETURN_INVALID_PARAMETER The size of the uncompressed data or the size of @@ -710,9 +710,9 @@ UefiDecompressGetInfo ( Extracts decompressed data to its original form. This function is designed so that the decompression algorithm can be implemented without using any memory services. As a result, this function is not allowed to - call any memory allocation services in its implementation. It is the caller's r - esponsibility to allocate and free the Destination and Scratch buffers. - If the compressed source data specified by Source is sucessfully decompressed + call any memory allocation services in its implementation. It is the caller's + responsibility to allocate and free the Destination and Scratch buffers. + If the compressed source data specified by Source is successfully decompressed into Destination, then RETURN_SUCCESS is returned. If the compressed source data specified by Source is not in a valid compressed data format, then RETURN_INVALID_PARAMETER is returned. @@ -722,7 +722,7 @@ UefiDecompressGetInfo ( If the required scratch buffer size > 0 and Scratch is NULL, then ASSERT(). @param Source The source buffer containing the compressed data. - @param Destination The destination buffer to store the decompressed data + @param Destination The destination buffer to store the decompressed data. @param Scratch A temporary scratch buffer that is used to perform the decompression. This is an optional parameter that may be NULL if the required scratch buffer size is 0. @@ -741,7 +741,6 @@ UefiDecompress ( IN OUT VOID *Scratch OPTIONAL ) { - volatile UINT32 Index; UINT32 CompSize; UINT32 OrigSize; SCRATCH_DATA *Sd; @@ -768,10 +767,8 @@ UefiDecompress ( } Src = Src + 8; + SetMem (Sd, sizeof (SCRATCH_DATA), 0); - for (Index = 0; Index < sizeof (SCRATCH_DATA); Index++) { - ((UINT8 *) Sd)[Index] = 0; - } // // The length of the field 'Position Set Code Length Array Size' in Block Header. // For UEFI 2.0 de/compression algorithm(Version 1), mPBit = 4 @@ -780,7 +777,7 @@ UefiDecompress ( Sd->mSrcBase = (UINT8 *)Src; Sd->mDstBase = Dst; // - // CompSize and OrigSize are caculated in bytes + // CompSize and OrigSize are calculated in bytes // Sd->mCompSize = CompSize; Sd->mOrigSize = OrigSize;