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