| 4 | cycrow | 1 | /*****************************************************************************
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 |   LiteralHuffTree
 | 
        
           |  |  | 4 |   ---------------
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 |   Class for generating/emitting Huffman trees on a byte base.
 | 
        
           |  |  | 7 |   Could be used for adaptive and non-adaptive Huffman tree models.
 | 
        
           |  |  | 8 |   | 
        
           |  |  | 9 |   by yoda
 | 
        
           |  |  | 10 |   | 
        
           |  |  | 11 |   WWW:      y0da.cjb.net
 | 
        
           |  |  | 12 |   E-mail:   LordPE@gmx.net
 | 
        
           |  |  | 13 |   | 
        
           |  |  | 14 |   You are allowed to use this class in your own projects if you keep this
 | 
        
           |  |  | 15 |   trademark.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | *****************************************************************************/
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | #pragma once
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | #include <windows.h>
 | 
        
           |  |  | 22 | #include "ByteReader.h"
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | //
 | 
        
           |  |  | 25 | // constants
 | 
        
           |  |  | 26 | //
 | 
        
           |  |  | 27 | #define HT_LEFT  0
 | 
        
           |  |  | 28 | #define HT_RIGHT 1
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | //
 | 
        
           |  |  | 31 | // structures
 | 
        
           |  |  | 32 | //
 | 
        
           |  |  | 33 | typedef struct _LHT_ITEM
 | 
        
           |  |  | 34 | {
 | 
        
           |  |  | 35 | 	WORD                     id;                           // a unique value
 | 
        
           |  |  | 36 | 	BOOL                     bLinked;                      // already linked in the tree ?
 | 
        
           |  |  | 37 | 	DWORD                    dwDadId;                      // 0 == root item
 | 
        
           |  |  | 38 | 	DWORD                    dwLeftId;
 | 
        
           |  |  | 39 | 	DWORD                    dwRightId;  
 | 
        
           |  |  | 40 | 	DWORD                    dwcOccurr;
 | 
        
           |  |  | 41 | //	float                    probability;                  // how often the character appears in the input block
 | 
        
           |  |  | 42 | 	DWORD                    dwSequence;                   // bit signature for the character
 | 
        
           |  |  | 43 | 	BYTE                     bySequLen;                    // length of the bit signature
 | 
        
           |  |  | 44 | } LHT_ITEM, *PLHT_ITEM;
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 | //
 | 
        
           |  |  | 47 | // LiteralHuffTree class
 | 
        
           |  |  | 48 | //
 | 
        
           |  |  | 49 | class LiteralHuffTree
 | 
        
           |  |  | 50 | {
 | 
        
           |  |  | 51 | public:
 | 
        
           |  |  | 52 | 	LiteralHuffTree(void);
 | 
        
           |  |  | 53 | 	~LiteralHuffTree(void);
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 | 	DWORD      m_dwOccurr[256];
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 | 	BOOL       Generate( PBYTE buff, DWORD cb );
 | 
        
           |  |  | 58 | 	BOOL       GenerateWithOccurrences();
 | 
        
           |  |  | 59 | 	BOOL       GenerateAdaptiveModel();
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 | 	DWORD      QueryLiteralByBitSequence( PByteReader reader );
 | 
        
           |  |  | 62 | 	PLHT_ITEM  operator[]( UINT index ) { return &m_tree[index]; };
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | private:
 | 
        
           |  |  | 65 | 	LHT_ITEM   m_tree[(256 * 2) - 1];
 | 
        
           |  |  | 66 | 	UINT       m_ucTreeItems;
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 | 	void       GetByteOccurrences( PBYTE pby, DWORD cb );
 | 
        
           |  |  | 69 | 	void       BuildInternalTree();
 | 
        
           |  |  | 70 | 	void       LinkTreeElements();
 | 
        
           |  |  | 71 | 	bool       GenerateBitSequences();
 | 
        
           |  |  | 72 | 	DWORD      GetBitCountForOccurrence();
 | 
        
           |  |  | 73 | };
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 | typedef LiteralHuffTree *PLiteralHuffTree;
 |