mirror of
https://github.com/LongSoft/UEFITool.git
synced 2024-11-25 09:28:22 +08:00
Fix and reindent LZMA
This commit is contained in:
parent
d87cbe3210
commit
bbdfe28449
@ -25,8 +25,8 @@ static ISzAlloc SzAllocForLzma = { &AllocForLzma, &FreeForLzma };
|
|||||||
|
|
||||||
SRes OnProgress(void *p, UInt64 inSize, UInt64 outSize)
|
SRes OnProgress(void *p, UInt64 inSize, UInt64 outSize)
|
||||||
{
|
{
|
||||||
(void)p; (void)inSize; (void)outSize;
|
(void)p; (void)inSize; (void)outSize;
|
||||||
return SZ_OK;
|
return SZ_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ICompressProgress g_ProgressCallback = { &OnProgress };
|
static ICompressProgress g_ProgressCallback = { &OnProgress };
|
||||||
@ -34,77 +34,77 @@ static ICompressProgress g_ProgressCallback = { &OnProgress };
|
|||||||
STATIC
|
STATIC
|
||||||
UINT64
|
UINT64
|
||||||
EFIAPI
|
EFIAPI
|
||||||
RShiftU64(
|
RShiftU64 (
|
||||||
UINT64 Operand,
|
UINT64 Operand,
|
||||||
UINT32 Count
|
UINT32 Count
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return Operand >> Count;
|
return Operand >> Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
VOID
|
VOID
|
||||||
SetEncodedSizeOfBuf(
|
SetEncodedSizeOfBuf(
|
||||||
UINT64 EncodedSize,
|
UINT64 EncodedSize,
|
||||||
UINT8* EncodedData
|
UINT8* EncodedData
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
INT32 Index;
|
INT32 Index;
|
||||||
|
|
||||||
EncodedData[LZMA_PROPS_SIZE] = EncodedSize & 0xFF;
|
EncodedData[LZMA_PROPS_SIZE] = EncodedSize & 0xFF;
|
||||||
for (Index = LZMA_PROPS_SIZE + 1; Index <= LZMA_PROPS_SIZE + 7; Index++)
|
for (Index = LZMA_PROPS_SIZE + 1; Index <= LZMA_PROPS_SIZE + 7; Index++)
|
||||||
{
|
{
|
||||||
EncodedSize = RShiftU64(EncodedSize, 8);
|
EncodedSize = RShiftU64(EncodedSize, 8);
|
||||||
EncodedData[Index] = EncodedSize & 0xFF;
|
EncodedData[Index] = EncodedSize & 0xFF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
INT32
|
USTATUS
|
||||||
EFIAPI
|
EFIAPI
|
||||||
LzmaCompress(
|
LzmaCompress (
|
||||||
CONST UINT8 *Source,
|
CONST UINT8 *Source,
|
||||||
UINTN SourceSize,
|
UINT32 SourceSize,
|
||||||
UINT8 *Destination,
|
UINT8 *Destination,
|
||||||
UINTN *DestinationSize
|
UINT32 *DestinationSize
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
SRes LzmaResult;
|
SRes LzmaResult;
|
||||||
CLzmaEncProps props;
|
CLzmaEncProps props;
|
||||||
SizeT propsSize = LZMA_PROPS_SIZE;
|
SizeT propsSize = LZMA_PROPS_SIZE;
|
||||||
SizeT destLen = SourceSize + SourceSize / 3 + 128;
|
SizeT destLen = SourceSize + SourceSize / 3 + 128;
|
||||||
|
|
||||||
if (*DestinationSize < destLen)
|
if (*DestinationSize < (UINT32)destLen)
|
||||||
{
|
{
|
||||||
*DestinationSize = (UINTN)destLen;
|
*DestinationSize = (UINT32)destLen;
|
||||||
return EFI_BUFFER_TOO_SMALL;
|
return EFI_BUFFER_TOO_SMALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
LzmaEncProps_Init(&props);
|
LzmaEncProps_Init(&props);
|
||||||
// TOOD: need to detect this instead of hardcoding
|
// TODO: need to detect this instead of hardcoding
|
||||||
props.dictSize = LZMA_DICTIONARY_SIZE;
|
props.dictSize = LZMA_DICTIONARY_SIZE;
|
||||||
props.level = 9;
|
props.level = 9;
|
||||||
props.fb = 273;
|
props.fb = 273;
|
||||||
|
|
||||||
LzmaResult = LzmaEncode(
|
LzmaResult = LzmaEncode(
|
||||||
(Byte*)((UINT8*)Destination + LZMA_HEADER_SIZE),
|
(Byte*)((UINT8*)Destination + LZMA_HEADER_SIZE),
|
||||||
&destLen,
|
&destLen,
|
||||||
Source,
|
Source,
|
||||||
SourceSize,
|
(SizeT)SourceSize,
|
||||||
&props,
|
&props,
|
||||||
(UINT8*)Destination,
|
(UINT8*)Destination,
|
||||||
&propsSize,
|
&propsSize,
|
||||||
props.writeEndMark,
|
props.writeEndMark,
|
||||||
&g_ProgressCallback,
|
&g_ProgressCallback,
|
||||||
&SzAllocForLzma,
|
&SzAllocForLzma,
|
||||||
&SzAllocForLzma);
|
&SzAllocForLzma);
|
||||||
|
|
||||||
*DestinationSize = destLen + LZMA_HEADER_SIZE;
|
*DestinationSize = (UINT32)(destLen + LZMA_HEADER_SIZE);
|
||||||
|
|
||||||
SetEncodedSizeOfBuf(SourceSize, Destination);
|
SetEncodedSizeOfBuf(SourceSize, Destination);
|
||||||
|
|
||||||
if (LzmaResult == SZ_OK) {
|
if (LzmaResult == SZ_OK) {
|
||||||
return EFI_SUCCESS;
|
return EFI_SUCCESS;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return EFI_INVALID_PARAMETER;
|
return EFI_INVALID_PARAMETER;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,14 +24,14 @@ extern "C" {
|
|||||||
#define LZMA_DICTIONARY_SIZE 0x800000
|
#define LZMA_DICTIONARY_SIZE 0x800000
|
||||||
#define _LZMA_SIZE_OPT
|
#define _LZMA_SIZE_OPT
|
||||||
|
|
||||||
INT32
|
USTATUS
|
||||||
EFIAPI
|
EFIAPI
|
||||||
LzmaCompress(
|
LzmaCompress (
|
||||||
const UINT8 *Source,
|
const UINT8 *Source,
|
||||||
UINTN SourceSize,
|
UINT32 SourceSize,
|
||||||
UINT8 *Destination,
|
UINT8 *Destination,
|
||||||
UINTN *DestinationSize
|
UINT32 *DestinationSize
|
||||||
);
|
);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -19,10 +19,10 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|||||||
|
|
||||||
UINT64
|
UINT64
|
||||||
EFIAPI
|
EFIAPI
|
||||||
LShiftU64(
|
LShiftU64 (
|
||||||
UINT64 Operand,
|
UINT64 Operand,
|
||||||
UINT32 Count
|
UINT32 Count
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return Operand << Count;
|
return Operand << Count;
|
||||||
}
|
}
|
||||||
@ -39,12 +39,12 @@ Get the size of the uncompressed buffer by parsing EncodeData header.
|
|||||||
@return The size of the uncompressed buffer.
|
@return The size of the uncompressed buffer.
|
||||||
*/
|
*/
|
||||||
UINT64
|
UINT64
|
||||||
GetDecodedSizeOfBuf(
|
GetDecodedSizeOfBuf (
|
||||||
UINT8 *EncodedData
|
UINT8 *EncodedData
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
UINT64 DecodedSize;
|
UINT64 DecodedSize;
|
||||||
INT32 Index;
|
INT32 Index;
|
||||||
|
|
||||||
// Parse header
|
// Parse header
|
||||||
DecodedSize = 0;
|
DecodedSize = 0;
|
||||||
@ -85,18 +85,27 @@ DestinationSize and the size of the scratch
|
|||||||
buffer was returned ScratchSize.
|
buffer was returned ScratchSize.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
INT32
|
USTATUS
|
||||||
EFIAPI
|
EFIAPI
|
||||||
LzmaGetInfo(
|
LzmaGetInfo (
|
||||||
CONST VOID *Source,
|
CONST VOID *Source,
|
||||||
UINTN SourceSize,
|
UINT32 SourceSize,
|
||||||
UINTN *DestinationSize
|
UINT32 *DestinationSize
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
ASSERT(SourceSize >= LZMA_HEADER_SIZE); (void)SourceSize;
|
UINT64 DecodedSize;
|
||||||
|
ASSERT(SourceSize >= LZMA_HEADER_SIZE);
|
||||||
|
(void)SourceSize;
|
||||||
|
|
||||||
*DestinationSize = (UINTN)GetDecodedSizeOfBuf((UINT8*)Source);
|
DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);
|
||||||
return U_SUCCESS;
|
|
||||||
|
if (DecodedSize <= UINT32_MAX) {
|
||||||
|
*DestinationSize = (UINT32)DecodedSize;
|
||||||
|
return U_SUCCESS;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return U_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -118,13 +127,13 @@ the uncompressed buffer is returned Destination.
|
|||||||
The source buffer specified by Source is corrupted
|
The source buffer specified by Source is corrupted
|
||||||
(not a valid compressed format).
|
(not a valid compressed format).
|
||||||
*/
|
*/
|
||||||
INT32
|
USTATUS
|
||||||
EFIAPI
|
EFIAPI
|
||||||
LzmaDecompress(
|
LzmaDecompress (
|
||||||
CONST VOID *Source,
|
CONST VOID *Source,
|
||||||
UINTN SourceSize,
|
UINT32 SourceSize,
|
||||||
VOID *Destination
|
VOID *Destination
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
SRes LzmaResult;
|
SRes LzmaResult;
|
||||||
ELzmaStatus Status;
|
ELzmaStatus Status;
|
||||||
|
@ -23,13 +23,6 @@ extern "C" {
|
|||||||
|
|
||||||
#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
|
#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
|
||||||
|
|
||||||
UINT64
|
|
||||||
EFIAPI
|
|
||||||
LShiftU64(
|
|
||||||
UINT64 Operand,
|
|
||||||
UINT32 Count
|
|
||||||
);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Given a Lzma compressed source buffer, this function retrieves the size of
|
Given a Lzma compressed source buffer, this function retrieves the size of
|
||||||
the uncompressed buffer and the size of the scratch buffer required
|
the uncompressed buffer and the size of the scratch buffer required
|
||||||
@ -57,12 +50,12 @@ extern "C" {
|
|||||||
buffer was returned ScratchSize.
|
buffer was returned ScratchSize.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
INT32
|
USTATUS
|
||||||
EFIAPI
|
EFIAPI
|
||||||
LzmaGetInfo(
|
LzmaGetInfo (
|
||||||
const VOID *Source,
|
CONST VOID *Source,
|
||||||
UINT32 SourceSize,
|
UINT32 SourceSize,
|
||||||
UINT32 *DestinationSize
|
UINT32 *DestinationSize
|
||||||
);
|
);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -84,12 +77,12 @@ extern "C" {
|
|||||||
The source buffer specified by Source is corrupted
|
The source buffer specified by Source is corrupted
|
||||||
(not a valid compressed format).
|
(not a valid compressed format).
|
||||||
*/
|
*/
|
||||||
INT32
|
USTATUS
|
||||||
EFIAPI
|
EFIAPI
|
||||||
LzmaDecompress(
|
LzmaDecompress (
|
||||||
const VOID *Source,
|
CONST VOID *Source,
|
||||||
UINT32 SourceSize,
|
UINT32 SourceSize,
|
||||||
VOID *Destination
|
VOID *Destination
|
||||||
);
|
);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
Loading…
Reference in New Issue
Block a user