mirror of
https://github.com/LongSoft/UEFITool.git
synced 2025-06-03 08:38:48 -04:00
Backport decompression updates from new_engine
This commit is contained in:
parent
b3f7beb236
commit
f507d71ead
11 changed files with 3296 additions and 3312 deletions
|
@ -25,8 +25,8 @@ static ISzAlloc SzAllocForLzma = { &AllocForLzma, &FreeForLzma };
|
|||
|
||||
SRes OnProgress(void *p, UInt64 inSize, UInt64 outSize)
|
||||
{
|
||||
(void)p; (void)inSize; (void)outSize;
|
||||
return SZ_OK;
|
||||
(void)p; (void)inSize; (void)outSize;
|
||||
return SZ_OK;
|
||||
}
|
||||
|
||||
static ICompressProgress g_ProgressCallback = { &OnProgress };
|
||||
|
@ -35,75 +35,76 @@ STATIC
|
|||
UINT64
|
||||
EFIAPI
|
||||
RShiftU64 (
|
||||
UINT64 Operand,
|
||||
UINT32 Count
|
||||
UINT64 Operand,
|
||||
UINT32 Count
|
||||
)
|
||||
{
|
||||
return Operand >> Count;
|
||||
return Operand >> Count;
|
||||
}
|
||||
|
||||
VOID
|
||||
SetEncodedSizeOfBuf (
|
||||
UINT64 EncodedSize,
|
||||
UINT8 *EncodedData
|
||||
SetEncodedSizeOfBuf(
|
||||
UINT64 EncodedSize,
|
||||
UINT8* EncodedData
|
||||
)
|
||||
{
|
||||
INT32 Index;
|
||||
INT32 Index;
|
||||
|
||||
EncodedData[LZMA_PROPS_SIZE] = EncodedSize & 0xFF;
|
||||
for (Index = LZMA_PROPS_SIZE + 1; Index <= LZMA_PROPS_SIZE + 7; Index++)
|
||||
{
|
||||
EncodedSize = RShiftU64(EncodedSize, 8);
|
||||
EncodedData[Index] = EncodedSize & 0xFF;
|
||||
}
|
||||
EncodedData[LZMA_PROPS_SIZE] = EncodedSize & 0xFF;
|
||||
for (Index = LZMA_PROPS_SIZE + 1; Index <= LZMA_PROPS_SIZE + 7; Index++)
|
||||
{
|
||||
EncodedSize = RShiftU64(EncodedSize, 8);
|
||||
EncodedData[Index] = EncodedSize & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
INT32
|
||||
EFI_STATUS
|
||||
EFIAPI
|
||||
LzmaCompress(
|
||||
CONST UINT8 *Source,
|
||||
UINTN SourceSize,
|
||||
UINT8 *Destination,
|
||||
UINTN *DestinationSize
|
||||
LzmaCompress (
|
||||
CONST UINT8 *Source,
|
||||
UINT32 SourceSize,
|
||||
UINT8 *Destination,
|
||||
UINT32 *DestinationSize
|
||||
)
|
||||
{
|
||||
SRes LzmaResult;
|
||||
CLzmaEncProps props;
|
||||
SizeT propsSize = LZMA_PROPS_SIZE;
|
||||
SizeT destLen = SourceSize + SourceSize / 3 + 128;
|
||||
SRes LzmaResult;
|
||||
CLzmaEncProps props;
|
||||
SizeT propsSize = LZMA_PROPS_SIZE;
|
||||
SizeT destLen = SourceSize + SourceSize / 3 + 128;
|
||||
|
||||
if (*DestinationSize < destLen)
|
||||
{
|
||||
*DestinationSize = (UINTN)destLen;
|
||||
return ERR_BUFFER_TOO_SMALL;
|
||||
}
|
||||
if (*DestinationSize < (UINT32)destLen)
|
||||
{
|
||||
*DestinationSize = (UINT32)destLen;
|
||||
return EFI_BUFFER_TOO_SMALL;
|
||||
}
|
||||
|
||||
LzmaEncProps_Init(&props);
|
||||
props.dictSize = LZMA_DICTIONARY_SIZE;
|
||||
props.level = 9;
|
||||
props.fb = 273;
|
||||
LzmaEncProps_Init(&props);
|
||||
// TODO: need to detect this instead of hardcoding
|
||||
props.dictSize = LZMA_DICTIONARY_SIZE;
|
||||
props.level = 9;
|
||||
props.fb = 273;
|
||||
|
||||
LzmaResult = LzmaEncode(
|
||||
(Byte*)((UINT8*)Destination + LZMA_HEADER_SIZE),
|
||||
&destLen,
|
||||
Source,
|
||||
SourceSize,
|
||||
&props,
|
||||
(UINT8*)Destination,
|
||||
&propsSize,
|
||||
props.writeEndMark,
|
||||
&g_ProgressCallback,
|
||||
&SzAllocForLzma,
|
||||
&SzAllocForLzma);
|
||||
LzmaResult = LzmaEncode(
|
||||
(Byte*)((UINT8*)Destination + LZMA_HEADER_SIZE),
|
||||
&destLen,
|
||||
Source,
|
||||
(SizeT)SourceSize,
|
||||
&props,
|
||||
(UINT8*)Destination,
|
||||
&propsSize,
|
||||
props.writeEndMark,
|
||||
&g_ProgressCallback,
|
||||
&SzAllocForLzma,
|
||||
&SzAllocForLzma);
|
||||
|
||||
*DestinationSize = destLen + LZMA_HEADER_SIZE;
|
||||
*DestinationSize = (UINT32)(destLen + LZMA_HEADER_SIZE);
|
||||
|
||||
SetEncodedSizeOfBuf(SourceSize, Destination);
|
||||
SetEncodedSizeOfBuf(SourceSize, Destination);
|
||||
|
||||
if (LzmaResult == SZ_OK) {
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
else {
|
||||
return ERR_INVALID_PARAMETER;
|
||||
}
|
||||
if (LzmaResult == SZ_OK) {
|
||||
return EFI_SUCCESS;
|
||||
}
|
||||
else {
|
||||
return EFI_INVALID_PARAMETER;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,8 +11,8 @@
|
|||
|
||||
*/
|
||||
|
||||
#ifndef __LZMACOMPRESS_H__
|
||||
#define __LZMACOMPRESS_H__
|
||||
#ifndef LZMACOMPRESS_H
|
||||
#define LZMACOMPRESS_H
|
||||
|
||||
#include "SDK/C/Types.h"
|
||||
#include "../basetypes.h"
|
||||
|
@ -24,16 +24,17 @@ extern "C" {
|
|||
#define LZMA_DICTIONARY_SIZE 0x800000
|
||||
#define _LZMA_SIZE_OPT
|
||||
|
||||
INT32
|
||||
EFI_STATUS
|
||||
EFIAPI
|
||||
LzmaCompress(
|
||||
const UINT8 *Source,
|
||||
UINTN SourceSize,
|
||||
UINT8 *Destination,
|
||||
UINTN *DestinationSize
|
||||
LzmaCompress (
|
||||
const UINT8 *Source,
|
||||
UINT32 SourceSize,
|
||||
UINT8 *Destination,
|
||||
UINT32 *DestinationSize
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // LZMACOMPRESS_H
|
||||
|
|
|
@ -19,12 +19,12 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|||
|
||||
UINT64
|
||||
EFIAPI
|
||||
LShiftU64(
|
||||
UINT64 Operand,
|
||||
UINT32 Count
|
||||
)
|
||||
LShiftU64 (
|
||||
UINT64 Operand,
|
||||
UINT32 Count
|
||||
)
|
||||
{
|
||||
return Operand << Count;
|
||||
return Operand << Count;
|
||||
}
|
||||
|
||||
static void * AllocForLzma(void *p, size_t size) { (void)p; return malloc(size); }
|
||||
|
@ -39,19 +39,19 @@ Get the size of the uncompressed buffer by parsing EncodeData header.
|
|||
@return The size of the uncompressed buffer.
|
||||
*/
|
||||
UINT64
|
||||
GetDecodedSizeOfBuf(
|
||||
UINT8 *EncodedData
|
||||
)
|
||||
GetDecodedSizeOfBuf (
|
||||
UINT8 *EncodedData
|
||||
)
|
||||
{
|
||||
UINT64 DecodedSize;
|
||||
INT32 Index;
|
||||
UINT64 DecodedSize;
|
||||
INT32 Index;
|
||||
|
||||
// Parse header
|
||||
DecodedSize = 0;
|
||||
for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)
|
||||
DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];
|
||||
// Parse header
|
||||
DecodedSize = 0;
|
||||
for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)
|
||||
DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];
|
||||
|
||||
return DecodedSize;
|
||||
return DecodedSize;
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -85,18 +85,27 @@ DestinationSize and the size of the scratch
|
|||
buffer was returned ScratchSize.
|
||||
|
||||
*/
|
||||
INT32
|
||||
EFI_STATUS
|
||||
EFIAPI
|
||||
LzmaGetInfo(
|
||||
CONST VOID *Source,
|
||||
UINTN SourceSize,
|
||||
UINTN *DestinationSize
|
||||
)
|
||||
LzmaGetInfo (
|
||||
CONST VOID *Source,
|
||||
UINT32 SourceSize,
|
||||
UINT32 *DestinationSize
|
||||
)
|
||||
{
|
||||
ASSERT(SourceSize >= LZMA_HEADER_SIZE); (void)SourceSize;
|
||||
UINT64 DecodedSize;
|
||||
ASSERT(SourceSize >= LZMA_HEADER_SIZE);
|
||||
(void)SourceSize;
|
||||
|
||||
*DestinationSize = (UINTN)GetDecodedSizeOfBuf((UINT8*)Source);
|
||||
return ERR_SUCCESS;
|
||||
DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);
|
||||
|
||||
if (DecodedSize <= UINT32_MAX) {
|
||||
*DestinationSize = (UINT32)DecodedSize;
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
else {
|
||||
return ERR_INVALID_PARAMETER;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -118,38 +127,38 @@ the uncompressed buffer is returned Destination.
|
|||
The source buffer specified by Source is corrupted
|
||||
(not a valid compressed format).
|
||||
*/
|
||||
INT32
|
||||
EFI_STATUS
|
||||
EFIAPI
|
||||
LzmaDecompress(
|
||||
CONST VOID *Source,
|
||||
UINTN SourceSize,
|
||||
VOID *Destination
|
||||
)
|
||||
LzmaDecompress (
|
||||
CONST VOID *Source,
|
||||
UINT32 SourceSize,
|
||||
VOID *Destination
|
||||
)
|
||||
{
|
||||
SRes LzmaResult;
|
||||
ELzmaStatus Status;
|
||||
SizeT DecodedBufSize;
|
||||
SizeT EncodedDataSize;
|
||||
SRes LzmaResult;
|
||||
ELzmaStatus Status;
|
||||
SizeT DecodedBufSize;
|
||||
SizeT EncodedDataSize;
|
||||
|
||||
DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);
|
||||
EncodedDataSize = (SizeT)(SourceSize - LZMA_HEADER_SIZE);
|
||||
DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);
|
||||
EncodedDataSize = (SizeT)(SourceSize - LZMA_HEADER_SIZE);
|
||||
|
||||
LzmaResult = LzmaDecode(
|
||||
(Byte*)Destination,
|
||||
&DecodedBufSize,
|
||||
(Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),
|
||||
&EncodedDataSize,
|
||||
(CONST Byte*) Source,
|
||||
LZMA_PROPS_SIZE,
|
||||
LZMA_FINISH_END,
|
||||
&Status,
|
||||
&SzAllocForLzma
|
||||
);
|
||||
LzmaResult = LzmaDecode(
|
||||
(Byte*)Destination,
|
||||
&DecodedBufSize,
|
||||
(Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),
|
||||
&EncodedDataSize,
|
||||
(CONST Byte*) Source,
|
||||
LZMA_PROPS_SIZE,
|
||||
LZMA_FINISH_END,
|
||||
&Status,
|
||||
&SzAllocForLzma
|
||||
);
|
||||
|
||||
if (LzmaResult == SZ_OK) {
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
else {
|
||||
return ERR_INVALID_PARAMETER;
|
||||
}
|
||||
if (LzmaResult == SZ_OK) {
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
else {
|
||||
return ERR_INVALID_PARAMETER;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,8 +11,8 @@
|
|||
|
||||
*/
|
||||
|
||||
#ifndef __LZMADECOMPRESS_H__
|
||||
#define __LZMADECOMPRESS_H__
|
||||
#ifndef LZMADECOMPRESS_H
|
||||
#define LZMADECOMPRESS_H
|
||||
|
||||
#include "../basetypes.h"
|
||||
#include "SDK/C/LzmaDec.h"
|
||||
|
@ -23,13 +23,6 @@ extern "C" {
|
|||
|
||||
#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
|
||||
the uncompressed buffer and the size of the scratch buffer required
|
||||
|
@ -57,12 +50,12 @@ extern "C" {
|
|||
buffer was returned ScratchSize.
|
||||
|
||||
*/
|
||||
INT32
|
||||
EFI_STATUS
|
||||
EFIAPI
|
||||
LzmaGetInfo(
|
||||
const VOID *Source,
|
||||
UINTN SourceSize,
|
||||
UINTN *DestinationSize
|
||||
LzmaGetInfo (
|
||||
CONST VOID *Source,
|
||||
UINT32 SourceSize,
|
||||
UINT32 *DestinationSize
|
||||
);
|
||||
|
||||
/*
|
||||
|
@ -84,15 +77,16 @@ extern "C" {
|
|||
The source buffer specified by Source is corrupted
|
||||
(not a valid compressed format).
|
||||
*/
|
||||
INT32
|
||||
EFI_STATUS
|
||||
EFIAPI
|
||||
LzmaDecompress(
|
||||
const VOID *Source,
|
||||
UINTN SourceSize,
|
||||
VOID *Destination
|
||||
LzmaDecompress (
|
||||
CONST VOID *Source,
|
||||
UINT32 SourceSize,
|
||||
VOID *Destination
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // LZMADECOMPRESS_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue