2 Brotli Decompress interfaces
4 Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #include <BrotliDecompressLibInternal.h>
11 Dummy malloc function for compiler.
23 Dummy free function for compiler.
34 Allocation routine used by BROTLI decompression.
36 @param Ptr Pointer to the BROTLI_BUFF instance.
37 @param Size The size in bytes to be allocated.
39 @return The allocated pointer address, or NULL on failure
50 Private
= (BROTLI_BUFF
*)Ptr
;
52 if (Private
->BuffSize
>= Size
) {
54 Private
->Buff
= (VOID
*) ((UINT8
*)Addr
+ Size
);
55 Private
->BuffSize
-= Size
;
64 Free routine used by BROTLI decompression.
66 @param Ptr Pointer to the BROTLI_BUFF instance
67 @param Address The address to be freed
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.
83 Decompresses a Brotli compressed source buffer.
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.
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.
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).
105 IN CONST VOID
* Source
,
107 IN OUT VOID
* Destination
,
108 IN OUT UINTN DestSize
,
114 const UINT8
* NextIn
;
120 BrotliDecoderResult Result
;
121 BrotliDecoderState
* BroState
;
124 AvailableOut
= FILE_BUFFER_SIZE
;
125 Result
= BROTLI_DECODER_RESULT_ERROR
;
126 BroState
= BrotliDecoderCreateInstance(BrAlloc
, BrFree
, BuffInfo
);
129 if (BroState
== NULL
) {
130 return EFI_INVALID_PARAMETER
;
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
;
141 Result
= BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT
;
143 if (Result
== BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT
) {
144 if (SourceSize
== 0) {
147 if (SourceSize
>= FILE_BUFFER_SIZE
) {
148 AvailableIn
= FILE_BUFFER_SIZE
;
150 AvailableIn
= SourceSize
;
152 CopyMem(Input
, Source
, AvailableIn
);
153 Source
= (VOID
*)((UINT8
*)Source
+ AvailableIn
);
154 SourceSize
-= AvailableIn
;
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
);
162 break; /* Error or success. */
164 Result
= BrotliDecoderDecompressStream(
173 if (NextOut
!= Output
) {
174 CopyMem(Temp
, Output
, (size_t)(NextOut
- Output
));
179 BrFree(BuffInfo
, Input
);
180 BrFree(BuffInfo
, Output
);
181 BrotliDecoderDestroyInstance(BroState
);
182 return (Result
== BROTLI_DECODER_RESULT_SUCCESS
) ? EFI_SUCCESS
: EFI_INVALID_PARAMETER
;
186 Get the size of the uncompressed buffer by parsing EncodeData header.
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.
192 @return The size of the uncompressed buffer.
195 BrGetDecodedSizeOfBuf(
196 IN UINT8
* EncodedData
,
197 IN UINT8 StartOffset
,
206 for (Index
= EndOffset
- 1; Index
>= StartOffset
; Index
--)
207 DecodedSize
= LShiftU64(DecodedSize
, 8) + EncodedData
[Index
];
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.
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.
226 If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().
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.
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.
243 BrotliUefiDecompressGetInfo (
244 IN CONST VOID
* Source
,
245 IN UINT32 SourceSize
,
246 OUT UINT32
* DestinationSize
,
247 OUT UINT32
* ScratchSize
253 ASSERT(SourceSize
>= BROTLI_SCRATCH_MAX
);
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
;
265 Decompresses a Brotli compressed source buffer.
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.
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.
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).
288 BrotliUefiDecompress (
289 IN CONST VOID
* Source
,
291 IN OUT VOID
* Destination
,
292 IN OUT VOID
* Scratch
301 MaxOffset
= BROTLI_SCRATCH_MAX
;
302 GetSize
= BrGetDecodedSizeOfBuf((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
304 BroBuff
.Buff
= Scratch
;
305 BroBuff
.BuffSize
= (UINTN
)GetSize
;
307 Status
= BrotliDecompress(
308 (VOID
*)((UINT8
*)Source
+ BROTLI_SCRATCH_MAX
),
309 SourceSize
- BROTLI_SCRATCH_MAX
,