f11df343eb88a249fa298b7890e338fae3cef73b
[mirror_edk2.git] / MdePkg / Library / BaseUefiDecompressLib / BaseUefiDecompressLibInternals.h
1 /** @file
2 Internal include file for Base UEFI Decompress Libary.
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: BaseUefiDecompressLibInternals.h
14
15 **/
16
17 #ifndef __BASE_UEFI_DECOMPRESS_LIB_INTERNALS_H__
18 #define __BASE_UEFI_DECOMPRESS_LIB_INTERNALS_H__
19
20 //
21 // Include common header file for this module.
22 //
23 #include "CommonHeader.h"
24
25 //
26 // Decompression algorithm begins here
27 //
28 #define BITBUFSIZ 32
29 #define MAXMATCH 256
30 #define THRESHOLD 3
31 #define CODE_BIT 16
32 #define BAD_TABLE - 1
33
34 //
35 // C: Char&Len Set; P: Position Set; T: exTra Set
36 //
37 #define NC (0xff + MAXMATCH + 2 - THRESHOLD)
38 #define CBIT 9
39 #define MAXPBIT 5
40 #define TBIT 5
41 #define MAXNP ((1U << MAXPBIT) - 1)
42 #define NT (CODE_BIT + 3)
43 #if NT > MAXNP
44 #define NPT NT
45 #else
46 #define NPT MAXNP
47 #endif
48
49 typedef struct {
50 UINT8 *mSrcBase; ///< Starting address of compressed data
51 UINT8 *mDstBase; ///< Starting address of decompressed data
52 UINT32 mOutBuf;
53 UINT32 mInBuf;
54
55 UINT16 mBitCount;
56 UINT32 mBitBuf;
57 UINT32 mSubBitBuf;
58 UINT16 mBlockSize;
59 UINT32 mCompSize;
60 UINT32 mOrigSize;
61
62 UINT16 mBadTableFlag;
63
64 UINT16 mLeft[2 * NC - 1];
65 UINT16 mRight[2 * NC - 1];
66 UINT8 mCLen[NC];
67 UINT8 mPTLen[NPT];
68 UINT16 mCTable[4096];
69 UINT16 mPTTable[256];
70
71 ///
72 /// The length of the field 'Position Set Code Length Array Size' in Block Header.
73 /// For EFI 1.1 de/compression algorithm, mPBit = 4
74 /// For Tiano de/compression algorithm, mPBit = 5
75 ///
76 UINT8 mPBit;
77 } SCRATCH_DATA;
78
79 /**
80 Read NumOfBit of bits from source into mBitBuf
81
82 Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.
83
84 @param Sd The global scratch data
85 @param NumOfBits The number of bits to shift and read.
86
87 **/
88 VOID
89 FillBuf (
90 IN SCRATCH_DATA *Sd,
91 IN UINT16 NumOfBits
92 );
93
94 /**
95 Get NumOfBits of bits out from mBitBuf
96
97 Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent
98 NumOfBits of bits from source. Returns NumOfBits of bits that are
99 popped out.
100
101 @param Sd The global scratch data.
102 @param NumOfBits The number of bits to pop and read.
103
104 @return The bits that are popped out.
105
106 **/
107 UINT32
108 GetBits (
109 IN SCRATCH_DATA *Sd,
110 IN UINT16 NumOfBits
111 );
112
113 /**
114 Creates Huffman Code mapping table according to code length array.
115
116 Creates Huffman Code mapping table for Extra Set, Char&Len Set
117 and Position Set according to code length array.
118
119 @param Sd The global scratch data
120 @param NumOfChar Number of symbols in the symbol set
121 @param BitLen Code length array
122 @param TableBits The width of the mapping table
123 @param Table The table
124
125 @retval 0 OK.
126 @retval BAD_TABLE The table is corrupted.
127
128 **/
129 UINT16
130 MakeTable (
131 IN SCRATCH_DATA *Sd,
132 IN UINT16 NumOfChar,
133 IN UINT8 *BitLen,
134 IN UINT16 TableBits,
135 OUT UINT16 *Table
136 );
137
138 /**
139 Decodes a position value.
140
141 Get a position value according to Position Huffman Table.
142
143 @param Sd the global scratch data
144
145 @return The position value decoded.
146
147 **/
148 UINT32
149 DecodeP (
150 IN SCRATCH_DATA *Sd
151 );
152
153 /**
154 Reads code lengths for the Extra Set or the Position Set.
155
156 Read in the Extra Set or Pointion Set Length Arrary, then
157 generate the Huffman code mapping for them.
158
159 @param Sd The global scratch data.
160 @param nn Number of symbols.
161 @param nbit Number of bits needed to represent nn.
162 @param Special The special symbol that needs to be taken care of.
163
164 @retval 0 OK.
165 @retval BAD_TABLE Table is corrupted.
166
167 **/
168 UINT16
169 ReadPTLen (
170 IN SCRATCH_DATA *Sd,
171 IN UINT16 nn,
172 IN UINT16 nbit,
173 IN UINT16 Special
174 );
175
176 /**
177 Reads code lengths for Char&Len Set.
178
179 Read in and decode the Char&Len Set Code Length Array, then
180 generate the Huffman Code mapping table for the Char&Len Set.
181
182 @param Sd the global scratch data
183
184 **/
185 VOID
186 ReadCLen (
187 SCRATCH_DATA *Sd
188 );
189
190 /**
191 Decode a character/length value.
192
193 Read one value from mBitBuf, Get one code from mBitBuf. If it is at block boundary, generates
194 Huffman code mapping table for Extra Set, Code&Len Set and
195 Position Set.
196
197 @param Sd The global scratch data.
198
199 @return The value decoded.
200
201 **/
202 UINT16
203 DecodeC (
204 SCRATCH_DATA *Sd
205 );
206
207 /**
208 Decode the source data and put the resulting data into the destination buffer.
209
210 Decode the source data and put the resulting data into the destination buffer.
211
212 @param Sd The global scratch data
213
214 **/
215 VOID
216 Decode (
217 SCRATCH_DATA *Sd
218 );
219
220 #endif