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