TianoCore EDK2 master
Loading...
Searching...
No Matches
BrotliDecompress.c
Go to the documentation of this file.
1
9
18VOID *
20 IN VOID *Ptr,
21 IN size_t Size
22 )
23{
24 VOID *Addr;
25 BROTLI_BUFF *Private;
26
27 Private = (BROTLI_BUFF *)Ptr;
28
29 if (Private->BuffSize >= Size) {
30 Addr = Private->Buff;
31 Private->Buff = (VOID *)((UINT8 *)Addr + Size);
32 Private->BuffSize -= Size;
33 return Addr;
34 } else {
35 ASSERT (FALSE);
36 return NULL;
37 }
38}
39
46VOID
48 IN VOID *Ptr,
49 IN VOID *Address
50 )
51{
52 //
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.
56 //
57}
58
82 IN CONST VOID *Source,
83 IN UINTN SourceSize,
84 IN OUT VOID *Destination,
85 IN OUT UINTN DestSize,
86 IN VOID *BuffInfo
87 )
88{
89 UINT8 *Input;
90 UINT8 *Output;
91 const UINT8 *NextIn;
92 UINT8 *NextOut;
93 size_t TotalOut;
94 size_t AvailableIn;
95 size_t AvailableOut;
96 VOID *Temp;
97 BrotliDecoderResult Result;
98 BrotliDecoderState *BroState;
99
100 TotalOut = 0;
101 AvailableOut = FILE_BUFFER_SIZE;
102 Result = BROTLI_DECODER_RESULT_ERROR;
103 BroState = BrotliDecoderCreateInstance (BrAlloc, BrFree, BuffInfo);
104 Temp = Destination;
105
106 if (BroState == NULL) {
107 return EFI_INVALID_PARAMETER;
108 }
109
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;
117 }
118
119 NextOut = Output;
120 Result = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
121 while (1) {
122 if (Result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
123 if (SourceSize == 0) {
124 break;
125 }
126
127 if (SourceSize >= FILE_BUFFER_SIZE) {
128 AvailableIn = FILE_BUFFER_SIZE;
129 } else {
130 AvailableIn = SourceSize;
131 }
132
133 CopyMem (Input, Source, AvailableIn);
134 Source = (VOID *)((UINT8 *)Source + AvailableIn);
135 SourceSize -= AvailableIn;
136 NextIn = Input;
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);
141 NextOut = Output;
142 } else {
143 break; /* Error or success. */
144 }
145
146 Result = BrotliDecoderDecompressStream (
147 BroState,
148 &AvailableIn,
149 &NextIn,
150 &AvailableOut,
151 &NextOut,
152 &TotalOut
153 );
154 }
155
156 if (NextOut != Output) {
157 CopyMem (Temp, Output, (size_t)(NextOut - Output));
158 }
159
160 DestSize = TotalOut;
161
162 BrFree (BuffInfo, Input);
163 BrFree (BuffInfo, Output);
164 BrotliDecoderDestroyInstance (BroState);
165 return (Result == BROTLI_DECODER_RESULT_SUCCESS) ? EFI_SUCCESS : EFI_INVALID_PARAMETER;
166}
167
177UINT64
179 IN UINT8 *EncodedData,
180 IN UINT8 StartOffset,
181 IN UINT8 EndOffset
182 )
183{
184 UINT64 DecodedSize;
185 INTN Index;
186
187 /* Parse header */
188 DecodedSize = 0;
189 for (Index = EndOffset - 1; Index >= StartOffset; Index--) {
190 DecodedSize = LShiftU64 (DecodedSize, 8) + EncodedData[Index];
191 }
192
193 return DecodedSize;
194}
195
226EFIAPI
228 IN CONST VOID *Source,
229 IN UINT32 SourceSize,
230 OUT UINT32 *DestinationSize,
231 OUT UINT32 *ScratchSize
232 )
233{
234 UINT64 GetSize;
235 UINT8 MaxOffset;
236
237 ASSERT (SourceSize >= BROTLI_SCRATCH_MAX);
238
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;
245 return EFI_SUCCESS;
246}
247
271EFIAPI
273 IN CONST VOID *Source,
274 IN UINTN SourceSize,
275 IN OUT VOID *Destination,
276 IN OUT VOID *Scratch
277 )
278{
279 UINTN DestSize = 0;
280 EFI_STATUS Status;
281 BROTLI_BUFF BroBuff;
282 UINT64 GetSize;
283 UINT8 MaxOffset;
284
285 MaxOffset = BROTLI_SCRATCH_MAX;
286 GetSize = BrGetDecodedSizeOfBuf ((UINT8 *)Source, MaxOffset - BROTLI_INFO_SIZE, MaxOffset);
287
288 BroBuff.Buff = Scratch;
289 BroBuff.BuffSize = (UINTN)GetSize;
290
291 Status = BrotliDecompress (
292 (VOID *)((UINT8 *)Source + BROTLI_SCRATCH_MAX),
293 SourceSize - BROTLI_SCRATCH_MAX,
294 Destination,
295 DestSize,
296 (VOID *)(&BroBuff)
297 );
298
299 return Status;
300}
UINT64 UINTN
INT64 INTN
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: LShiftU64.c:28
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
EFI_STATUS BrotliDecompress(IN CONST VOID *Source, IN UINTN SourceSize, IN OUT VOID *Destination, IN OUT UINTN DestSize, IN VOID *BuffInfo)
EFI_STATUS EFIAPI BrotliUefiDecompressGetInfo(IN CONST VOID *Source, IN UINT32 SourceSize, OUT UINT32 *DestinationSize, OUT UINT32 *ScratchSize)
VOID * BrAlloc(IN VOID *Ptr, IN size_t Size)
EFI_STATUS EFIAPI BrotliUefiDecompress(IN CONST VOID *Source, IN UINTN SourceSize, IN OUT VOID *Destination, IN OUT VOID *Scratch)
VOID BrFree(IN VOID *Ptr, IN VOID *Address)
UINT64 BrGetDecodedSizeOfBuf(IN UINT8 *EncodedData, IN UINT8 StartOffset, IN UINT8 EndOffset)
VOID EFIAPI Input(IN CHAR16 *Prompt OPTIONAL, OUT CHAR16 *InStr, IN UINTN StrLen)
Definition: EdbSupportUI.c:187
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112