TianoCore EDK2 master
Loading...
Searching...
No Matches
LzmaDecompress.c
Go to the documentation of this file.
1
10#include "Sdk/C/7zTypes.h"
11#include "Sdk/C/7zVersion.h"
12#include "Sdk/C/LzmaDec.h"
13
14#define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB
15
16typedef struct {
17 ISzAlloc Functions;
18 VOID *Buffer;
19 UINTN BufferSize;
21
30VOID *
32 CONST ISzAlloc *P,
33 size_t Size
34 )
35{
36 VOID *Addr;
37 ISzAllocWithData *Private;
38
39 Private = (ISzAllocWithData *)P;
40
41 if (Private->BufferSize >= Size) {
42 Addr = Private->Buffer;
43 Private->Buffer = (VOID *)((UINT8 *)Addr + Size);
44 Private->BufferSize -= Size;
45 return Addr;
46 } else {
47 ASSERT (FALSE);
48 return NULL;
49 }
50}
51
58VOID
60 CONST ISzAlloc *P,
61 VOID *Address
62 )
63{
64 //
65 // We use the 'scratch buffer' for allocations, so there is no free
66 // operation required. The scratch buffer will be freed by the caller
67 // of the decompression code.
68 //
69}
70
71#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
72
80UINT64
82 UINT8 *EncodedData
83 )
84{
85 UINT64 DecodedSize;
86 INTN Index;
87
88 /* Parse header */
89 DecodedSize = 0;
90 for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--) {
91 DecodedSize = LShiftU64 (DecodedSize, 8) + EncodedData[Index];
92 }
93
94 return DecodedSize;
95}
96
97//
98// LZMA functions and data as defined in local LzmaDecompressLibInternal.h
99//
100
135RETURN_STATUS
136EFIAPI
138 IN CONST VOID *Source,
139 IN UINT32 SourceSize,
140 OUT UINT32 *DestinationSize,
141 OUT UINT32 *ScratchSize
142 )
143{
144 UInt64 DecodedSize;
145
146 ASSERT (SourceSize >= LZMA_HEADER_SIZE);
147
148 DecodedSize = GetDecodedSizeOfBuf ((UINT8 *)Source);
149 if (DecodedSize > MAX_UINT32) {
150 return RETURN_UNSUPPORTED;
151 }
152
153 *DestinationSize = (UINT32)DecodedSize;
154 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;
155 return RETURN_SUCCESS;
156}
157
180RETURN_STATUS
181EFIAPI
183 IN CONST VOID *Source,
184 IN UINTN SourceSize,
185 IN OUT VOID *Destination,
186 IN OUT VOID *Scratch
187 )
188{
189 SRes LzmaResult;
190 ELzmaStatus Status;
191 SizeT DecodedBufSize;
192 SizeT EncodedDataSize;
193 ISzAllocWithData AllocFuncs;
194
195 AllocFuncs.Functions.Alloc = SzAlloc;
196 AllocFuncs.Functions.Free = SzFree;
197 AllocFuncs.Buffer = Scratch;
198 AllocFuncs.BufferSize = SCRATCH_BUFFER_REQUEST_SIZE;
199
200 DecodedBufSize = (SizeT)GetDecodedSizeOfBuf ((UINT8 *)Source);
201 EncodedDataSize = (SizeT)(SourceSize - LZMA_HEADER_SIZE);
202
203 LzmaResult = LzmaDecode (
204 Destination,
205 &DecodedBufSize,
206 (Byte *)((UINT8 *)Source + LZMA_HEADER_SIZE),
207 &EncodedDataSize,
208 Source,
209 LZMA_PROPS_SIZE,
210 LZMA_FINISH_END,
211 &Status,
212 &(AllocFuncs.Functions)
213 );
214
215 if (LzmaResult == SZ_OK) {
216 return RETURN_SUCCESS;
217 } else {
219 }
220}
UINT64 UINTN
INT64 INTN
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: LShiftU64.c:28
RETURN_STATUS EFIAPI LzmaUefiDecompress(IN CONST VOID *Source, IN UINTN SourceSize, IN OUT VOID *Destination, IN OUT VOID *Scratch)
UINT64 GetDecodedSizeOfBuf(UINT8 *EncodedData)
VOID SzFree(CONST ISzAlloc *P, VOID *Address)
RETURN_STATUS EFIAPI LzmaUefiDecompressGetInfo(IN CONST VOID *Source, IN UINT32 SourceSize, OUT UINT32 *DestinationSize, OUT UINT32 *ScratchSize)
VOID * SzAlloc(CONST ISzAlloc *P, size_t Size)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define RETURN_UNSUPPORTED
Definition: Base.h:1081
#define RETURN_SUCCESS
Definition: Base.h:1066
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define RETURN_INVALID_PARAMETER
Definition: Base.h:1076