]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
MdeModulePkg: Unify the definitions of size_t
[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
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
146 *DestinationSize = (UINT32)DecodedSize;\r
147 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
148 return RETURN_SUCCESS;\r
149}\r
150\r
151/**\r
152 Decompresses a Lzma compressed source buffer.\r
153\r
154 Extracts decompressed data to its original form.\r
d1102dba
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
11ff2c71
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
161 @param SourceSize The size of source buffer.\r
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
d1102dba 164 This is an optional parameter that may be NULL if the\r
11ff2c71 165 required scratch buffer size is 0.\r
d1102dba
LG
166\r
167 @retval RETURN_SUCCESS Decompression completed successfully, and\r
11ff2c71 168 the uncompressed buffer is returned in Destination.\r
d1102dba
LG
169 @retval RETURN_INVALID_PARAMETER\r
170 The source buffer specified by Source is corrupted\r
11ff2c71
LG
171 (not in a valid compressed format).\r
172**/\r
173RETURN_STATUS\r
174EFIAPI\r
175LzmaUefiDecompress (\r
176 IN CONST VOID *Source,\r
177 IN UINTN SourceSize,\r
178 IN OUT VOID *Destination,\r
179 IN OUT VOID *Scratch\r
180 )\r
181{\r
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
d1102dba 192\r
11ff2c71
LG
193 DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);\r
194 EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);\r
195\r
196 LzmaResult = LzmaDecode(\r
197 Destination,\r
198 &DecodedBufSize,\r
199 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
200 &EncodedDataSize,\r
201 Source,\r
202 LZMA_PROPS_SIZE,\r
203 LZMA_FINISH_END,\r
204 &Status,\r
205 &(AllocFuncs.Functions)\r
206 );\r
207\r
208 if (LzmaResult == SZ_OK) {\r
209 return RETURN_SUCCESS;\r
210 } else {\r
211 return RETURN_INVALID_PARAMETER;\r
212 }\r
213}\r
214\r