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