2 LZMA Decompress routines for edk2
4 Portions based on LZMA SDK 4.65:
5 LzmaUtil.c -- Test application for LZMA compression
6 2008-11-23 : Igor Pavlov : Public domain
8 Copyright (c) 2006 - 2009, Intel Corporation<BR>
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/MemoryAllocationLib.h>
24 #include <Library/UefiDecompressLib.h>
25 #include <Library/ExtractGuidedSectionLib.h>
26 #include <Guid/LzmaDecompress.h>
28 #include "Sdk/C/Types.h"
29 #include "Sdk/C/7zVersion.h"
30 #include "Sdk/C/LzmaDec.h"
33 Allocation routine used by LZMA decompression.
35 @param p Pointer to the ISzAlloc instance
36 @param size The size in bytes to be allocated
38 @return The allocated pointer address, or NULL on failure
47 return AllocatePool (size
);
51 Free routine used by LZMA decompression.
53 @param p Pointer to the ISzAlloc instance
54 @param address The address to be freed
63 if (address
!= NULL
) {
68 STATIC ISzAlloc g_Alloc
= { SzAlloc
, SzFree
};
70 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
83 for (Index
= LZMA_PROPS_SIZE
+ 7; Index
>= LZMA_PROPS_SIZE
; Index
--)
84 DecodedSize
= LShiftU64(DecodedSize
, 8) + encodedData
[Index
];
90 // LZMA functions and data as defined in local LzmaDecompress.h
93 STATIC CONST VOID
*mSourceLastUsedWithGetInfo
;
94 STATIC UINT32 mSizeOfLastSource
;
95 STATIC UINT32 mDecompressedSizeForLastSource
;
98 The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().
100 @param Source The source buffer containing the compressed data.
101 @param SourceSize The size of source buffer
102 @param DestinationSize The size of destination buffer.
103 @param ScratchSize The size of scratch buffer.
105 @retval RETURN_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
106 @retval RETURN_INVALID_PARAMETER - The source data is corrupted
110 LzmaUefiDecompressGetInfo (
111 IN CONST VOID
*Source
,
112 IN UINT32 SourceSize
,
113 OUT UINT32
*DestinationSize
,
114 OUT UINT32
*ScratchSize
119 ASSERT(SourceSize
>= LZMA_HEADER_SIZE
);
121 DecodedSize
= GetDecodedSizeOfBuf((UINT8
*)Source
);
123 mSourceLastUsedWithGetInfo
= Source
;
124 mSizeOfLastSource
= SourceSize
;
125 mDecompressedSizeForLastSource
= (UInt32
)DecodedSize
;
126 *DestinationSize
= mDecompressedSizeForLastSource
;
128 return RETURN_SUCCESS
;
133 The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().
135 @param Source - The source buffer containing the compressed data.
136 @param Destination - The destination buffer to store the decompressed data
137 @param Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
139 @retval RETURN_SUCCESS - Decompression is successfull
140 @retval RETURN_INVALID_PARAMETER - The source data is corrupted
145 IN CONST VOID
*Source
,
146 IN OUT VOID
*Destination
,
152 SizeT decodedBufSize
;
153 SizeT encodedDataSize
;
155 if (Source
!= mSourceLastUsedWithGetInfo
) {
156 return RETURN_INVALID_PARAMETER
;
159 decodedBufSize
= (SizeT
)mDecompressedSizeForLastSource
;
160 encodedDataSize
= (SizeT
)(mSizeOfLastSource
- LZMA_HEADER_SIZE
);
162 lzmaResult
= LzmaDecode(
165 (Byte
*)((UINT8
*)Source
+ LZMA_HEADER_SIZE
),
174 if (lzmaResult
== SZ_OK
) {
175 return RETURN_SUCCESS
;
177 return RETURN_INVALID_PARAMETER
;