]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
IntelFrameworkModulePkg: Replace BSD License with BSD+Patent License
[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
c0a00b14 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
306bf4e2 6\r
7**/\r
8\r
182b1d16 9#include "LzmaDecompressLibInternal.h"\r
00f5e119 10#include "Sdk/C/7zTypes.h"\r
306bf4e2 11#include "Sdk/C/7zVersion.h"\r
12#include "Sdk/C/LzmaDec.h"\r
13\r
57cca89e 14#define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB\r
15\r
090d3088 16typedef struct\r
17{\r
18 ISzAlloc Functions;\r
19 VOID *Buffer;\r
20 UINTN BufferSize;\r
21} ISzAllocWithData;\r
22\r
19a4a0a0 23/**\r
24 Allocation routine used by LZMA decompression.\r
25\r
182b1d16
LG
26 @param P Pointer to the ISzAlloc instance\r
27 @param Size The size in bytes to be allocated\r
19a4a0a0 28\r
29 @return The allocated pointer address, or NULL on failure\r
30**/\r
19a4a0a0 31VOID *\r
32SzAlloc (\r
39bbbc87 33 CONST ISzAlloc *P,\r
182b1d16 34 size_t Size\r
19a4a0a0 35 )\r
36{\r
182b1d16 37 VOID *Addr;\r
75a635ac 38 ISzAllocWithData *Private;\r
39\r
40 Private = (ISzAllocWithData*) P;\r
57cca89e 41\r
090d3088 42 if (Private->BufferSize >= Size) {\r
43 Addr = Private->Buffer;\r
44 Private->Buffer = (VOID*) ((UINT8*)Addr + Size);\r
45 Private->BufferSize -= Size;\r
182b1d16 46 return Addr;\r
57cca89e 47 } else {\r
48 ASSERT (FALSE);\r
49 return NULL;\r
50 }\r
19a4a0a0 51}\r
52\r
53/**\r
54 Free routine used by LZMA decompression.\r
55\r
182b1d16
LG
56 @param P Pointer to the ISzAlloc instance\r
57 @param Address The address to be freed\r
19a4a0a0 58**/\r
19a4a0a0 59VOID\r
60SzFree (\r
39bbbc87 61 CONST ISzAlloc *P,\r
182b1d16 62 VOID *Address\r
19a4a0a0 63 )\r
64{\r
57cca89e 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
19a4a0a0 70}\r
71\r
306bf4e2 72#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
73\r
182b1d16
LG
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
306bf4e2 81UINT64\r
82GetDecodedSizeOfBuf(\r
182b1d16 83 UINT8 *EncodedData\r
306bf4e2 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
182b1d16 92 DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];\r
306bf4e2 93\r
94 return DecodedSize;\r
95}\r
96\r
97//\r
182b1d16 98// LZMA functions and data as defined in local LzmaDecompressLibInternal.h\r
306bf4e2 99//\r
100\r
306bf4e2 101/**\r
0a6f4824
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
182b1d16
LG
104 to decompress the compressed source buffer.\r
105\r
0a6f4824 106 Retrieves the size of the uncompressed buffer and the temporary scratch buffer\r
182b1d16 107 required to decompress the buffer specified by Source and SourceSize.\r
0a6f4824 108 The size of the uncompressed buffer is returned in DestinationSize,\r
182b1d16 109 the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.\r
0a6f4824 110 This function does not have scratch buffer available to perform a thorough\r
182b1d16
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
0a6f4824 123 is required to decompress the compressed buffer specified\r
182b1d16
LG
124 by Source and SourceSize.\r
125\r
0a6f4824
LG
126 @retval RETURN_SUCCESS The size of the uncompressed data was returned\r
127 in DestinationSize and the size of the scratch\r
182b1d16
LG
128 buffer was returned in ScratchSize.\r
129\r
306bf4e2 130**/\r
131RETURN_STATUS\r
132EFIAPI\r
133LzmaUefiDecompressGetInfo (\r
134 IN CONST VOID *Source,\r
135 IN UINT32 SourceSize,\r
136 OUT UINT32 *DestinationSize,\r
137 OUT UINT32 *ScratchSize\r
138 )\r
139{\r
140 UInt64 DecodedSize;\r
141\r
142 ASSERT(SourceSize >= LZMA_HEADER_SIZE);\r
143\r
144 DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
145\r
090d3088 146 *DestinationSize = (UINT32)DecodedSize;\r
57cca89e 147 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
306bf4e2 148 return RETURN_SUCCESS;\r
149}\r
150\r
306bf4e2 151/**\r
182b1d16
LG
152 Decompresses a Lzma compressed source buffer.\r
153\r
154 Extracts decompressed data to its original form.\r
0a6f4824
LG
155 If the compressed source data specified by Source is successfully decompressed\r
156 into Destination, then RETURN_SUCCESS is returned. If the compressed source data\r
182b1d16
LG
157 specified by Source is not in a valid compressed data format,\r
158 then RETURN_INVALID_PARAMETER is returned.\r
159\r
160 @param Source The source buffer containing the compressed data.\r
75a635ac 161 @param SourceSize The size of source buffer.\r
182b1d16
LG
162 @param Destination The destination buffer to store the decompressed data\r
163 @param Scratch A temporary scratch buffer that is used to perform the decompression.\r
0a6f4824 164 This is an optional parameter that may be NULL if the\r
182b1d16 165 required scratch buffer size is 0.\r
0a6f4824
LG
166\r
167 @retval RETURN_SUCCESS Decompression completed successfully, and\r
182b1d16 168 the uncompressed buffer is returned in Destination.\r
0a6f4824
LG
169 @retval RETURN_INVALID_PARAMETER\r
170 The source buffer specified by Source is corrupted\r
182b1d16 171 (not in a valid compressed format).\r
306bf4e2 172**/\r
173RETURN_STATUS\r
174EFIAPI\r
175LzmaUefiDecompress (\r
176 IN CONST VOID *Source,\r
090d3088 177 IN UINTN SourceSize,\r
306bf4e2 178 IN OUT VOID *Destination,\r
179 IN OUT VOID *Scratch\r
180 )\r
181{\r
76bc52ed 182 SRes LzmaResult;\r
183 ELzmaStatus Status;\r
184 SizeT DecodedBufSize;\r
185 SizeT EncodedDataSize;\r
186 ISzAllocWithData AllocFuncs;\r
187\r
188 AllocFuncs.Functions.Alloc = SzAlloc;\r
189 AllocFuncs.Functions.Free = SzFree;\r
190 AllocFuncs.Buffer = Scratch;\r
191 AllocFuncs.BufferSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
0a6f4824 192\r
76bc52ed 193 DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);\r
090d3088 194 EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);\r
57cca89e 195\r
182b1d16 196 LzmaResult = LzmaDecode(\r
306bf4e2 197 Destination,\r
182b1d16 198 &DecodedBufSize,\r
306bf4e2 199 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
182b1d16 200 &EncodedDataSize,\r
306bf4e2 201 Source,\r
202 LZMA_PROPS_SIZE,\r
203 LZMA_FINISH_END,\r
182b1d16 204 &Status,\r
76bc52ed 205 &(AllocFuncs.Functions)\r
306bf4e2 206 );\r
207\r
182b1d16 208 if (LzmaResult == SZ_OK) {\r
306bf4e2 209 return RETURN_SUCCESS;\r
210 } else {\r
211 return RETURN_INVALID_PARAMETER;\r
212 }\r
213}\r
214\r