]>
Commit | Line | Data |
---|---|---|
e1f414b6 | 1 | /** @file\r |
00b7cc0f | 2 | Internal data structure defintions for Base UEFI Decompress Library.\r |
e1f414b6 | 3 | \r |
3f0055c8 | 4 | Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r |
9344f092 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
e1f414b6 | 6 | \r |
e1f414b6 | 7 | **/\r |
8 | \r | |
9 | #ifndef __BASE_UEFI_DECOMPRESS_LIB_INTERNALS_H__\r | |
10 | #define __BASE_UEFI_DECOMPRESS_LIB_INTERNALS_H__\r | |
11 | \r | |
3f0055c8 DB |
12 | #include <Base.h>\r |
13 | #include <Library/BaseLib.h>\r | |
14 | #include <Library/DebugLib.h>\r | |
15 | #include <Library/BaseMemoryLib.h>\r | |
16 | #include <Library/UefiDecompressLib.h>\r | |
e1f414b6 | 17 | //\r |
18 | // Decompression algorithm begins here\r | |
19 | //\r | |
2f88bd3a MK |
20 | #define BITBUFSIZ 32\r |
21 | #define MAXMATCH 256\r | |
22 | #define THRESHOLD 3\r | |
23 | #define CODE_BIT 16\r | |
24 | #define BAD_TABLE - 1\r | |
e1f414b6 | 25 | \r |
26 | //\r | |
27 | // C: Char&Len Set; P: Position Set; T: exTra Set\r | |
28 | //\r | |
2f88bd3a MK |
29 | #define NC (0xff + MAXMATCH + 2 - THRESHOLD)\r |
30 | #define CBIT 9\r | |
31 | #define MAXPBIT 5\r | |
32 | #define TBIT 5\r | |
33 | #define MAXNP ((1U << MAXPBIT) - 1)\r | |
34 | #define NT (CODE_BIT + 3)\r | |
e1f414b6 | 35 | #if NT > MAXNP\r |
2f88bd3a | 36 | #define NPT NT\r |
e1f414b6 | 37 | #else\r |
2f88bd3a | 38 | #define NPT MAXNP\r |
e1f414b6 | 39 | #endif\r |
40 | \r | |
41 | typedef struct {\r | |
2f88bd3a MK |
42 | UINT8 *mSrcBase; // The starting address of compressed data\r |
43 | UINT8 *mDstBase; // The starting address of decompressed data\r | |
44 | UINT32 mOutBuf;\r | |
45 | UINT32 mInBuf;\r | |
46 | \r | |
47 | UINT16 mBitCount;\r | |
48 | UINT32 mBitBuf;\r | |
49 | UINT32 mSubBitBuf;\r | |
50 | UINT16 mBlockSize;\r | |
51 | UINT32 mCompSize;\r | |
52 | UINT32 mOrigSize;\r | |
53 | \r | |
54 | UINT16 mBadTableFlag;\r | |
55 | \r | |
56 | UINT16 mLeft[2 * NC - 1];\r | |
57 | UINT16 mRight[2 * NC - 1];\r | |
58 | UINT8 mCLen[NC];\r | |
59 | UINT8 mPTLen[NPT];\r | |
60 | UINT16 mCTable[4096];\r | |
61 | UINT16 mPTTable[256];\r | |
e1f414b6 | 62 | \r |
63 | ///\r | |
64 | /// The length of the field 'Position Set Code Length Array Size' in Block Header.\r | |
58380e9c | 65 | /// For UEFI 2.0 de/compression algorithm, mPBit = 4.\r |
3f0055c8 | 66 | /// For Tiano de/compression algorithm, mPBit = 5.\r |
e1f414b6 | 67 | ///\r |
2f88bd3a | 68 | UINT8 mPBit;\r |
e1f414b6 | 69 | } SCRATCH_DATA;\r |
70 | \r | |
71 | /**\r | |
eceb3a4c | 72 | Read NumOfBit of bits from source into mBitBuf.\r |
e1f414b6 | 73 | \r |
74 | Shift mBitBuf NumOfBits left. Read in NumOfBits of bits from source.\r | |
75 | \r | |
58380e9c | 76 | @param Sd The global scratch data.\r |
e1f414b6 | 77 | @param NumOfBits The number of bits to shift and read.\r |
78 | \r | |
79 | **/\r | |
80 | VOID\r | |
81 | FillBuf (\r | |
82 | IN SCRATCH_DATA *Sd,\r | |
83 | IN UINT16 NumOfBits\r | |
84 | );\r | |
85 | \r | |
86 | /**\r | |
eceb3a4c | 87 | Get NumOfBits of bits out from mBitBuf.\r |
e1f414b6 | 88 | \r |
8693ca5d | 89 | Get NumOfBits of bits out from mBitBuf. Fill mBitBuf with subsequent\r |
90 | NumOfBits of bits from source. Returns NumOfBits of bits that are\r | |
e1f414b6 | 91 | popped out.\r |
92 | \r | |
93 | @param Sd The global scratch data.\r | |
94 | @param NumOfBits The number of bits to pop and read.\r | |
95 | \r | |
96 | @return The bits that are popped out.\r | |
97 | \r | |
98 | **/\r | |
99 | UINT32\r | |
100 | GetBits (\r | |
101 | IN SCRATCH_DATA *Sd,\r | |
102 | IN UINT16 NumOfBits\r | |
103 | );\r | |
104 | \r | |
105 | /**\r | |
106 | Creates Huffman Code mapping table according to code length array.\r | |
107 | \r | |
8693ca5d | 108 | Creates Huffman Code mapping table for Extra Set, Char&Len Set\r |
e1f414b6 | 109 | and Position Set according to code length array.\r |
61f0f437 | 110 | If TableBits > 16, then ASSERT ().\r |
e1f414b6 | 111 | \r |
58380e9c | 112 | @param Sd The global scratch data.\r |
113 | @param NumOfChar The number of symbols in the symbol set.\r | |
114 | @param BitLen Code length array.\r | |
115 | @param TableBits The width of the mapping table.\r | |
eceb3a4c | 116 | @param Table The table to be created.\r |
e1f414b6 | 117 | \r |
118 | @retval 0 OK.\r | |
119 | @retval BAD_TABLE The table is corrupted.\r | |
120 | \r | |
121 | **/\r | |
122 | UINT16\r | |
123 | MakeTable (\r | |
124 | IN SCRATCH_DATA *Sd,\r | |
125 | IN UINT16 NumOfChar,\r | |
126 | IN UINT8 *BitLen,\r | |
127 | IN UINT16 TableBits,\r | |
128 | OUT UINT16 *Table\r | |
129 | );\r | |
130 | \r | |
131 | /**\r | |
132 | Decodes a position value.\r | |
133 | \r | |
134 | Get a position value according to Position Huffman Table.\r | |
8693ca5d | 135 | \r |
58380e9c | 136 | @param Sd The global scratch data.\r |
e1f414b6 | 137 | \r |
138 | @return The position value decoded.\r | |
139 | \r | |
140 | **/\r | |
141 | UINT32\r | |
142 | DecodeP (\r | |
143 | IN SCRATCH_DATA *Sd\r | |
144 | );\r | |
145 | \r | |
146 | /**\r | |
147 | Reads code lengths for the Extra Set or the Position Set.\r | |
148 | \r | |
a750b4ae | 149 | Read in the Extra Set or Position Set Length Array, then\r |
e1f414b6 | 150 | generate the Huffman code mapping for them.\r |
151 | \r | |
152 | @param Sd The global scratch data.\r | |
2fc59a00 | 153 | @param nn The number of symbols.\r |
154 | @param nbit The number of bits needed to represent nn.\r | |
e1f414b6 | 155 | @param Special The special symbol that needs to be taken care of.\r |
156 | \r | |
157 | @retval 0 OK.\r | |
158 | @retval BAD_TABLE Table is corrupted.\r | |
159 | \r | |
160 | **/\r | |
161 | UINT16\r | |
162 | ReadPTLen (\r | |
163 | IN SCRATCH_DATA *Sd,\r | |
164 | IN UINT16 nn,\r | |
165 | IN UINT16 nbit,\r | |
166 | IN UINT16 Special\r | |
167 | );\r | |
168 | \r | |
169 | /**\r | |
170 | Reads code lengths for Char&Len Set.\r | |
8693ca5d | 171 | \r |
e1f414b6 | 172 | Read in and decode the Char&Len Set Code Length Array, then\r |
173 | generate the Huffman Code mapping table for the Char&Len Set.\r | |
174 | \r | |
58380e9c | 175 | @param Sd The global scratch data.\r |
e1f414b6 | 176 | \r |
177 | **/\r | |
178 | VOID\r | |
179 | ReadCLen (\r | |
180 | SCRATCH_DATA *Sd\r | |
181 | );\r | |
182 | \r | |
183 | /**\r | |
184 | Decode a character/length value.\r | |
8693ca5d | 185 | \r |
e1f414b6 | 186 | Read one value from mBitBuf, Get one code from mBitBuf. If it is at block boundary, generates\r |
187 | Huffman code mapping table for Extra Set, Code&Len Set and\r | |
188 | Position Set.\r | |
189 | \r | |
190 | @param Sd The global scratch data.\r | |
191 | \r | |
192 | @return The value decoded.\r | |
193 | \r | |
194 | **/\r | |
195 | UINT16\r | |
196 | DecodeC (\r | |
197 | SCRATCH_DATA *Sd\r | |
198 | );\r | |
199 | \r | |
200 | /**\r | |
201 | Decode the source data and put the resulting data into the destination buffer.\r | |
202 | \r | |
58380e9c | 203 | @param Sd The global scratch data.\r |
e1f414b6 | 204 | \r |
205 | **/\r | |
206 | VOID\r | |
207 | Decode (\r | |
208 | SCRATCH_DATA *Sd\r | |
209 | );\r | |
210 | \r | |
3f0055c8 DB |
211 | /**\r |
212 | Decompresses a compressed source buffer.\r | |
213 | \r | |
214 | Extracts decompressed data to its original form.\r | |
215 | This function is designed so that the decompression algorithm can be implemented\r | |
216 | without using any memory services. As a result, this function is not allowed to\r | |
217 | call any memory allocation services in its implementation. It is the caller's\r | |
218 | responsibility to allocate and free the Destination and Scratch buffers.\r | |
219 | If the compressed source data specified by Source is successfully decompressed\r | |
220 | into Destination, then RETURN_SUCCESS is returned. If the compressed source data\r | |
221 | specified by Source is not in a valid compressed data format,\r | |
222 | then RETURN_INVALID_PARAMETER is returned.\r | |
223 | \r | |
224 | If Source is NULL, then ASSERT().\r | |
225 | If Destination is NULL, then ASSERT().\r | |
226 | If the required scratch buffer size > 0 and Scratch is NULL, then ASSERT().\r | |
227 | \r | |
228 | @param Source The source buffer containing the compressed data.\r | |
229 | @param Destination The destination buffer to store the decompressed data.\r | |
230 | @param Scratch A temporary scratch buffer that is used to perform the decompression.\r | |
231 | This is an optional parameter that may be NULL if the\r | |
232 | required scratch buffer size is 0.\r | |
c0e174bd | 233 | @param Version 1 for UEFI Decompress algoruthm, 2 for Tiano Decompess algorithm.\r |
3f0055c8 DB |
234 | \r |
235 | @retval RETURN_SUCCESS Decompression completed successfully, and\r | |
236 | the uncompressed buffer is returned in Destination.\r | |
237 | @retval RETURN_INVALID_PARAMETER\r | |
238 | The source buffer specified by Source is corrupted\r | |
239 | (not in a valid compressed format).\r | |
240 | **/\r | |
241 | RETURN_STATUS\r | |
242 | UefiTianoDecompress (\r | |
243 | IN CONST VOID *Source,\r | |
244 | IN OUT VOID *Destination,\r | |
245 | IN OUT VOID *Scratch,\r | |
246 | IN UINT32 Version\r | |
247 | );\r | |
2f88bd3a | 248 | \r |
e1f414b6 | 249 | #endif\r |