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