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