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