]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
MdeModulePkg: BaseSortLib and UefiBootManagerLib support DXE_RUNTIME_DRIVER.
[mirror_edk2.git] / MdeModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
CommitLineData
11ff2c71
LG
1/** @file\r
2 LZMA Decompress interfaces\r
3\r
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
5 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
15#include "LzmaDecompressLibInternal.h"\r
16#include "Sdk/C/Types.h"\r
17#include "Sdk/C/7zVersion.h"\r
18#include "Sdk/C/LzmaDec.h"\r
19\r
20#define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB\r
21\r
22typedef struct\r
23{\r
24 ISzAlloc Functions;\r
25 VOID *Buffer;\r
26 UINTN BufferSize;\r
27} ISzAllocWithData;\r
28\r
29/**\r
30 Allocation routine used by LZMA decompression.\r
31\r
32 @param P Pointer to the ISzAlloc instance\r
33 @param Size The size in bytes to be allocated\r
34\r
35 @return The allocated pointer address, or NULL on failure\r
36**/\r
37VOID *\r
38SzAlloc (\r
39 VOID *P,\r
40 size_t Size\r
41 )\r
42{\r
43 VOID *Addr;\r
44 ISzAllocWithData *Private;\r
45\r
46 Private = (ISzAllocWithData*) P;\r
47\r
48 if (Private->BufferSize >= Size) {\r
49 Addr = Private->Buffer;\r
50 Private->Buffer = (VOID*) ((UINT8*)Addr + Size);\r
51 Private->BufferSize -= Size;\r
52 return Addr;\r
53 } else {\r
54 ASSERT (FALSE);\r
55 return NULL;\r
56 }\r
57}\r
58\r
59/**\r
60 Free routine used by LZMA decompression.\r
61\r
62 @param P Pointer to the ISzAlloc instance\r
63 @param Address The address to be freed\r
64**/\r
65VOID\r
66SzFree (\r
67 VOID *P,\r
68 VOID *Address\r
69 )\r
70{\r
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
76}\r
77\r
78#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
79\r
80/**\r
81 Get the size of the uncompressed buffer by parsing EncodeData header.\r
82\r
83 @param EncodedData Pointer to the compressed data.\r
84\r
85 @return The size of the uncompressed buffer.\r
86**/\r
87UINT64\r
88GetDecodedSizeOfBuf(\r
89 UINT8 *EncodedData\r
90 )\r
91{\r
92 UINT64 DecodedSize;\r
93 INTN Index;\r
94\r
95 /* Parse header */\r
96 DecodedSize = 0;\r
97 for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)\r
98 DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];\r
99\r
100 return DecodedSize;\r
101}\r
102\r
103//\r
104// LZMA functions and data as defined in local LzmaDecompressLibInternal.h\r
105//\r
106\r
107/**\r
108 Given a Lzma compressed source buffer, this function retrieves the size of \r
109 the uncompressed buffer and the size of the scratch buffer required \r
110 to decompress the compressed source buffer.\r
111\r
112 Retrieves the size of the uncompressed buffer and the temporary scratch buffer \r
113 required to decompress the buffer specified by Source and SourceSize.\r
114 The size of the uncompressed buffer is returned in DestinationSize, \r
115 the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.\r
116 This function does not have scratch buffer available to perform a thorough \r
117 checking of the validity of the source data. It just retrieves the "Original Size"\r
118 field from the LZMA_HEADER_SIZE beginning bytes of the source data and output it as DestinationSize.\r
119 And ScratchSize is specific to the decompression implementation.\r
120\r
121 If SourceSize is less than LZMA_HEADER_SIZE, then ASSERT().\r
122\r
123 @param Source The source buffer containing the compressed data.\r
124 @param SourceSize The size, in bytes, of the source buffer.\r
125 @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer\r
126 that will be generated when the compressed buffer specified\r
127 by Source and SourceSize is decompressed.\r
128 @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that\r
129 is required to decompress the compressed buffer specified \r
130 by Source and SourceSize.\r
131\r
132 @retval RETURN_SUCCESS The size of the uncompressed data was returned \r
133 in DestinationSize and the size of the scratch \r
134 buffer was returned in ScratchSize.\r
135\r
136**/\r
137RETURN_STATUS\r
138EFIAPI\r
139LzmaUefiDecompressGetInfo (\r
140 IN CONST VOID *Source,\r
141 IN UINT32 SourceSize,\r
142 OUT UINT32 *DestinationSize,\r
143 OUT UINT32 *ScratchSize\r
144 )\r
145{\r
146 UInt64 DecodedSize;\r
147\r
148 ASSERT(SourceSize >= LZMA_HEADER_SIZE);\r
149\r
150 DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
151\r
152 *DestinationSize = (UINT32)DecodedSize;\r
153 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
154 return RETURN_SUCCESS;\r
155}\r
156\r
157/**\r
158 Decompresses a Lzma compressed source buffer.\r
159\r
160 Extracts decompressed data to its original form.\r
161 If the compressed source data specified by Source is successfully decompressed \r
162 into Destination, then RETURN_SUCCESS is returned. If the compressed source data \r
163 specified by Source is not in a valid compressed data format,\r
164 then RETURN_INVALID_PARAMETER is returned.\r
165\r
166 @param Source The source buffer containing the compressed data.\r
167 @param SourceSize The size of source buffer.\r
168 @param Destination The destination buffer to store the decompressed data\r
169 @param Scratch A temporary scratch buffer that is used to perform the decompression.\r
170 This is an optional parameter that may be NULL if the \r
171 required scratch buffer size is 0.\r
172 \r
173 @retval RETURN_SUCCESS Decompression completed successfully, and \r
174 the uncompressed buffer is returned in Destination.\r
175 @retval RETURN_INVALID_PARAMETER \r
176 The source buffer specified by Source is corrupted \r
177 (not in a valid compressed format).\r
178**/\r
179RETURN_STATUS\r
180EFIAPI\r
181LzmaUefiDecompress (\r
182 IN CONST VOID *Source,\r
183 IN UINTN SourceSize,\r
184 IN OUT VOID *Destination,\r
185 IN OUT VOID *Scratch\r
186 )\r
187{\r
188 SRes LzmaResult;\r
189 ELzmaStatus Status;\r
190 SizeT DecodedBufSize;\r
191 SizeT EncodedDataSize;\r
192 ISzAllocWithData AllocFuncs;\r
193\r
194 AllocFuncs.Functions.Alloc = SzAlloc;\r
195 AllocFuncs.Functions.Free = SzFree;\r
196 AllocFuncs.Buffer = Scratch;\r
197 AllocFuncs.BufferSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
198 \r
199 DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);\r
200 EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);\r
201\r
202 LzmaResult = LzmaDecode(\r
203 Destination,\r
204 &DecodedBufSize,\r
205 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
206 &EncodedDataSize,\r
207 Source,\r
208 LZMA_PROPS_SIZE,\r
209 LZMA_FINISH_END,\r
210 &Status,\r
211 &(AllocFuncs.Functions)\r
212 );\r
213\r
214 if (LzmaResult == SZ_OK) {\r
215 return RETURN_SUCCESS;\r
216 } else {\r
217 return RETURN_INVALID_PARAMETER;\r
218 }\r
219}\r
220\r