lzari.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**************************************************************
  2. LZARI.C -- A Data Compression Program
  3. (tab = 4 spaces)
  4. ***************************************************************
  5. 4/7/1989 Haruhiko Okumura
  6. Use, distribute, and modify this program freely.
  7. Please send me your improved versions.
  8. PC-VAN SCIENCE
  9. NIFTY-Serve PAF01022
  10. CompuServe 74050,1022
  11. **************************************************************/
  12. /********** Bit I/O **********/
  13. #ifndef _FILE_H_COMPRESSION_LZARI_
  14. #define _FILE_H_COMPRESSION_LZARI_
  15. #pragma warning(disable:4786)
  16. #include <VECTOR>
  17. #define _OUTPUT_STATUS
  18. #define N 4096 /* size of ring buffer */
  19. #define F 60 /* upper limit for match_length */
  20. #define THRESHOLD 2 /* encode string into position and length
  21. if match_length is greater than this */
  22. #define NIL N /* index for root of binary search trees */
  23. /********** Arithmetic Compression **********/
  24. /* If you are not familiar with arithmetic compression, you should read
  25. I. E. Witten, R. M. Neal, and J. G. Cleary,
  26. Communications of the ACM, Vol. 30, pp. 520-540 (1987),
  27. from which much have been borrowed. */
  28. #define M 15
  29. /* Q1 (= 2 to the M) must be sufficiently large, but not so
  30. large as the unsigned long 4 * Q1 * (Q1 - 1) overflows. */
  31. #define Q1 (1UL << M)
  32. #define Q2 (2 * Q1)
  33. #define Q3 (3 * Q1)
  34. #define Q4 (4 * Q1)
  35. #define MAX_CUM (Q1 - 1)
  36. #define N_CHAR (256 - THRESHOLD + F)
  37. class LZARI
  38. {
  39. public:
  40. LZARI();
  41. virtual ~LZARI();
  42. protected:
  43. FILE *infile, *outfile;
  44. unsigned long textsize;
  45. unsigned long codesize;
  46. unsigned long printcount;
  47. unsigned char text_buf[N + F - 1]; /* ring buffer of size N,with extra F-1 bytes to facilitate string comparison */
  48. int match_position;
  49. int match_length; /* of longest match. These areset by the InsertNode() procedure. */
  50. int lson[N + 1];
  51. int rson[N + 257];
  52. int dad[N + 1]; /* left & right children &parents -- These constitute binary search trees. */
  53. /* character code = 0, 1, ..., N_CHAR - 1 */
  54. unsigned long low;
  55. unsigned long high;
  56. unsigned long value;
  57. int shifts; /* counts for magnifying low and high around Q2 */
  58. int char_to_sym[N_CHAR];
  59. int sym_to_char[N_CHAR + 1];
  60. unsigned int sym_freq[N_CHAR + 1]; /* frequency for symbols */
  61. unsigned int sym_cum[N_CHAR + 1]; /* cumulative freq for symbols */
  62. unsigned int position_cum[N + 1]; /* cumulative freq for positions */
  63. // Compress in memory;
  64. bool m_bMem;
  65. std::vector<BYTE> m_OutBuffer;
  66. //BYTE *m_pOutBuffer;
  67. int m_nOutLength;
  68. //int m_nOutCur;
  69. const BYTE *m_pInBuffer;
  70. int m_nInLength;
  71. int m_nInCur;
  72. unsigned int buffer_putbit, mask_putbit;
  73. unsigned int buffer_getbit, mask_getbit;
  74. private:
  75. void Error(char *message);
  76. void PutBit(int bit); /* Output one bit (bit = 0,1) */
  77. void FlushBitBuffer(void); /* Send remaining bits */
  78. int GetBit(void); /* Get one bit (0 or 1) */
  79. /********** LZSS with multiple binary trees **********/
  80. void InitTree(void); /* Initialize trees */
  81. void InsertNode(int r);
  82. void DeleteNode(int p); /* Delete node p from tree */
  83. void StartModel(void); /* Initialize model */
  84. void UpdateModel(int sym);
  85. void Output(int bit); /* Output 1 bit, followed by its complements */
  86. void EncodeChar(int ch);
  87. void EncodePosition(int position);
  88. void EncodeEnd(void);
  89. int BinarySearchSym(unsigned int x);
  90. int BinarySearchPos(unsigned int x);
  91. void StartDecode(void);
  92. int DecodeChar(void);
  93. int DecodePosition(void);
  94. void Encode(void);
  95. void Decode(void);
  96. public:
  97. void Compress(const char *lpszInfile, const char *lpszOutfile);
  98. void UnCompress(const char *lpszInfile, const char *lpszOutfile);
  99. void Compress(const BYTE *pInBuffer, int nInLength, const BYTE * &pOutBuffer, int &nOutLength);
  100. void UnCompress(const BYTE *pInBuffer, int nInLength, const BYTE * &pOutBuffer, int &nOutLength);
  101. void Release();
  102. };
  103. #endif