lzari.cpp 14 KB

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