/** @file
UEFI and Tiano Custom Decompress Library
It will do Tiano or UEFI decompress with different verison parameter.
Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
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
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "BaseUefiTianoCustomDecompressLibInternals.h"
/**
Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.
@param Sd The global scratch data
@param NumOfBits The number of bits to shift and read.
**/
FillBuf (
)
{
//
// Left shift NumOfBits of bits in advance
//
//
// Copy data needed in bytes into mSbuBitBuf
//
//
// Get 1 byte into SubBitBuf
//
Sd->mSubBitBuf = 0;
} else {
//
// No more bits from the source, just pad zero bit.
//
Sd->mSubBitBuf = 0;
}
}
//
// Caculate additional bit count read to update mBitCount
//
//
// Copy NumOfBits of bits from mSubBitBuf into mBitBuf
//
}
/**
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
popped out.
@param Sd The global scratch data.
@param NumOfBits The number of bits to pop and read.
@return The bits that are popped out.
**/
GetBits (
)
{
//
// Pop NumOfBits of Bits from Left
//
//
// Fill up mBitBuf from source
//
return OutBits;
}
/**
Creates Huffman Code mapping table according to code length array.
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.
@retval 0 OK.
@retval BAD_TABLE The table is corrupted.
**/
)
{
//
// The maximum mapping table width supported by this internal
// working function is 16.
//
}
}
Start[0] = 0;
Start[1] = 0;
}
if (Start[17] != 0) {
/*(1U << 16)*/
}
Weight[0] = 0;
}
while (Index <= 16) {
Index++;
}
if (Index != 0) {
}
}
continue;
}
}
} else {
while (Index != 0) {
}
} else {
}
}
Index3 <<= 1;
Index--;
}
}
}
//
// Succeeds
//
return 0;
}
/**
Decodes a position value.
Get a position value according to Position Huffman Table.
@param Sd the global scratch data
@return The position value decoded.
**/
DecodeP (
)
{
do {
} else {
}
Mask >>= 1;
}
//
// Advance what we have read
//
if (Val > 1) {
}
return Pos;
}
/**
Reads code lengths for the Extra Set or the Position Set.
Read in the Extra Set or Pointion Set Length Arrary, 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 Special The special symbol that needs to be taken care of.
@retval 0 OK.
@retval BAD_TABLE Table is corrupted.
**/
)
{
//
// Read Extra Set Code Length Array size
//
if (Number == 0) {
//
// This represents only Huffman code used
//
}
return 0;
}
Index = 0;
//
// 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
// terminating "0". The number of "1"s = Code length - 4.
//
if (CharC == 7) {
Mask >>= 1;
CharC += 1;
}
}
//
// 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
// zero lengths after the third length.
//
}
}
}
}
}
/**
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
**/
ReadCLen (
)
{
if (Number == 0) {
//
// This represents only Huffman code used
//
}
return ;
}
Index = 0;
do {
} else {
}
Mask >>= 1;
}
//
// Advance what we have read
//
if (CharC <= 2) {
if (CharC == 0) {
CharC = 1;
} else if (CharC == 1) {
} else if (CharC == 2) {
}
}
} else {
}
}
return ;
}
/**
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.
@param Sd The global scratch data.
@return The value decoded.
**/
DecodeC (
)
{
if (Sd->mBlockSize == 0) {
//
// Starting a new block
// Read BlockSize from block header
//
//
// Read in the Extra Set Code Length Arrary,
// Generate the Huffman code mapping table for Extra Set.
//
if (Sd->mBadTableFlag != 0) {
return 0;
}
//
// Read in and decode the Char&Len Set Code Length Arrary,
// Generate the Huffman code mapping table for Char&Len Set.
//
//
// Read in the Position Set Code Length Arrary,
// Generate the Huffman code mapping table for the Position Set.
//
if (Sd->mBadTableFlag != 0) {
return 0;
}
}
//
// Get one code according to Code&Set Huffman Table
//
Sd->mBlockSize--;
do {
} else {
}
Mask >>= 1;
}
//
// Advance what we have read
//
return Index2;
}
/**
Decode the source data and put the resulting data into the destination buffer.
@param Sd The global scratch data
**/
Decode (
)
{
DataIdx = 0;
for (;;) {
//
// Get one code from mBitBuf
//
if (Sd->mBadTableFlag != 0) {
goto Done;
}
if (CharC < 256) {
//
// Process an Original character
//
goto Done;
} else {
//
// Write orignal character into mDstBase
//
}
} else {
//
// Process a Pointer
//
//
// Get string length
//
BytesRemain = CharC;
//
// Locate string position
//
//
// Write BytesRemain of bytes into mDstBase
//
BytesRemain--;
while ((INT16) (BytesRemain) >= 0) {
goto Done ;
}
BytesRemain--;
}
}
}
Done:
return ;
}
/**
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
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,
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
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.
If Source is NULL, then ASSERT().
If DestinationSize is NULL, then ASSERT().
If ScratchSize is NULL, then ASSERT().
@param Source The source buffer containing the compressed data.
@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..
@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
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
and SourceSize.
**/
)
{
if (SourceSize < 8) {
return RETURN_INVALID_PARAMETER;
}
return RETURN_INVALID_PARAMETER;
}
*ScratchSize = sizeof (SCRATCH_DATA);
return RETURN_SUCCESS;
}
/**
Decompresses a compressed source buffer by EFI or Tiano algorithm.
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
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.
If Source is NULL, then ASSERT().
If Destination is NULL, then ASSERT().
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 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.
@param Version 1 for UEFI Decompress algoruthm, 2 for Tiano Decompess algorithm.
@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
(not in a valid compressed format).
**/
)
{
Dst = Destination;
//
// If compressed file size is 0, return
//
if (OrigSize == 0) {
return RETURN_SUCCESS;
}
//
// 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
// For Tiano de/compression algorithm(Version 2), mPBit = 5
//
switch (Version) {
case 1 :
break;
case 2 :
break;
default:
}
//
// CompSize and OrigSize are caculated in bytes
//
//
// Fill the first BITBUFSIZ bits
//
//
// Decompress it
//
if (Sd->mBadTableFlag != 0) {
//
// Something wrong with the source
//
return RETURN_INVALID_PARAMETER;
}
return RETURN_SUCCESS;
}
/**
Decompresses a UEFI compressed source buffer.
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
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.
If Source is NULL, then ASSERT().
If Destination is NULL, then ASSERT().
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 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.
@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
(not in a valid compressed format).
**/
)
{
}
/**
Examines a GUIDed section and returns the size of the decoded buffer and the
size of an optional scratch buffer required to actually decode the data in a GUIDed section.
Examines a GUIDed section specified by InputSection.
If GUID for InputSection does not match the GUID that this handler supports,
then RETURN_UNSUPPORTED is returned.
If the required information can not be retrieved from InputSection,
then RETURN_INVALID_PARAMETER is returned.
If the GUID of InputSection does match the GUID that this handler supports,
then the size required to hold the decoded buffer is returned in OututBufferSize,
the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field
from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.
If InputSection is NULL, then ASSERT().
If OutputBufferSize is NULL, then ASSERT().
If ScratchBufferSize is NULL, then ASSERT().
If SectionAttribute is NULL, then ASSERT().
@param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
@param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required
if the buffer specified by InputSection were decoded.
@param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space
if the buffer specified by InputSection were decoded.
@param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes
field of EFI_GUID_DEFINED_SECTION in the PI Specification.
@retval RETURN_SUCCESS The information about InputSection was returned.
@retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
@retval RETURN_INVALID_PARAMETER The information can not be retrieved from the section specified by InputSection.
**/
)
{
if (InputSection == NULL) {
return RETURN_INVALID_PARAMETER;
}
if (IS_SECTION2 (InputSection)) {
if (!CompareGuid (
return RETURN_INVALID_PARAMETER;
}
//
// Get guid attribute of guid section.
//
//
// Call Tiano GetInfo to get the required size info.
//
return UefiDecompressGetInfo (
);
} else {
if (!CompareGuid (
return RETURN_INVALID_PARAMETER;
}
//
// Get guid attribute of guid section.
//
//
// Call Tiano GetInfo to get the required size info.
//
return UefiDecompressGetInfo (
);
}
}
/**
Decompress a Tiano compressed GUIDed section into a caller allocated output buffer.
Decodes the GUIDed section specified by InputSection.
If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.
If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.
If the GUID of InputSection does match the GUID that this handler supports, then InputSection
is decoded into the buffer specified by OutputBuffer and the authentication status of this
decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the
data in InputSection, then OutputBuffer is set to point at the data in InputSection. Otherwise,
the decoded data will be placed in caller allocated buffer specified by OutputBuffer.
If InputSection is NULL, then ASSERT().
If OutputBuffer is NULL, then ASSERT().
If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
If AuthenticationStatus is NULL, then ASSERT().
@param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
@param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
@param[in] ScratchBuffer A caller allocated buffer that may be required by this function
as a scratch buffer to perform the decode operation.
@param[out] AuthenticationStatus
A pointer to the authentication status of the decoded output buffer.
See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must
never be set by this handler.
@retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
@retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
@retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
**/
)
{
if (IS_SECTION2 (InputSection)) {
if (!CompareGuid (
return RETURN_INVALID_PARAMETER;
}
//
// Set Authentication to Zero.
//
*AuthenticationStatus = 0;
//
// Call Tiano Decompress to get the raw data
//
return UefiTianoDecompress (
2
);
} else {
if (!CompareGuid (
return RETURN_INVALID_PARAMETER;
}
//
// Set Authentication to Zero.
//
*AuthenticationStatus = 0;
//
// Call Tiano Decompress to get the raw data
//
return UefiTianoDecompress (
2
);
}
}
/**
Registers TianoDecompress and TianoDecompressGetInfo handlers with TianoCustomerDecompressGuid
@retval RETURN_SUCCESS Register successfully.
@retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler.
**/
)
{
return ExtractGuidedSectionRegisterHandlers (
);
}