Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync LZMA library with UEFITool #125

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions uefi_firmware/compression/LZMA/LzmaCompress.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
/* LZMA Compress Implementation

Copyright (c) 2012, Nikolaj Schlej. All rights reserved.
Expand All @@ -14,97 +12,99 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/

#include "LzmaCompress.h"
//#include "Sdk/C/7zVersion.h"
#include "SDK/C/7zVersion.h"
#include "SDK/C/LzmaEnc.h"

#include <stdlib.h>

#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)

static void * AllocForLzma(void *p, size_t size) { return malloc(size); }
static void FreeForLzma(void *p, void *address) { free(address); }
static void * AllocForLzma(ISzAllocPtr p, size_t size) { (void)p; return malloc(size); }
static void FreeForLzma(ISzAllocPtr p, void *address) { (void)p; free(address); }
static ISzAlloc SzAllocForLzma = { &AllocForLzma, &FreeForLzma };

SRes OnProgress(void *p, UInt64 inSize, UInt64 outSize)
SRes OnProgress(const ICompressProgress *p, UInt64 inSize, UInt64 outSize)
{
(void)p; (void)inSize; (void)outSize;
return SZ_OK;
}

static ICompressProgress g_ProgressCallback = { &OnProgress };

STATIC
UINT64
RShiftU64 (
UINT64 Operand,
UINT32 Count
)
UINT64
EFIAPI
RShiftU64 (
UINT64 Operand,
UINT32 Count
)
{
return Operand >> Count;
}

VOID
SetEncodedSizeOfBuf(
SetEncodedSizeOfBuf(
UINT64 EncodedSize,
UINT8 *EncodedData
)
UINT8* EncodedData
)
{
INT32 Index;

EncodedData[LZMA_PROPS_SIZE] = EncodedSize & 0xFF;
for (Index = LZMA_PROPS_SIZE+1; Index <= LZMA_PROPS_SIZE + 7; 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;
}
}

EFI_STATUS
LzmaCompress (
IN UINT8 *Source,
IN SizeT SourceSize,
IN UINT8 *Destination,
IN OUT SizeT *DestinationSize
)
EFIAPI
LzmaCompress (
CONST UINT8 *Source,
UINT32 SourceSize,
UINT8 *Destination,
UINT32 *DestinationSize,
UINT32 DictionarySize
)
{
SRes LzmaResult;
CLzmaEncProps props;
SizeT propsSize = LZMA_PROPS_SIZE;
SizeT destLen = SourceSize + SourceSize / 3 + 128;

if (*DestinationSize < destLen)
if (*DestinationSize < (UINT32)destLen)
{
*DestinationSize = destLen;
*DestinationSize = (UINT32)destLen;
return EFI_BUFFER_TOO_SMALL;
}

LzmaEncProps_Init(&props);
props.dictSize = LZMA_DICTIONARY_SIZE;
props.dictSize = DictionarySize;
props.level = 9;
props.fb = 273;

LzmaResult = LzmaEncode(
(Byte*)((UINT8*)Destination + LZMA_HEADER_SIZE),
(SizeT*)&destLen,
(VOID*)Source,
SourceSize,
&props,
(UINT8*)Destination,
(SizeT*)&propsSize,
(Byte*)((UINT8*)Destination + LZMA_HEADER_SIZE),
&destLen,
Source,
(SizeT)SourceSize,
&props,
(UINT8*)Destination,
&propsSize,
props.writeEndMark,
&g_ProgressCallback,
&SzAllocForLzma,
&g_ProgressCallback,
&SzAllocForLzma,
&SzAllocForLzma);

*DestinationSize = destLen + LZMA_HEADER_SIZE;
*DestinationSize = (UINT32)(destLen + LZMA_HEADER_SIZE);

SetEncodedSizeOfBuf((UINT64)SourceSize, Destination);
SetEncodedSizeOfBuf(SourceSize, Destination);

if (LzmaResult == SZ_OK) {
return EFI_SUCCESS;
} else {
}
else {
return EFI_INVALID_PARAMETER;
}
}



39 changes: 25 additions & 14 deletions uefi_firmware/compression/LZMA/LzmaCompress.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* LZMA Compress Header

Copyright (c) 2012, Nikolaj Schlej. All rights reserved.
Copyright (c) 2014, Nikolaj Schlej. 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
Expand All @@ -9,22 +9,33 @@
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

*/
*/

#ifndef __LZMACOMPRESS_H__
#define __LZMACOMPRESS_H__
#ifndef LZMACOMPRESS_H
#define LZMACOMPRESS_H

#include "SDK/C/Types.h"
#include "SDK/C/7zTypes.h"
#include "BaseTypes.h"

#define LZMA_DICTIONARY_SIZE 0x800000
#ifdef __cplusplus
extern "C" {
#endif

EFI_STATUS
LzmaCompress (
IN UINT8 *Source,
IN SizeT SourceSize,
IN UINT8 *Destination,
IN OUT SizeT *DestinationSize
);
#define DEFAULT_LZMA_DICTIONARY_SIZE 0x800000
#define _LZMA_SIZE_OPT

#endif
EFI_STATUS
EFIAPI
LzmaCompress (
const UINT8 *Source,
UINT32 SourceSize,
UINT8 *Destination,
UINT32 *DestinationSize,
UINT32 DictionarySize
);

#ifdef __cplusplus
}
#endif

#endif // LZMACOMPRESS_H
98 changes: 48 additions & 50 deletions uefi_firmware/compression/LZMA/LzmaDecompress.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
/* LZMA Decompress Implementation

Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
Expand All @@ -14,28 +12,25 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/

#include "LzmaDecompress.h"
#include "SDK/C/Types.h"
//#include "Sdk/C/7zVersion.h"
#include "SDK/C/LzmaDec.h"
#include "SDK/C/7zTypes.h"
#include "SDK/C/7zVersion.h"

#include <stdlib.h>
#include <stddef.h>

UINT64
LShiftU64 (
UINT64 Operand,
UINT32 Count
EFIAPI
LShiftU64 (
UINT64 Operand,
UINT32 Count
)
{
return Operand << Count;
}

static void * AllocForLzma(void *p, size_t size) { return malloc(size); }
static void FreeForLzma(void *p, void *address) { free(address); }
static void * AllocForLzma(ISzAllocPtr p, size_t size) { (void)p; return malloc(size); }
static void FreeForLzma(ISzAllocPtr p, void *address) { (void)p; free(address); }
static ISzAlloc SzAllocForLzma = { &AllocForLzma, &FreeForLzma };

#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)

/*
Get the size of the uncompressed buffer by parsing EncodeData header.

Expand All @@ -44,14 +39,14 @@ Get the size of the uncompressed buffer by parsing EncodeData header.
@return The size of the uncompressed buffer.
*/
UINT64
GetDecodedSizeOfBuf(
const UINT8 *EncodedData
GetDecodedSizeOfBuf (
UINT8 *EncodedData
)
{
UINT64 DecodedSize;
INT32 Index;
INT32 Index;

// Parse header
// Parse header
DecodedSize = 0;
for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)
DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];
Expand All @@ -64,15 +59,15 @@ UINT64
//

/*
Given a Lzma compressed source buffer, this function retrieves the size of
the uncompressed buffer and the size of the scratch buffer required
Given a Lzma 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.
The size of the uncompressed buffer is returned DestinationSize,
The size of the uncompressed buffer is returned DestinationSize,
the size of the scratch buffer is returned 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 LZMA_HEADER_SIZE beginning bytes of the source data and output it as DestinationSize.
And ScratchSize is specific to the decompression implementation.
Expand All @@ -85,56 +80,59 @@ If SourceSize is less than LZMA_HEADER_SIZE, then ASSERT().
that will be generated when the compressed buffer specified
by Source and SourceSize is decompressed.

@retval EFI_SUCCESS The size of the uncompressed data was returned
DestinationSize and the size of the scratch
@retval EFI_SUCCESS The size of the uncompressed data was returned
DestinationSize and the size of the scratch
buffer was returned ScratchSize.

*/
EFI_STATUS
EFIAPI
LzmaGetInfo (
IN VOID *Source,
IN size_t SourceSize,
OUT size_t *DestinationSize,
OUT size_t *ScratchSize
)
CONST VOID *Source,
UINT32 SourceSize,
UINT32 *DestinationSize
)
{
UInt64 DecodedSize;

UINT64 DecodedSize;
ASSERT(SourceSize >= LZMA_HEADER_SIZE);
(void)SourceSize;

DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);

*DestinationSize = DecodedSize;
return EFI_SUCCESS;
if (DecodedSize <= UINT32_MAX) {
*DestinationSize = (UINT32)DecodedSize;
return EFI_SUCCESS;
}
else {
return EFI_INVALID_PARAMETER;
}
}

/*
Decompresses a Lzma compressed source buffer.

Extracts decompressed data to its original form.
If the compressed source data specified by Source is successfully decompressed
into Destination, then RETURN_SUCCESS is returned. If the compressed source data
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 a valid compressed data format,
then RETURN_INVALID_PARAMETER is returned.

@param Source The source buffer containing the compressed data.
@param SourceSize The size of source buffer.
@param Destination The destination buffer to store the decompressed data

@retval EFI_SUCCESS Decompression completed successfully, and
@retval EFI_SUCCESS Decompression completed successfully, and
the uncompressed buffer is returned Destination.
@retval EFI_INVALID_PARAMETER
The source buffer specified by Source is corrupted
@retval EFI_INVALID_PARAMETER
The source buffer specified by Source is corrupted
(not a valid compressed format).
*/
EFI_STATUS
LzmaDecompress (
IN VOID *Source,
IN size_t SourceSize,
IN OUT VOID *Destination,
IN size_t DstSize,
IN OUT VOID *Scratch,
IN size_t ScratchSize
EFIAPI
LzmaDecompress (
CONST VOID *Source,
UINT32 SourceSize,
VOID *Destination
)
{
SRes LzmaResult;
Expand All @@ -143,14 +141,14 @@ EFI_STATUS
SizeT EncodedDataSize;

DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);
EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);
EncodedDataSize = (SizeT)(SourceSize - LZMA_HEADER_SIZE);

LzmaResult = LzmaDecode(
Destination,
(Byte*)Destination,
&DecodedBufSize,
(Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),
&EncodedDataSize,
Source,
(CONST Byte*) Source,
LZMA_PROPS_SIZE,
LZMA_FINISH_END,
&Status,
Expand All @@ -159,8 +157,8 @@ EFI_STATUS

if (LzmaResult == SZ_OK) {
return EFI_SUCCESS;
} else {
}
else {
return EFI_INVALID_PARAMETER;
}
}

Loading
Loading