2 LZMA Decompress interfaces
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "LzmaDecompressLibInternal.h"
10 #include "Sdk/C/7zTypes.h"
11 #include "Sdk/C/7zVersion.h"
12 #include "Sdk/C/LzmaDec.h"
14 #define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB
24 Allocation routine used by LZMA decompression.
26 @param P Pointer to the ISzAlloc instance
27 @param Size The size in bytes to be allocated
29 @return The allocated pointer address, or NULL on failure
38 ISzAllocWithData
*Private
;
40 Private
= (ISzAllocWithData
*) P
;
42 if (Private
->BufferSize
>= Size
) {
43 Addr
= Private
->Buffer
;
44 Private
->Buffer
= (VOID
*) ((UINT8
*)Addr
+ Size
);
45 Private
->BufferSize
-= Size
;
54 Free routine used by LZMA decompression.
56 @param P Pointer to the ISzAlloc instance
57 @param Address The address to be freed
66 // We use the 'scratch buffer' for allocations, so there is no free
67 // operation required. The scratch buffer will be freed by the caller
68 // of the decompression code.
72 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
75 Get the size of the uncompressed buffer by parsing EncodeData header.
77 @param EncodedData Pointer to the compressed data.
79 @return The size of the uncompressed buffer.
91 for (Index
= LZMA_PROPS_SIZE
+ 7; Index
>= LZMA_PROPS_SIZE
; Index
--)
92 DecodedSize
= LShiftU64(DecodedSize
, 8) + EncodedData
[Index
];
98 // LZMA functions and data as defined in local LzmaDecompressLibInternal.h
102 Given a Lzma compressed source buffer, this function retrieves the size of
103 the uncompressed buffer and the size of the scratch buffer required
104 to decompress the compressed source buffer.
106 Retrieves the size of the uncompressed buffer and the temporary scratch buffer
107 required to decompress the buffer specified by Source and SourceSize.
108 The size of the uncompressed buffer is returned in DestinationSize,
109 the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.
110 This function does not have scratch buffer available to perform a thorough
111 checking of the validity of the source data. It just retrieves the "Original Size"
112 field from the LZMA_HEADER_SIZE beginning bytes of the source data and output it as DestinationSize.
113 And ScratchSize is specific to the decompression implementation.
115 If SourceSize is less than LZMA_HEADER_SIZE, then ASSERT().
117 @param Source The source buffer containing the compressed data.
118 @param SourceSize The size, in bytes, of the source buffer.
119 @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
120 that will be generated when the compressed buffer specified
121 by Source and SourceSize is decompressed.
122 @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that
123 is required to decompress the compressed buffer specified
124 by Source and SourceSize.
126 @retval RETURN_SUCCESS The size of the uncompressed data was returned
127 in DestinationSize and the size of the scratch
128 buffer was returned in ScratchSize.
130 @retval RETURN_UNSUPPORTED DestinationSize cannot be output because the
131 uncompressed buffer size (in bytes) does not fit
132 in a UINT32. Output parameters have not been
137 LzmaUefiDecompressGetInfo (
138 IN CONST VOID
*Source
,
139 IN UINT32 SourceSize
,
140 OUT UINT32
*DestinationSize
,
141 OUT UINT32
*ScratchSize
146 ASSERT(SourceSize
>= LZMA_HEADER_SIZE
);
148 DecodedSize
= GetDecodedSizeOfBuf((UINT8
*)Source
);
149 if (DecodedSize
> MAX_UINT32
) {
150 return RETURN_UNSUPPORTED
;
153 *DestinationSize
= (UINT32
)DecodedSize
;
154 *ScratchSize
= SCRATCH_BUFFER_REQUEST_SIZE
;
155 return RETURN_SUCCESS
;
159 Decompresses a Lzma compressed source buffer.
161 Extracts decompressed data to its original form.
162 If the compressed source data specified by Source is successfully decompressed
163 into Destination, then RETURN_SUCCESS is returned. If the compressed source data
164 specified by Source is not in a valid compressed data format,
165 then RETURN_INVALID_PARAMETER is returned.
167 @param Source The source buffer containing the compressed data.
168 @param SourceSize The size of source buffer.
169 @param Destination The destination buffer to store the decompressed data
170 @param Scratch A temporary scratch buffer that is used to perform the decompression.
171 This is an optional parameter that may be NULL if the
172 required scratch buffer size is 0.
174 @retval RETURN_SUCCESS Decompression completed successfully, and
175 the uncompressed buffer is returned in Destination.
176 @retval RETURN_INVALID_PARAMETER
177 The source buffer specified by Source is corrupted
178 (not in a valid compressed format).
183 IN CONST VOID
*Source
,
185 IN OUT VOID
*Destination
,
191 SizeT DecodedBufSize
;
192 SizeT EncodedDataSize
;
193 ISzAllocWithData AllocFuncs
;
195 AllocFuncs
.Functions
.Alloc
= SzAlloc
;
196 AllocFuncs
.Functions
.Free
= SzFree
;
197 AllocFuncs
.Buffer
= Scratch
;
198 AllocFuncs
.BufferSize
= SCRATCH_BUFFER_REQUEST_SIZE
;
200 DecodedBufSize
= (SizeT
)GetDecodedSizeOfBuf((UINT8
*)Source
);
201 EncodedDataSize
= (SizeT
) (SourceSize
- LZMA_HEADER_SIZE
);
203 LzmaResult
= LzmaDecode(
206 (Byte
*)((UINT8
*)Source
+ LZMA_HEADER_SIZE
),
212 &(AllocFuncs
.Functions
)
215 if (LzmaResult
== SZ_OK
) {
216 return RETURN_SUCCESS
;
218 return RETURN_INVALID_PARAMETER
;