CompressedFile.h
资源名称:compress.rar [点击查看]
上传用户:deer201011
上传日期:2022-06-24
资源大小:10k
文件大小:5k
源码类别:
压缩解压
开发平台:
Visual C++
- // CompressedFile.h
- #include "stdafx.h"
- #define BUFFERSIZE 32000
- // File format for compressed files is 8 bytes of signature/identifier.
- // DWORD size of uncompressed file.
- // DWORD size of compressed file.
- #define NUMBER_OF_SIGNATURES 3
- #define SIGNATURE_LENGTH 8
- static char *szSignatures[] = {
- "**++HUFF",
- "**++LZSS",
- "**++LZW "
- };
- #define HUFF 0
- #define LZSS 1
- #define LZW 2
- #define UNCOMP -1
- #ifndef FALSE
- #define FALSE 0
- #endif
- #ifndef TRUE
- #define TRUE !FALSE
- #endif
- // Huffman defines and structures follow...
- #define ESCAPE 257
- #define SYMBOL_COUNT 258
- #define NODE_TABLE_COUNT ( ( SYMBOL_COUNT * 2 ) - 1 )
- #define ROOT_NODE 0
- #define MAX_WEIGHT 0x8000
- #pragma pack(1)
- typedef struct node{
- unsigned int nWeight;
- int nParent;
- int nChildIsLeaf;
- int nChild;
- } NODE;
- typedef struct tree{
- int nLeaf[SYMBOL_COUNT];
- int nNextFreeNode;
- struct node nodes[NODE_TABLE_COUNT];
- } TREE;
- // LZSS Defines and Stucts
- #define INDEX_BIT_COUNT 12
- #define LENGTH_BIT_COUNT 4
- #define WINDOW_SIZE ( 1 << INDEX_BIT_COUNT )
- #define RAW_LOOK_AHEAD_SIZE ( 1 << LENGTH_BIT_COUNT )
- #define BREAK_EVEN ( ( 1 + INDEX_BIT_COUNT + LENGTH_BIT_COUNT ) / 9 )
- #define LOOK_AHEAD_SIZE ( RAW_LOOK_AHEAD_SIZE + BREAK_EVEN )
- #define TREE_ROOT WINDOW_SIZE
- #define MOD_WINDOW( a ) ( ( a ) & ( WINDOW_SIZE - 1 ) )
- typedef struct{
- int nParent;
- int nSmallerChild;
- int nLargerChild;
- } LZSS_TREE;
- // LZW Defines and structs
- #define BITS 15
- #define MAX_CODE 0x7fff
- #define TABLE_SIZE 35023L
- #define TABLE_BANKS ( ( TABLE_SIZE >> 8 ) + 1 )
- #define BUMP_CODE 257
- #define FLUSH_CODE 258
- #define FIRST_CODE 259
- typedef struct{
- int nCodeValue;
- int nParentCode;
- char nCharacter;
- } DICTIONARY;
- #define DICT( i ) m_Dict[i>>8][i&0x00ff]
- #pragma pack()
- class CCompressedFile : public CFile
- {
- public:
- CCompressedFile( int nType = LZW );
- ~CCompressedFile();
- virtual BOOL Open( const char *, unsigned int, CFileException *pError = NULL );
- virtual void Close( void );
- DWORD GetPosition( void );
- virtual unsigned int Read( void far *, unsigned int );
- virtual void Write( void far *, unsigned int );
- DWORD GetLength( void );
- BOOL FindFileInArchive( int );
- static int FileType( char * );
- static CCompressedFile *MakeNewFile( char *, int Typ = LZW );
- unsigned int ReadBytes( void far *, unsigned int );
- int ReadByte( void );
- void WriteBytes( void far *, unsigned int );
- void WriteByte( int );
- void OutputBit( int );
- void OutputBits( DWORD, int );
- int InputBit( void );
- DWORD InputBits( int );
- unsigned char *m_pBuffer;
- DWORD m_dwFilePointer;
- DWORD m_dwBytesInBuffer;
- DWORD m_dwBufferPointer;
- DWORD m_dwFileLength;
- BOOL m_bOpened;
- BOOL m_bCanRead;
- BOOL m_bCanWrite;
- int m_nCompressionType;
- int m_nRack, m_nPacifierCount, m_nMask;
- unsigned char m_ucBit;
- DWORD m_dwSeekTo;
- char m_szFilename[130];
- DWORD m_dwCompressedSize, m_dwUncompressedSize;
- unsigned int m_nFlags;
- void SetSizeToFileLength( void );
- };
- class CHuffmanFile : public CCompressedFile
- {
- public:
- virtual unsigned int Read( void far *, unsigned int );
- virtual void Write( void far *, unsigned int );
- virtual BOOL Open( const char *, unsigned int, CFileException *pError = NULL );
- virtual void Close( void );
- private:
- void UpdateModel( int );
- void EncodeSymbol( unsigned int );
- void AddNewNode( int );
- int DecodeSymbol( void );
- void InitializeTree( void );
- void RebuildTree( void );
- void SwapNodes( int, int );
- TREE Tree;
- };
- class CLzssFile : public CCompressedFile
- {
- public:
- virtual unsigned int Read( void far *, unsigned int );
- virtual void Write( void far *, unsigned int );
- virtual BOOL Open( const char *, unsigned int, CFileException *pError = NULL );
- virtual void Close( void );
- void EncodeSymbols( unsigned char *, BOOL EndFlag = FALSE );
- void InitializeTree( void );
- void ContractNode( int, int );
- void ReplaceNode( int, int );
- int FindNextNode( int );
- void DeleteString( int );
- int AddString( int, int * );
- BOOL m_bInitialized;
- int m_nCurrentPosition;
- int m_nLookAheadBytes;
- int m_nReplaceCount;
- int m_nMatchLength;
- int m_nMatchPosition;
- DWORD m_dwBytesToEncode;
- int m_nStoreLookAhead;
- DWORD m_dwPointer;
- DWORD m_dwExtra;
- DWORD m_dwExtraPointer;
- unsigned char *m_pExtraBuffer;
- unsigned char ucWindow[WINDOW_SIZE];
- LZSS_TREE Tree[WINDOW_SIZE+1];
- };
- class CLzwFile : public CCompressedFile
- {
- public:
- CLzwFile();
- ~CLzwFile();
- virtual unsigned int Read( void far *, unsigned int );
- virtual void Write( void far *, unsigned int );
- virtual BOOL Open( const char *, unsigned int, CFileException *pError = NULL );
- virtual void Close( void );
- void InitializeDictionary( void );
- void AllocateStorage( void );
- void DeleteStorage( void );
- unsigned int FindChildNode( int, int );
- unsigned int DecodeString( unsigned int, unsigned int );
- DICTIONARY *m_Dict[TABLE_BANKS];
- char m_cbDecodeStack[TABLE_SIZE];
- unsigned int m_nNextCode;
- int m_nCurrentCodeBits;
- unsigned int m_nNextBumpCode;
- BOOL m_bStorageAllocated;
- BOOL m_bStarted;
- int m_nCharacter, m_nStringCode, m_nIndex;
- int m_nNewCode, m_nOldCode;
- unsigned int m_nCount;
- int m_nEntryPoint;
- };
- class CUncompressedFile : public CCompressedFile
- {
- public:
- virtual BOOL Open( const char *, unsigned int, CFileException *pError = NULL );
- virtual unsigned int Read( void far *, unsigned int );
- virtual void Write( void far *, unsigned int );
- };