1
/*****************************************************************************/
2
/* huffman.h Copyright (c) Ladislav Zezula 2003 */
3
/*---------------------------------------------------------------------------*/
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
/*****************************************************************************/
17
//-----------------------------------------------------------------------------
21
#define SWITCH_ITEMS 2 // Switch the item1 and item2
23
#define PTR_NOT(ptr) (THTreeItem *)(~(uint32_t)(ptr))
24
#define PTR_PTR(ptr) ((THTreeItem *)(ptr))
25
#define PTR_INT(ptr) (long)(ptr)
31
//-----------------------------------------------------------------------------
32
// Structures and classes
34
// Input stream for Huffmann decompression
37
TInputStream(uint8_t *data, uint32_t data_size) {
39
this->buffer_bit_size = ((int64_t)data_size) << 3;
50
void ConsumeBits(uint32_t count);
54
int64_t buffer_bit_size;
60
// Output stream for Huffmann compression
65
void PutBits(unsigned long dwBuff, unsigned int nPutBits);
67
unsigned char * pbOutBuffer; // 00 : Output buffer
68
unsigned long dwOutSize; // 04 : Size of output buffer
69
unsigned char * pbOutPos; // 08 : Current output position
70
unsigned long dwBitBuff; // 0C : Bit buffer
71
unsigned long nBits; // 10 : Number of bits in the bit buffer
74
// Huffmann tree item (?)
79
THTreeItem * Call1501DB70(THTreeItem * pLast);
80
THTreeItem * GetPrevItem(long value);
81
void ClearItemLinks();
84
THTreeItem * next; // 00 - Pointer to next THTreeItem
85
THTreeItem * prev; // 04 - Pointer to prev THTreeItem (< 0 if none)
86
unsigned long dcmpByte; // 08 - Index of this item in item pointer array, decompressed byte value
87
unsigned long byteValue; // 0C - Some byte value
88
THTreeItem * parent; // 10 - Pointer to parent THTreeItem (NULL if none)
89
THTreeItem * child; // 14 - Pointer to child THTreeItem
92
// Structure used for quick decompress. The 'bitCount' contains number of bits
93
// and byte value contains result decompressed byte value.
94
// After each walk through Huffman tree are filled all entries which are
95
// multiplies of number of bits loaded from input stream. These entries
96
// contain number of bits and result value. At the next 7 bits is tested this
97
// structure first. If corresponding entry found, decompression routine will
98
// not walk through Huffman tree and directly stores output byte to output stream.
101
unsigned long offs00; // 00 - 1 if resolved
102
unsigned long nBits; // 04 - Bit count
105
unsigned long dcmpByte; // 08 - Byte value for decompress (if bitCount <= 7)
106
THTreeItem * pItem; // 08 - THTreeItem (if number of bits is greater than 7
110
// Structure for Huffman tree (Size 0x3674 bytes). Because I'm not expert
111
// for the decompression, I do not know actually if the class is really a Hufmann
112
// tree. If someone knows the decompression details, please let me know
117
void InitTree(bool bCompression);
118
void BuildTree(unsigned int nCmpType);
119
// void ModifyTree(unsigned long dwIndex);
120
// void UninitTree();
122
// void Call15007010(Bit32 dwInLength, THTreeItem * item);
123
THTreeItem * Call1500E740(unsigned int nValue);
124
void Call1500E820(THTreeItem * pItem);
125
unsigned int DoCompression(TOutputStream * os, unsigned char * pbInBuffer, int nInLength, int nCmpType);
126
unsigned int DoDecompression(unsigned char * pbOutBuffer, unsigned int dwOutLength, TInputStream * is);
128
unsigned long bIsCmp0; // 0000 - 1 if compression type 0
129
unsigned long offs0004; // 0004 - Some flag
130
THTreeItem items0008[0x203]; // 0008 - HTree items
132
//- Sometimes used as HTree item -----------
133
THTreeItem * pItem3050; // 3050 - Always NULL (?)
134
THTreeItem * pItem3054; // 3054 - Pointer to Huffman tree item
135
THTreeItem * pItem3058; // 3058 - Pointer to Huffman tree item (< 0 if invalid)
137
//- Sometimes used as HTree item -----------
138
THTreeItem * pItem305C; // 305C - Usually NULL
139
THTreeItem * pFirst; // 3060 - Pointer to top (first) Huffman tree item
140
THTreeItem * pLast; // 3064 - Pointer to bottom (last) Huffman tree item (< 0 if invalid)
141
unsigned long nItems; // 3068 - Number of used HTree items
143
//-------------------------------------------
144
THTreeItem * items306C[0x102]; // 306C - THTreeItem pointer array
145
TQDecompress qd3474[0x80]; // 3474 - Array for quick decompression
147
static unsigned char Table1502A630[];// Some table
150
#endif // __HUFFMAN_H__