]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
Remove usage of MemoryAllocationLib, and use a simplistic allocation
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / LzmaDecompress.c
CommitLineData
306bf4e2 1/** @file\r
2 LZMA Decompress routines for edk2\r
3\r
4 Portions based on LZMA SDK 4.65:\r
5 LzmaUtil.c -- Test application for LZMA compression\r
6 2008-11-23 : Igor Pavlov : Public domain\r
7\r
8 Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
9 All rights reserved. This program and the accompanying materials\r
10 are licensed and made available under the terms and conditions of the BSD License\r
11 which accompanies this distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19#include <Uefi.h>\r
20#include <Library/BaseLib.h>\r
21#include <Library/BaseMemoryLib.h>\r
22#include <Library/DebugLib.h>\r
306bf4e2 23#include <Library/UefiDecompressLib.h>\r
24#include <Library/ExtractGuidedSectionLib.h>\r
25#include <Guid/LzmaDecompress.h>\r
26\r
27#include "Sdk/C/Types.h"\r
28#include "Sdk/C/7zVersion.h"\r
29#include "Sdk/C/LzmaDec.h"\r
30\r
57cca89e 31//\r
32// Global data\r
33//\r
34\r
35STATIC CONST VOID *mSourceLastUsedWithGetInfo;\r
36STATIC UINT32 mSizeOfLastSource;\r
37STATIC UINT32 mDecompressedSizeForLastSource;\r
38STATIC VOID *mScratchBuffer;\r
39STATIC UINTN mScratchBufferSize;\r
40#define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB\r
41\r
19a4a0a0 42/**\r
43 Allocation routine used by LZMA decompression.\r
44\r
45 @param p Pointer to the ISzAlloc instance\r
46 @param size The size in bytes to be allocated\r
47\r
48 @return The allocated pointer address, or NULL on failure\r
49**/\r
50STATIC\r
51VOID *\r
52SzAlloc (\r
53 void *p,\r
54 size_t size\r
55 )\r
56{\r
57cca89e 57 VOID *addr;\r
58\r
59 if (mScratchBufferSize >= size) {\r
60 addr = mScratchBuffer;\r
61 mScratchBuffer = (VOID*) ((UINT8*)addr + size);\r
62 mScratchBufferSize -= size;\r
63 return addr;\r
64 } else {\r
65 ASSERT (FALSE);\r
66 return NULL;\r
67 }\r
19a4a0a0 68}\r
69\r
70/**\r
71 Free routine used by LZMA decompression.\r
72\r
73 @param p Pointer to the ISzAlloc instance\r
74 @param address The address to be freed\r
75**/\r
76STATIC\r
77VOID\r
78SzFree (\r
79 void *p,\r
80 void *address\r
81 )\r
82{\r
57cca89e 83 //\r
84 // We use the 'scratch buffer' for allocations, so there is no free\r
85 // operation required. The scratch buffer will be freed by the caller\r
86 // of the decompression code.\r
87 //\r
19a4a0a0 88}\r
89\r
90STATIC ISzAlloc g_Alloc = { SzAlloc, SzFree };\r
306bf4e2 91\r
92#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
93\r
94STATIC\r
95UINT64\r
96GetDecodedSizeOfBuf(\r
97 UINT8 *encodedData\r
98 )\r
99{\r
100 UINT64 DecodedSize;\r
101 INTN Index;\r
102\r
103 /* Parse header */\r
104 DecodedSize = 0;\r
105 for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)\r
106 DecodedSize = LShiftU64(DecodedSize, 8) + encodedData[Index];\r
107\r
108 return DecodedSize;\r
109}\r
110\r
111//\r
112// LZMA functions and data as defined in local LzmaDecompress.h\r
113//\r
114\r
306bf4e2 115/**\r
116 The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().\r
117 \r
118 @param Source The source buffer containing the compressed data.\r
119 @param SourceSize The size of source buffer\r
120 @param DestinationSize The size of destination buffer.\r
121 @param ScratchSize The size of scratch buffer.\r
122\r
123 @retval RETURN_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
124 @retval RETURN_INVALID_PARAMETER - The source data is corrupted\r
125**/\r
126RETURN_STATUS\r
127EFIAPI\r
128LzmaUefiDecompressGetInfo (\r
129 IN CONST VOID *Source,\r
130 IN UINT32 SourceSize,\r
131 OUT UINT32 *DestinationSize,\r
132 OUT UINT32 *ScratchSize\r
133 )\r
134{\r
135 UInt64 DecodedSize;\r
136\r
137 ASSERT(SourceSize >= LZMA_HEADER_SIZE);\r
138\r
139 DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
140\r
141 mSourceLastUsedWithGetInfo = Source;\r
142 mSizeOfLastSource = SourceSize;\r
143 mDecompressedSizeForLastSource = (UInt32)DecodedSize;\r
144 *DestinationSize = mDecompressedSizeForLastSource;\r
57cca89e 145 *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
306bf4e2 146 return RETURN_SUCCESS;\r
147}\r
148\r
149\r
150/**\r
151 The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r
152 \r
153 @param Source - The source buffer containing the compressed data.\r
154 @param Destination - The destination buffer to store the decompressed data\r
155 @param Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.\r
156\r
157 @retval RETURN_SUCCESS - Decompression is successfull\r
158 @retval RETURN_INVALID_PARAMETER - The source data is corrupted \r
159**/\r
160RETURN_STATUS\r
161EFIAPI\r
162LzmaUefiDecompress (\r
163 IN CONST VOID *Source,\r
164 IN OUT VOID *Destination,\r
165 IN OUT VOID *Scratch\r
166 )\r
167{\r
168 SRes lzmaResult;\r
169 ELzmaStatus status;\r
170 SizeT decodedBufSize;\r
171 SizeT encodedDataSize;\r
172\r
173 if (Source != mSourceLastUsedWithGetInfo) {\r
174 return RETURN_INVALID_PARAMETER;\r
175 }\r
176\r
177 decodedBufSize = (SizeT)mDecompressedSizeForLastSource;\r
178 encodedDataSize = (SizeT)(mSizeOfLastSource - LZMA_HEADER_SIZE);\r
179\r
57cca89e 180 mScratchBuffer = Scratch;\r
181 mScratchBufferSize = SCRATCH_BUFFER_REQUEST_SIZE;\r
182\r
306bf4e2 183 lzmaResult = LzmaDecode(\r
184 Destination,\r
185 &decodedBufSize,\r
186 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
187 &encodedDataSize,\r
188 Source,\r
189 LZMA_PROPS_SIZE,\r
190 LZMA_FINISH_END,\r
191 &status,\r
192 &g_Alloc\r
193 );\r
194\r
195 if (lzmaResult == SZ_OK) {\r
196 return RETURN_SUCCESS;\r
197 } else {\r
198 return RETURN_INVALID_PARAMETER;\r
199 }\r
200}\r
201\r