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