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