]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/TianoCompress/TianoCompress.h
Sync EDKII BaseTools to BaseTools project r1971
[mirror_edk2.git] / BaseTools / Source / C / TianoCompress / TianoCompress.h
1 /** @file
2 Internal include file for Tiano Decompress Libary.
3
4 Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
5 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: TianoDecompress.h
14
15 **/
16
17 #ifndef __TIANO_DECOMPRESS_H__
18 #define __TIANO_DECOMPRESS_H__
19
20 #include <stdio.h>
21 #include <assert.h>
22 #include <Common/UefiBaseTypes.h>
23
24
25 //
26 // Decompression algorithm begins here
27 //
28 #define UTILITY_NAME "TianoCompress"
29 #define UTILITY_MAJOR_VERSION 0
30 #define UTILITY_MINOR_VERSION 1
31
32 //
33 // Default output file name
34 //
35 #define DEFAULT_OUTPUT_FILE "file.tmp"
36
37 #define BITBUFSIZ 32
38 #define MAXMATCH 256
39 #define THRESHOLD 3
40 #define CODE_BIT 16
41 #define BAD_TABLE - 1
42
43 typedef INT32 NODE;
44
45 //
46 // C: Char&Len Set; P: Position Set; T: exTra Set
47 //
48 #define NC (0xff + MAXMATCH + 2 - THRESHOLD)
49 #define CBIT 9
50 #define MAXPBIT 5
51 #define TBIT 5
52 #define MAXNP ((1U << MAXPBIT) - 1)
53 #define NT (CODE_BIT + 3)
54 #if NT > MAXNP
55 #define NPT NT
56 #else
57 #define NPT MAXNP
58 #endif
59
60 typedef struct {
61 UINT8 *mSrcBase; // Starting address of compressed data
62 UINT8 *mDstBase; // Starting address of decompressed data
63 UINT32 mOutBuf;
64 UINT32 mInBuf;
65
66 UINT16 mBitCount;
67 UINT32 mBitBuf;
68 UINT32 mSubBitBuf;
69 UINT16 mBlockSize;
70 UINT32 mCompSize;
71 UINT32 mOrigSize;
72
73 UINT16 mBadTableFlag;
74
75 UINT16 mLeft[2 * NC - 1];
76 UINT16 mRight[2 * NC - 1];
77 UINT8 mCLen[NC];
78 UINT8 mPTLen[NPT];
79 UINT16 mCTable[4096];
80 UINT16 mPTTable[256];
81
82 //
83 // The length of the field 'Position Set Code Length Array Size' in Block Header.
84 // For EFI 1.1 de/compression algorithm, mPBit = 4
85 // For Tiano de/compression algorithm, mPBit = 5
86 //
87 UINT8 mPBit;
88 } SCRATCH_DATA;
89
90 //
91 // Function Prototypes
92 //
93
94 EFI_STATUS
95 GetFileContents (
96 IN char *InputFileName,
97 OUT UINT8 *FileBuffer,
98 OUT UINT32 *BufferLength
99 );
100
101 STATIC
102 VOID
103 PutDword(
104 IN UINT32 Data
105 );
106
107 STATIC
108 EFI_STATUS
109 AllocateMemory (
110 VOID
111 );
112
113 STATIC
114 VOID
115 FreeMemory (
116 VOID
117 );
118
119 STATIC
120 VOID
121 InitSlide (
122 VOID
123 );
124
125 STATIC
126 NODE
127 Child (
128 IN NODE NodeQ,
129 IN UINT8 CharC
130 );
131
132 STATIC
133 VOID
134 MakeChild (
135 IN NODE NodeQ,
136 IN UINT8 CharC,
137 IN NODE NodeR
138 );
139
140 STATIC
141 VOID
142 Split (
143 IN NODE Old
144 );
145
146 STATIC
147 VOID
148 InsertNode (
149 VOID
150 );
151
152 STATIC
153 VOID
154 DeleteNode (
155 VOID
156 );
157
158 STATIC
159 VOID
160 GetNextMatch (
161 VOID
162 );
163
164 STATIC
165 EFI_STATUS
166 Encode (
167 VOID
168 );
169
170 STATIC
171 VOID
172 CountTFreq (
173 VOID
174 );
175
176 STATIC
177 VOID
178 WritePTLen (
179 IN INT32 Number,
180 IN INT32 nbit,
181 IN INT32 Special
182 );
183
184 STATIC
185 VOID
186 WriteCLen (
187 VOID
188 );
189
190 STATIC
191 VOID
192 EncodeC (
193 IN INT32 Value
194 );
195
196 STATIC
197 VOID
198 EncodeP (
199 IN UINT32 Value
200 );
201
202 STATIC
203 VOID
204 SendBlock (
205 VOID
206 );
207
208 STATIC
209 VOID
210 Output (
211 IN UINT32 c,
212 IN UINT32 p
213 );
214
215 STATIC
216 VOID
217 HufEncodeStart (
218 VOID
219 );
220
221 STATIC
222 VOID
223 HufEncodeEnd (
224 VOID
225 );
226
227 STATIC
228 VOID
229 MakeCrcTable (
230 VOID
231 );
232
233
234 STATIC
235 VOID
236 PutBits (
237 IN INT32 Number,
238 IN UINT32 Value
239 );
240
241 STATIC
242 INT32
243 FreadCrc (
244 OUT UINT8 *Pointer,
245 IN INT32 Number
246 );
247
248 STATIC
249 VOID
250 InitPutBits (
251 VOID
252 );
253
254 STATIC
255 VOID
256 CountLen (
257 IN INT32 Index
258 );
259
260 STATIC
261 VOID
262 MakeLen (
263 IN INT32 Root
264 );
265
266 STATIC
267 VOID
268 DownHeap (
269 IN INT32 Index
270 );
271
272 STATIC
273 VOID
274 MakeCode (
275 IN INT32 Number,
276 IN UINT8 Len[ ],
277 OUT UINT16 Code[]
278 );
279
280 STATIC
281 INT32
282 MakeTree (
283 IN INT32 NParm,
284 IN UINT16 FreqParm[],
285 OUT UINT8 LenParm[ ],
286 OUT UINT16 CodeParm[]
287 );
288
289 /**
290 Read NumOfBit of bits from source into mBitBuf
291
292 Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.
293
294 @param Sd The global scratch data
295 @param NumOfBits The number of bits to shift and read.
296
297 **/
298 VOID
299 FillBuf (
300 IN SCRATCH_DATA *Sd,
301 IN UINT16 NumOfBits
302 );
303
304 /**
305 Get NumOfBits of bits out from mBitBuf
306
307 Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent
308 NumOfBits of bits from source. Returns NumOfBits of bits that are
309 popped out.
310
311 @param Sd The global scratch data.
312 @param NumOfBits The number of bits to pop and read.
313
314 @return The bits that are popped out.
315
316 **/
317 UINT32
318 GetBits (
319 IN SCRATCH_DATA *Sd,
320 IN UINT16 NumOfBits
321 );
322
323 /**
324 Creates Huffman Code mapping table according to code length array.
325
326 Creates Huffman Code mapping table for Extra Set, Char&Len Set
327 and Position Set according to code length array.
328
329 @param Sd The global scratch data
330 @param NumOfChar Number of symbols in the symbol set
331 @param BitLen Code length array
332 @param TableBits The width of the mapping table
333 @param Table The table
334
335 @retval 0 OK.
336 @retval BAD_TABLE The table is corrupted.
337
338 **/
339 UINT16
340 MakeTable (
341 IN SCRATCH_DATA *Sd,
342 IN UINT16 NumOfChar,
343 IN UINT8 *BitLen,
344 IN UINT16 TableBits,
345 OUT UINT16 *Table
346 );
347
348 /**
349 Decodes a position value.
350
351 Get a position value according to Position Huffman Table.
352
353 @param Sd the global scratch data
354
355 @return The position value decoded.
356
357 **/
358 UINT32
359 DecodeP (
360 IN SCRATCH_DATA *Sd
361 );
362
363 /**
364 Reads code lengths for the Extra Set or the Position Set.
365
366 Read in the Extra Set or Pointion Set Length Arrary, then
367 generate the Huffman code mapping for them.
368
369 @param Sd The global scratch data.
370 @param nn Number of symbols.
371 @param nbit Number of bits needed to represent nn.
372 @param Special The special symbol that needs to be taken care of.
373
374 @retval 0 OK.
375 @retval BAD_TABLE Table is corrupted.
376
377 **/
378 UINT16
379 ReadPTLen (
380 IN SCRATCH_DATA *Sd,
381 IN UINT16 nn,
382 IN UINT16 nbit,
383 IN UINT16 Special
384 );
385
386 /**
387 Reads code lengths for Char&Len Set.
388
389 Read in and decode the Char&Len Set Code Length Array, then
390 generate the Huffman Code mapping table for the Char&Len Set.
391
392 @param Sd the global scratch data
393
394 **/
395 VOID
396 ReadCLen (
397 SCRATCH_DATA *Sd
398 );
399
400 /**
401 Decode a character/length value.
402
403 Read one value from mBitBuf, Get one code from mBitBuf. If it is at block boundary, generates
404 Huffman code mapping table for Extra Set, Code&Len Set and
405 Position Set.
406
407 @param Sd The global scratch data.
408
409 @return The value decoded.
410
411 **/
412 UINT16
413 DecodeC (
414 SCRATCH_DATA *Sd
415 );
416
417 /**
418 Decode the source data and put the resulting data into the destination buffer.
419
420 Decode the source data and put the resulting data into the destination buffer.
421
422 @param Sd The global scratch data
423
424 **/
425 VOID
426 Decode (
427 SCRATCH_DATA *Sd
428 );
429
430 RETURN_STATUS
431 EFIAPI
432 Decompress (
433 IN VOID *Source,
434 IN OUT VOID *Destination,
435 IN OUT VOID *Scratch,
436 IN UINT32 Version
437 );
438
439 #endif