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
;
129 AvailableOut
= FILE_BUFFER_SIZE
;
130 Result
= BROTLI_RESULT_ERROR
;
131 BroState
= BrotliCreateState(BrAlloc
, BrFree
, BuffInfo
);
134 if (BroState
== NULL
) {
135 return EFI_INVALID_PARAMETER
;
137 Input
= (UINT8
*)BrAlloc(BuffInfo
, FILE_BUFFER_SIZE
);
138 Output
= (UINT8
*)BrAlloc(BuffInfo
, FILE_BUFFER_SIZE
);
139 if ((Input
==NULL
) || (Output
==NULL
)) {
140 BrFree(BuffInfo
, Input
);
141 BrFree(BuffInfo
, Output
);
142 BrotliDestroyState(BroState
);
143 return EFI_INVALID_PARAMETER
;
146 Result
= BROTLI_RESULT_NEEDS_MORE_INPUT
;
148 if (Result
== BROTLI_RESULT_NEEDS_MORE_INPUT
) {
149 if (SourceSize
== 0) {
152 if (SourceSize
>= FILE_BUFFER_SIZE
) {
153 AvailableIn
= FILE_BUFFER_SIZE
;
155 AvailableIn
= SourceSize
;
157 CopyMem(Input
, Source
, AvailableIn
);
158 Source
= (VOID
*)((UINT8
*)Source
+ AvailableIn
);
159 SourceSize
-= AvailableIn
;
161 } else if (Result
== BROTLI_RESULT_NEEDS_MORE_OUTPUT
) {
162 CopyMem(Temp
, Output
, FILE_BUFFER_SIZE
);
163 AvailableOut
= FILE_BUFFER_SIZE
;
164 Temp
= (VOID
*)((UINT8
*)Temp
+FILE_BUFFER_SIZE
);
167 break; /* Error or success. */
169 Result
= BrotliDecompressStream(
178 if (NextOut
!= Output
) {
179 CopyMem(Temp
, Output
, (size_t)(NextOut
- Output
));
184 BrFree(BuffInfo
, Input
);
185 BrFree(BuffInfo
, Output
);
186 BrotliDestroyState(BroState
);
187 return (Result
== BROTLI_RESULT_SUCCESS
) ? EFI_SUCCESS
: EFI_INVALID_PARAMETER
;
191 Get the size of the uncompressed buffer by parsing EncodeData header.
193 @param EncodedData Pointer to the compressed data.
194 @param StartOffset Start offset of the compressed data.
195 @param EndOffset End offset of the compressed data.
197 @return The size of the uncompressed buffer.
201 IN UINT8
* EncodedData
,
202 IN UINT8 StartOffset
,
211 for (Index
= EndOffset
- 1; Index
>= StartOffset
; Index
--)
212 DecodedSize
= LShiftU64(DecodedSize
, 8) + EncodedData
[Index
];
218 Given a Brotli compressed source buffer, this function retrieves the size of
219 the uncompressed buffer and the size of the scratch buffer required
220 to decompress the compressed source buffer.
222 Retrieves the size of the uncompressed buffer and the temporary scratch buffer
223 required to decompress the buffer specified by Source and SourceSize.
224 The size of the uncompressed buffer is returned in DestinationSize,
225 the size of the scratch buffer is returned in ScratchSize, and EFI_SUCCESS is returned.
226 This function does not have scratch buffer available to perform a thorough
227 checking of the validity of the source data. It just retrieves the "Original Size"
228 field from the BROTLI_SCRATCH_MAX beginning bytes of the source data and output it as DestinationSize.
229 And ScratchSize is specific to the decompression implementation.
231 If SourceSize is less than BROTLI_SCRATCH_MAX, then ASSERT().
233 @param Source The source buffer containing the compressed data.
234 @param SourceSize The size, in bytes, of the source buffer.
235 @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
236 that will be generated when the compressed buffer specified
237 by Source and SourceSize is decompressed.
238 @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that
239 is required to decompress the compressed buffer specified
240 by Source and SourceSize.
242 @retval EFI_SUCCESS The size of the uncompressed data was returned
243 in DestinationSize and the size of the scratch
244 buffer was returned in ScratchSize.
248 BrotliUefiDecompressGetInfo (
249 IN CONST VOID
* Source
,
250 IN UINT32 SourceSize
,
251 OUT UINT32
* DestinationSize
,
252 OUT UINT32
* ScratchSize
258 ASSERT(SourceSize
>= BROTLI_SCRATCH_MAX
);
260 MaxOffset
= BROTLI_DECODE_MAX
;
261 GetSize
= GetDecodedSizeOfBuf((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
262 *DestinationSize
= (UINT32
)GetSize
;
263 MaxOffset
= BROTLI_SCRATCH_MAX
;
264 GetSize
= GetDecodedSizeOfBuf((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
265 *ScratchSize
= (UINT32
)GetSize
;
270 Decompresses a Brotli compressed source buffer.
272 Extracts decompressed data to its original form.
273 If the compressed source data specified by Source is successfully decompressed
274 into Destination, then RETURN_SUCCESS is returned. If the compressed source data
275 specified by Source is not in a valid compressed data format,
276 then RETURN_INVALID_PARAMETER is returned.
278 @param Source The source buffer containing the compressed data.
279 @param SourceSize The size of source buffer.
280 @param Destination The destination buffer to store the decompressed data
281 @param Scratch A temporary scratch buffer that is used to perform the decompression.
282 This is an optional parameter that may be NULL if the
283 required scratch buffer size is 0.
285 @retval EFI_SUCCESS Decompression completed successfully, and
286 the uncompressed buffer is returned in Destination.
287 @retval EFI_INVALID_PARAMETER
288 The source buffer specified by Source is corrupted
289 (not in a valid compressed format).
293 BrotliUefiDecompress (
294 IN CONST VOID
* Source
,
296 IN OUT VOID
* Destination
,
297 IN OUT VOID
* Scratch
306 MaxOffset
= BROTLI_SCRATCH_MAX
;
307 GetSize
= GetDecodedSizeOfBuf((UINT8
*)Source
, MaxOffset
- BROTLI_INFO_SIZE
, MaxOffset
);
309 BroBuff
.Buff
= Scratch
;
310 BroBuff
.BuffSize
= (UINTN
)GetSize
;
312 Status
= BrotliDecompress(
313 (VOID
*)((UINT8
*)Source
+ BROTLI_SCRATCH_MAX
),
314 SourceSize
- BROTLI_SCRATCH_MAX
,