1 /** @file
2   Internal data structure and interfaces defintions for UEFI and Tiano Decompress Libary.
3 
4   Copyright (c) 2006 - 2009, 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 **/
14 
15 #ifndef __BASE_UEFI_TIANO_CUSTOM_DECOMPRESS_LIB_INTERNALS_H__
16 #define __BASE_UEFI_TIANO_CUSTOM_DECOMPRESS_LIB_INTERNALS_H__
17 
18 #include <PiPei.h>
19 
20 #include <Guid/TianoDecompress.h>
21 #include <Library/BaseLib.h>
22 #include <Library/UefiDecompressLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/ExtractGuidedSectionLib.h>
26 
27 //
28 // Decompression algorithm begins here
29 //
30 #define BITBUFSIZ 32
31 #define MAXMATCH  256
32 #define THRESHOLD 3
33 #define CODE_BIT  16
34 #define BAD_TABLE - 1
35 
36 //
37 // C: Char&Len Set; P: Position Set; T: exTra Set
38 //
39 #define NC      (0xff + MAXMATCH + 2 - THRESHOLD)
40 #define CBIT    9
41 #define MAXPBIT 5
42 #define TBIT    5
43 #define MAXNP   ((1U << MAXPBIT) - 1)
44 #define NT      (CODE_BIT + 3)
45 #if NT > MAXNP
46 #define NPT NT
47 #else
48 #define NPT MAXNP
49 #endif
50 
51 typedef struct {
52   UINT8   *mSrcBase;  // Starting address of compressed data
53   UINT8   *mDstBase;  // Starting address of decompressed data
54   UINT32  mOutBuf;
55   UINT32  mInBuf;
56 
57   UINT16  mBitCount;
58   UINT32  mBitBuf;
59   UINT32  mSubBitBuf;
60   UINT16  mBlockSize;
61   UINT32  mCompSize;
62   UINT32  mOrigSize;
63 
64   UINT16  mBadTableFlag;
65 
66   UINT16  mLeft[2 * NC - 1];
67   UINT16  mRight[2 * NC - 1];
68   UINT8   mCLen[NC];
69   UINT8   mPTLen[NPT];
70   UINT16  mCTable[4096];
71   UINT16  mPTTable[256];
72 
73   ///
74   /// The length of the field 'Position Set Code Length Array Size' in Block Header.
75   /// For UEFI 2.0 de/compression algorithm, mPBit = 4
76   /// For Tiano de/compression algorithm, mPBit = 5
77   ///
78   UINT8   mPBit;
79 } SCRATCH_DATA;
80 
81 /**
82   Read NumOfBit of bits from source into mBitBuf.
83 
84   Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.
85 
86   @param  Sd        The global scratch data
87   @param  NumOfBits The number of bits to shift and read.
88 
89 **/
90 VOID
91 FillBuf (
92   IN  SCRATCH_DATA  *Sd,
93   IN  UINT16        NumOfBits
94   );
95 
96 /**
97   Get NumOfBits of bits out from mBitBuf.
98 
99   Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent
100   NumOfBits of bits from source. Returns NumOfBits of bits that are
101   popped out.
102 
103   @param  Sd        The global scratch data.
104   @param  NumOfBits The number of bits to pop and read.
105 
106   @return The bits that are popped out.
107 
108 **/
109 UINT32
110 GetBits (
111   IN  SCRATCH_DATA  *Sd,
112   IN  UINT16        NumOfBits
113   );
114 
115 /**
116   Creates Huffman Code mapping table according to code length array.
117 
118   Creates Huffman Code mapping table for Extra Set, Char&Len Set
119   and Position Set according to code length array.
120 
121   @param  Sd        The global scratch data
122   @param  NumOfChar Number of symbols in the symbol set
123   @param  BitLen    Code length array
124   @param  TableBits The width of the mapping table
125   @param  Table     The table to be created.
126 
127   @retval  0 OK.
128   @retval  BAD_TABLE The table is corrupted.
129 
130 **/
131 UINT16
132 MakeTable (
133   IN  SCRATCH_DATA  *Sd,
134   IN  UINT16        NumOfChar,
135   IN  UINT8         *BitLen,
136   IN  UINT16        TableBits,
137   OUT UINT16        *Table
138   );
139 
140 /**
141   Decodes a position value.
142 
143   Get a position value according to Position Huffman Table.
144 
145   @param  Sd the global scratch data
146 
147   @return The position value decoded.
148 
149 **/
150 UINT32
151 DecodeP (
152   IN  SCRATCH_DATA  *Sd
153   );
154 
155 /**
156   Reads code lengths for the Extra Set or the Position Set.
157 
158   Read in the Extra Set or Pointion Set Length Arrary, then
159   generate the Huffman code mapping for them.
160 
161   @param  Sd      The global scratch data.
162   @param  nn      Number of symbols.
163   @param  nbit    Number of bits needed to represent nn.
164   @param  Special The special symbol that needs to be taken care of.
165 
166   @retval  0 OK.
167   @retval  BAD_TABLE Table is corrupted.
168 
169 **/
170 UINT16
171 ReadPTLen (
172   IN  SCRATCH_DATA  *Sd,
173   IN  UINT16        nn,
174   IN  UINT16        nbit,
175   IN  UINT16        Special
176   );
177 
178 /**
179   Reads code lengths for Char&Len Set.
180 
181   Read in and decode the Char&Len Set Code Length Array, then
182   generate the Huffman Code mapping table for the Char&Len Set.
183 
184   @param  Sd the global scratch data
185 
186 **/
187 VOID
188 ReadCLen (
189   SCRATCH_DATA  *Sd
190   );
191 
192 /**
193   Decode a character/length value.
194 
195   Read one value from mBitBuf, Get one code from mBitBuf. If it is at block boundary, generates
196   Huffman code mapping table for Extra Set, Code&Len Set and
197   Position Set.
198 
199   @param  Sd The global scratch data.
200 
201   @return The value decoded.
202 
203 **/
204 UINT16
205 DecodeC (
206   SCRATCH_DATA  *Sd
207   );
208 
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
221