1 | /** @file
|
---|
2 | LZMA Decompress interfaces
|
---|
3 |
|
---|
4 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include "LzmaDecompressLibInternal.h"
|
---|
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 |
|
---|
16 | typedef struct {
|
---|
17 | ISzAlloc Functions;
|
---|
18 | VOID *Buffer;
|
---|
19 | UINTN BufferSize;
|
---|
20 | } ISzAllocWithData;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | Allocation routine used by LZMA decompression.
|
---|
24 |
|
---|
25 | @param P Pointer to the ISzAlloc instance
|
---|
26 | @param Size The size in bytes to be allocated
|
---|
27 |
|
---|
28 | @return The allocated pointer address, or NULL on failure
|
---|
29 | **/
|
---|
30 | VOID *
|
---|
31 | SzAlloc (
|
---|
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 |
|
---|
52 | /**
|
---|
53 | Free routine used by LZMA decompression.
|
---|
54 |
|
---|
55 | @param P Pointer to the ISzAlloc instance
|
---|
56 | @param Address The address to be freed
|
---|
57 | **/
|
---|
58 | VOID
|
---|
59 | SzFree (
|
---|
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 |
|
---|
73 | /**
|
---|
74 | Get the size of the uncompressed buffer by parsing EncodeData header.
|
---|
75 |
|
---|
76 | @param EncodedData Pointer to the compressed data.
|
---|
77 |
|
---|
78 | @return The size of the uncompressed buffer.
|
---|
79 | **/
|
---|
80 | UINT64
|
---|
81 | GetDecodedSizeOfBuf (
|
---|
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 |
|
---|
101 | /**
|
---|
102 | Given a Lzma compressed source buffer, this function retrieves the size of
|
---|
103 | the uncompressed buffer and the size of the scratch buffer required
|
---|
104 | to decompress the compressed source buffer.
|
---|
105 |
|
---|
106 | Retrieves the size of the uncompressed buffer and the temporary scratch buffer
|
---|
107 | required to decompress the buffer specified by Source and SourceSize.
|
---|
108 | The size of the uncompressed buffer is returned in DestinationSize,
|
---|
109 | the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.
|
---|
110 | This function does not have scratch buffer available to perform a thorough
|
---|
111 | checking of the validity of the source data. It just retrieves the "Original Size"
|
---|
112 | field from the LZMA_HEADER_SIZE beginning bytes of the source data and output it as DestinationSize.
|
---|
113 | And ScratchSize is specific to the decompression implementation.
|
---|
114 |
|
---|
115 | If SourceSize is less than LZMA_HEADER_SIZE, then ASSERT().
|
---|
116 |
|
---|
117 | @param Source The source buffer containing the compressed data.
|
---|
118 | @param SourceSize The size, in bytes, of the source buffer.
|
---|
119 | @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer
|
---|
120 | that will be generated when the compressed buffer specified
|
---|
121 | by Source and SourceSize is decompressed.
|
---|
122 | @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that
|
---|
123 | is required to decompress the compressed buffer specified
|
---|
124 | by Source and SourceSize.
|
---|
125 |
|
---|
126 | @retval RETURN_SUCCESS The size of the uncompressed data was returned
|
---|
127 | in DestinationSize and the size of the scratch
|
---|
128 | buffer was returned in ScratchSize.
|
---|
129 |
|
---|
130 | @retval RETURN_UNSUPPORTED DestinationSize cannot be output because the
|
---|
131 | uncompressed buffer size (in bytes) does not fit
|
---|
132 | in a UINT32. Output parameters have not been
|
---|
133 | modified.
|
---|
134 | **/
|
---|
135 | RETURN_STATUS
|
---|
136 | EFIAPI
|
---|
137 | LzmaUefiDecompressGetInfo (
|
---|
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 |
|
---|
158 | /**
|
---|
159 | Decompresses a Lzma compressed source buffer.
|
---|
160 |
|
---|
161 | Extracts decompressed data to its original form.
|
---|
162 | If the compressed source data specified by Source is successfully decompressed
|
---|
163 | into Destination, then RETURN_SUCCESS is returned. If the compressed source data
|
---|
164 | specified by Source is not in a valid compressed data format,
|
---|
165 | then RETURN_INVALID_PARAMETER is returned.
|
---|
166 |
|
---|
167 | @param Source The source buffer containing the compressed data.
|
---|
168 | @param SourceSize The size of source buffer.
|
---|
169 | @param Destination The destination buffer to store the decompressed data
|
---|
170 | @param Scratch A temporary scratch buffer that is used to perform the decompression.
|
---|
171 | This is an optional parameter that may be NULL if the
|
---|
172 | required scratch buffer size is 0.
|
---|
173 |
|
---|
174 | @retval RETURN_SUCCESS Decompression completed successfully, and
|
---|
175 | the uncompressed buffer is returned in Destination.
|
---|
176 | @retval RETURN_INVALID_PARAMETER
|
---|
177 | The source buffer specified by Source is corrupted
|
---|
178 | (not in a valid compressed format).
|
---|
179 | **/
|
---|
180 | RETURN_STATUS
|
---|
181 | EFIAPI
|
---|
182 | LzmaUefiDecompress (
|
---|
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 {
|
---|
218 | return RETURN_INVALID_PARAMETER;
|
---|
219 | }
|
---|
220 | }
|
---|