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