X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FBaseUefiDecompressLib%2FBaseUefiDecompressLib.c;h=3d5b7a737ad6fa88d56d418b54536a582ec6b143;hb=8028f03032182f2c72e7699e1d14322bb5586581;hp=98492c88f8102d4c713d96a48bfffbaaea8cc9c7;hpb=6d6a1098d998e3b7cf6ef7fceee301d3065e86f6;p=mirror_edk2.git diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c index 98492c88f8..3d5b7a737a 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 - 2018, 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. **/ @@ -40,14 +40,14 @@ FillBuf ( // // Left shift NumOfBits of bits in advance // - Sd->mBitBuf = (UINT32) (Sd->mBitBuf << NumOfBits); + Sd->mBitBuf = (UINT32) LShiftU64 (((UINT64)Sd->mBitBuf), NumOfBits); // // Copy data needed in bytes into mSbuBitBuf // while (NumOfBits > Sd->mBitCount) { - - Sd->mBitBuf |= (UINT32) (Sd->mSubBitBuf << (NumOfBits = (UINT16) (NumOfBits - Sd->mBitCount))); + NumOfBits = (UINT16) (NumOfBits - Sd->mBitCount); + Sd->mBitBuf |= (UINT32) LShiftU64 (((UINT64)Sd->mSubBitBuf), NumOfBits); if (Sd->mCompSize > 0) { // @@ -68,10 +68,10 @@ FillBuf ( } // - // Caculate additional bit count read to update mBitCount + // Calculate additional bit count read to update mBitCount // Sd->mBitCount = (UINT16) (Sd->mBitCount - NumOfBits); - + // // Copy NumOfBits of bits from mSubBitBuf into mBitBuf // @@ -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. @@ -101,7 +101,7 @@ GetBits ( // // Pop NumOfBits of Bits from Left - // + // OutBits = (UINT32) (Sd->mBitBuf >> (BITBUFSIZ - NumOfBits)); // @@ -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; @@ -151,16 +152,26 @@ MakeTable ( UINT16 Mask; UINT16 WordOfStart; UINT16 WordOfCount; + UINT16 MaxTableLength; + // + // 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++) { + if (BitLen[Index] > 16) { + return (UINT16) BAD_TABLE; + } Count[BitLen[Index]]++; } + Start[0] = 0; Start[1] = 0; for (Index = 1; Index <= 16; Index++) { @@ -176,6 +187,7 @@ MakeTable ( JuBits = (UINT16) (16 - TableBits); + Weight[0] = 0; for (Index = 1; Index <= TableBits; Index++) { Start[Index] >>= JuBits; Weight[Index] = (UINT16) (1U << (TableBits - Index)); @@ -183,25 +195,26 @@ MakeTable ( while (Index <= 16) { Weight[Index] = (UINT16) (1U << (16 - Index)); - Index++; + Index++; } Index = (UINT16) (Start[TableBits + 1] >> JuBits); if (Index != 0) { Index3 = (UINT16) (1U << TableBits); - while (Index != Index3) { - Table[Index++] = 0; + if (Index < Index3) { + SetMem16 (Table + Index, (Index3 - Index) * sizeof (*Table), 0); } } Avail = NumOfChar; Mask = (UINT16) (1U << (15 - TableBits)); + MaxTableLength = (UINT16) (1U << TableBits); for (Char = 0; Char < NumOfChar; Char++) { Len = BitLen[Char]; - if (Len == 0) { + if (Len == 0 || Len >= 17) { continue; } @@ -209,6 +222,10 @@ MakeTable ( if (Len <= TableBits) { + if (Start[Len] >= NextCode || NextCode > MaxTableLength){ + return (UINT16) BAD_TABLE; + } + for (Index = Start[Len]; Index < NextCode; Index++) { Table[Index] = Char; } @@ -220,15 +237,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 +270,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. @@ -298,12 +317,12 @@ DecodeP ( /** Reads code lengths for the Extra Set or the Position Set. - Read in the Extra Set or Pointion Set Length Arrary, then + Read in the Extra Set or Position Set Length Array, then 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,11 +339,12 @@ ReadPTLen ( { UINT16 Number; UINT16 CharC; - volatile UINT16 Index; + UINT16 Index; UINT32 Mask; + ASSERT (nn <= NPT); // - // Read Extra Set Code Length Array size + // Read Extra Set Code Length Array size // Number = (UINT16) GetBits (Sd, nbit); @@ -334,26 +354,22 @@ 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)); // // If a code length is less than 7, then it is encoded as a 3-bit - // value. Or it is encoded as a series of "1"s followed by a + // value. Or it is encoded as a series of "1"s followed by a // terminating "0". The number of "1"s = Code length - 4. // if (CharC == 7) { @@ -363,39 +379,39 @@ ReadPTLen ( CharC += 1; } } - + FillBuf (Sd, (UINT16) ((CharC < 7) ? 3 : CharC - 3)); Sd->mPTLen[Index++] = (UINT8) CharC; - + // - // For Code&Len Set, + // For Code&Len Set, // After the third length of the code length concatenation, - // a 2-bit value is used to indicated the number of consecutive + // a 2-bit value is used to indicated the number of consecutive // zero lengths after the third length. // 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; } - + return MakeTable (Sd, nn, Sd->mPTLen, 8, Sd->mPTTable); } /** 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 +421,7 @@ ReadCLen ( { UINT16 Number; UINT16 CharC; - volatile UINT16 Index; + UINT16 Index; UINT32 Mask; Number = (UINT16) GetBits (Sd, CBIT); @@ -416,19 +432,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 +471,7 @@ ReadCLen ( CharC = (UINT16) (GetBits (Sd, CBIT) + 20); } - while ((INT16) (--CharC) >= 0) { + while ((INT16) (--CharC) >= 0 && Index < NC) { Sd->mCLen[Index++] = 0; } @@ -471,9 +482,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 +491,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. @@ -504,11 +513,11 @@ DecodeC ( // // Starting a new block // Read BlockSize from block header - // + // Sd->mBlockSize = (UINT16) GetBits (Sd, 16); // - // Read in the Extra Set Code Length Arrary, + // Read in the Extra Set Code Length Array, // Generate the Huffman code mapping table for Extra Set. // Sd->mBadTableFlag = ReadPTLen (Sd, NT, TBIT, 3); @@ -517,13 +526,13 @@ DecodeC ( } // - // Read in and decode the Char&Len Set Code Length Arrary, + // Read in and decode the Char&Len Set Code Length Array, // Generate the Huffman code mapping table for Char&Len Set. // ReadCLen (Sd); // - // Read in the Position Set Code Length Arrary, + // Read in the Position Set Code Length Array, // Generate the Huffman code mapping table for the Position Set. // Sd->mBadTableFlag = ReadPTLen (Sd, MAXNP, Sd->mPBit, (UINT16) (-1)); @@ -561,8 +570,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 @@ -581,7 +590,7 @@ Decode ( for (;;) { // // Get one code from mBitBuf - // + // CharC = DecodeC (Sd); if (Sd->mBadTableFlag != 0) { goto Done; @@ -604,8 +613,8 @@ Decode ( // // Process a Pointer // - CharC = (UINT16) (CharC - (UINT8_MAX + 1 - THRESHOLD)); - + CharC = (UINT16) (CharC - (BIT8 - THRESHOLD)); + // // Get string length // @@ -620,14 +629,25 @@ Decode ( // Write BytesRemain of bytes into mDstBase // BytesRemain--; + while ((INT16) (BytesRemain) >= 0) { - Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++]; if (Sd->mOutBuf >= Sd->mOrigSize) { goto Done; } + if (DataIdx >= Sd->mOrigSize) { + Sd->mBadTableFlag = (UINT16) BAD_TABLE; + goto Done; + } + Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++]; BytesRemain--; } + // + // Once mOutBuf is fully filled, directly return + // + if (Sd->mOutBuf >= Sd->mOrigSize) { + goto Done; + } } } @@ -636,18 +656,18 @@ Done: } /** - Given a compressed source buffer, this function retrieves the size of - the uncompressed buffer and the size of the scratch buffer required + Given a compressed source buffer, this function retrieves the size of + the uncompressed buffer and the size of the scratch buffer required to decompress the compressed source buffer. - Retrieves the size of the uncompressed buffer and the temporary scratch buffer + Retrieves the size of the uncompressed buffer and the temporary scratch buffer required to decompress the buffer specified by Source and SourceSize. If the size of the uncompressed buffer or the size of the scratch buffer cannot - be determined from the compressed data specified by Source and SourceData, + be determined from the compressed data specified by Source and SourceData, then RETURN_INVALID_PARAMETER is returned. Otherwise, the size of the uncompressed buffer is returned in DestinationSize, the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned. - This function does not have scratch buffer available to perform a thorough + This function does not have scratch buffer available to perform a thorough checking of the validity of the source data. It just retrieves the "Original Size" field from the beginning bytes of the source data and output it as DestinationSize. And ScratchSize is specific to the decompression implementation. @@ -660,18 +680,18 @@ 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 + 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 + @retval RETURN_SUCCESS The size of the uncompressed data was returned + 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 - the scratch buffer cannot be determined from - the compressed data specified by Source + @retval RETURN_INVALID_PARAMETER + The size of the uncompressed data or the size of + the scratch buffer cannot be determined from + the compressed data specified by Source and SourceSize. **/ RETURN_STATUS @@ -694,7 +714,7 @@ UefiDecompressGetInfo ( } CompressedSize = ReadUnaligned32 ((UINT32 *)Source); - if (SourceSize < (CompressedSize + 8)) { + if (SourceSize < (CompressedSize + 8) || (CompressedSize + 8) < 8) { return RETURN_INVALID_PARAMETER; } @@ -710,10 +730,10 @@ 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 - into Destination, then RETURN_SUCCESS is returned. If the compressed source data + 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,15 +742,15 @@ 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 + This is an optional parameter that may be NULL if the required scratch buffer size is 0. - - @retval RETURN_SUCCESS Decompression completed successfully, and + + @retval RETURN_SUCCESS Decompression completed successfully, and the uncompressed buffer is returned in Destination. - @retval RETURN_INVALID_PARAMETER - The source buffer specified by Source is corrupted + @retval RETURN_INVALID_PARAMETER + The source buffer specified by Source is corrupted (not in a valid compressed format). **/ RETURN_STATUS @@ -741,7 +761,6 @@ UefiDecompress ( IN OUT VOID *Scratch OPTIONAL ) { - volatile UINT32 Index; UINT32 CompSize; UINT32 OrigSize; SCRATCH_DATA *Sd; @@ -768,10 +787,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 +797,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;