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"
32 extern ISzAlloc g_Alloc
;
34 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
47 for (Index
= LZMA_PROPS_SIZE
+ 7; Index
>= LZMA_PROPS_SIZE
; Index
--)
48 DecodedSize
= LShiftU64(DecodedSize
, 8) + encodedData
[Index
];
54 // LZMA functions and data as defined in local LzmaDecompress.h
57 STATIC CONST VOID
*mSourceLastUsedWithGetInfo
;
58 STATIC UINT32 mSizeOfLastSource
;
59 STATIC UINT32 mDecompressedSizeForLastSource
;
62 The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().
64 @param Source The source buffer containing the compressed data.
65 @param SourceSize The size of source buffer
66 @param DestinationSize The size of destination buffer.
67 @param ScratchSize The size of scratch buffer.
69 @retval RETURN_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.
70 @retval RETURN_INVALID_PARAMETER - The source data is corrupted
74 LzmaUefiDecompressGetInfo (
75 IN CONST VOID
*Source
,
77 OUT UINT32
*DestinationSize
,
78 OUT UINT32
*ScratchSize
83 ASSERT(SourceSize
>= LZMA_HEADER_SIZE
);
85 DecodedSize
= GetDecodedSizeOfBuf((UINT8
*)Source
);
87 mSourceLastUsedWithGetInfo
= Source
;
88 mSizeOfLastSource
= SourceSize
;
89 mDecompressedSizeForLastSource
= (UInt32
)DecodedSize
;
90 *DestinationSize
= mDecompressedSizeForLastSource
;
92 return RETURN_SUCCESS
;
97 The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().
99 @param Source - The source buffer containing the compressed data.
100 @param Destination - The destination buffer to store the decompressed data
101 @param Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.
103 @retval RETURN_SUCCESS - Decompression is successfull
104 @retval RETURN_INVALID_PARAMETER - The source data is corrupted
109 IN CONST VOID
*Source
,
110 IN OUT VOID
*Destination
,
116 SizeT decodedBufSize
;
117 SizeT encodedDataSize
;
119 if (Source
!= mSourceLastUsedWithGetInfo
) {
120 return RETURN_INVALID_PARAMETER
;
123 decodedBufSize
= (SizeT
)mDecompressedSizeForLastSource
;
124 encodedDataSize
= (SizeT
)(mSizeOfLastSource
- LZMA_HEADER_SIZE
);
126 lzmaResult
= LzmaDecode(
129 (Byte
*)((UINT8
*)Source
+ LZMA_HEADER_SIZE
),
138 if (lzmaResult
== SZ_OK
) {
139 return RETURN_SUCCESS
;
141 return RETURN_INVALID_PARAMETER
;