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 Allocation routine used by BROTLI decompression.
13 @param Ptr Pointer to the BROTLI_BUFF instance.
14 @param Size The size in bytes to be allocated.
16 @return The allocated pointer address, or NULL on failure
27 Private
= (BROTLI_BUFF
*)Ptr
;
29 if (Private
->BuffSize
>= Size
) {
31 Private
->Buff
= (VOID
*)((UINT8
*)Addr
+ Size
);
32 Private
->BuffSize
-= Size
;
41 Free routine used by BROTLI decompression.
43 @param Ptr Pointer to the BROTLI_BUFF instance
44 @param Address The address to be freed
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.
60 Decompresses a Brotli compressed source buffer.
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.
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.
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).
82 IN CONST VOID
*Source
,
84 IN OUT VOID
*Destination
,
85 IN OUT UINTN DestSize
,
97 BrotliDecoderResult Result
;
98 BrotliDecoderState
*BroState
;
101 AvailableOut
= FILE_BUFFER_SIZE
;
102 Result
= BROTLI_DECODER_RESULT_ERROR
;
103 BroState
= BrotliDecoderCreateInstance (BrAlloc
, BrFree
, BuffInfo
);
106 if (BroState
== NULL
) {
107 return EFI_INVALID_PARAMETER
;
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
;
120 Result
= BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT
;
122 if (Result
== BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT
) {
123 if (SourceSize
== 0) {
127 if (SourceSize
>= FILE_BUFFER_SIZE
) {
128 AvailableIn
= FILE_BUFFER_SIZE
;
130 AvailableIn
= SourceSize
;
133 CopyMem (Input
, Source
, AvailableIn
);
134 Source
= (VOID
*)((UINT8
*)Source
+ AvailableIn
);
135 SourceSize
-= AvailableIn
;
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
);
143 break; /* Error or success. */
146 Result
= BrotliDecoderDecompressStream (
156 if (NextOut
!= Output
) {
157 CopyMem (Temp
, Output
, (size_t)(NextOut
- Output
));
162 BrFree (BuffInfo
, Input
);
163 BrFree (BuffInfo
, Output
);
164 BrotliDecoderDestroyInstance (BroState
);
165 return (Result
== BROTLI_DECODER_RESULT_SUCCESS
) ? EFI_SUCCESS
: EFI_INVALID_PARAMETER
;
169 Get the size of the uncompressed buffer by parsing EncodeData header.
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.
175 @return The size of the uncompressed buffer.
178 BrGetDecodedSizeOfBuf (
179 IN UINT8
*EncodedData
,
180 IN UINT8 StartOffset
,
189 for (Index
= EndOffset
- 1; Index
>= StartOffset
; Index
--) {
190 DecodedSize
= LShiftU64 (DecodedSize
, 8) + EncodedData
[Index
];
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.
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.
210 If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().
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.
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.
227 BrotliUefiDecompressGetInfo (
228 IN CONST VOID
*Source
,
229 IN UINT32 SourceSize
,
230 OUT UINT32
*DestinationSize
,
231 OUT UINT32
*ScratchSize
237 ASSERT (SourceSize
>= BROTLI_SCRATCH_MAX
);
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
;
249 Decompresses a Brotli compressed source buffer.
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.
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.
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).
272 BrotliUefiDecompress (
273 IN CONST VOID
*Source
,
275 IN OUT VOID
*Destination
,
285 MaxOffset
= BROTLI_SCRATCH_MAX
;
286 GetSize
= BrGetDecodedSizeOfBuf ((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
288 BroBuff
.Buff
= Scratch
;
289 BroBuff
.BuffSize
= (UINTN
)GetSize
;
291 Status
= BrotliDecompress (
292 (VOID
*)((UINT8
*)Source
+ BROTLI_SCRATCH_MAX
),
293 SourceSize
- BROTLI_SCRATCH_MAX
,