]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
MdeModulePkg: Unify the definitions of size_t
[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 **/
131 RETURN_STATUS
132 EFIAPI
133 LzmaUefiDecompressGetInfo (
134 IN CONST VOID *Source,
135 IN UINT32 SourceSize,
136 OUT UINT32 *DestinationSize,
137 OUT UINT32 *ScratchSize
138 )
139 {
140 UInt64 DecodedSize;
141
142 ASSERT(SourceSize >= LZMA_HEADER_SIZE);
143
144 DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);
145
146 *DestinationSize = (UINT32)DecodedSize;
147 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;
148 return RETURN_SUCCESS;
149 }
150
151 /**
152 Decompresses a Lzma compressed source buffer.
153
154 Extracts decompressed data to its original form.
155 If the compressed source data specified by Source is successfully decompressed
156 into Destination, then RETURN_SUCCESS is returned. If the compressed source data
157 specified by Source is not in a valid compressed data format,
158 then RETURN_INVALID_PARAMETER is returned.
159
160 @param Source The source buffer containing the compressed data.
161 @param SourceSize The size of source buffer.
162 @param Destination The destination buffer to store the decompressed data
163 @param Scratch A temporary scratch buffer that is used to perform the decompression.
164 This is an optional parameter that may be NULL if the
165 required scratch buffer size is 0.
166
167 @retval RETURN_SUCCESS Decompression completed successfully, and
168 the uncompressed buffer is returned in Destination.
169 @retval RETURN_INVALID_PARAMETER
170 The source buffer specified by Source is corrupted
171 (not in a valid compressed format).
172 **/
173 RETURN_STATUS
174 EFIAPI
175 LzmaUefiDecompress (
176 IN CONST VOID *Source,
177 IN UINTN SourceSize,
178 IN OUT VOID *Destination,
179 IN OUT VOID *Scratch
180 )
181 {
182 SRes LzmaResult;
183 ELzmaStatus Status;
184 SizeT DecodedBufSize;
185 SizeT EncodedDataSize;
186 ISzAllocWithData AllocFuncs;
187
188 AllocFuncs.Functions.Alloc = SzAlloc;
189 AllocFuncs.Functions.Free = SzFree;
190 AllocFuncs.Buffer = Scratch;
191 AllocFuncs.BufferSize = SCRATCH_BUFFER_REQUEST_SIZE;
192
193 DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);
194 EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);
195
196 LzmaResult = LzmaDecode(
197 Destination,
198 &DecodedBufSize,
199 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),
200 &EncodedDataSize,
201 Source,
202 LZMA_PROPS_SIZE,
203 LZMA_FINISH_END,
204 &Status,
205 &(AllocFuncs.Functions)
206 );
207
208 if (LzmaResult == SZ_OK) {
209 return RETURN_SUCCESS;
210 } else {
211 return RETURN_INVALID_PARAMETER;
212 }
213 }
214