Inconsistent with specification here:
[edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
blobb8523f4b4fe2a227a7cabaafe7df39bc5f98146f
1 /** @file
2 LZMA Decompress interfaces
4 Copyright (c) 2009, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 **/
15 #include "LzmaDecompressLibInternal.h"
16 #include "Sdk/C/Types.h"
17 #include "Sdk/C/7zVersion.h"
18 #include "Sdk/C/LzmaDec.h"
21 // Global data
24 CONST VOID *mSourceLastUsedWithGetInfo;
25 UINT32 mSizeOfLastSource;
26 UINT32 mDecompressedSizeForLastSource;
27 VOID *mScratchBuffer;
28 UINTN mScratchBufferSize;
30 #define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB
32 /**
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
39 **/
40 VOID *
41 SzAlloc (
42 VOID *P,
43 size_t Size
46 VOID *Addr;
48 if (mScratchBufferSize >= Size) {
49 Addr = mScratchBuffer;
50 mScratchBuffer = (VOID*) ((UINT8*)Addr + Size);
51 mScratchBufferSize -= Size;
52 return Addr;
53 } else {
54 ASSERT (FALSE);
55 return NULL;
59 /**
60 Free routine used by LZMA decompression.
62 @param P Pointer to the ISzAlloc instance
63 @param Address The address to be freed
64 **/
65 VOID
66 SzFree (
67 VOID *P,
68 VOID *Address
72 // We use the 'scratch buffer' for allocations, so there is no free
73 // operation required. The scratch buffer will be freed by the caller
74 // of the decompression code.
78 STATIC ISzAlloc g_Alloc = { SzAlloc, SzFree };
80 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
82 /**
83 Get the size of the uncompressed buffer by parsing EncodeData header.
85 @param EncodedData Pointer to the compressed data.
87 @return The size of the uncompressed buffer.
88 **/
89 UINT64
90 GetDecodedSizeOfBuf(
91 UINT8 *EncodedData
94 UINT64 DecodedSize;
95 INTN Index;
97 /* Parse header */
98 DecodedSize = 0;
99 for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)
100 DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];
102 return DecodedSize;
106 // LZMA functions and data as defined in local LzmaDecompressLibInternal.h
110 Given a Lzma compressed source buffer, this function retrieves the size of
111 the uncompressed buffer and the size of the scratch buffer required
112 to decompress the compressed source buffer.
114 Retrieves the size of the uncompressed buffer and the temporary scratch buffer
115 required to decompress the buffer specified by Source and SourceSize.
116 The size of the uncompressed buffer is returned in DestinationSize,
117 the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.
118 This function does not have scratch buffer available to perform a thorough
119 checking of the validity of the source data. It just retrieves the "Original Size"
120 field from the LZMA_HEADER_SIZE beginning bytes of the source data and output it as DestinationSize.
121 And ScratchSize is specific to the decompression implementation.
123 If SourceSize is less than LZMA_HEADER_SIZE, then ASSERT().
125 @param Source The source buffer containing the compressed data.
126 @param SourceSize The size, in bytes, of the source buffer.
127 @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
128 that will be generated when the compressed buffer specified
129 by Source and SourceSize is decompressed.
130 @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that
131 is required to decompress the compressed buffer specified
132 by Source and SourceSize.
134 @retval RETURN_SUCCESS The size of the uncompressed data was returned
135 in DestinationSize and the size of the scratch
136 buffer was returned in ScratchSize.
139 RETURN_STATUS
140 EFIAPI
141 LzmaUefiDecompressGetInfo (
142 IN CONST VOID *Source,
143 IN UINT32 SourceSize,
144 OUT UINT32 *DestinationSize,
145 OUT UINT32 *ScratchSize
148 UInt64 DecodedSize;
150 ASSERT(SourceSize >= LZMA_HEADER_SIZE);
152 DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);
154 mSourceLastUsedWithGetInfo = Source;
155 mSizeOfLastSource = SourceSize;
156 mDecompressedSizeForLastSource = (UInt32)DecodedSize;
157 *DestinationSize = mDecompressedSizeForLastSource;
158 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;
159 return RETURN_SUCCESS;
164 Decompresses a Lzma compressed source buffer.
166 Extracts decompressed data to its original form.
167 If the compressed source data specified by Source is successfully decompressed
168 into Destination, then RETURN_SUCCESS is returned. If the compressed source data
169 specified by Source is not in a valid compressed data format,
170 then RETURN_INVALID_PARAMETER is returned.
172 @param Source The source buffer containing the compressed data.
173 @param Destination The destination buffer to store the decompressed data
174 @param Scratch A temporary scratch buffer that is used to perform the decompression.
175 This is an optional parameter that may be NULL if the
176 required scratch buffer size is 0.
178 @retval RETURN_SUCCESS Decompression completed successfully, and
179 the uncompressed buffer is returned in Destination.
180 @retval RETURN_INVALID_PARAMETER
181 The source buffer specified by Source is corrupted
182 (not in a valid compressed format).
184 RETURN_STATUS
185 EFIAPI
186 LzmaUefiDecompress (
187 IN CONST VOID *Source,
188 IN OUT VOID *Destination,
189 IN OUT VOID *Scratch
192 SRes LzmaResult;
193 ELzmaStatus Status;
194 SizeT DecodedBufSize;
195 SizeT EncodedDataSize;
197 if (Source != mSourceLastUsedWithGetInfo) {
198 return RETURN_INVALID_PARAMETER;
201 DecodedBufSize = (SizeT)mDecompressedSizeForLastSource;
202 EncodedDataSize = (SizeT)(mSizeOfLastSource - LZMA_HEADER_SIZE);
204 mScratchBuffer = Scratch;
205 mScratchBufferSize = SCRATCH_BUFFER_REQUEST_SIZE;
207 LzmaResult = LzmaDecode(
208 Destination,
209 &DecodedBufSize,
210 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),
211 &EncodedDataSize,
212 Source,
213 LZMA_PROPS_SIZE,
214 LZMA_FINISH_END,
215 &Status,
216 &g_Alloc
219 if (LzmaResult == SZ_OK) {
220 return RETURN_SUCCESS;
221 } else {
222 return RETURN_INVALID_PARAMETER;