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/UefiDecompressLib.h>
24 #include <Library/ExtractGuidedSectionLib.h>
25 #include <Guid/LzmaDecompress.h>
27 #include "Sdk/C/Types.h"
28 #include "Sdk/C/7zVersion.h"
29 #include "Sdk/C/LzmaDec.h"
35 STATIC CONST VOID
*mSourceLastUsedWithGetInfo
;
36 STATIC UINT32 mSizeOfLastSource
;
37 STATIC UINT32 mDecompressedSizeForLastSource
;
38 STATIC VOID
*mScratchBuffer
;
39 STATIC UINTN mScratchBufferSize
;
40 #define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB
43 Allocation routine used by LZMA decompression.
45 @param p Pointer to the ISzAlloc instance
46 @param size The size in bytes to be allocated
48 @return The allocated pointer address, or NULL on failure
59 if (mScratchBufferSize
>= size
) {
60 addr
= mScratchBuffer
;
61 mScratchBuffer
= (VOID
*) ((UINT8
*)addr
+ size
);
62 mScratchBufferSize
-= size
;
71 Free routine used by LZMA decompression.
73 @param p Pointer to the ISzAlloc instance
74 @param address The address to be freed
84 // We use the 'scratch buffer' for allocations, so there is no free
85 // operation required. The scratch buffer will be freed by the caller
86 // of the decompression code.
90 STATIC ISzAlloc g_Alloc
= { SzAlloc
, SzFree
};
92 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
105 for (Index
= LZMA_PROPS_SIZE
+ 7; Index
>= LZMA_PROPS_SIZE
; Index
--)
106 DecodedSize
= LShiftU64(DecodedSize
, 8) + encodedData
[Index
];
112 // LZMA functions and data as defined in local LzmaDecompress.h
116 The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().
118 @param Source The source buffer containing the compressed data.
119 @param SourceSize The size of source buffer
120 @param DestinationSize The size of destination buffer.
121 @param ScratchSize The size of scratch buffer.
123 @retval RETURN_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
124 @retval RETURN_INVALID_PARAMETER - The source data is corrupted
128 LzmaUefiDecompressGetInfo (
129 IN CONST VOID
*Source
,
130 IN UINT32 SourceSize
,
131 OUT UINT32
*DestinationSize
,
132 OUT UINT32
*ScratchSize
137 ASSERT(SourceSize
>= LZMA_HEADER_SIZE
);
139 DecodedSize
= GetDecodedSizeOfBuf((UINT8
*)Source
);
141 mSourceLastUsedWithGetInfo
= Source
;
142 mSizeOfLastSource
= SourceSize
;
143 mDecompressedSizeForLastSource
= (UInt32
)DecodedSize
;
144 *DestinationSize
= mDecompressedSizeForLastSource
;
145 *ScratchSize
= SCRATCH_BUFFER_REQUEST_SIZE
;
146 return RETURN_SUCCESS
;
151 The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().
153 @param Source - The source buffer containing the compressed data.
154 @param Destination - The destination buffer to store the decompressed data
155 @param Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
157 @retval RETURN_SUCCESS - Decompression is successfull
158 @retval RETURN_INVALID_PARAMETER - The source data is corrupted
163 IN CONST VOID
*Source
,
164 IN OUT VOID
*Destination
,
170 SizeT decodedBufSize
;
171 SizeT encodedDataSize
;
173 if (Source
!= mSourceLastUsedWithGetInfo
) {
174 return RETURN_INVALID_PARAMETER
;
177 decodedBufSize
= (SizeT
)mDecompressedSizeForLastSource
;
178 encodedDataSize
= (SizeT
)(mSizeOfLastSource
- LZMA_HEADER_SIZE
);
180 mScratchBuffer
= Scratch
;
181 mScratchBufferSize
= SCRATCH_BUFFER_REQUEST_SIZE
;
183 lzmaResult
= LzmaDecode(
186 (Byte
*)((UINT8
*)Source
+ LZMA_HEADER_SIZE
),
195 if (lzmaResult
== SZ_OK
) {
196 return RETURN_SUCCESS
;
198 return RETURN_INVALID_PARAMETER
;