PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/StormLib/stormlib/huffman/huff.h

http://ghostcb.googlecode.com/
C++ Header | 143 lines | 83 code | 26 blank | 34 comment | 0 complexity | a29d763e8af1cb9568c6bc79c9b1073f MD5 | raw file
  1/*****************************************************************************/
  2/* huffman.h                              Copyright (c) Ladislav Zezula 2003 */
  3/*---------------------------------------------------------------------------*/
  4/* Description :                                                             */
  5/*---------------------------------------------------------------------------*/
  6/*   Date    Ver   Who  Comment                                              */
  7/* --------  ----  ---  -------                                              */
  8/* xx.xx.xx  1.00  Lad  The first version of huffman.h                       */
  9/* 03.05.03  2.00  Lad  Added compression                                    */
 10/* 08.12.03  2.01  Dan  High-memory handling (> 0x80000000)                  */
 11/*****************************************************************************/
 12 
 13#ifndef __HUFFMAN_H__
 14#define __HUFFMAN_H__
 15
 16#include "../StormPort.h"
 17 
 18//-----------------------------------------------------------------------------
 19// Defines
 20 
 21#define INSERT_ITEM    1                   
 22#define SWITCH_ITEMS   2                    // Switch the item1 and item2
 23 
 24#define PTR_NOT(ptr)  (THTreeItem *)(~(DWORD_PTR)(ptr))
 25#define PTR_PTR(ptr)  ((THTreeItem *)(ptr))
 26#define PTR_INT(ptr)  (INT_PTR)(ptr)
 27 
 28#ifndef NULL
 29#define NULL 0
 30#endif
 31 
 32//-----------------------------------------------------------------------------
 33// Structures and classes
 34 
 35// Input stream for Huffmann decompression
 36class TInputStream
 37{
 38    public:
 39 
 40    unsigned long GetBit();
 41    unsigned long Get7Bits();
 42    unsigned long Get8Bits();
 43    void SkipBits(unsigned int BitCount);
 44 
 45    unsigned char * pbInBuffer;         // 00 - Input data
 46    unsigned long   BitBuffer;          // 04 - Input bit buffer
 47    unsigned int    BitCount;           // 08 - Number of bits remaining in 'dwBitBuff'
 48};
 49 
 50// Output stream for Huffmann compression
 51class TOutputStream
 52{
 53    public:
 54 
 55    void PutBits(unsigned long dwBuff, unsigned int nPutBits);
 56 
 57    unsigned char * pbOutBuffer;        // 00 : Output buffer
 58    unsigned long   dwOutSize;          // 04 : Size of output buffer
 59    unsigned char * pbOutPos;           // 08 : Current output position
 60    unsigned long   dwBitBuff;          // 0C : Bit buffer
 61    unsigned long   nBits;              // 10 : Number of bits in the bit buffer
 62};
 63 
 64// Huffmann tree item (?)
 65struct THTreeItem
 66{
 67    public:
 68    
 69    THTreeItem * Call1501DB70(THTreeItem * pLast);
 70    THTreeItem * GetPrevItem(LONG_PTR value);
 71    void         ClearItemLinks();
 72    void         RemoveItem();
 73 
 74    THTreeItem  * next;                 // 00 - Pointer to next THTreeItem
 75    THTreeItem  * prev;                 // 04 - Pointer to prev THTreeItem (< 0 if none)
 76    unsigned long dcmpByte;             // 08 - Index of this item in item pointer array, decompressed byte value
 77    unsigned long byteValue;            // 0C - Some byte value
 78    THTreeItem  * parent;               // 10 - Pointer to parent THTreeItem (NULL if none)
 79    THTreeItem  * child;                // 14 - Pointer to child  THTreeItem
 80    int           addressMultiplier;    // -1 if object on negative address (>0x80000000), +1 if positive
 81};
 82 
 83// Structure used for quick decompress. The 'bitCount' contains number of bits
 84// and byte value contains result decompressed byte value.
 85// After each walk through Huffman tree are filled all entries which are
 86// multiplies of number of bits loaded from input stream. These entries
 87// contain number of bits and result value. At the next 7 bits is tested this
 88// structure first. If corresponding entry found, decompression routine will
 89// not walk through Huffman tree and directly stores output byte to output stream.
 90struct TQDecompress
 91{
 92    unsigned long offs00;               // 00 - 1 if resolved
 93    unsigned long nBits;                // 04 - Bit count
 94    union
 95    {
 96        unsigned long dcmpByte;         // 08 - Byte value for decompress (if bitCount <= 7)
 97        THTreeItem  * pItem;            // 08 - THTreeItem (if number of bits is greater than 7
 98    };
 99};
100 
101// Structure for Huffman tree (Size 0x3674 bytes). Because I'm not expert
102// for the decompression, I do not know actually if the class is really a Hufmann
103// tree. If someone knows the decompression details, please let me know
104class THuffmannTree
105{
106    public:
107    
108    THuffmannTree();
109    void  InitTree(bool bCompression);
110    void  BuildTree(unsigned int nCmpType);
111//  void  ModifyTree(unsigned long dwIndex);
112//  void  UninitTree();
113 
114//  void  Call15007010(Bit32 dwInLength, THTreeItem * item);
115    THTreeItem * Call1500E740(unsigned int nValue);
116    void         Call1500E820(THTreeItem * pItem);
117    unsigned int DoCompression(TOutputStream * os, unsigned char * pbInBuffer, int nInLength, int nCmpType);
118    unsigned int DoDecompression(unsigned char * pbOutBuffer, unsigned int dwOutLength, TInputStream * is);
119 
120    unsigned long bIsCmp0;              // 0000 - 1 if compression type 0
121    unsigned long offs0004;             // 0004 - Some flag
122    THTreeItem    items0008[0x203];     // 0008 - HTree items
123 
124    //- Sometimes used as HTree item -----------
125    THTreeItem  * pItem3050;            // 3050 - Always NULL (?)
126    THTreeItem  * pItem3054;            // 3054 - Pointer to Huffman tree item
127    THTreeItem  * pItem3058;            // 3058 - Pointer to Huffman tree item (< 0 if invalid)
128 
129    //- Sometimes used as HTree item -----------
130    THTreeItem  * pItem305C;            // 305C - Usually NULL
131    THTreeItem  * pFirst;               // 3060 - Pointer to top (first) Huffman tree item
132    THTreeItem  * pLast;                // 3064 - Pointer to bottom (last) Huffman tree item (< 0 if invalid)
133    unsigned long nItems;               // 3068 - Number of used HTree items
134 
135    //-------------------------------------------
136    THTreeItem * items306C[0x102];      // 306C - THTreeItem pointer array
137    TQDecompress qd3474[0x80];          // 3474 - Array for quick decompression
138    int          addressMultiplier;     // -1 if object on negative address (>0x80000000), +1 if positive
139 
140    static unsigned char Table1502A630[];// Some table
141};
142 
143#endif // __HUFFMAN_H__