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