]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
Update links for broken urls in BaseTools/gcc/mingw-gcc-build.py script.
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
CommitLineData
306bf4e2 1/** @file\r
182b1d16 2 LZMA Decompress interfaces\r
306bf4e2 3\r
182b1d16 4 Copyright (c) 2009, Intel Corporation<BR>\r
306bf4e2 5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
182b1d16 15#include "LzmaDecompressLibInternal.h"\r
306bf4e2 16#include "Sdk/C/Types.h"\r
17#include "Sdk/C/7zVersion.h"\r
18#include "Sdk/C/LzmaDec.h"\r
19\r
57cca89e 20//\r
21// Global data\r
22//\r
23\r
182b1d16
LG
24CONST VOID *mSourceLastUsedWithGetInfo;\r
25UINT32 mSizeOfLastSource;\r
26UINT32 mDecompressedSizeForLastSource;\r
27VOID *mScratchBuffer;\r
28UINTN mScratchBufferSize;\r
29\r
57cca89e 30#define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB\r
31\r
19a4a0a0 32/**\r
33 Allocation routine used by LZMA decompression.\r
34\r
182b1d16
LG
35 @param P Pointer to the ISzAlloc instance\r
36 @param Size The size in bytes to be allocated\r
19a4a0a0 37\r
38 @return The allocated pointer address, or NULL on failure\r
39**/\r
19a4a0a0 40VOID *\r
41SzAlloc (\r
182b1d16
LG
42 VOID *P,\r
43 size_t Size\r
19a4a0a0 44 )\r
45{\r
182b1d16 46 VOID *Addr;\r
57cca89e 47\r
182b1d16
LG
48 if (mScratchBufferSize >= Size) {\r
49 Addr = mScratchBuffer;\r
50 mScratchBuffer = (VOID*) ((UINT8*)Addr + Size);\r
51 mScratchBufferSize -= Size;\r
52 return Addr;\r
57cca89e 53 } else {\r
54 ASSERT (FALSE);\r
55 return NULL;\r
56 }\r
19a4a0a0 57}\r
58\r
59/**\r
60 Free routine used by LZMA decompression.\r
61\r
182b1d16
LG
62 @param P Pointer to the ISzAlloc instance\r
63 @param Address The address to be freed\r
19a4a0a0 64**/\r
19a4a0a0 65VOID\r
66SzFree (\r
182b1d16
LG
67 VOID *P,\r
68 VOID *Address\r
19a4a0a0 69 )\r
70{\r
57cca89e 71 //\r
72 // We use the 'scratch buffer' for allocations, so there is no free\r
73 // operation required. The scratch buffer will be freed by the caller\r
74 // of the decompression code.\r
75 //\r
19a4a0a0 76}\r
77\r
78STATIC ISzAlloc g_Alloc = { SzAlloc, SzFree };\r
306bf4e2 79\r
80#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
81\r
182b1d16
LG
82/**\r
83 Get the size of the uncompressed buffer by parsing EncodeData header.\r
84\r
85 @param EncodedData Pointer to the compressed data.\r
86\r
87 @return The size of the uncompressed buffer.\r
88**/\r
306bf4e2 89UINT64\r
90GetDecodedSizeOfBuf(\r
182b1d16 91 UINT8 *EncodedData\r
306bf4e2 92 )\r
93{\r
94 UINT64 DecodedSize;\r
95 INTN Index;\r
96\r
97 /* Parse header */\r
98 DecodedSize = 0;\r
99 for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)\r
182b1d16 100 DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];\r
306bf4e2 101\r
102 return DecodedSize;\r
103}\r
104\r
105//\r
182b1d16 106// LZMA functions and data as defined in local LzmaDecompressLibInternal.h\r
306bf4e2 107//\r
108\r
306bf4e2 109/**\r
182b1d16
LG
110 Given a Lzma compressed source buffer, this function retrieves the size of \r
111 the uncompressed buffer and the size of the scratch buffer required \r
112 to decompress the compressed source buffer.\r
113\r
114 Retrieves the size of the uncompressed buffer and the temporary scratch buffer \r
115 required to decompress the buffer specified by Source and SourceSize.\r
116 The size of the uncompressed buffer is returned in DestinationSize, \r
117 the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.\r
118 This function does not have scratch buffer available to perform a thorough \r
119 checking of the validity of the source data. It just retrieves the "Original Size"\r
120 field from the LZMA_HEADER_SIZE beginning bytes of the source data and output it as DestinationSize.\r
121 And ScratchSize is specific to the decompression implementation.\r
122\r
123 If SourceSize is less than LZMA_HEADER_SIZE, then ASSERT().\r
124\r
125 @param Source The source buffer containing the compressed data.\r
126 @param SourceSize The size, in bytes, of the source buffer.\r
127 @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer\r
128 that will be generated when the compressed buffer specified\r
129 by Source and SourceSize is decompressed.\r
130 @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that\r
131 is required to decompress the compressed buffer specified \r
132 by Source and SourceSize.\r
133\r
134 @retval RETURN_SUCCESS The size of the uncompressed data was returned \r
135 in DestinationSize and the size of the scratch \r
136 buffer was returned in ScratchSize.\r
137\r
306bf4e2 138**/\r
139RETURN_STATUS\r
140EFIAPI\r
141LzmaUefiDecompressGetInfo (\r
142 IN CONST VOID *Source,\r
143 IN UINT32 SourceSize,\r
144 OUT UINT32 *DestinationSize,\r
145 OUT UINT32 *ScratchSize\r
146 )\r
147{\r
148 UInt64 DecodedSize;\r
149\r
150 ASSERT(SourceSize >= LZMA_HEADER_SIZE);\r
151\r
152 DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
153\r
154 mSourceLastUsedWithGetInfo = Source;\r
155 mSizeOfLastSource = SourceSize;\r
156 mDecompressedSizeForLastSource = (UInt32)DecodedSize;\r
157 *DestinationSize = mDecompressedSizeForLastSource;\r
57cca89e 158 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
306bf4e2 159 return RETURN_SUCCESS;\r
160}\r
161\r
162\r
163/**\r
182b1d16
LG
164 Decompresses a Lzma compressed source buffer.\r
165\r
166 Extracts decompressed data to its original form.\r
167 If the compressed source data specified by Source is successfully decompressed \r
168 into Destination, then RETURN_SUCCESS is returned. If the compressed source data \r
169 specified by Source is not in a valid compressed data format,\r
170 then RETURN_INVALID_PARAMETER is returned.\r
171\r
172 @param Source The source buffer containing the compressed data.\r
173 @param Destination The destination buffer to store the decompressed data\r
174 @param Scratch A temporary scratch buffer that is used to perform the decompression.\r
175 This is an optional parameter that may be NULL if the \r
176 required scratch buffer size is 0.\r
177 \r
178 @retval RETURN_SUCCESS Decompression completed successfully, and \r
179 the uncompressed buffer is returned in Destination.\r
180 @retval RETURN_INVALID_PARAMETER \r
181 The source buffer specified by Source is corrupted \r
182 (not in a valid compressed format).\r
306bf4e2 183**/\r
184RETURN_STATUS\r
185EFIAPI\r
186LzmaUefiDecompress (\r
187 IN CONST VOID *Source,\r
188 IN OUT VOID *Destination,\r
189 IN OUT VOID *Scratch\r
190 )\r
191{\r
182b1d16
LG
192 SRes LzmaResult;\r
193 ELzmaStatus Status;\r
194 SizeT DecodedBufSize;\r
195 SizeT EncodedDataSize;\r
306bf4e2 196\r
197 if (Source != mSourceLastUsedWithGetInfo) {\r
198 return RETURN_INVALID_PARAMETER;\r
199 }\r
200\r
182b1d16
LG
201 DecodedBufSize = (SizeT)mDecompressedSizeForLastSource;\r
202 EncodedDataSize = (SizeT)(mSizeOfLastSource - LZMA_HEADER_SIZE);\r
306bf4e2 203\r
57cca89e 204 mScratchBuffer = Scratch;\r
205 mScratchBufferSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
206\r
182b1d16 207 LzmaResult = LzmaDecode(\r
306bf4e2 208 Destination,\r
182b1d16 209 &DecodedBufSize,\r
306bf4e2 210 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
182b1d16 211 &EncodedDataSize,\r
306bf4e2 212 Source,\r
213 LZMA_PROPS_SIZE,\r
214 LZMA_FINISH_END,\r
182b1d16 215 &Status,\r
306bf4e2 216 &g_Alloc\r
217 );\r
218\r
182b1d16 219 if (LzmaResult == SZ_OK) {\r
306bf4e2 220 return RETURN_SUCCESS;\r
221 } else {\r
222 return RETURN_INVALID_PARAMETER;\r
223 }\r
224}\r
225\r