2 Brotli Decompress interfaces
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #include <BrotliDecompressLibInternal.h>
17 Dummy malloc function for compiler.
29 Dummy free function for compiler.
40 Allocation routine used by BROTLI decompression.
42 @param Ptr Pointer to the BROTLI_BUFF instance.
43 @param Size The size in bytes to be allocated.
45 @return The allocated pointer address, or NULL on failure
56 Private
= (BROTLI_BUFF
*)Ptr
;
58 if (Private
->BuffSize
>= Size
) {
60 Private
->Buff
= (VOID
*) ((UINT8
*)Addr
+ Size
);
61 Private
->BuffSize
-= Size
;
70 Free routine used by BROTLI decompression.
72 @param Ptr Pointer to the BROTLI_BUFF instance
73 @param Address The address to be freed
82 // We use the 'scratch buffer' for allocations, so there is no free
83 // operation required. The scratch buffer will be freed by the caller
84 // of the decompression code.
89 Decompresses a Brotli compressed source buffer.
91 Extracts decompressed data to its original form.
92 If the compressed source data specified by Source is successfully decompressed
93 into Destination, then EFI_SUCCESS is returned. If the compressed source data
94 specified by Source is not in a valid compressed data format,
95 then EFI_INVALID_PARAMETER is returned.
97 @param Source The source buffer containing the compressed data.
98 @param SourceSize The size of source buffer.
99 @param Destination The destination buffer to store the decompressed data.
100 @param DestSize The destination buffer size.
101 @param BuffInfo The pointer to the BROTLI_BUFF instance.
103 @retval EFI_SUCCESS Decompression completed successfully, and
104 the uncompressed buffer is returned in Destination.
105 @retval EFI_INVALID_PARAMETER
106 The source buffer specified by Source is corrupted
107 (not in a valid compressed format).
111 IN CONST VOID
* Source
,
113 IN OUT VOID
* Destination
,
114 IN OUT UINTN DestSize
,
120 const UINT8
* NextIn
;
126 BrotliState
* BroState
;
130 AvailableOut
= FILE_BUFFER_SIZE
;
131 Result
= BROTLI_RESULT_ERROR
;
132 BroState
= BrotliCreateState(BrAlloc
, BrFree
, BuffInfo
);
135 if (BroState
== NULL
) {
136 return EFI_INVALID_PARAMETER
;
138 Input
= (UINT8
*)BrAlloc(BuffInfo
, FILE_BUFFER_SIZE
);
139 Output
= (UINT8
*)BrAlloc(BuffInfo
, FILE_BUFFER_SIZE
);
140 if ((Input
==NULL
) || (Output
==NULL
)) {
141 BrFree(BuffInfo
, Input
);
142 BrFree(BuffInfo
, Output
);
143 BrotliDestroyState(BroState
);
144 return EFI_INVALID_PARAMETER
;
147 Result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
149 if (Result
== BROTLI_RESULT_NEEDS_MORE_INPUT
) {
150 if (SourceSize
== 0) {
153 if (SourceSize
>= FILE_BUFFER_SIZE
) {
154 AvailableIn
= FILE_BUFFER_SIZE
;
156 AvailableIn
= SourceSize
;
158 CopyMem(Input
, Source
, AvailableIn
);
159 Source
= (VOID
*)((UINT8
*)Source
+ AvailableIn
);
160 SourceSize
-= AvailableIn
;
162 } else if (Result
== BROTLI_RESULT_NEEDS_MORE_OUTPUT
) {
163 CopyMem(Temp
, Output
, FILE_BUFFER_SIZE
);
164 AvailableOut
= FILE_BUFFER_SIZE
;
165 Temp
= (VOID
*)((UINT8
*)Temp
+FILE_BUFFER_SIZE
);
168 break; /* Error or success. */
170 Result
= BrotliDecompressStream(
179 if (NextOut
!= Output
) {
180 CopyMem(Temp
, Output
, (size_t)(NextOut
- Output
));
185 BrFree(BuffInfo
, Input
);
186 BrFree(BuffInfo
, Output
);
187 BrotliDestroyState(BroState
);
188 return (Result
== BROTLI_RESULT_SUCCESS
) ? EFI_SUCCESS
: EFI_INVALID_PARAMETER
;
192 Get the size of the uncompressed buffer by parsing EncodeData header.
194 @param EncodedData Pointer to the compressed data.
195 @param StartOffset Start offset of the compressed data.
196 @param EndOffset End offset of the compressed data.
198 @return The size of the uncompressed buffer.
202 IN UINT8
* EncodedData
,
203 IN UINT8 StartOffset
,
212 for (Index
= EndOffset
- 1; Index
>= StartOffset
; Index
--)
213 DecodedSize
= LShiftU64(DecodedSize
, 8) + EncodedData
[Index
];
219 Given a Brotli compressed source buffer, this function retrieves the size of
220 the uncompressed buffer and the size of the scratch buffer required
221 to decompress the compressed source buffer.
223 Retrieves the size of the uncompressed buffer and the temporary scratch buffer
224 required to decompress the buffer specified by Source and SourceSize.
225 The size of the uncompressed buffer is returned in DestinationSize,
226 the size of the scratch buffer is returned in ScratchSize, and EFI_SUCCESS is returned.
227 This function does not have scratch buffer available to perform a thorough
228 checking of the validity of the source data. It just retrieves the "Original Size"
229 field from the BROTLI_SCRATCH_MAX beginning bytes of the source data and output it as DestinationSize.
230 And ScratchSize is specific to the decompression implementation.
232 If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().
234 @param Source The source buffer containing the compressed data.
235 @param SourceSize The size, in bytes, of the source buffer.
236 @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
237 that will be generated when the compressed buffer specified
238 by Source and SourceSize is decompressed.
239 @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that
240 is required to decompress the compressed buffer specified
241 by Source and SourceSize.
243 @retval EFI_SUCCESS The size of the uncompressed data was returned
244 in DestinationSize and the size of the scratch
245 buffer was returned in ScratchSize.
249 BrotliUefiDecompressGetInfo (
250 IN CONST VOID
* Source
,
251 IN UINT32 SourceSize
,
252 OUT UINT32
* DestinationSize
,
253 OUT UINT32
* ScratchSize
259 ASSERT(SourceSize
>= BROTLI_SCRATCH_MAX
);
261 MaxOffset
= BROTLI_DECODE_MAX
;
262 GetSize
= GetDecodedSizeOfBuf((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
263 *DestinationSize
= (UINT32
)GetSize
;
264 MaxOffset
= BROTLI_SCRATCH_MAX
;
265 GetSize
= GetDecodedSizeOfBuf((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
266 *ScratchSize
= (UINT32
)GetSize
;
271 Decompresses a Brotli compressed source buffer.
273 Extracts decompressed data to its original form.
274 If the compressed source data specified by Source is successfully decompressed
275 into Destination, then RETURN_SUCCESS is returned. If the compressed source data
276 specified by Source is not in a valid compressed data format,
277 then RETURN_INVALID_PARAMETER is returned.
279 @param Source The source buffer containing the compressed data.
280 @param SourceSize The size of source buffer.
281 @param Destination The destination buffer to store the decompressed data
282 @param Scratch A temporary scratch buffer that is used to perform the decompression.
283 This is an optional parameter that may be NULL if the
284 required scratch buffer size is 0.
286 @retval EFI_SUCCESS Decompression completed successfully, and
287 the uncompressed buffer is returned in Destination.
288 @retval EFI_INVALID_PARAMETER
289 The source buffer specified by Source is corrupted
290 (not in a valid compressed format).
294 BrotliUefiDecompress (
295 IN CONST VOID
* Source
,
297 IN OUT VOID
* Destination
,
298 IN OUT VOID
* Scratch
307 MaxOffset
= BROTLI_SCRATCH_MAX
;
308 GetSize
= GetDecodedSizeOfBuf((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
310 BroBuff
.Buff
= Scratch
;
311 BroBuff
.BuffSize
= (UINTN
)GetSize
;
313 Status
= BrotliDecompress(
314 (VOID
*)((UINT8
*)Source
+ BROTLI_SCRATCH_MAX
),
315 SourceSize
- BROTLI_SCRATCH_MAX
,