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