]>
Commit | Line | Data |
---|---|---|
11ff2c71 LG |
1 | /** @file\r |
2 | LZMA Decompress interfaces\r | |
3 | \r | |
d1102dba | 4 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r |
11ff2c71 LG |
5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "LzmaDecompressLibInternal.h"\r | |
1e230224 | 16 | #include "Sdk/C/7zTypes.h"\r |
11ff2c71 LG |
17 | #include "Sdk/C/7zVersion.h"\r |
18 | #include "Sdk/C/LzmaDec.h"\r | |
19 | \r | |
20 | #define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB\r | |
21 | \r | |
22 | typedef struct\r | |
23 | {\r | |
24 | ISzAlloc Functions;\r | |
25 | VOID *Buffer;\r | |
26 | UINTN BufferSize;\r | |
27 | } ISzAllocWithData;\r | |
28 | \r | |
29 | /**\r | |
30 | Allocation routine used by LZMA decompression.\r | |
31 | \r | |
32 | @param P Pointer to the ISzAlloc instance\r | |
33 | @param Size The size in bytes to be allocated\r | |
34 | \r | |
35 | @return The allocated pointer address, or NULL on failure\r | |
36 | **/\r | |
37 | VOID *\r | |
38 | SzAlloc (\r | |
f0737de8 | 39 | CONST ISzAlloc *P,\r |
11ff2c71 LG |
40 | size_t Size\r |
41 | )\r | |
42 | {\r | |
43 | VOID *Addr;\r | |
44 | ISzAllocWithData *Private;\r | |
45 | \r | |
46 | Private = (ISzAllocWithData*) P;\r | |
47 | \r | |
48 | if (Private->BufferSize >= Size) {\r | |
49 | Addr = Private->Buffer;\r | |
50 | Private->Buffer = (VOID*) ((UINT8*)Addr + Size);\r | |
51 | Private->BufferSize -= Size;\r | |
52 | return Addr;\r | |
53 | } else {\r | |
54 | ASSERT (FALSE);\r | |
55 | return NULL;\r | |
56 | }\r | |
57 | }\r | |
58 | \r | |
59 | /**\r | |
60 | Free routine used by LZMA decompression.\r | |
61 | \r | |
62 | @param P Pointer to the ISzAlloc instance\r | |
63 | @param Address The address to be freed\r | |
64 | **/\r | |
65 | VOID\r | |
66 | SzFree (\r | |
f0737de8 | 67 | CONST ISzAlloc *P,\r |
11ff2c71 LG |
68 | VOID *Address\r |
69 | )\r | |
70 | {\r | |
71 | //\r | |
72 | // We use the 'scratch buffer' for allocations, so there is no free\r | |
73 | // operation required. The scratch buffer will be freed by the caller\r | |
74 | // of the decompression code.\r | |
75 | //\r | |
76 | }\r | |
77 | \r | |
78 | #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r | |
79 | \r | |
80 | /**\r | |
81 | Get the size of the uncompressed buffer by parsing EncodeData header.\r | |
82 | \r | |
83 | @param EncodedData Pointer to the compressed data.\r | |
84 | \r | |
85 | @return The size of the uncompressed buffer.\r | |
86 | **/\r | |
87 | UINT64\r | |
88 | GetDecodedSizeOfBuf(\r | |
89 | UINT8 *EncodedData\r | |
90 | )\r | |
91 | {\r | |
92 | UINT64 DecodedSize;\r | |
93 | INTN Index;\r | |
94 | \r | |
95 | /* Parse header */\r | |
96 | DecodedSize = 0;\r | |
97 | for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)\r | |
98 | DecodedSize = LShiftU64(DecodedSize, 8) + EncodedData[Index];\r | |
99 | \r | |
100 | return DecodedSize;\r | |
101 | }\r | |
102 | \r | |
103 | //\r | |
104 | // LZMA functions and data as defined in local LzmaDecompressLibInternal.h\r | |
105 | //\r | |
106 | \r | |
107 | /**\r | |
d1102dba LG |
108 | Given a Lzma compressed source buffer, this function retrieves the size of\r |
109 | the uncompressed buffer and the size of the scratch buffer required\r | |
11ff2c71 LG |
110 | to decompress the compressed source buffer.\r |
111 | \r | |
d1102dba | 112 | Retrieves the size of the uncompressed buffer and the temporary scratch buffer\r |
11ff2c71 | 113 | required to decompress the buffer specified by Source and SourceSize.\r |
d1102dba | 114 | The size of the uncompressed buffer is returned in DestinationSize,\r |
11ff2c71 | 115 | the size of the scratch buffer is returned in ScratchSize, and RETURN_SUCCESS is returned.\r |
d1102dba | 116 | This function does not have scratch buffer available to perform a thorough\r |
11ff2c71 LG |
117 | checking of the validity of the source data. It just retrieves the "Original Size"\r |
118 | field from the LZMA_HEADER_SIZE beginning bytes of the source data and output it as DestinationSize.\r | |
119 | And ScratchSize is specific to the decompression implementation.\r | |
120 | \r | |
121 | If SourceSize is less than LZMA_HEADER_SIZE, then ASSERT().\r | |
122 | \r | |
123 | @param Source The source buffer containing the compressed data.\r | |
124 | @param SourceSize The size, in bytes, of the source buffer.\r | |
125 | @param DestinationSize A pointer to the size, in bytes, of the uncompressed buffer\r | |
126 | that will be generated when the compressed buffer specified\r | |
127 | by Source and SourceSize is decompressed.\r | |
128 | @param ScratchSize A pointer to the size, in bytes, of the scratch buffer that\r | |
d1102dba | 129 | is required to decompress the compressed buffer specified\r |
11ff2c71 LG |
130 | by Source and SourceSize.\r |
131 | \r | |
d1102dba LG |
132 | @retval RETURN_SUCCESS The size of the uncompressed data was returned\r |
133 | in DestinationSize and the size of the scratch\r | |
11ff2c71 LG |
134 | buffer was returned in ScratchSize.\r |
135 | \r | |
136 | **/\r | |
137 | RETURN_STATUS\r | |
138 | EFIAPI\r | |
139 | LzmaUefiDecompressGetInfo (\r | |
140 | IN CONST VOID *Source,\r | |
141 | IN UINT32 SourceSize,\r | |
142 | OUT UINT32 *DestinationSize,\r | |
143 | OUT UINT32 *ScratchSize\r | |
144 | )\r | |
145 | {\r | |
146 | UInt64 DecodedSize;\r | |
147 | \r | |
148 | ASSERT(SourceSize >= LZMA_HEADER_SIZE);\r | |
149 | \r | |
150 | DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r | |
151 | \r | |
152 | *DestinationSize = (UINT32)DecodedSize;\r | |
153 | *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r | |
154 | return RETURN_SUCCESS;\r | |
155 | }\r | |
156 | \r | |
157 | /**\r | |
158 | Decompresses a Lzma compressed source buffer.\r | |
159 | \r | |
160 | Extracts decompressed data to its original form.\r | |
d1102dba LG |
161 | If the compressed source data specified by Source is successfully decompressed\r |
162 | into Destination, then RETURN_SUCCESS is returned. If the compressed source data\r | |
11ff2c71 LG |
163 | specified by Source is not in a valid compressed data format,\r |
164 | then RETURN_INVALID_PARAMETER is returned.\r | |
165 | \r | |
166 | @param Source The source buffer containing the compressed data.\r | |
167 | @param SourceSize The size of source buffer.\r | |
168 | @param Destination The destination buffer to store the decompressed data\r | |
169 | @param Scratch A temporary scratch buffer that is used to perform the decompression.\r | |
d1102dba | 170 | This is an optional parameter that may be NULL if the\r |
11ff2c71 | 171 | required scratch buffer size is 0.\r |
d1102dba LG |
172 | \r |
173 | @retval RETURN_SUCCESS Decompression completed successfully, and\r | |
11ff2c71 | 174 | the uncompressed buffer is returned in Destination.\r |
d1102dba LG |
175 | @retval RETURN_INVALID_PARAMETER\r |
176 | The source buffer specified by Source is corrupted\r | |
11ff2c71 LG |
177 | (not in a valid compressed format).\r |
178 | **/\r | |
179 | RETURN_STATUS\r | |
180 | EFIAPI\r | |
181 | LzmaUefiDecompress (\r | |
182 | IN CONST VOID *Source,\r | |
183 | IN UINTN SourceSize,\r | |
184 | IN OUT VOID *Destination,\r | |
185 | IN OUT VOID *Scratch\r | |
186 | )\r | |
187 | {\r | |
188 | SRes LzmaResult;\r | |
189 | ELzmaStatus Status;\r | |
190 | SizeT DecodedBufSize;\r | |
191 | SizeT EncodedDataSize;\r | |
192 | ISzAllocWithData AllocFuncs;\r | |
193 | \r | |
194 | AllocFuncs.Functions.Alloc = SzAlloc;\r | |
195 | AllocFuncs.Functions.Free = SzFree;\r | |
196 | AllocFuncs.Buffer = Scratch;\r | |
197 | AllocFuncs.BufferSize = SCRATCH_BUFFER_REQUEST_SIZE;\r | |
d1102dba | 198 | \r |
11ff2c71 LG |
199 | DecodedBufSize = (SizeT)GetDecodedSizeOfBuf((UINT8*)Source);\r |
200 | EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE);\r | |
201 | \r | |
202 | LzmaResult = LzmaDecode(\r | |
203 | Destination,\r | |
204 | &DecodedBufSize,\r | |
205 | (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r | |
206 | &EncodedDataSize,\r | |
207 | Source,\r | |
208 | LZMA_PROPS_SIZE,\r | |
209 | LZMA_FINISH_END,\r | |
210 | &Status,\r | |
211 | &(AllocFuncs.Functions)\r | |
212 | );\r | |
213 | \r | |
214 | if (LzmaResult == SZ_OK) {\r | |
215 | return RETURN_SUCCESS;\r | |
216 | } else {\r | |
217 | return RETURN_INVALID_PARAMETER;\r | |
218 | }\r | |
219 | }\r | |
220 | \r |