]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
Fix a bug in UefiHiiLib. The size for allocating a buffer is StrSize instead of StrLen.
[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
32extern ISzAlloc g_Alloc;\r
33\r
34#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)\r
35\r
36STATIC\r
37UINT64\r
38GetDecodedSizeOfBuf(\r
39 UINT8 *encodedData\r
40 )\r
41{\r
42 UINT64 DecodedSize;\r
43 INTN Index;\r
44\r
45 /* Parse header */\r
46 DecodedSize = 0;\r
47 for (Index = LZMA_PROPS_SIZE + 7; Index >= LZMA_PROPS_SIZE; Index--)\r
48 DecodedSize = LShiftU64(DecodedSize, 8) + encodedData[Index];\r
49\r
50 return DecodedSize;\r
51}\r
52\r
53//\r
54// LZMA functions and data as defined in local LzmaDecompress.h\r
55//\r
56\r
57STATIC CONST VOID *mSourceLastUsedWithGetInfo;\r
58STATIC UINT32 mSizeOfLastSource;\r
59STATIC UINT32 mDecompressedSizeForLastSource;\r
60\r
61/**\r
62 The internal implementation of *_DECOMPRESS_PROTOCOL.GetInfo().\r
63 \r
64 @param Source The source buffer containing the compressed data.\r
65 @param SourceSize The size of source buffer\r
66 @param DestinationSize The size of destination buffer.\r
67 @param ScratchSize The size of scratch buffer.\r
68\r
69 @retval RETURN_SUCCESS - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
70 @retval RETURN_INVALID_PARAMETER - The source data is corrupted\r
71**/\r
72RETURN_STATUS\r
73EFIAPI\r
74LzmaUefiDecompressGetInfo (\r
75 IN CONST VOID *Source,\r
76 IN UINT32 SourceSize,\r
77 OUT UINT32 *DestinationSize,\r
78 OUT UINT32 *ScratchSize\r
79 )\r
80{\r
81 UInt64 DecodedSize;\r
82\r
83 ASSERT(SourceSize >= LZMA_HEADER_SIZE);\r
84\r
85 DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);\r
86\r
87 mSourceLastUsedWithGetInfo = Source;\r
88 mSizeOfLastSource = SourceSize;\r
89 mDecompressedSizeForLastSource = (UInt32)DecodedSize;\r
90 *DestinationSize = mDecompressedSizeForLastSource;\r
91 *ScratchSize = 0x10;\r
92 return RETURN_SUCCESS;\r
93}\r
94\r
95\r
96/**\r
97 The internal implementation of *_DECOMPRESS_PROTOCOL.Decompress().\r
98 \r
99 @param Source - The source buffer containing the compressed data.\r
100 @param Destination - The destination buffer to store the decompressed data\r
101 @param Scratch - The buffer used internally by the decompress routine. This buffer is needed to store intermediate data.\r
102\r
103 @retval RETURN_SUCCESS - Decompression is successfull\r
104 @retval RETURN_INVALID_PARAMETER - The source data is corrupted \r
105**/\r
106RETURN_STATUS\r
107EFIAPI\r
108LzmaUefiDecompress (\r
109 IN CONST VOID *Source,\r
110 IN OUT VOID *Destination,\r
111 IN OUT VOID *Scratch\r
112 )\r
113{\r
114 SRes lzmaResult;\r
115 ELzmaStatus status;\r
116 SizeT decodedBufSize;\r
117 SizeT encodedDataSize;\r
118\r
119 if (Source != mSourceLastUsedWithGetInfo) {\r
120 return RETURN_INVALID_PARAMETER;\r
121 }\r
122\r
123 decodedBufSize = (SizeT)mDecompressedSizeForLastSource;\r
124 encodedDataSize = (SizeT)(mSizeOfLastSource - LZMA_HEADER_SIZE);\r
125\r
126 lzmaResult = LzmaDecode(\r
127 Destination,\r
128 &decodedBufSize,\r
129 (Byte*)((UINT8*)Source + LZMA_HEADER_SIZE),\r
130 &encodedDataSize,\r
131 Source,\r
132 LZMA_PROPS_SIZE,\r
133 LZMA_FINISH_END,\r
134 &status,\r
135 &g_Alloc\r
136 );\r
137\r
138 if (lzmaResult == SZ_OK) {\r
139 return RETURN_SUCCESS;\r
140 } else {\r
141 return RETURN_INVALID_PARAMETER;\r
142 }\r
143}\r
144\r