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