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
;
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
;
118 Result
= BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT
;
120 if (Result
== BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT
) {
121 if (SourceSize
== 0) {
124 if (SourceSize
>= FILE_BUFFER_SIZE
) {
125 AvailableIn
= FILE_BUFFER_SIZE
;
127 AvailableIn
= SourceSize
;
129 CopyMem(Input
, Source
, AvailableIn
);
130 Source
= (VOID
*)((UINT8
*)Source
+ AvailableIn
);
131 SourceSize
-= AvailableIn
;
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
);
139 break; /* Error or success. */
141 Result
= BrotliDecoderDecompressStream(
150 if (NextOut
!= Output
) {
151 CopyMem(Temp
, Output
, (size_t)(NextOut
- Output
));
156 BrFree(BuffInfo
, Input
);
157 BrFree(BuffInfo
, Output
);
158 BrotliDecoderDestroyInstance(BroState
);
159 return (Result
== BROTLI_DECODER_RESULT_SUCCESS
) ? EFI_SUCCESS
: EFI_INVALID_PARAMETER
;
163 Get the size of the uncompressed buffer by parsing EncodeData header.
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.
169 @return The size of the uncompressed buffer.
172 BrGetDecodedSizeOfBuf(
173 IN UINT8
* EncodedData
,
174 IN UINT8 StartOffset
,
183 for (Index
= EndOffset
- 1; Index
>= StartOffset
; Index
--)
184 DecodedSize
= LShiftU64(DecodedSize
, 8) + EncodedData
[Index
];
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.
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.
203 If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().
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.
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.
220 BrotliUefiDecompressGetInfo (
221 IN CONST VOID
* Source
,
222 IN UINT32 SourceSize
,
223 OUT UINT32
* DestinationSize
,
224 OUT UINT32
* ScratchSize
230 ASSERT(SourceSize
>= BROTLI_SCRATCH_MAX
);
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
;
242 Decompresses a Brotli compressed source buffer.
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.
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.
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).
265 BrotliUefiDecompress (
266 IN CONST VOID
* Source
,
268 IN OUT VOID
* Destination
,
269 IN OUT VOID
* Scratch
278 MaxOffset
= BROTLI_SCRATCH_MAX
;
279 GetSize
= BrGetDecodedSizeOfBuf((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
281 BroBuff
.Buff
= Scratch
;
282 BroBuff
.BuffSize
= (UINTN
)GetSize
;
284 Status
= BrotliDecompress(
285 (VOID
*)((UINT8
*)Source
+ BROTLI_SCRATCH_MAX
),
286 SourceSize
- BROTLI_SCRATCH_MAX
,