Backport decompression updates from new_engine

This commit is contained in:
vit9696 2018-05-08 19:22:07 +03:00
parent b3f7beb236
commit f507d71ead
11 changed files with 3296 additions and 3312 deletions

View file

@ -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;
}
}

View file

@ -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

View file

@ -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;
}
}

View file

@ -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

File diff suppressed because it is too large Load diff

View file

@ -20,8 +20,8 @@ Header file for compression routine.
*/
#ifndef _EFITIANOCOMPRESS_H_
#define _EFITIANOCOMPRESS_H_
#ifndef EFITIANOCOMPRESS_H
#define EFITIANOCOMPRESS_H
#include <string.h>
#include <stdlib.h>
@ -32,84 +32,87 @@ Header file for compression routine.
extern "C" {
#endif
/*++
/*++
Routine Description:
Routine Description:
Tiano compression routine.
Tiano compression routine.
Arguments:
Arguments:
SrcBuffer - The buffer storing the source data
SrcSize - The size of source data
DstBuffer - The buffer to store the compressed data
DstSize - On input, the size of DstBuffer; On output,
the size of the actual compressed data.
SrcBuffer - The buffer storing the source data
SrcSize - The size of source data
DstBuffer - The buffer to store the compressed data
DstSize - On input, the size of DstBuffer; On output,
the size of the actual compressed data.
Returns:
Returns:
EFI_BUFFER_TOO_SMALL - The DstBuffer is too small. this case,
DstSize contains the size needed.
EFI_SUCCESS - Compression is successful.
EFI_OUT_OF_RESOURCES - No resource to complete function.
EFI_INVALID_PARAMETER - Parameter supplied is wrong.
EFI_BUFFER_TOO_SMALL - The DstBuffer is too small. this case,
DstSize contains the size needed.
EFI_SUCCESS - Compression is successful.
EFI_OUT_OF_RESOURCES - No resource to complete function.
EFI_INVALID_PARAMETER - Parameter supplied is wrong.
--*/
EFI_STATUS
TianoCompress(
IN CONST VOID *SrcBuffer,
IN UINT32 SrcSize,
IN VOID *DstBuffer,
IN OUT UINT32 *DstSize
);
--*/
EFI_STATUS
TianoCompress(
CONST VOID *SrcBuffer,
UINT32 SrcSize,
VOID *DstBuffer,
UINT32 *DstSize
)
;
EFI_STATUS
TianoCompressLegacy(
CONST VOID *SrcBuffer,
UINT32 SrcSize,
VOID *DstBuffer,
UINT32 *DstSize
);
/*++
EFI_STATUS
TianoCompressLegacy(
CONST VOID *SrcBuffer,
UINT32 SrcSize,
VOID *DstBuffer,
UINT32 *DstSize
)
;
/*++
Routine Description:
Routine Description:
EFI 1.1 compression routine.
EFI 1.1 compression routine.
Arguments:
Arguments:
SrcBuffer - The buffer storing the source data
SrcSize - The size of source data
DstBuffer - The buffer to store the compressed data
DstSize - On input, the size of DstBuffer; On output,
the size of the actual compressed data.
SrcBuffer - The buffer storing the source data
SrcSize - The size of source data
DstBuffer - The buffer to store the compressed data
DstSize - On input, the size of DstBuffer; On output,
the size of the actual compressed data.
Returns:
Returns:
EFI_BUFFER_TOO_SMALL - The DstBuffer is too small. this case,
DstSize contains the size needed.
EFI_SUCCESS - Compression is successful.
EFI_OUT_OF_RESOURCES - No resource to complete function.
EFI_INVALID_PARAMETER - Parameter supplied is wrong.
EFI_BUFFER_TOO_SMALL - The DstBuffer is too small. this case,
DstSize contains the size needed.
EFI_SUCCESS - Compression is successful.
EFI_OUT_OF_RESOURCES - No resource to complete function.
EFI_INVALID_PARAMETER - Parameter supplied is wrong.
--*/
EFI_STATUS
EfiCompress(
IN CONST VOID *SrcBuffer,
IN UINT32 SrcSize,
IN VOID *DstBuffer,
IN OUT UINT32 *DstSize
);
EFI_STATUS
EfiCompressLegacy(
CONST VOID *SrcBuffer,
UINT32 SrcSize,
VOID *DstBuffer,
UINT32 *DstSize
);
--*/
EFI_STATUS
EfiCompress(
CONST VOID *SrcBuffer,
UINT32 SrcSize,
VOID *DstBuffer,
UINT32 *DstSize
)
;
EFI_STATUS
EfiCompressLegacy(
CONST VOID *SrcBuffer,
UINT32 SrcSize,
VOID *DstBuffer,
UINT32 *DstSize
)
;
#ifdef __cplusplus
}
#endif
#endif
#endif // EFITIANOCOMPRESS_H

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -21,8 +21,8 @@ Providing both EFI and Tiano decompress algorithms.
--*/
#ifndef _EFITIANODECOMPRESS_H_
#define _EFITIANODECOMPRESS_H_
#ifndef EFITIANODECOMPRESS_H
#define EFITIANODECOMPRESS_H
#include <string.h>
#include <stdlib.h>
@ -32,109 +32,107 @@ Providing both EFI and Tiano decompress algorithms.
extern "C" {
#endif
typedef struct {
UINT32 CompSize;
UINT32 OrigSize;
} EFI_TIANO_HEADER;
typedef struct EFI_TIANO_HEADER_ {
UINT32 CompSize;
UINT32 OrigSize;
} EFI_TIANO_HEADER;
EFI_STATUS
EfiTianoGetInfo(
IN const VOID *Source,
IN UINT32 SrcSize,
OUT UINT32 *DstSize,
OUT UINT32 *ScratchSize
)
/*++
EFI_STATUS
EFIAPI
EfiTianoGetInfo(
const VOID *Source,
UINT32 SrcSize,
UINT32 *DstSize,
UINT32 *ScratchSize
)
/*++
Routine Description:
Routine Description:
The implementation is same as that of EFI_DECOMPRESS_PROTOCOL.GetInfo().
The implementation is same as that of EFI_DECOMPRESS_PROTOCOL.GetInfo().
Arguments:
Arguments:
This - The protocol instance pointer
Source - The source buffer containing the compressed data.
SrcSize - The size of source buffer
DstSize - The size of destination buffer.
ScratchSize - The size of scratch buffer.
Source - The source buffer containing the compressed data.
SrcSize - The size of source buffer
DstSize - The size of destination buffer.
ScratchSize - The size of scratch buffer.
Returns:
Returns:
EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successfully retrieved.
EFI_INVALID_PARAMETER - The source data is corrupted
EFI_SUCCESS - The size of destination buffer and the size of scratch buffer are successfully retrieved.
EFI_INVALID_PARAMETER - The source data is corrupted
--*/
;
--*/
;
EFI_STATUS
EFIAPI
EfiDecompress(
IN const VOID *Source,
IN UINT32 SrcSize,
IN OUT VOID *Destination,
IN UINT32 DstSize,
IN OUT VOID *Scratch,
IN UINT32 ScratchSize
)
/*++
EFI_STATUS
EFIAPI
EfiDecompress(
const VOID *Source,
UINT32 SrcSize,
VOID *Destination,
UINT32 DstSize,
VOID *Scratch,
UINT32 ScratchSize
);
/*++
Routine Description:
Routine Description:
The implementation is same as that of EFI_DECOMPRESS_PROTOCOL.Decompress().
The implementation is same as that of EFI_DECOMPRESS_PROTOCOL.Decompress().
Arguments:
Arguments:
This - The protocol instance pointer
Source - The source buffer containing the compressed data.
SrcSize - The size of source buffer
Destination - The destination buffer to store the decompressed data
DstSize - The size of destination buffer.
Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
ScratchSize - The size of scratch buffer.
Source - The source buffer containing the compressed data.
SrcSize - The size of source buffer
Destination - The destination buffer to store the decompressed data
DstSize - The size of destination buffer.
Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
ScratchSize - The size of scratch buffer.
Returns:
Returns:
EFI_SUCCESS - Decompression is successful
EFI_INVALID_PARAMETER - The source data is corrupted
EFI_SUCCESS - Decompression is successful
EFI_INVALID_PARAMETER - The source data is corrupted
--*/
;
--*/
;
EFI_STATUS
EFIAPI
TianoDecompress(
IN const VOID *Source,
IN UINT32 SrcSize,
IN OUT VOID *Destination,
IN UINT32 DstSize,
IN OUT VOID *Scratch,
IN UINT32 ScratchSize
)
/*++
EFI_STATUS
EFIAPI
TianoDecompress(
const VOID *Source,
UINT32 SrcSize,
VOID *Destination,
UINT32 DstSize,
VOID *Scratch,
UINT32 ScratchSize
)
/*++
Routine Description:
Routine Description:
The implementation is same as that of EFI_TIANO_DECOMPRESS_PROTOCOL.Decompress().
The implementation is same as that of EFI_TIANO_DECOMPRESS_PROTOCOL.Decompress().
Arguments:
Arguments:
This - The protocol instance pointer
Source - The source buffer containing the compressed data.
SrcSize - The size of source buffer
Destination - The destination buffer to store the decompressed data
DstSize - The size of destination buffer.
Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
ScratchSize - The size of scratch buffer.
Source - The source buffer containing the compressed data.
SrcSize - The size of source buffer
Destination - The destination buffer to store the decompressed data
DstSize - The size of destination buffer.
Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
ScratchSize - The size of scratch buffer.
Returns:
Returns:
EFI_SUCCESS - Decompression is successful
EFI_INVALID_PARAMETER - The source data is corrupted
EFI_SUCCESS - Decompression is successful
EFI_INVALID_PARAMETER - The source data is corrupted
--*/
;
--*/
;
#ifdef __cplusplus
}
#endif
#endif
#endif // EFITIANODECOMPRESS_H

View file

@ -16,6 +16,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
typedef uint8_t BOOLEAN;
typedef int8_t INT8;
@ -28,7 +29,7 @@ typedef int64_t INT64;
typedef uint64_t UINT64;
typedef char CHAR8;
typedef uint16_t CHAR16;
typedef unsigned int UINTN;
typedef size_t UINTN;
#define CONST const
#define VOID void

View file

@ -2741,7 +2741,13 @@ UINT8 FfsEngine::decompress(const QByteArray & compressedData, const UINT8 compr
else if (algorithm)
*algorithm = COMPRESSION_ALGORITHM_TIANO;
decompressedData = QByteArray((const char*)decompressed, decompressedSize);
if (decompressedSize > INT32_MAX) {
delete[] decompressed;
delete[] scratch;
return ERR_STANDARD_DECOMPRESSION_FAILED;
}
decompressedData = QByteArray((const char*)decompressed, (int)decompressedSize);
delete[] decompressed;
delete[] scratch;
@ -2778,7 +2784,8 @@ UINT8 FfsEngine::decompress(const QByteArray & compressedData, const UINT8 compr
}
// Decompress section data again
if (ERR_SUCCESS != LzmaDecompress(data, dataSize, decompressed)) {
if (ERR_SUCCESS != LzmaDecompress(data, dataSize, decompressed)
|| decompressedSize > INT32_MAX) {
if (algorithm)
*algorithm = COMPRESSION_ALGORITHM_UNKNOWN;
delete[] decompressed;
@ -2787,13 +2794,17 @@ UINT8 FfsEngine::decompress(const QByteArray & compressedData, const UINT8 compr
else {
if (algorithm)
*algorithm = COMPRESSION_ALGORITHM_IMLZMA;
decompressedData = QByteArray((const char*)decompressed, decompressedSize);
decompressedData = QByteArray((const char*)decompressed, (int)decompressedSize);
}
}
else {
if (decompressedSize > INT32_MAX) {
delete[] decompressed;
return ERR_CUSTOMIZED_DECOMPRESSION_FAILED;
}
if (algorithm)
*algorithm = COMPRESSION_ALGORITHM_LZMA;
decompressedData = QByteArray((const char*)decompressed, decompressedSize);
decompressedData = QByteArray((const char*)decompressed, (int)decompressedSize);
}
delete[] decompressed;