lzari.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /************************************************************************/
  2. /* Copyright (C), 2016-2020, [IT], 保留所有权利;
  3. /* 模 块 名:;
  4. /* 描 述:;
  5. /*
  6. /* 版 本:[V];
  7. /* 作 者:[IT];
  8. /* 日 期:[7/24/2016];
  9. /*
  10. /*
  11. /* 注 意:;
  12. /*
  13. /* 修改记录:[IT];
  14. /* 修改日期:;
  15. /* 修改版本:;
  16. /* 修改内容:;
  17. /************************************************************************/
  18. #ifndef _FILE_H_COMPRESSION_LZARI_
  19. #define _FILE_H_COMPRESSION_LZARI_
  20. #define N 4096 /* size of ring buffer */
  21. #define F 60 /* upper limit for match_length */
  22. #define THRESHOLD 2 /* encode string into position and length
  23. if match_length is greater than this */
  24. #define NIL N /* index for root of binary search trees */
  25. /********** Arithmetic Compression **********/
  26. /* If you are not familiar with arithmetic compression, you should read
  27. I. E. Witten, R. M. Neal, and J. G. Cleary,
  28. Communications of the ACM, Vol. 30, pp. 520-540 (1987),
  29. from which much have been borrowed. */
  30. #define M 15
  31. /* Q1 (= 2 to the M) must be sufficiently large, but not so
  32. large as the unsigned long 4 * Q1 * (Q1 - 1) overflows. */
  33. #define Q1 (1UL << M)
  34. #define Q2 (2 * Q1)
  35. #define Q3 (3 * Q1)
  36. #define Q4 (4 * Q1)
  37. #define MAX_CUM (Q1 - 1)
  38. #define N_CHAR (256 - THRESHOLD + F)
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <VECTOR>
  44. #define FALSE 0
  45. #define TRUE 1
  46. namespace uitilitySdk
  47. {
  48. class __declspec(dllexport) LZARI
  49. {
  50. public:
  51. LZARI();
  52. virtual ~LZARI();
  53. protected:
  54. FILE *infile, *outfile;
  55. unsigned long textsize;
  56. unsigned long codesize;
  57. unsigned long printcount;
  58. unsigned char text_buf[N + F - 1]; /* ring buffer of size N,with extra F-1 bytes to facilitate string comparison */
  59. int match_position;
  60. int match_length; /* of longest match. These areset by the InsertNode() procedure. */
  61. int lson[N + 1];
  62. int rson[N + 257];
  63. int dad[N + 1]; /* left & right children &parents -- These constitute binary search trees. */
  64. /* character code = 0, 1, ..., N_CHAR - 1 */
  65. unsigned long low;
  66. unsigned long high;
  67. unsigned long value;
  68. int shifts; /* counts for magnifying low and high around Q2 */
  69. int char_to_sym[N_CHAR];
  70. int sym_to_char[N_CHAR + 1];
  71. unsigned int sym_freq[N_CHAR + 1]; /* frequency for symbols */
  72. unsigned int sym_cum[N_CHAR + 1]; /* cumulative freq for symbols */
  73. unsigned int position_cum[N + 1]; /* cumulative freq for positions */
  74. // Compress in memory;
  75. bool m_bMem;
  76. std::vector<BYTE> m_OutBuffer;
  77. //BYTE *m_pOutBuffer;
  78. int m_nOutLength;
  79. //int m_nOutCur;
  80. const BYTE *m_pInBuffer;
  81. int m_nInLength;
  82. int m_nInCur;
  83. unsigned int buffer_putbit, mask_putbit;
  84. unsigned int buffer_getbit, mask_getbit;
  85. private:
  86. void Error(char *message);
  87. void PutBit(int bit); /* Output one bit (bit = 0,1) */
  88. void FlushBitBuffer(void); /* Send remaining bits */
  89. int GetBit(void); /* Get one bit (0 or 1) */
  90. /********** LZSS with multiple binary trees **********/
  91. void InitTree(void); /* Initialize trees */
  92. void InsertNode(int r);
  93. void DeleteNode(int p); /* Delete node p from tree */
  94. void StartModel(void); /* Initialize model */
  95. void UpdateModel(int sym);
  96. void Output(int bit); /* Output 1 bit, followed by its complements */
  97. void EncodeChar(int ch);
  98. void EncodePosition(int position);
  99. void EncodeEnd(void);
  100. int BinarySearchSym(unsigned int x);
  101. int BinarySearchPos(unsigned int x);
  102. void StartDecode(void);
  103. int DecodeChar(void);
  104. int DecodePosition(void);
  105. void Encode(void);
  106. void Decode(void);
  107. public:
  108. void Compress(const char *lpszInfile,const char *lpszOutfile);
  109. void UnCompress(const char *lpszInfile,const char *lpszOutfile);
  110. void Compress(const BYTE *pInBuffer,int nInLength,const BYTE * &pOutBuffer ,int &nOutLength);
  111. void UnCompress(const BYTE *pInBuffer,int nInLength,const BYTE * &pOutBuffer,int &nOutLength);
  112. void Release();
  113. };
  114. };
  115. #endif