lzari.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include <VECTOR>
  42. typedef unsigned char BYTE;
  43. #define FALSE 0
  44. #define TRUE 1
  45. class LZARI
  46. {
  47. public:
  48. LZARI();
  49. virtual ~LZARI();
  50. protected:
  51. FILE *infile, *outfile;
  52. unsigned long textsize;
  53. unsigned long codesize;
  54. unsigned long printcount;
  55. unsigned char text_buf[N + F - 1]; /* ring buffer of size N,with extra F-1 bytes to facilitate string comparison */
  56. int match_position;
  57. int match_length; /* of longest match. These areset by the InsertNode() procedure. */
  58. int lson[N + 1];
  59. int rson[N + 257];
  60. int dad[N + 1]; /* left & right children &parents -- These constitute binary search trees. */
  61. /* character code = 0, 1, ..., N_CHAR - 1 */
  62. unsigned long low;
  63. unsigned long high;
  64. unsigned long value;
  65. int shifts; /* counts for magnifying low and high around Q2 */
  66. int char_to_sym[N_CHAR];
  67. int sym_to_char[N_CHAR + 1];
  68. unsigned int sym_freq[N_CHAR + 1]; /* frequency for symbols */
  69. unsigned int sym_cum[N_CHAR + 1]; /* cumulative freq for symbols */
  70. unsigned int position_cum[N + 1]; /* cumulative freq for positions */
  71. // Compress in memory;
  72. bool m_bMem;
  73. std::vector<BYTE> m_OutBuffer;
  74. //BYTE *m_pOutBuffer;
  75. int m_nOutLength;
  76. //int m_nOutCur;
  77. const BYTE *m_pInBuffer;
  78. int m_nInLength;
  79. int m_nInCur;
  80. unsigned int buffer_putbit, mask_putbit;
  81. unsigned int buffer_getbit, mask_getbit;
  82. private:
  83. void Error(char *message);
  84. void PutBit(int bit); /* Output one bit (bit = 0,1) */
  85. void FlushBitBuffer(void); /* Send remaining bits */
  86. int GetBit(void); /* Get one bit (0 or 1) */
  87. /********** LZSS with multiple binary trees **********/
  88. void InitTree(void); /* Initialize trees */
  89. void InsertNode(int r);
  90. void DeleteNode(int p); /* Delete node p from tree */
  91. void StartModel(void); /* Initialize model */
  92. void UpdateModel(int sym);
  93. void Output(int bit); /* Output 1 bit, followed by its complements */
  94. void EncodeChar(int ch);
  95. void EncodePosition(int position);
  96. void EncodeEnd(void);
  97. int BinarySearchSym(unsigned int x);
  98. int BinarySearchPos(unsigned int x);
  99. void StartDecode(void);
  100. int DecodeChar(void);
  101. int DecodePosition(void);
  102. void Encode(void);
  103. void Decode(void);
  104. public:
  105. void Compress(const char *lpszInfile,const char *lpszOutfile);
  106. void UnCompress(const char *lpszInfile,const char *lpszOutfile);
  107. void Compress(const BYTE *pInBuffer,int nInLength,const BYTE * &pOutBuffer ,int &nOutLength);
  108. void UnCompress(const BYTE *pInBuffer,int nInLength,const BYTE * &pOutBuffer,int &nOutLength);
  109. void Release();
  110. };
  111. #endif