MdeModulePkg BrotliLib: Rename function with the specific lib name
[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 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 BrDummyMalloc (
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 BrDummyFree (
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 TotalOut = 0;
130 AvailableOut = FILE_BUFFER_SIZE;
131 Result = BROTLI_RESULT_ERROR;
132 BroState = BrotliCreateState(BrAlloc, BrFree, BuffInfo);
133 Temp = Destination;
134
135 if (BroState == NULL) {
136 return EFI_INVALID_PARAMETER;
137 }
138 Input = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);
139 Output = (UINT8 *)BrAlloc(BuffInfo, FILE_BUFFER_SIZE);
140 if ((Input==NULL) || (Output==NULL)) {
141 BrFree(BuffInfo, Input);
142 BrFree(BuffInfo, Output);
143 BrotliDestroyState(BroState);
144 return EFI_INVALID_PARAMETER;
145 }
146 NextOut = Output;
147 Result = BROTLI_RESULT_NEEDS_MORE_INPUT;
148 while (1) {
149 if (Result == BROTLI_RESULT_NEEDS_MORE_INPUT) {
150 if (SourceSize == 0) {
151 break;
152 }
153 if (SourceSize >= FILE_BUFFER_SIZE) {
154 AvailableIn = FILE_BUFFER_SIZE;
155 }else{
156 AvailableIn = SourceSize;
157 }
158 CopyMem(Input, Source, AvailableIn);
159 Source = (VOID *)((UINT8 *)Source + AvailableIn);
160 SourceSize -= AvailableIn;
161 NextIn = Input;
162 } else if (Result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
163 CopyMem(Temp, Output, FILE_BUFFER_SIZE);
164 AvailableOut = FILE_BUFFER_SIZE;
165 Temp = (VOID *)((UINT8 *)Temp +FILE_BUFFER_SIZE);
166 NextOut = Output;
167 } else {
168 break; /* Error or success. */
169 }
170 Result = BrotliDecompressStream(
171 &AvailableIn,
172 &NextIn,
173 &AvailableOut,
174 &NextOut,
175 &TotalOut,
176 BroState
177 );
178 }
179 if (NextOut != Output) {
180 CopyMem(Temp, Output, (size_t)(NextOut - Output));
181 }
182
183 DestSize = TotalOut;
184
185 BrFree(BuffInfo, Input);
186 BrFree(BuffInfo, Output);
187 BrotliDestroyState(BroState);
188 return (Result == BROTLI_RESULT_SUCCESS) ? EFI_SUCCESS : EFI_INVALID_PARAMETER;
189 }
190
191 /**
192 Get the size of the uncompressed buffer by parsing EncodeData header.
193
194 @param EncodedData Pointer to the compressed data.
195 @param StartOffset Start offset of the compressed data.
196 @param EndOffset End offset of the compressed data.
197
198 @return The size of the uncompressed buffer.
199 **/
200 UINT64
201 BrGetDecodedSizeOfBuf(
202 IN UINT8 * EncodedData,
203 IN UINT8 StartOffset,
204 IN UINT8 EndOffset
205 )
206 {
207 UINT64 DecodedSize;
208 INTN Index;
209
210 /* Parse header */
211 DecodedSize = 0;
212 for (Index = EndOffset - 1; Index >= StartOffset; Index--)
213 DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];
214
215 return DecodedSize;
216 }
217
218 /**
219 Given a Brotli compressed source buffer, this function retrieves the size of
220 the uncompressed buffer and the size of the scratch buffer required
221 to decompress the compressed source buffer.
222
223 Retrieves the size of the uncompressed buffer and the temporary scratch buffer
224 required to decompress the buffer specified by Source and SourceSize.
225 The size of the uncompressed buffer is returned in DestinationSize,
226 the size of the scratch buffer is returned in ScratchSize, and EFI_SUCCESS is returned.
227 This function does not have scratch buffer available to perform a thorough
228 checking of the validity of the source data. It just retrieves the "Original Size"
229 field from the BROTLI_SCRATCH_MAX beginning bytes of the source data and output it as DestinationSize.
230 And ScratchSize is specific to the decompression implementation.
231
232 If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().
233
234 @param Source The source buffer containing the compressed data.
235 @param SourceSize The size, in bytes, of the source buffer.
236 @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
237 that will be generated when the compressed buffer specified
238 by Source and SourceSize is decompressed.
239 @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that
240 is required to decompress the compressed buffer specified
241 by Source and SourceSize.
242
243 @retval EFI_SUCCESS The size of the uncompressed data was returned
244 in DestinationSize and the size of the scratch
245 buffer was returned in ScratchSize.
246 **/
247 EFI_STATUS
248 EFIAPI
249 BrotliUefiDecompressGetInfo (
250 IN CONST VOID * Source,
251 IN UINT32 SourceSize,
252 OUT UINT32 * DestinationSize,
253 OUT UINT32 * ScratchSize
254 )
255 {
256 UINT64 GetSize;
257 UINT8 MaxOffset;
258
259 ASSERT(SourceSize >= BROTLI_SCRATCH_MAX);
260
261 MaxOffset = BROTLI_DECODE_MAX;
262 GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
263 *DestinationSize = (UINT32)GetSize;
264 MaxOffset = BROTLI_SCRATCH_MAX;
265 GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
266 *ScratchSize = (UINT32)GetSize;
267 return EFI_SUCCESS;
268 }
269
270 /**
271 Decompresses a Brotli compressed source buffer.
272
273 Extracts decompressed data to its original form.
274 If the compressed source data specified by Source is successfully decompressed
275 into Destination, then RETURN_SUCCESS is returned. If the compressed source data
276 specified by Source is not in a valid compressed data format,
277 then RETURN_INVALID_PARAMETER is returned.
278
279 @param Source The source buffer containing the compressed data.
280 @param SourceSize The size of source buffer.
281 @param Destination The destination buffer to store the decompressed data
282 @param Scratch A temporary scratch buffer that is used to perform the decompression.
283 This is an optional parameter that may be NULL if the
284 required scratch buffer size is 0.
285
286 @retval EFI_SUCCESS Decompression completed successfully, and
287 the uncompressed buffer is returned in Destination.
288 @retval EFI_INVALID_PARAMETER
289 The source buffer specified by Source is corrupted
290 (not in a valid compressed format).
291 **/
292 EFI_STATUS
293 EFIAPI
294 BrotliUefiDecompress (
295 IN CONST VOID * Source,
296 IN UINTN SourceSize,
297 IN OUT VOID * Destination,
298 IN OUT VOID * Scratch
299 )
300 {
301 UINTN DestSize = 0;
302 EFI_STATUS Status;
303 BROTLI_BUFF BroBuff;
304 UINT64 GetSize;
305 UINT8 MaxOffset;
306
307 MaxOffset = BROTLI_SCRATCH_MAX;
308 GetSize = BrGetDecodedSizeOfBuf((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
309
310 BroBuff.Buff = Scratch;
311 BroBuff.BuffSize = (UINTN)GetSize;
312
313 Status = BrotliDecompress(
314 (VOID *)((UINT8 *)Source + BROTLI_SCRATCH_MAX),
315 SourceSize - BROTLI_SCRATCH_MAX,
316 Destination,
317 DestSize,
318 (VOID *)(&BroBuff)
319 );
320
321 return Status;
322 }