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