]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/BrotliCustomDecompressLib/BrotliDecompress.c
MdeModulePkg BrotliCustomDecompressLib: Remove the duplicated functions
[mirror_edk2.git] / MdeModulePkg / Library / BrotliCustomDecompressLib / BrotliDecompress.c
CommitLineData
841b2590
SB
1/** @file\r
2 Brotli Decompress interfaces\r
3\r
792ace0a 4 Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
841b2590
SB
6\r
7**/\r
8#include <BrotliDecompressLibInternal.h>\r
9\r
841b2590
SB
10/**\r
11 Allocation routine used by BROTLI decompression.\r
12\r
13 @param Ptr Pointer to the BROTLI_BUFF instance.\r
14 @param Size The size in bytes to be allocated.\r
15\r
16 @return The allocated pointer address, or NULL on failure\r
17**/\r
18VOID *\r
19BrAlloc (\r
20 IN VOID * Ptr,\r
21 IN size_t Size\r
22 )\r
23{\r
24 VOID *Addr;\r
25 BROTLI_BUFF *Private;\r
26\r
27 Private = (BROTLI_BUFF *)Ptr;\r
28\r
29 if (Private->BuffSize >= Size) {\r
30 Addr = Private->Buff;\r
31 Private->Buff = (VOID *) ((UINT8 *)Addr + Size);\r
32 Private->BuffSize -= Size;\r
33 return Addr;\r
34 } else {\r
35 ASSERT (FALSE);\r
36 return NULL;\r
37 }\r
38}\r
39\r
40/**\r
41 Free routine used by BROTLI decompression.\r
42\r
43 @param Ptr Pointer to the BROTLI_BUFF instance\r
44 @param Address The address to be freed\r
45**/\r
46VOID\r
47BrFree (\r
48 IN VOID * Ptr,\r
49 IN VOID * Address\r
50 )\r
51{\r
52 //\r
53 // We use the 'scratch buffer' for allocations, so there is no free\r
54 // operation required. The scratch buffer will be freed by the caller\r
55 // of the decompression code.\r
56 //\r
57}\r
58\r
59/**\r
60 Decompresses a Brotli compressed source buffer.\r
61\r
62 Extracts decompressed data to its original form.\r
63 If the compressed source data specified by Source is successfully decompressed\r
64 into Destination, then EFI_SUCCESS is returned. If the compressed source data\r
65 specified by Source is not in a valid compressed data format,\r
66 then EFI_INVALID_PARAMETER is returned.\r
67\r
68 @param Source The source buffer containing the compressed data.\r
69 @param SourceSize The size of source buffer.\r
70 @param Destination The destination buffer to store the decompressed data.\r
71 @param DestSize The destination buffer size.\r
72 @param BuffInfo The pointer to the BROTLI_BUFF instance.\r
73\r
74 @retval EFI_SUCCESS Decompression completed successfully, and\r
75 the uncompressed buffer is returned in Destination.\r
76 @retval EFI_INVALID_PARAMETER\r
77 The source buffer specified by Source is corrupted\r
78 (not in a valid compressed format).\r
79**/\r
80EFI_STATUS\r
81BrotliDecompress (\r
82 IN CONST VOID* Source,\r
83 IN UINTN SourceSize,\r
84 IN OUT VOID* Destination,\r
85 IN OUT UINTN DestSize,\r
86 IN VOID * BuffInfo\r
87 )\r
88{\r
89 UINT8 * Input;\r
90 UINT8 * Output;\r
91 const UINT8 * NextIn;\r
92 UINT8 * NextOut;\r
93 size_t TotalOut;\r
94 size_t AvailableIn;\r
95 size_t AvailableOut;\r
841b2590 96 VOID * Temp;\r
2730470f
LG
97 BrotliDecoderResult Result;\r
98 BrotliDecoderState * BroState;\r
841b2590 99\r
36a0d5ca 100 TotalOut = 0;\r
841b2590 101 AvailableOut = FILE_BUFFER_SIZE;\r
2730470f
LG
102 Result = BROTLI_DECODER_RESULT_ERROR;\r
103 BroState = BrotliDecoderCreateInstance(BrAlloc, BrFree, BuffInfo);\r
841b2590
SB
104 Temp = Destination;\r
105\r
106 if (BroState == NULL) {\r
107 return EFI_INVALID_PARAMETER;\r
108 }\r
109 Input = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);\r
110 Output = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);\r
111 if ((Input==NULL) || (Output==NULL)) {\r
112 BrFree(BuffInfo, Input);\r
113 BrFree(BuffInfo, Output);\r
2730470f 114 BrotliDecoderDestroyInstance(BroState);\r
841b2590
SB
115 return EFI_INVALID_PARAMETER;\r
116 }\r
117 NextOut = Output;\r
2730470f 118 Result = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;\r
841b2590 119 while (1) {\r
2730470f 120 if (Result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {\r
841b2590
SB
121 if (SourceSize == 0) {\r
122 break;\r
123 }\r
124 if (SourceSize >= FILE_BUFFER_SIZE) {\r
125 AvailableIn = FILE_BUFFER_SIZE;\r
126 }else{\r
127 AvailableIn = SourceSize;\r
128 }\r
129 CopyMem(Input, Source, AvailableIn);\r
130 Source = (VOID *)((UINT8 *)Source + AvailableIn);\r
131 SourceSize -= AvailableIn;\r
132 NextIn = Input;\r
2730470f 133 } else if (Result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {\r
841b2590
SB
134 CopyMem(Temp, Output, FILE_BUFFER_SIZE);\r
135 AvailableOut = FILE_BUFFER_SIZE;\r
136 Temp = (VOID *)((UINT8 *)Temp +FILE_BUFFER_SIZE);\r
137 NextOut = Output;\r
138 } else {\r
139 break; /* Error or success. */\r
140 }\r
2730470f
LG
141 Result = BrotliDecoderDecompressStream(\r
142 BroState,\r
841b2590
SB
143 &AvailableIn,\r
144 &NextIn,\r
145 &AvailableOut,\r
146 &NextOut,\r
2730470f 147 &TotalOut\r
841b2590
SB
148 );\r
149 }\r
150 if (NextOut != Output) {\r
151 CopyMem(Temp, Output, (size_t)(NextOut - Output));\r
152 }\r
153\r
154 DestSize = TotalOut;\r
155\r
156 BrFree(BuffInfo, Input);\r
157 BrFree(BuffInfo, Output);\r
2730470f
LG
158 BrotliDecoderDestroyInstance(BroState);\r
159 return (Result == BROTLI_DECODER_RESULT_SUCCESS) ? EFI_SUCCESS : EFI_INVALID_PARAMETER;\r
841b2590
SB
160}\r
161\r
162/**\r
163 Get the size of the uncompressed buffer by parsing EncodeData header.\r
164\r
165 @param EncodedData Pointer to the compressed data.\r
166 @param StartOffset Start offset of the compressed data.\r
167 @param EndOffset End offset of the compressed data.\r
168\r
169 @return The size of the uncompressed buffer.\r
170**/\r
171UINT64\r
792ace0a 172BrGetDecodedSizeOfBuf(\r
841b2590
SB
173 IN UINT8 * EncodedData,\r
174 IN UINT8 StartOffset,\r
175 IN UINT8 EndOffset\r
176 )\r
177{\r
178 UINT64 DecodedSize;\r
179 INTN Index;\r
180\r
181 /* Parse header */\r
182 DecodedSize = 0;\r
183 for (Index = EndOffset - 1; Index >= StartOffset; Index--)\r
184 DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];\r
185\r
186 return DecodedSize;\r
187}\r
188\r
189/**\r
190 Given a Brotli compressed source buffer, this function retrieves the size of\r
191 the uncompressed buffer and the size of the scratch buffer required\r
192 to decompress the compressed source buffer.\r
193\r
194 Retrieves the size of the uncompressed buffer and the temporary scratch buffer\r
195 required to decompress the buffer specified by Source and SourceSize.\r
196 The size of the uncompressed buffer is returned in DestinationSize,\r
197 the size of the scratch buffer is returned in ScratchSize, and EFI_SUCCESS is returned.\r
198 This function does not have scratch buffer available to perform a thorough\r
199 checking of the validity of the source data. It just retrieves the "Original Size"\r
200 field from the BROTLI_SCRATCH_MAX beginning bytes of the source data and output it as DestinationSize.\r
201 And ScratchSize is specific to the decompression implementation.\r
202\r
203 If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().\r
204\r
205 @param Source The source buffer containing the compressed data.\r
206 @param SourceSize The size, in bytes, of the source buffer.\r
207 @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer\r
208 that will be generated when the compressed buffer specified\r
209 by Source and SourceSize is decompressed.\r
210 @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that\r
211 is required to decompress the compressed buffer specified\r
212 by Source and SourceSize.\r
213\r
214 @retval EFI_SUCCESS The size of the uncompressed data was returned\r
215 in DestinationSize and the size of the scratch\r
216 buffer was returned in ScratchSize.\r
217**/\r
218EFI_STATUS\r
219EFIAPI\r
220BrotliUefiDecompressGetInfo (\r
221 IN CONST VOID * Source,\r
222 IN UINT32 SourceSize,\r
223 OUT UINT32 * DestinationSize,\r
224 OUT UINT32 * ScratchSize\r
225 )\r
226{\r
227 UINT64 GetSize;\r
228 UINT8 MaxOffset;\r
229\r
230 ASSERT(SourceSize >= BROTLI_SCRATCH_MAX);\r
231\r
232 MaxOffset = BROTLI_DECODE_MAX;\r
792ace0a 233 GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);\r
841b2590
SB
234 *DestinationSize = (UINT32)GetSize;\r
235 MaxOffset = BROTLI_SCRATCH_MAX;\r
792ace0a 236 GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);\r
841b2590
SB
237 *ScratchSize = (UINT32)GetSize;\r
238 return EFI_SUCCESS;\r
239}\r
240\r
241/**\r
242 Decompresses a Brotli compressed source buffer.\r
243\r
244 Extracts decompressed data to its original form.\r
245 If the compressed source data specified by Source is successfully decompressed\r
246 into Destination, then RETURN_SUCCESS is returned. If the compressed source data\r
247 specified by Source is not in a valid compressed data format,\r
248 then RETURN_INVALID_PARAMETER is returned.\r
249\r
250 @param Source The source buffer containing the compressed data.\r
251 @param SourceSize The size of source buffer.\r
252 @param Destination The destination buffer to store the decompressed data\r
253 @param Scratch A temporary scratch buffer that is used to perform the decompression.\r
254 This is an optional parameter that may be NULL if the\r
255 required scratch buffer size is 0.\r
256\r
257 @retval EFI_SUCCESS Decompression completed successfully, and\r
258 the uncompressed buffer is returned in Destination.\r
259 @retval EFI_INVALID_PARAMETER\r
260 The source buffer specified by Source is corrupted\r
261 (not in a valid compressed format).\r
262**/\r
263EFI_STATUS\r
264EFIAPI\r
265BrotliUefiDecompress (\r
266 IN CONST VOID * Source,\r
267 IN UINTN SourceSize,\r
268 IN OUT VOID * Destination,\r
269 IN OUT VOID * Scratch\r
270 )\r
271{\r
272 UINTN DestSize = 0;\r
273 EFI_STATUS Status;\r
274 BROTLI_BUFF BroBuff;\r
275 UINT64 GetSize;\r
276 UINT8 MaxOffset;\r
277\r
278 MaxOffset = BROTLI_SCRATCH_MAX;\r
792ace0a 279 GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);\r
841b2590
SB
280\r
281 BroBuff.Buff = Scratch;\r
282 BroBuff.BuffSize = (UINTN)GetSize;\r
283\r
284 Status = BrotliDecompress(\r
285 (VOID *)((UINT8 *)Source + BROTLI_SCRATCH_MAX),\r
286 SourceSize - BROTLI_SCRATCH_MAX,\r
287 Destination,\r
288 DestSize,\r
289 (VOID *)(&BroBuff)\r
290 );\r
291\r
292 return Status;\r
293}\r