]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
Add the optional EfiRom CodeRevision option.
[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
23#include <Library/MemoryAllocationLib.h>\r
24#include <Library/UefiDecompressLib.h>\r
25#include <Library/ExtractGuidedSectionLib.h>\r
26#include <Guid/LzmaDecompress.h>\r
27\r
28#include "Sdk/C/Types.h"\r
29#include "Sdk/C/7zVersion.h"\r
30#include "Sdk/C/LzmaDec.h"\r
31\r
19a4a0a0 32/**\r
33 Allocation routine used by LZMA decompression.\r
34\r
35 @param p Pointer to the ISzAlloc instance\r
36 @param size The size in bytes to be allocated\r
37\r
38 @return The allocated pointer address, or NULL on failure\r
39**/\r
40STATIC\r
41VOID *\r
42SzAlloc (\r
43 void *p,\r
44 size_t size\r
45 )\r
46{\r
47 return AllocatePool (size);\r
48}\r
49\r
50/**\r
51 Free routine used by LZMA decompression.\r
52\r
53 @param p Pointer to the ISzAlloc instance\r
54 @param address The address to be freed\r
55**/\r
56STATIC\r
57VOID\r
58SzFree (\r
59 void *p,\r
60 void *address\r
61 )\r
62{\r
63 if (address != NULL) {\r
64 FreePool (address);\r
65 }\r
66}\r
67\r
68STATIC ISzAlloc g_Alloc = { SzAlloc, SzFree };\r
306bf4e2 69\r
70#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
71\r
72STATIC\r
73UINT64\r
74GetDecodedSizeOfBuf(\r
75 UINT8 *encodedData\r
76 )\r
77{\r
78 UINT64 DecodedSize;\r
79 INTN Index;\r
80\r
81 /* Parse header */\r
82 DecodedSize = 0;\r
83 for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)\r
84 DecodedSize = LShiftU64(DecodedSize, 8) + encodedData[Index];\r
85\r
86 return DecodedSize;\r
87}\r
88\r
89//\r
90// LZMA functions and data as defined in local LzmaDecompress.h\r
91//\r
92\r
93STATIC CONST VOID *mSourceLastUsedWithGetInfo;\r
94STATIC UINT32 mSizeOfLastSource;\r
95STATIC UINT32 mDecompressedSizeForLastSource;\r
96\r
97/**\r
98 The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().\r
99 \r
100 @param Source The source buffer containing the compressed data.\r
101 @param SourceSize The size of source buffer\r
102 @param DestinationSize The size of destination buffer.\r
103 @param ScratchSize The size of scratch buffer.\r
104\r
105 @retval RETURN_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
106 @retval RETURN_INVALID_PARAMETER - The source data is corrupted\r
107**/\r
108RETURN_STATUS\r
109EFIAPI\r
110LzmaUefiDecompressGetInfo (\r
111 IN CONST VOID *Source,\r
112 IN UINT32 SourceSize,\r
113 OUT UINT32 *DestinationSize,\r
114 OUT UINT32 *ScratchSize\r
115 )\r
116{\r
117 UInt64 DecodedSize;\r
118\r
119 ASSERT(SourceSize >= LZMA_HEADER_SIZE);\r
120\r
121 DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
122\r
123 mSourceLastUsedWithGetInfo = Source;\r
124 mSizeOfLastSource = SourceSize;\r
125 mDecompressedSizeForLastSource = (UInt32)DecodedSize;\r
126 *DestinationSize = mDecompressedSizeForLastSource;\r
127 *ScratchSize = 0x10;\r
128 return RETURN_SUCCESS;\r
129}\r
130\r
131\r
132/**\r
133 The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r
134 \r
135 @param Source - The source buffer containing the compressed data.\r
136 @param Destination - The destination buffer to store the decompressed data\r
137 @param Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.\r
138\r
139 @retval RETURN_SUCCESS - Decompression is successfull\r
140 @retval RETURN_INVALID_PARAMETER - The source data is corrupted \r
141**/\r
142RETURN_STATUS\r
143EFIAPI\r
144LzmaUefiDecompress (\r
145 IN CONST VOID *Source,\r
146 IN OUT VOID *Destination,\r
147 IN OUT VOID *Scratch\r
148 )\r
149{\r
150 SRes lzmaResult;\r
151 ELzmaStatus status;\r
152 SizeT decodedBufSize;\r
153 SizeT encodedDataSize;\r
154\r
155 if (Source != mSourceLastUsedWithGetInfo) {\r
156 return RETURN_INVALID_PARAMETER;\r
157 }\r
158\r
159 decodedBufSize = (SizeT)mDecompressedSizeForLastSource;\r
160 encodedDataSize = (SizeT)(mSizeOfLastSource - LZMA_HEADER_SIZE);\r
161\r
162 lzmaResult = LzmaDecode(\r
163 Destination,\r
164 &decodedBufSize,\r
165 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
166 &encodedDataSize,\r
167 Source,\r
168 LZMA_PROPS_SIZE,\r
169 LZMA_FINISH_END,\r
170 &status,\r
171 &g_Alloc\r
172 );\r
173\r
174 if (lzmaResult == SZ_OK) {\r
175 return RETURN_SUCCESS;\r
176 } else {\r
177 return RETURN_INVALID_PARAMETER;\r
178 }\r
179}\r
180\r