lzari.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. /********************************************************************
  13. lzari.cpp -- A Data Compression Class
  14. created: 2004/10/04
  15. created: 4:10:2004 16:44
  16. file base: lzari
  17. file ext: cpp
  18. author: 阙荣文 (querw@sina.com)
  19. purpose: 如上所述,lzari.c提供了lzari压缩算法的实现,基于lzari.c我把它
  20. 做成了一个c++类方便使用
  21. *********************************************************************/
  22. #include "StdAfx.h"
  23. //#include <stdio.h>
  24. //#include <stdlib.h>
  25. //#include <string.h>
  26. //#include <ctype.h>
  27. #include "Lzari.h"
  28. LZARI::LZARI()
  29. {
  30. infile = NULL;
  31. outfile = NULL;
  32. textsize = 0;
  33. codesize = 0;
  34. printcount = 0;
  35. low = 0;
  36. high = Q4;
  37. value = 0;
  38. shifts = 0;/* counts for magnifying low and high around Q2 */
  39. m_bMem = FALSE;
  40. m_pInBuffer = NULL;
  41. m_nInLength = 0;
  42. m_nInCur = 0;
  43. //m_pOutBuffer = NULL;
  44. m_nOutLength = 0;
  45. // m_nOutCur = 0;
  46. buffer_putbit = 0;
  47. mask_putbit = 128;
  48. buffer_getbit = 0;
  49. mask_getbit = 0;
  50. }
  51. LZARI::~LZARI()
  52. {
  53. Release();
  54. }
  55. void LZARI::Error(char *message)
  56. {
  57. #ifdef _OUTPUT_STATUS
  58. printf("\n%s\n", message);
  59. #endif
  60. //exit(EXIT_FAILURE);
  61. int e = 1;
  62. throw e;
  63. }
  64. void LZARI::PutBit(int bit) /* Output one bit (bit = 0,1) */
  65. {
  66. if (bit) buffer_putbit |= mask_putbit;
  67. if ((mask_putbit >>= 1) == 0)
  68. {
  69. if (!m_bMem)
  70. {
  71. if (putc(buffer_putbit, outfile) == EOF) Error("Write Error");
  72. }
  73. else
  74. {
  75. //if (m_nOutCur == m_nOutLength) Error("Write Error");
  76. //m_pOutBuffer[m_nOutCur++] = buffer;
  77. m_OutBuffer.push_back(buffer_putbit);
  78. //m_nOutCur++;
  79. }
  80. buffer_putbit = 0;
  81. mask_putbit = 128;
  82. codesize++;
  83. }
  84. }
  85. void LZARI::FlushBitBuffer(void) /* Send remaining bits */
  86. {
  87. int i;
  88. for (i = 0; i < 7; i++) PutBit(0);
  89. }
  90. int LZARI::GetBit(void) /* Get one bit (0 or 1) */
  91. {
  92. if ((mask_getbit >>= 1) == 0)
  93. {
  94. if (!m_bMem)
  95. buffer_getbit = getc(infile);
  96. else
  97. buffer_getbit = m_pInBuffer[m_nInCur++];
  98. mask_getbit = 128;
  99. }
  100. return ((buffer_getbit & mask_getbit) != 0);
  101. }
  102. /********** LZSS with multiple binary trees **********/
  103. void LZARI::InitTree(void) /* Initialize trees */
  104. {
  105. int i;
  106. /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
  107. left children of node i. These nodes need not be initialized.
  108. Also, dad[i] is the parent of node i. These are initialized to
  109. NIL (= N), which stands for 'not used.'
  110. For i = 0 to 255, rson[N + i + 1] is the root of the tree
  111. for strings that begin with character i. These are initialized
  112. to NIL. Note there are 256 trees. */
  113. for (i = N + 1; i <= N + 256; i++) rson[i] = NIL; /* root */
  114. for (i = 0; i < N; i++) dad[i] = NIL; /* node */
  115. }
  116. void LZARI::InsertNode(int r)
  117. /* Inserts string of length F, text_buf[r..r+F-1], into one of the
  118. trees (text_buf[r]'th tree) and returns the longest-match position
  119. and length via the global variables match_position and match_length.
  120. If match_length = F, then removes the old node in favor of the new
  121. one, because the old one will be deleted sooner.
  122. Note r plays double role, as tree node and position in buffer. */
  123. {
  124. int i, p, cmp, temp;
  125. unsigned char *key;
  126. cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
  127. rson[r] = lson[r] = NIL; match_length = 0;
  128. for (;;)
  129. {
  130. if (cmp >= 0)
  131. {
  132. if (rson[p] != NIL) p = rson[p];
  133. else { rson[p] = r; dad[r] = p; return; }
  134. }
  135. else
  136. {
  137. if (lson[p] != NIL) p = lson[p];
  138. else { lson[p] = r; dad[r] = p; return; }
  139. }
  140. for (i = 1; i < F; i++)
  141. if ((cmp = key[i] - text_buf[p + i]) != 0) break;
  142. if (i > THRESHOLD)
  143. {
  144. if (i > match_length)
  145. {
  146. match_position = (r - p) & (N - 1);
  147. if ((match_length = i) >= F) break;
  148. }
  149. else if (i == match_length)
  150. {
  151. if ((temp = (r - p) & (N - 1)) < match_position)
  152. match_position = temp;
  153. }
  154. }
  155. }
  156. dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
  157. dad[lson[p]] = r; dad[rson[p]] = r;
  158. if (rson[dad[p]] == p) rson[dad[p]] = r;
  159. else lson[dad[p]] = r;
  160. dad[p] = NIL; /* remove p */
  161. }
  162. void LZARI::DeleteNode(int p) /* Delete node p from tree */
  163. {
  164. int q;
  165. if (dad[p] == NIL) return; /* not in tree */
  166. if (rson[p] == NIL) q = lson[p];
  167. else if (lson[p] == NIL) q = rson[p];
  168. else
  169. {
  170. q = lson[p];
  171. if (rson[q] != NIL)
  172. {
  173. do { q = rson[q]; } while (rson[q] != NIL);
  174. rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
  175. lson[q] = lson[p]; dad[lson[p]] = q;
  176. }
  177. rson[q] = rson[p]; dad[rson[p]] = q;
  178. }
  179. dad[q] = dad[p];
  180. if (rson[dad[p]] == p) rson[dad[p]] = q;
  181. else lson[dad[p]] = q;
  182. dad[p] = NIL;
  183. }
  184. /********** Arithmetic Compression **********/
  185. /* If you are not familiar with arithmetic compression, you should read
  186. I. E. Witten, R. M. Neal, and J. G. Cleary,
  187. Communications of the ACM, Vol. 30, pp. 520-540 (1987),
  188. from which much have been borrowed. */
  189. /* character code = 0, 1, ..., N_CHAR - 1 */
  190. void LZARI::StartModel(void) /* Initialize model */
  191. {
  192. int ch, sym, i;
  193. sym_cum[N_CHAR] = 0;
  194. for (sym = N_CHAR; sym >= 1; sym--)
  195. {
  196. ch = sym - 1;
  197. char_to_sym[ch] = sym; sym_to_char[sym] = ch;
  198. sym_freq[sym] = 1;
  199. sym_cum[sym - 1] = sym_cum[sym] + sym_freq[sym];
  200. }
  201. sym_freq[0] = 0; /* sentinel (!= sym_freq[1]) */
  202. position_cum[N] = 0;
  203. for (i = N; i >= 1; i--)
  204. position_cum[i - 1] = position_cum[i] + 10000 / (i + 200);
  205. /* empirical distribution function (quite tentative) */
  206. /* Please devise a better mechanism! */
  207. }
  208. void LZARI::UpdateModel(int sym)
  209. {
  210. int i, c, ch_i, ch_sym;
  211. if (sym_cum[0] >= MAX_CUM)
  212. {
  213. c = 0;
  214. for (i = N_CHAR; i > 0; i--)
  215. {
  216. sym_cum[i] = c;
  217. c += (sym_freq[i] = (sym_freq[i] + 1) >> 1);
  218. }
  219. sym_cum[0] = c;
  220. }
  221. for (i = sym; sym_freq[i] == sym_freq[i - 1]; i--);
  222. if (i < sym)
  223. {
  224. ch_i = sym_to_char[i]; ch_sym = sym_to_char[sym];
  225. sym_to_char[i] = ch_sym; sym_to_char[sym] = ch_i;
  226. char_to_sym[ch_i] = sym; char_to_sym[ch_sym] = i;
  227. }
  228. sym_freq[i]++;
  229. while (--i >= 0) sym_cum[i]++;
  230. }
  231. void LZARI::Output(int bit) /* Output 1 bit, followed by its complements */
  232. {
  233. PutBit(bit);
  234. for (; shifts > 0; shifts--) PutBit(!bit);
  235. }
  236. void LZARI::EncodeChar(int ch)
  237. {
  238. int sym;
  239. unsigned long int range;
  240. sym = char_to_sym[ch];
  241. range = high - low;
  242. high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
  243. low += (range * sym_cum[sym]) / sym_cum[0];
  244. for (;;)
  245. {
  246. if (high <= Q2) Output(0);
  247. else if (low >= Q2)
  248. {
  249. Output(1); low -= Q2; high -= Q2;
  250. }
  251. else if (low >= Q1 && high <= Q3)
  252. {
  253. shifts++; low -= Q1; high -= Q1;
  254. }
  255. else break;
  256. low += low;
  257. high += high;
  258. }
  259. UpdateModel(sym);
  260. }
  261. void LZARI::EncodePosition(int position)
  262. {
  263. unsigned long int range;
  264. range = high - low;
  265. high = low + (range * position_cum[position]) / position_cum[0];
  266. low += (range * position_cum[position + 1]) / position_cum[0];
  267. for (;;)
  268. {
  269. if (high <= Q2) Output(0);
  270. else if (low >= Q2)
  271. {
  272. Output(1); low -= Q2; high -= Q2;
  273. }
  274. else if (low >= Q1 && high <= Q3)
  275. {
  276. shifts++; low -= Q1; high -= Q1;
  277. }
  278. else break;
  279. low += low;
  280. high += high;
  281. }
  282. }
  283. void LZARI::EncodeEnd(void)
  284. {
  285. shifts++;
  286. if (low < Q1) Output(0); else Output(1);
  287. FlushBitBuffer(); /* flush bits remaining in buffer */
  288. }
  289. int LZARI::BinarySearchSym(unsigned int x)
  290. /* 1 if x >= sym_cum[1],
  291. N_CHAR if sym_cum[N_CHAR] > x,
  292. i such that sym_cum[i - 1] > x >= sym_cum[i] otherwise */
  293. {
  294. int i, j, k;
  295. i = 1; j = N_CHAR;
  296. while (i < j)
  297. {
  298. k = (i + j) / 2;
  299. if (sym_cum[k] > x) i = k + 1; else j = k;
  300. }
  301. return i;
  302. }
  303. int LZARI::BinarySearchPos(unsigned int x)
  304. /* 0 if x >= position_cum[1],
  305. N - 1 if position_cum[N] > x,
  306. i such that position_cum[i] > x >= position_cum[i + 1] otherwise */
  307. {
  308. int i, j, k;
  309. i = 1; j = N;
  310. while (i < j)
  311. {
  312. k = (i + j) / 2;
  313. if (position_cum[k] > x) i = k + 1; else j = k;
  314. }
  315. return i - 1;
  316. }
  317. void LZARI::StartDecode(void)
  318. {
  319. int i;
  320. for (i = 0; i < M + 2; i++)
  321. value = 2 * value + GetBit();
  322. }
  323. int LZARI::DecodeChar(void)
  324. {
  325. int sym, ch;
  326. unsigned long int range;
  327. range = high - low;
  328. sym = BinarySearchSym((unsigned int)
  329. (((value - low + 1) * sym_cum[0] - 1) / range));
  330. high = low + (range * sym_cum[sym - 1]) / sym_cum[0];
  331. low += (range * sym_cum[sym]) / sym_cum[0];
  332. for (;;) {
  333. if (low >= Q2) {
  334. value -= Q2; low -= Q2; high -= Q2;
  335. }
  336. else if (low >= Q1 && high <= Q3) {
  337. value -= Q1; low -= Q1; high -= Q1;
  338. }
  339. else if (high > Q2) break;
  340. low += low; high += high;
  341. value = 2 * value + GetBit();
  342. }
  343. ch = sym_to_char[sym];
  344. UpdateModel(sym);
  345. return ch;
  346. }
  347. int LZARI::DecodePosition(void)
  348. {
  349. int position;
  350. unsigned long int range;
  351. range = high - low;
  352. position = BinarySearchPos((unsigned int)
  353. (((value - low + 1) * position_cum[0] - 1) / range));
  354. high = low + (range * position_cum[position]) / position_cum[0];
  355. low += (range * position_cum[position + 1]) / position_cum[0];
  356. for (;;) {
  357. if (low >= Q2) {
  358. value -= Q2; low -= Q2; high -= Q2;
  359. }
  360. else if (low >= Q1 && high <= Q3) {
  361. value -= Q1; low -= Q1; high -= Q1;
  362. }
  363. else if (high > Q2) break;
  364. low += low; high += high;
  365. value = 2 * value + GetBit();
  366. }
  367. return position;
  368. }
  369. /********** Encode and Decode **********/
  370. void LZARI::Encode(void)
  371. {
  372. int i, c, len, r, s, last_match_length;
  373. if (!m_bMem)
  374. {
  375. fseek(infile, 0L, SEEK_END);
  376. textsize = ftell(infile);
  377. if (fwrite(&textsize, sizeof textsize, 1, outfile) < 1)
  378. Error("Write Error"); /* output size of text */
  379. codesize += sizeof textsize;
  380. if (textsize == 0) return;
  381. rewind(infile);
  382. textsize = 0;
  383. }
  384. else
  385. {
  386. textsize = m_nInLength;
  387. m_OutBuffer.resize(sizeof textsize);
  388. memcpy(&m_OutBuffer[0], &textsize, sizeof textsize);
  389. //m_nOutCur += sizeof textsize;
  390. codesize += sizeof textsize;
  391. if (textsize == 0) return;
  392. m_nInCur = 0;
  393. textsize = 0;
  394. }
  395. StartModel();
  396. InitTree();
  397. s = 0; r = N - F;
  398. for (i = s; i < r; i++) text_buf[i] = ' ';
  399. if (!m_bMem)
  400. for (len = 0; len < F && (c = getc(infile)) != EOF; len++) text_buf[r + len] = c;
  401. else
  402. for (len = 0; len < F && m_nInCur < m_nInLength; len++)
  403. {
  404. c = m_pInBuffer[m_nInCur++];
  405. text_buf[r + len] = c;
  406. }
  407. textsize = len;
  408. for (i = 1; i <= F; i++) InsertNode(r - i);
  409. InsertNode(r);
  410. do {
  411. if (match_length > len) match_length = len;
  412. if (match_length <= THRESHOLD)
  413. {
  414. match_length = 1; EncodeChar(text_buf[r]);
  415. }
  416. else
  417. {
  418. EncodeChar(255 - THRESHOLD + match_length);
  419. EncodePosition(match_position - 1);
  420. }
  421. last_match_length = match_length;
  422. if (!m_bMem)
  423. {
  424. for (i = 0; i < last_match_length && (c = getc(infile)) != EOF; i++)
  425. {
  426. DeleteNode(s); text_buf[s] = c;
  427. if (s < F - 1) text_buf[s + N] = c;
  428. s = (s + 1) & (N - 1);
  429. r = (r + 1) & (N - 1);
  430. InsertNode(r);
  431. }
  432. }
  433. else
  434. {
  435. for (i = 0; i < last_match_length && m_nInCur < m_nInLength; i++)
  436. {
  437. c = m_pInBuffer[m_nInCur++];
  438. DeleteNode(s);
  439. text_buf[s] = c;
  440. if (s < F - 1) text_buf[s + N] = c;
  441. s = (s + 1) & (N - 1);
  442. r = (r + 1) & (N - 1);
  443. InsertNode(r);
  444. }
  445. }
  446. if ((textsize += i) > printcount)
  447. {
  448. #ifdef _OUTPUT_STATUS
  449. printf("%12ld\r", textsize);
  450. #endif
  451. printcount += 1024;
  452. }
  453. while (i++ < last_match_length)
  454. {
  455. DeleteNode(s);
  456. s = (s + 1) & (N - 1);
  457. r = (r + 1) & (N - 1);
  458. if (--len) InsertNode(r);
  459. }
  460. } while (len > 0);
  461. EncodeEnd();
  462. #ifdef _OUTPUT_STATUS
  463. printf("In : %lu bytes\n", textsize);
  464. printf("Out: %lu bytes\n", codesize);
  465. printf("Out/In: %.3f\n", (double)codesize / textsize);
  466. #endif
  467. }
  468. void LZARI::Decode(void)
  469. {
  470. int i, j, k, r, c;
  471. unsigned long int count;
  472. if (!m_bMem)
  473. {
  474. if (fread(&textsize, sizeof textsize, 1, infile) < 1)
  475. Error("Read Error"); /* read size of text */
  476. }
  477. else
  478. {
  479. if (m_nInLength < sizeof textsize)
  480. Error("Read Error");
  481. memcpy(&textsize, m_pInBuffer + m_nInCur, sizeof textsize);
  482. //m_OutBuffer.reserve(textsize);
  483. m_nOutLength = textsize;
  484. //m_nOutCur = 0;
  485. m_nInCur += sizeof textsize;
  486. }
  487. if (textsize == 0) return;
  488. StartDecode();
  489. StartModel();
  490. for (i = 0; i < N - F; i++) text_buf[i] = ' ';
  491. r = N - F;
  492. for (count = 0; count < textsize;)
  493. {
  494. c = DecodeChar();
  495. if (c < 256)
  496. {
  497. if (!m_bMem)
  498. putc(c, outfile);
  499. else
  500. {
  501. //m_OutBuffer[m_nOutCur++] = c;
  502. m_OutBuffer.push_back(c);
  503. //m_nOutCur++;
  504. }
  505. text_buf[r++] = c;
  506. r &= (N - 1);
  507. count++;
  508. }
  509. else
  510. {
  511. i = (r - DecodePosition() - 1) & (N - 1);
  512. j = c - 255 + THRESHOLD;
  513. for (k = 0; k < j; k++)
  514. {
  515. c = text_buf[(i + k) & (N - 1)];
  516. if (!m_bMem)
  517. putc(c, outfile);
  518. else
  519. {
  520. // m_pOutBuffer[m_nOutCur++] = c;
  521. m_OutBuffer.push_back(c);
  522. //m_nOutCur ++;
  523. }
  524. text_buf[r++] = c;
  525. r &= (N - 1);
  526. count++;
  527. }
  528. }
  529. if (count > printcount)
  530. {
  531. #ifdef _OUTPUT_STATUS
  532. printf("%12lu\r", count);
  533. #endif
  534. printcount += 1024;
  535. }
  536. }
  537. #ifdef _OUTPUT_STATUS
  538. printf("%12lu\n", count);
  539. #endif
  540. }
  541. void LZARI::Compress(const char *lpszInfile, const char *lpszOutfile)
  542. {
  543. m_bMem = FALSE;
  544. infile = fopen(lpszInfile, "rb");
  545. outfile = fopen(lpszOutfile, "wb");
  546. if (infile && outfile)
  547. {
  548. Encode();
  549. fclose(infile);
  550. fclose(outfile);
  551. infile = NULL;
  552. outfile = NULL;
  553. }
  554. }
  555. void LZARI::UnCompress(const char *lpszInfile, const char *lpszOutfile)
  556. {
  557. m_bMem = FALSE;
  558. infile = fopen(lpszInfile, "rb");
  559. outfile = fopen(lpszOutfile, "wb");
  560. if (infile && outfile)
  561. {
  562. Decode();
  563. fclose(infile);
  564. fclose(outfile);
  565. infile = NULL;
  566. outfile = NULL;
  567. }
  568. }
  569. void LZARI::Compress(const BYTE *pInBuffer, int nInLength, const BYTE *&pOutBuffer, int &nOutLength)
  570. {
  571. m_pInBuffer = pInBuffer;
  572. m_nInLength = nInLength;
  573. m_nInCur = 0;
  574. // m_nOutCur = 0;
  575. m_bMem = TRUE;
  576. Encode();
  577. pOutBuffer = &m_OutBuffer[0];
  578. nOutLength = m_OutBuffer.size();
  579. }
  580. void LZARI::UnCompress(const BYTE *pInBuffer, int nInLength, const BYTE *&pOutBuffer, int &nOutLength)
  581. {
  582. m_pInBuffer = pInBuffer;
  583. m_nInLength = nInLength;
  584. m_nInCur = 0;
  585. m_bMem = TRUE;
  586. Decode();
  587. pOutBuffer = &m_OutBuffer[0];
  588. nOutLength = m_OutBuffer.size();
  589. m_OutBuffer.push_back(0);
  590. }
  591. void LZARI::Release()
  592. {
  593. if (!m_OutBuffer.empty())
  594. {
  595. infile = NULL;
  596. outfile = NULL;
  597. textsize = 0;
  598. codesize = 0;
  599. printcount = 0;
  600. low = 0;
  601. high = Q4;
  602. value = 0;
  603. shifts = 0;
  604. m_bMem = FALSE;
  605. m_pInBuffer = NULL;
  606. m_nInLength = 0;
  607. m_nInCur = 0;
  608. m_OutBuffer.clear();
  609. m_nOutLength = 0;
  610. buffer_putbit = 0;
  611. mask_putbit = 128;
  612. buffer_getbit = 0;
  613. mask_getbit = 0;
  614. }
  615. }