]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
MdeModulePkg: Fix device path when boot manager menu is from different FV
[mirror_edk2.git] / MdeModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
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 {
18 ISzAlloc Functions;
19 VOID *Buffer;
20 UINTN BufferSize;
21 } ISzAllocWithData;
22
23 /**
24 Allocation routine used by LZMA decompression.
25
26 @param P Pointer to the ISzAlloc instance
27 @param Size The size in bytes to be allocated
28
29 @return The allocated pointer address, or NULL on failure
30 **/
31 VOID *
32 SzAlloc (
33 CONST ISzAlloc *P,
34 size_t Size
35 )
36 {
37 VOID *Addr;
38 ISzAllocWithData *Private;
39
40 Private = (ISzAllocWithData*) P;
41
42 if (Private->BufferSize >= Size) {
43 Addr = Private->Buffer;
44 Private->Buffer = (VOID*) ((UINT8*)Addr + Size);
45 Private->BufferSize -= Size;
46 return Addr;
47 } else {
48 ASSERT (FALSE);
49 return NULL;
50 }
51 }
52
53 /**
54 Free routine used by LZMA decompression.
55
56 @param P Pointer to the ISzAlloc instance
57 @param Address The address to be freed
58 **/
59 VOID
60 SzFree (
61 CONST ISzAlloc *P,
62 VOID *Address
63 )
64 {
65 //
66 // We use the 'scratch buffer' for allocations, so there is no free
67 // operation required. The scratch buffer will be freed by the caller
68 // of the decompression code.
69 //
70 }
71
72 #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
73
74 /**
75 Get the size of the uncompressed buffer by parsing EncodeData header.
76
77 @param EncodedData Pointer to the compressed data.
78
79 @return The size of the uncompressed buffer.
80 **/
81 UINT64
82 GetDecodedSizeOfBuf(
83 UINT8 *EncodedData
84 )
85 {
86 UINT64 DecodedSize;
87 INTN Index;
88
89 /* Parse header */
90 DecodedSize = 0;
91 for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)
92 DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];
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 }
221