]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
IntelFrameworkModulePkg: Clean up source files
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
CommitLineData
306bf4e2 1/** @file\r
182b1d16 2 LZMA Decompress interfaces\r
306bf4e2 3\r
0a6f4824 4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
180a5a35 5 This program and the accompanying materials\r
306bf4e2 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
00f5e119 16#include "Sdk/C/7zTypes.h"\r
306bf4e2 17#include "Sdk/C/7zVersion.h"\r
18#include "Sdk/C/LzmaDec.h"\r
19\r
57cca89e 20#define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB\r
21\r
090d3088 22typedef struct\r
23{\r
24 ISzAlloc Functions;\r
25 VOID *Buffer;\r
26 UINTN BufferSize;\r
27} ISzAllocWithData;\r
28\r
19a4a0a0 29/**\r
30 Allocation routine used by LZMA decompression.\r
31\r
182b1d16
LG
32 @param P Pointer to the ISzAlloc instance\r
33 @param Size The size in bytes to be allocated\r
19a4a0a0 34\r
35 @return The allocated pointer address, or NULL on failure\r
36**/\r
19a4a0a0 37VOID *\r
38SzAlloc (\r
182b1d16
LG
39 VOID *P,\r
40 size_t Size\r
19a4a0a0 41 )\r
42{\r
182b1d16 43 VOID *Addr;\r
75a635ac 44 ISzAllocWithData *Private;\r
45\r
46 Private = (ISzAllocWithData*) P;\r
57cca89e 47\r
090d3088 48 if (Private->BufferSize >= Size) {\r
49 Addr = Private->Buffer;\r
50 Private->Buffer = (VOID*) ((UINT8*)Addr + Size);\r
51 Private->BufferSize -= Size;\r
182b1d16 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
306bf4e2 78#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
79\r
182b1d16
LG
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
306bf4e2 87UINT64\r
88GetDecodedSizeOfBuf(\r
182b1d16 89 UINT8 *EncodedData\r
306bf4e2 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
182b1d16 98 DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];\r
306bf4e2 99\r
100 return DecodedSize;\r
101}\r
102\r
103//\r
182b1d16 104// LZMA functions and data as defined in local LzmaDecompressLibInternal.h\r
306bf4e2 105//\r
106\r
306bf4e2 107/**\r
0a6f4824
LG
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
182b1d16
LG
110 to decompress the compressed source buffer.\r
111\r
0a6f4824 112 Retrieves the size of the uncompressed buffer and the temporary scratch buffer\r
182b1d16 113 required to decompress the buffer specified by Source and SourceSize.\r
0a6f4824 114 The size of the uncompressed buffer is returned in DestinationSize,\r
182b1d16 115 the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.\r
0a6f4824 116 This function does not have scratch buffer available to perform a thorough\r
182b1d16
LG
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
0a6f4824 129 is required to decompress the compressed buffer specified\r
182b1d16
LG
130 by Source and SourceSize.\r
131\r
0a6f4824
LG
132 @retval RETURN_SUCCESS The size of the uncompressed data was returned\r
133 in DestinationSize and the size of the scratch\r
182b1d16
LG
134 buffer was returned in ScratchSize.\r
135\r
306bf4e2 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
090d3088 152 *DestinationSize = (UINT32)DecodedSize;\r
57cca89e 153 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
306bf4e2 154 return RETURN_SUCCESS;\r
155}\r
156\r
306bf4e2 157/**\r
182b1d16
LG
158 Decompresses a Lzma compressed source buffer.\r
159\r
160 Extracts decompressed data to its original form.\r
0a6f4824
LG
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
182b1d16
LG
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
75a635ac 167 @param SourceSize The size of source buffer.\r
182b1d16
LG
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
0a6f4824 170 This is an optional parameter that may be NULL if the\r
182b1d16 171 required scratch buffer size is 0.\r
0a6f4824
LG
172\r
173 @retval RETURN_SUCCESS Decompression completed successfully, and\r
182b1d16 174 the uncompressed buffer is returned in Destination.\r
0a6f4824
LG
175 @retval RETURN_INVALID_PARAMETER\r
176 The source buffer specified by Source is corrupted\r
182b1d16 177 (not in a valid compressed format).\r
306bf4e2 178**/\r
179RETURN_STATUS\r
180EFIAPI\r
181LzmaUefiDecompress (\r
182 IN CONST VOID *Source,\r
090d3088 183 IN UINTN SourceSize,\r
306bf4e2 184 IN OUT VOID *Destination,\r
185 IN OUT VOID *Scratch\r
186 )\r
187{\r
76bc52ed 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
0a6f4824 198\r
76bc52ed 199 DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);\r
090d3088 200 EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);\r
57cca89e 201\r
182b1d16 202 LzmaResult = LzmaDecode(\r
306bf4e2 203 Destination,\r
182b1d16 204 &DecodedBufSize,\r
306bf4e2 205 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
182b1d16 206 &EncodedDataSize,\r
306bf4e2 207 Source,\r
208 LZMA_PROPS_SIZE,\r
209 LZMA_FINISH_END,\r
182b1d16 210 &Status,\r
76bc52ed 211 &(AllocFuncs.Functions)\r
306bf4e2 212 );\r
213\r
182b1d16 214 if (LzmaResult == SZ_OK) {\r
306bf4e2 215 return RETURN_SUCCESS;\r
216 } else {\r
217 return RETURN_INVALID_PARAMETER;\r
218 }\r
219}\r
220\r