PeeperZip.cpp
上传用户:xztxsm
上传日期:2007-02-12
资源大小:150k
文件大小:38k
源码类别:

远程控制编程

开发平台:

Visual C++

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // 远程控制软件-偷窥者  压缩库                                               //
  3. // 日期:2001/10/02                                                           //
  4. // 作者:刘东发                                                               //
  5. // Email:dongfa@yeah.net                                                     //
  6. // http://dongfa.yeah.net                                                    //
  7. // OICQ:5584173  阿东                                                        //
  8. // 作者声明:                                                                 //
  9. //     此部分代码是从网上下载获得,经过本人的改写, 变得容易使用,希望能给   //
  10. // 大家带来方便.                                                             //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "PeeperZip.h"
  14. ///////////////////////////////////////////////////////////////////////////////
  15. //LZ77 Part START--------------------------------------------------------------
  16. ///////////////////////////////////////////////////////////////////////////////
  17. int CCompress::UpperLog2(int n)
  18. {
  19. int i = 0;
  20. if (n > 0)
  21. {
  22. int m = 1;
  23. while(1)
  24. {
  25. if (m >= n)
  26. return i;
  27. m <<= 1;
  28. i++;
  29. }
  30. }
  31. else 
  32. return -1;
  33. }
  34. int CCompress::LowerLog2(int n)
  35. {
  36. int i = 0;
  37. if (n > 0)
  38. {
  39. int m = 1;
  40. while(1)
  41. {
  42. if (m == n)
  43. return i;
  44. if (m > n)
  45. return i - 1;
  46. m <<= 1;
  47. i++;
  48. }
  49. }
  50. else 
  51. return -1;
  52. }
  53. void CCompress::MovePos(int* piByte, int* piBit, int num)
  54. {
  55. num += (*piBit);
  56. (*piByte) += num / 8;
  57. (*piBit) = num % 8;
  58. }
  59. BYTE CCompress::GetBit(BYTE byte, int pos)
  60. {
  61. int j = 1;
  62. j <<= 7 - pos;
  63. if (byte & j)
  64. return 1;
  65. else 
  66. return 0;
  67. }
  68. void CCompress::SetBit(BYTE* byte, int iBit, BYTE aBit)
  69. {
  70. if (aBit)
  71. (*byte) |= (1 << (7 - iBit));
  72. else
  73. (*byte) &= ~(1 << (7 - iBit));
  74. }
  75. void CCompress::InvertDWord(DWORD* pDW)
  76. {
  77. union UDWORD{ DWORD dw; BYTE b[4]; };
  78. UDWORD* pUDW = (UDWORD*)pDW;
  79. BYTE b;
  80. b = pUDW->b[0]; pUDW->b[0] = pUDW->b[3]; pUDW->b[3] = b;
  81. b = pUDW->b[1]; pUDW->b[1] = pUDW->b[2]; pUDW->b[2] = b;
  82. }
  83. void CCompress::CopyBits(BYTE* memDest, int nDestPos, 
  84.   BYTE* memSrc, int nSrcPos, int nBits)
  85. {
  86. int iByteDest = 0, iBitDest;
  87. int iByteSrc = 0, iBitSrc = nSrcPos;
  88. int nBitsToFill, nBitsCanFill;
  89. while (nBits > 0)
  90. {
  91. nBitsToFill = min(nBits, iByteDest ? 8 : 8 - nDestPos);
  92. iBitDest = iByteDest ? 0 : nDestPos;
  93. nBitsCanFill = min(nBitsToFill, 8 - iBitSrc);
  94. CopyBitsInAByte(memDest + iByteDest, iBitDest, 
  95. memSrc + iByteSrc, iBitSrc, nBitsCanFill);
  96. if (nBitsToFill > nBitsCanFill)
  97. {
  98. iByteSrc++; iBitSrc = 0; iBitDest += nBitsCanFill;
  99. CopyBitsInAByte(memDest + iByteDest, iBitDest, 
  100. memSrc + iByteSrc, iBitSrc, 
  101. nBitsToFill - nBitsCanFill);
  102. iBitSrc += nBitsToFill - nBitsCanFill;
  103. }
  104. else 
  105. {
  106. iBitSrc += nBitsCanFill;
  107. if (iBitSrc >= 8)
  108. {
  109. iByteSrc++; iBitSrc = 0;
  110. }
  111. }
  112. nBits -= nBitsToFill;
  113. iByteDest++;
  114. }
  115. }
  116. void CCompress::CopyBitsInAByte(BYTE* memDest, int nDestPos, 
  117.   BYTE* memSrc, int nSrcPos, int nBits)
  118. {
  119. BYTE b1, b2;
  120. b1 = *memSrc;
  121. b1 <<= nSrcPos; b1 >>= 8 - nBits;
  122. b1 <<= 8 - nBits - nDestPos;
  123. *memDest |= b1;
  124. b2 = 0xff; b2 <<= 8 - nDestPos;
  125. b1 |= b2;
  126. b2 = 0xff; b2 >>= nDestPos + nBits;
  127. b1 |= b2;
  128. *memDest &= b1;
  129. }
  130. CCompressLZ77::CCompressLZ77()
  131. {
  132. SortHeap = new struct STIDXNODE[_MAX_WINDOW_SIZE];
  133. }
  134. CCompressLZ77::~CCompressLZ77()
  135. {
  136. delete[] SortHeap;
  137. }
  138. void CCompressLZ77::_InitSortTable()
  139. {
  140. memset(SortTable, 0, sizeof(WORD) * 65536);
  141. nWndSize = 0;
  142. HeapPos = 1;
  143. }
  144. void CCompressLZ77::_InsertIndexItem(int off)
  145. {
  146. WORD q;
  147. BYTE ch1, ch2;
  148. ch1 = pWnd[off]; ch2 = pWnd[off + 1];
  149. if (ch1 != ch2)
  150. {
  151. q = HeapPos;
  152. HeapPos++;
  153. SortHeap[q].off = off;
  154. SortHeap[q].next = SortTable[ch1 * 256 + ch2];
  155. SortTable[ch1 * 256 + ch2] = q;
  156. }
  157. else
  158. {
  159. q = SortTable[ch1 * 256 + ch2];
  160. if (q != 0 && off == SortHeap[q].off2 + 1)
  161. {
  162. SortHeap[q].off2 = off;
  163. }
  164. else
  165. {
  166. q = HeapPos;
  167. HeapPos++;
  168. SortHeap[q].off = off;
  169. SortHeap[q].off2 = off;
  170. SortHeap[q].next = SortTable[ch1 * 256 + ch2];
  171. SortTable[ch1 * 256 + ch2] = q;
  172. }
  173. }
  174. }
  175. void CCompressLZ77::_ScrollWindow(int n)
  176. {
  177. for (int i = 0; i < n; i++)
  178. {
  179. nWndSize++;
  180. if (nWndSize > 1)
  181. _InsertIndexItem(nWndSize - 2);
  182. }
  183. }
  184. int CCompressLZ77::_GetSameLen(BYTE* src, int srclen, int nSeekStart, int offset)
  185. {
  186. int i = 2;
  187. int maxsame = min(srclen - nSeekStart, nWndSize - offset);
  188. while (i < maxsame
  189. && src[nSeekStart + i] == pWnd[offset + i])
  190. i++;
  191. _ASSERT(nSeekStart + i <= srclen && offset + i <= nWndSize);
  192. return i;
  193. }
  194. BOOL CCompressLZ77::_SeekPhase(BYTE* src, int srclen, int nSeekStart, int* offset, int* len)
  195. {
  196. int j, m, n;
  197. if (nSeekStart < srclen - 1)
  198. {
  199. BYTE ch1, ch2;
  200. ch1 = src[nSeekStart]; ch2 = src[nSeekStart + 1];
  201. WORD p;
  202. p = SortTable[ch1 * 256 + ch2];
  203. if (p != 0)
  204. {
  205. m = 2; n = SortHeap[p].off;
  206. while (p != 0)
  207. {
  208. j = _GetSameLen(src, srclen, 
  209. nSeekStart, SortHeap[p].off);
  210. if ( j > m )
  211. m = j; 
  212. n = SortHeap[p].off;
  213. }
  214. p = SortHeap[p].next;
  215. }
  216. (*offset) = n; 
  217. (*len) = m;
  218. return TRUE;
  219. }
  220. }
  221. return FALSE;
  222. }
  223. void CCompressLZ77::_OutCode(BYTE* dest, DWORD code, int bits, BOOL isGamma)
  224. {
  225. if ( isGamma )
  226. {
  227. BYTE* pb;
  228. DWORD out;
  229. int GammaCode = (int)code - 1;
  230. int q = LowerLog2(GammaCode);
  231. if (q > 0)
  232. {
  233. out = 0xffff;
  234. pb = (BYTE*)&out;
  235. CopyBits(dest + CurByte, CurBit, 
  236. pb, 0, q);
  237. MovePos(&CurByte, &CurBit, q);
  238. }
  239. out = 0;
  240. pb = (BYTE*)&out;
  241. CopyBits(dest + CurByte, CurBit, pb + 3, 7, 1);
  242. MovePos(&CurByte, &CurBit, 1);
  243. if (q > 0)
  244. {
  245. int sh = 1;
  246. sh <<= q;
  247. out = GammaCode - sh;
  248. pb = (BYTE*)&out;
  249. InvertDWord(&out);
  250. CopyBits(dest + CurByte, CurBit, 
  251. pb + (32 - q) / 8, (32 - q) % 8, q);
  252. MovePos(&CurByte, &CurBit, q);
  253. }
  254. }
  255. else 
  256. {
  257. DWORD dw = (DWORD)code;
  258. BYTE* pb = (BYTE*)&dw;
  259. InvertDWord(&dw);
  260. CopyBits(dest + CurByte, CurBit, 
  261. pb + (32 - bits) / 8, (32 - bits) % 8, bits);
  262. MovePos(&CurByte, &CurBit, bits);
  263. }
  264. }
  265. int CCompressLZ77::Compress(BYTE* src, int srclen, BYTE* dest)
  266. {
  267. int i;
  268. CurByte = 0; CurBit = 0;
  269. int off, len;
  270. if (srclen > 65536) 
  271. return -1;
  272. pWnd = src;
  273. _InitSortTable();
  274. for (i = 0; i < srclen; i++)
  275. {
  276. if (CurByte >= srclen)
  277. return 0;
  278. if (_SeekPhase(src, srclen, i, &off, &len))
  279. {
  280. _OutCode(dest, 1, 1, FALSE);
  281. _OutCode(dest, len, 0, TRUE);
  282. _OutCode(dest, off, UpperLog2(nWndSize), FALSE);
  283. _ScrollWindow(len);
  284. i += len - 1;
  285. }
  286. else
  287. {
  288. _OutCode(dest, 0, 1, FALSE);
  289. _OutCode(dest, (DWORD)(src[i]), 8, FALSE);
  290. _ScrollWindow(1);
  291. }
  292. }
  293. int destlen = CurByte + ((CurBit) ? 1 : 0);
  294. if (destlen >= srclen)
  295. return 0;
  296. return destlen;
  297. }
  298. BOOL CCompressLZ77::Decompress(BYTE* src, int srclen, BYTE* dest)
  299. {
  300. int i;
  301. CurByte = 0; CurBit = 0;
  302. pWnd = src; // 初始化窗口
  303. nWndSize = 0;
  304. if (srclen > 65536) 
  305. return FALSE;
  306. for (i = 0; i < srclen; i++)
  307. {
  308. BYTE b = GetBit(dest[CurByte], CurBit);
  309. MovePos(&CurByte, &CurBit, 1);
  310. if (b == 0) // 单个字符
  311. {
  312. CopyBits(src + i, 0, dest + CurByte, CurBit, 8);
  313. MovePos(&CurByte, &CurBit, 8);
  314. nWndSize++;
  315. }
  316. else // 窗口内的术语
  317. {
  318. int q = -1;
  319. while (b != 0)
  320. {
  321. q++;
  322. b = GetBit(dest[CurByte], CurBit);
  323. MovePos(&CurByte, &CurBit, 1);
  324. }
  325. int len, off;
  326. DWORD dw = 0;
  327. BYTE* pb;
  328. if (q > 0)
  329. {
  330. pb = (BYTE*)&dw;
  331. CopyBits(pb + (32 - q) / 8, (32 - q) % 8, dest + CurByte, CurBit, q);
  332. MovePos(&CurByte, &CurBit, q);
  333. InvertDWord(&dw);
  334. len = 1;
  335. len <<= q;
  336. len += dw;
  337. len += 1;
  338. }
  339. else
  340. len = 2;
  341. dw = 0;
  342. pb = (BYTE*)&dw;
  343. int bits = UpperLog2(nWndSize);
  344. CopyBits(pb + (32 - bits) / 8, (32 - bits) % 8, dest + CurByte, CurBit, bits);
  345. MovePos(&CurByte, &CurBit, bits);
  346. InvertDWord(&dw);
  347. off = (int)dw;
  348. for (int j = 0; j < len; j++)
  349. {
  350. _ASSERT(i + j <  srclen);
  351. _ASSERT(off + j <  _MAX_WINDOW_SIZE);
  352. src[i + j] = pWnd[off + j];
  353. }
  354. nWndSize += len;
  355. i += len - 1;
  356. }
  357. if (nWndSize > _MAX_WINDOW_SIZE)
  358. {
  359. pWnd += nWndSize - _MAX_WINDOW_SIZE;
  360. nWndSize = _MAX_WINDOW_SIZE;
  361. }
  362. }
  363. return TRUE;
  364. }
  365. ///////////////////////////////////////////////////////////////////////////////
  366. //LZ77 Part END--------------------------------------------------------------
  367. ///////////////////////////////////////////////////////////////////////////////
  368. ///////////////////////////////////////////////////////////////////////////////
  369. //LZSS Part START--------------------------------------------------------------
  370. ///////////////////////////////////////////////////////////////////////////////
  371. HGLOBAL C_LZSS::Encode(char *chData, int nSize)
  372. {
  373. if(!AfxIsValidAddress(chData, nSize))
  374. return NULL;
  375. //缓冲数据区大小
  376. const int nMaxSize = 65536;
  377. HGLOBAL hZip = NULL;
  378. hZip = ::GlobalAlloc(GHND, nMaxSize);
  379. if(hZip == NULL)
  380. {
  381. return NULL;
  382. }
  383. LPBYTE lpZipData = (LPBYTE)::GlobalLock(hZip);
  384. BYTE byMaxBuf[nMaxSize];
  385. ZeroMemory(byMaxBuf, nMaxSize);
  386. int nCurPos = 0; //记下数据的位置
  387. int nZipPos = 0; //记下压缩数据的位置
  388. int nBufPos = 0; //记下当前存储位置hZip
  389. int nZipSize = 0; //压缩后数据的大小
  390. int  i, c, len, r, s, last_match_length, code_buf_ptr;
  391. unsigned char code_buf[17], mask;
  392. InitTree();
  393. code_buf[0] = 0;
  394. code_buf_ptr = mask = 1;
  395. s = 0;  r = LZSS_N - LZSS_F;
  396. for(i = s; i < r; i++)
  397. {
  398. text_buf[i] = ' ';
  399. }
  400. for(len = 0; len < LZSS_F && (nCurPos < nSize/*getc(infile)*/); len++)
  401. {
  402. c = chData[nCurPos]; //add
  403. nCurPos ++; //add
  404. text_buf[r + len] = c;
  405. }
  406. if((textsize = len) == 0)
  407. {
  408. return false;
  409. }
  410. for(i = 1; i <= LZSS_F; i++)
  411. {
  412. InsertNode(r - i);
  413. }
  414. InsertNode(r);
  415. do
  416. {
  417. if(match_length > len)
  418. {
  419. match_length = len;
  420. }
  421. if(match_length <= LZSS_THRESHOLD)
  422. {
  423. match_length = 1;
  424. code_buf[0] |= mask;
  425. code_buf[code_buf_ptr++] = text_buf[r];
  426. }
  427. else
  428. {
  429. code_buf[code_buf_ptr++] = (unsigned char) match_position;
  430. code_buf[code_buf_ptr++] = (unsigned char)
  431. (((match_position >> 4) & 0xf0)
  432.   | (match_length - (LZSS_THRESHOLD + 1)));
  433. }
  434. if((mask <<= 1) == 0)
  435. {
  436. for(i = 0; i < code_buf_ptr; i++)
  437. {
  438. //存储这个压缩字节
  439. if(nZipPos >= nMaxSize)
  440. {
  441. nZipPos = 0;
  442. memcpy(lpZipData + nBufPos, byMaxBuf, nMaxSize);
  443. nBufPos += nMaxSize;
  444. ::GlobalUnlock(hZip); //重新分配内存
  445. hZip = ::GlobalReAlloc(hZip, nBufPos + nMaxSize, 0);
  446. lpZipData = (LPBYTE)::GlobalLock(hZip);
  447. }
  448. byMaxBuf[nZipPos] = code_buf[i];
  449. nZipPos ++;
  450. nZipSize ++; //当前压缩数据的大小
  451. }
  452. codesize += code_buf_ptr;
  453. code_buf[0] = 0;
  454. code_buf_ptr = mask = 1;
  455. }
  456. last_match_length = match_length;
  457. for(i = 0; i < last_match_length && (nCurPos < nSize)/*c = getc(infile)) != EOF*/; i++)
  458. {
  459. c = chData[nCurPos]; //add
  460. nCurPos ++; //add
  461. DeleteNode(s);
  462. text_buf[s] = c;
  463. if(s < LZSS_F - 1)
  464. {
  465. text_buf[s + LZSS_N] = c;
  466. }
  467. s = (s + 1) & (LZSS_N - 1);
  468. r = (r + 1) & (LZSS_N - 1);
  469. InsertNode(r);
  470. }
  471. if((textsize += i) > printcount)
  472. {
  473. printcount += 1024;
  474. }
  475. while(i++ < last_match_length)
  476. {
  477. DeleteNode(s);
  478. s = (s + 1) & (LZSS_N - 1);
  479. r = (r + 1) & (LZSS_N - 1);
  480. if(--len)
  481. {
  482. InsertNode(r);
  483. }
  484. }
  485. }while (len > 0);
  486. if(code_buf_ptr > 1)
  487. {
  488. for(i = 0; i < code_buf_ptr; i++)
  489. {
  490. //存储这个压缩字节
  491. if(nZipPos >= nMaxSize)
  492. {
  493. nZipPos = 0;
  494. memcpy(lpZipData + nBufPos, byMaxBuf, nMaxSize);
  495. nBufPos += nMaxSize;
  496. ::GlobalUnlock(hZip); //重新分配内存
  497. hZip = ::GlobalReAlloc(hZip, nBufPos + nMaxSize, 0);
  498. lpZipData = (LPBYTE)::GlobalLock(hZip);
  499. }
  500. byMaxBuf[nZipPos] = code_buf[i];
  501. nZipPos ++;
  502. nZipSize ++; //当前压缩数据的大小
  503. }
  504. codesize += code_buf_ptr;
  505. }
  506. //存储剩余的压缩数据
  507. if(nZipPos > 0)
  508. {
  509. memcpy(lpZipData + nBufPos, byMaxBuf, nZipPos);
  510. }
  511. ::GlobalUnlock(hZip); //重新分配内存
  512. hZip = ::GlobalReAlloc(hZip, nZipSize, 0);
  513. return hZip;
  514. }
  515. HGLOBAL C_LZSS::Decode(char *chZipData, int nZipSize)
  516. {
  517. if(!AfxIsValidAddress(chZipData, nZipSize))
  518. return NULL;
  519. //缓冲数据区大小
  520. const int nMaxSize = 65536;
  521. HGLOBAL hUnZip = NULL;
  522. hUnZip = ::GlobalAlloc(GHND, nMaxSize);
  523. if(hUnZip == NULL)
  524. {
  525. return NULL;
  526. }
  527. LPBYTE lpUnZipData = (LPBYTE)::GlobalLock(hUnZip);
  528. BYTE byMaxBuf[nMaxSize];
  529. ZeroMemory(byMaxBuf, nMaxSize);
  530. int nCurPos = 0; //记下数据的位置
  531. int nPos = 0; //记下解压数据的位置
  532. int nBufPos = 0; //记下当前存储位置hUnZip
  533. int nSize = 0; //解压后数据的大小
  534. int  i, j, k, r, c;
  535. unsigned char cc; //add
  536. unsigned int  flags;
  537. for (i = 0; i < LZSS_N - LZSS_F; i++)
  538. {
  539. text_buf[i] = ' ';
  540. }
  541. r = LZSS_N - LZSS_F;
  542. flags = 0;
  543. for( ; ; )
  544. {
  545. if(((flags >>= 1) & 256) == 0)
  546. {
  547. if(nCurPos >= nZipSize)//(c = getc(infile)) == EOF)
  548. {
  549. break;
  550. }
  551. c = (unsigned char)chZipData[nCurPos]; //add
  552. nCurPos ++; //add
  553. flags = c | 0xff00;
  554. }
  555. if(flags & 1)
  556. {
  557. if(nCurPos >= nZipSize)//(c = getc(infile)) == EOF)
  558. {
  559. break;
  560. }
  561. c = (unsigned char)chZipData[nCurPos]; //add
  562. cc = c; //add
  563. nCurPos ++; //add
  564. //存储这个压缩字节
  565. if(nPos >= nMaxSize)
  566. {
  567. nPos = 0;
  568. memcpy(lpUnZipData + nBufPos, byMaxBuf, nMaxSize);
  569. nBufPos += nMaxSize;
  570. ::GlobalUnlock(hUnZip); //重新分配内存
  571. hUnZip = ::GlobalReAlloc(hUnZip, nBufPos + nMaxSize, 0);
  572. lpUnZipData = (LPBYTE)::GlobalLock(hUnZip);
  573. }
  574. byMaxBuf[nPos] = cc;
  575. nPos ++;
  576. nSize ++; //当前压缩数据的大小
  577. text_buf[r++] = c;
  578. r &= (LZSS_N - 1);
  579. }
  580. else
  581. {
  582. if(nCurPos >= nZipSize)//(i = getc(infile)) == EOF)
  583. {
  584. break;
  585. }
  586. i = (unsigned char)chZipData[nCurPos]; //add
  587. nCurPos ++; //add
  588. if(nCurPos >= nZipSize)//(j = getc(infile)) == EOF)
  589. {
  590. break;
  591. }
  592. j = chZipData[nCurPos]; //add
  593. nCurPos ++; //add
  594. i |= ((j & 0xf0) << 4);
  595. j = (j & 0x0f) + LZSS_THRESHOLD;
  596. for(k = 0; k <= j; k++)
  597. {
  598. c = text_buf[(i + k) & (LZSS_N - 1)];
  599. cc = c; //add
  600. //存储这个压缩字节
  601. if(nPos >= nMaxSize)
  602. {
  603. nPos = 0;
  604. memcpy(lpUnZipData + nBufPos, byMaxBuf, nMaxSize);
  605. nBufPos += nMaxSize;
  606. ::GlobalUnlock(hUnZip); //重新分配内存
  607. hUnZip = ::GlobalReAlloc(hUnZip, nBufPos + nMaxSize, 0);
  608. lpUnZipData = (LPBYTE)::GlobalLock(hUnZip);
  609. }
  610. byMaxBuf[nPos] = cc;
  611. nPos ++;
  612. nSize ++; //当前压缩数据的大小
  613. text_buf[r++] = c; 
  614. r &= (LZSS_N - 1);
  615. }
  616. }
  617. }
  618. //存储剩余的解压数据
  619. if(nPos > 0)
  620. {
  621. memcpy(lpUnZipData + nBufPos, byMaxBuf, nPos);
  622. }
  623. ::GlobalUnlock(hUnZip); //重新分配内存
  624. hUnZip = ::GlobalReAlloc(hUnZip, nSize, 0);
  625. return hUnZip;
  626. }
  627. ///////////////////////////////////////////////////////////////////////////////
  628. //LZSS Part END--------------------------------------------------------------
  629. ///////////////////////////////////////////////////////////////////////////////
  630. ///////////////////////////////////////////////////////////////////////////////
  631. //ARI Part START--------------------------------------------------------------
  632. ///////////////////////////////////////////////////////////////////////////////
  633. HGLOBAL C_ARI::Encode(char *chData, int nSize)
  634. {
  635. if(!AfxIsValidAddress(chData, nSize))
  636. return NULL;
  637. if(nSize <= 0)
  638. return NULL;
  639. m_nTotalSize = nSize;
  640. textsize = 0;
  641. codesize = 0;
  642. printcount = 0;
  643. low = 0;
  644. high = ARI_Q4;
  645. value = 0;
  646. shifts = 0;
  647. nCurPos = 0; //记下数据的位置
  648. nZipPos = 0; //记下压缩数据的位置
  649. nBufPos = 0; //记下当前存储位置hZip
  650. nZipSize = 0; //压缩后数据的大小
  651. m_hData = NULL;
  652. m_lpMemData = NULL;
  653. m_lpBuffer = NULL;
  654. m_lpBuffer = (BYTE *)chData;
  655. m_hData = ::GlobalAlloc(GHND, MAXSIZE);
  656. if(m_hData == NULL)
  657. {
  658. return NULL;
  659. }
  660. m_lpMemData = (LPBYTE)::GlobalLock(m_hData);
  661. ZeroMemory(byMaxBuf, MAXSIZE);
  662. nCurPos = 0; //记下数据的位置
  663. nZipPos = 0; //记下压缩数据的位置
  664. nBufPos = 0; //记下当前存储位置hZip
  665. nZipSize = 0; //压缩后数据的大小
  666. int  i, c, len, r, s, last_match_length;
  667. textsize = nSize;
  668. int n1 = sizeof(textsize);
  669. memcpy(byMaxBuf, &textsize, sizeof(textsize));
  670. nZipSize += sizeof(textsize);
  671. nZipPos += sizeof(textsize);
  672. /* if(fwrite(&textsize, sizeof textsize, 1, outfile) < 1)
  673. {
  674. Error("Write Error");
  675. }
  676. */
  677. codesize += sizeof(textsize);
  678. /* if(textsize == 0)
  679. {
  680. return NULL;
  681. }
  682. */
  683. // rewind(infile);
  684. textsize = 0;
  685. StartModel(); 
  686. InitTree();
  687. s = 0;
  688. r = ARI_N - ARI_F;
  689. for(i = s; i < r; i++)
  690. {
  691. text_buf[i] = ' ';
  692. }
  693. for(len = 0; len < ARI_F && (nCurPos < nSize)/*c = getc(infile)) != EOF*/; len++)
  694. {
  695. c = m_lpBuffer[nCurPos]; //add
  696. nCurPos ++; //add
  697. text_buf[r + len] = c;
  698. }
  699. textsize = len;
  700. for(i = 1; i <= ARI_F; i++)
  701. {
  702. InsertNode(r - i);
  703. }
  704. InsertNode(r);
  705. do
  706. {
  707. if(match_length > len)
  708. {
  709. match_length = len;
  710. }
  711. if(match_length <= ARI_THRESHOLD)
  712. {
  713. match_length = 1; 
  714. EncodeChar(text_buf[r]);
  715. }
  716. else
  717. {
  718. EncodeChar(255 - ARI_THRESHOLD + match_length);
  719. EncodePosition(match_position - 1);
  720. }
  721. last_match_length = match_length;
  722. for(i = 0; i < last_match_length && (nCurPos < nSize)/*c = getc(infile)) != EOF*/; i++)
  723. {
  724. c = m_lpBuffer[nCurPos]; //add
  725. nCurPos ++; //add
  726. DeleteNode(s); 
  727. text_buf[s] = c;
  728. if(s < ARI_F - 1)
  729. {
  730. text_buf[s + ARI_N] = c;
  731. }
  732. s = (s + 1) & (ARI_N - 1);
  733. r = (r + 1) & (ARI_N - 1);
  734. InsertNode(r);
  735. }
  736. if((textsize += i) > printcount)
  737. {
  738. printcount += 1024;
  739. }
  740. while(i++ < last_match_length)
  741. {
  742. DeleteNode(s);
  743. s = (s + 1) & (ARI_N - 1);
  744. r = (r + 1) & (ARI_N - 1);
  745. if (--len) InsertNode(r);
  746. }
  747. } while (len > 0);
  748. EncodeEnd();
  749. //存储剩余的压缩数据
  750. if(nZipPos > 0)
  751. {
  752. memcpy(m_lpMemData + nBufPos, byMaxBuf, nZipPos);
  753. }
  754. ::GlobalUnlock(m_hData); //重新分配内存
  755. m_hData = ::GlobalReAlloc(m_hData, nZipSize, 0);
  756. return m_hData;
  757. }
  758. HGLOBAL C_ARI::Decode(char *chData, int nSize)
  759. {
  760. if(!AfxIsValidAddress(chData, nSize))
  761. return NULL;
  762. if(nSize <= 4)
  763. return NULL;
  764. m_nTotalSize = nSize;
  765. textsize = 0;
  766. codesize = 0;
  767. printcount = 0;
  768. low = 0;
  769. high = ARI_Q4;
  770. value = 0;
  771. shifts = 0;
  772. nCurPos = 0; //记下数据的位置
  773. nZipPos = 0; //记下压缩数据的位置
  774. nBufPos = 0; //记下当前存储位置hZip
  775. nZipSize = 0; //压缩后数据的大小
  776. m_hData = NULL;
  777. m_lpMemData = NULL;
  778. m_lpBuffer = NULL;
  779. m_lpBuffer = (BYTE *)chData;
  780. m_hData = ::GlobalAlloc(GHND, MAXSIZE);
  781. if(m_hData == NULL)
  782. {
  783. return NULL;
  784. }
  785. m_lpMemData = (LPBYTE)::GlobalLock(m_hData);
  786. ZeroMemory(byMaxBuf, MAXSIZE);
  787. nCurPos = 0; //记下数据的位置
  788. nZipPos = 0; //记下压缩数据的位置
  789. nBufPos = 0; //记下当前存储位置hZip
  790. nZipSize = 0; //压缩后数据的大小
  791. int  i, j, k, r, c;
  792. unsigned long int  count;
  793. textsize = *(unsigned long int *)(m_lpBuffer);
  794. /* if(fread(&textsize, sizeof textsize, 1, infile) < 1)
  795. {
  796. Error("Read Error");
  797. }
  798. */
  799. nCurPos += sizeof(textsize);
  800. if(textsize == 0)
  801. {
  802. return NULL;
  803. }
  804. StartDecode();
  805. StartModel();
  806. for(i = 0; i < ARI_N - ARI_F; i++)
  807. {
  808. text_buf[i] = ' ';
  809. }
  810. r = ARI_N - ARI_F;
  811. for(count = 0; count < textsize; )
  812. {
  813. c = DecodeChar();
  814. if(c < 256)
  815. {
  816. //存储这个压缩字节
  817. if(nZipPos >= MAXSIZE)
  818. {
  819. nZipPos = 0;
  820. memcpy(m_lpMemData + nBufPos, byMaxBuf, MAXSIZE);
  821. nBufPos += MAXSIZE;
  822. ::GlobalUnlock(m_hData); //重新分配内存
  823. m_hData = ::GlobalReAlloc(m_hData, nBufPos + MAXSIZE, 0);
  824. m_lpMemData = (LPBYTE)::GlobalLock(m_hData);
  825. }
  826. byMaxBuf[nZipPos] = c;
  827. nZipPos ++;
  828. nZipSize ++; //当前压缩数据的大小
  829. text_buf[r++] = c;
  830. r &= (ARI_N - 1);
  831. count++;
  832. else
  833. {
  834. i = (r - DecodePosition() - 1) & (ARI_N - 1);
  835. j = c - 255 + ARI_THRESHOLD;
  836. for(k = 0; k < j; k++)
  837. {
  838. c = text_buf[(i + k) & (ARI_N - 1)];
  839. //存储这个压缩字节
  840. if(nZipPos >= MAXSIZE)
  841. {
  842. nZipPos = 0;
  843. memcpy(m_lpMemData + nBufPos, byMaxBuf, MAXSIZE);
  844. nBufPos += MAXSIZE;
  845. ::GlobalUnlock(m_hData); //重新分配内存
  846. m_hData = ::GlobalReAlloc(m_hData, nBufPos + MAXSIZE, 0);
  847. m_lpMemData = (LPBYTE)::GlobalLock(m_hData);
  848. }
  849. byMaxBuf[nZipPos] = c;
  850. nZipPos ++;
  851. nZipSize ++; //当前压缩数据的大小
  852. text_buf[r++] = c;
  853. r &= (ARI_N - 1);
  854. count++;
  855. }
  856. }
  857. }
  858. //存储剩余的压缩数据
  859. if(nZipPos > 0)
  860. {
  861. memcpy(m_lpMemData + nBufPos, byMaxBuf, nZipPos);
  862. }
  863. ::GlobalUnlock(m_hData); //重新分配内存
  864. m_hData = ::GlobalReAlloc(m_hData, nZipSize, 0);
  865. return m_hData;
  866. }
  867. ///////////////////////////////////////////////////////////////////////////////
  868. //ARI Part END--------------------------------------------------------------
  869. ///////////////////////////////////////////////////////////////////////////////
  870. ///////////////////////////////////////////////////////////////////////////////
  871. //LZW Part START--------------------------------------------------------------
  872. ///////////////////////////////////////////////////////////////////////////////
  873. CLZWDecodeTable::CLZWDecodeTable(BOOL fInit)
  874. {
  875. m_pbContain=NULL;
  876. m_dwTableEntryNumber=0;
  877. if(fInit)
  878. InitLZWTable();
  879. }
  880. CLZWDecodeTable::~CLZWDecodeTable()
  881. {
  882. ClearDecodeTable();
  883. }
  884. void CLZWDecodeTable::ClearDecodeTable(void)
  885. {   
  886. if(m_pbContain==NULL)
  887. return;
  888. for(int i=0;i<4096;i++)
  889. {
  890. if(m_pbContain[i])
  891. delete (m_pbContain[i]);
  892. }
  893. delete m_pbContain;
  894. m_pbContain=NULL;
  895. m_dwTableEntryNumber=0;
  896. }
  897. void CLZWDecodeTable::InitLZWTable(void)
  898. {
  899. ClearDecodeTable();
  900. m_pbContain=new BYTE*[4096];
  901. int iSize=sizeof(m_pbContain);
  902. //if(NULL==m_pbContain)
  903. // AfxMessageBox("error new m_pbContain=BYTE*[4096]");
  904. for(int i=0;i<4096;i++)
  905. {
  906. m_pbContain[i]=NULL;
  907. }
  908. for(i=0;i<=255;i++)
  909. {
  910. m_pbContain[i]=new BYTE[1+2];
  911. *((WORD*)m_pbContain[i])=1;
  912. m_pbContain[i][2]=(BYTE)i;
  913. }
  914. m_dwTableEntryNumber=LZW_BEGIN_ENTRY;
  915. }
  916. BYTE* CLZWDecodeTable::GetMatchData(WORD wCode)
  917. {
  918. return m_pbContain[wCode];
  919. }
  920. void CLZWDecodeTable::AddToChild(WORD wCode,BYTE *pbContain,int iLength)
  921. {
  922. ASSERT(wCode<4096);
  923. if(m_pbContain[wCode])
  924. delete m_pbContain[wCode];
  925. m_pbContain[wCode]=new BYTE[iLength+2];
  926. *((WORD*)m_pbContain[wCode])=(WORD)iLength;
  927. memcpy(m_pbContain[wCode]+2,pbContain,iLength);
  928. }
  929. /*
  930. class CLZWEncodeTable
  931. */
  932. CLZWEncodeTable::CLZWEncodeTable(BOOL fInit)
  933. {
  934. if(fInit)
  935. InitLZWTable();
  936. else
  937. {
  938. m_dwTableEntryNumber=0;
  939. m_EntryHead.pRightBrother=NULL;
  940. m_EntryHead.pChild=NULL;
  941. }
  942. }
  943. CLZWEncodeTable::~CLZWEncodeTable()
  944. {
  945. ClearLZWTable();
  946. }
  947. void CLZWEncodeTable::ClearLZWTable(void)
  948. {
  949. if(m_EntryHead.pChild==NULL)
  950. return;
  951. m_dwTableEntryNumber=0;
  952. int iRe=0;
  953. while(m_EntryHead.pChild)
  954. {
  955. RemoveFirstChild();
  956. iRe++;
  957. //printf("remove %dn",++iRe);;
  958. }
  959. }
  960. void CLZWEncodeTable::RemoveFirstChild(void)
  961. {
  962. PLZWENCODEENTRY pFirstChild=m_EntryHead.pChild;// this child will be removed
  963. if(pFirstChild->pChild)
  964. {
  965. m_EntryHead.pChild=pFirstChild->pChild;
  966. if(m_EntryHead.pChild->pRightBrother)
  967. {
  968. PLZWENCODEENTRY pRightBrother=FindRightBrother(m_EntryHead.pChild);
  969. pRightBrother->pRightBrother=pFirstChild->pRightBrother;
  970. }
  971. else
  972. (m_EntryHead.pChild)->pRightBrother=pFirstChild->pRightBrother;
  973. //delete pFirstChild;
  974. }
  975. else
  976. m_EntryHead.pChild=pFirstChild->pRightBrother;
  977. delete pFirstChild;
  978. }
  979. PLZWENCODEENTRY CLZWEncodeTable::FindRightBrother(PLZWENCODEENTRY pCurrent)
  980. {
  981. PLZWENCODEENTRY pFind;
  982. pFind=pCurrent;
  983. while(pFind->pRightBrother)
  984. {
  985. pFind=pFind->pRightBrother;
  986. }
  987. return pFind;
  988. }
  989. void CLZWEncodeTable::InitLZWTable(void)
  990. {// init the table ,it has 256 items code from 0 to 255
  991. ClearLZWTable();
  992. PLZWENCODEENTRY pEntryFirst=new LZWENCODEENTRY;
  993. pEntryFirst->wCode=(WORD)0;
  994. pEntryFirst->bLast=(BYTE)0;
  995. pEntryFirst->pRightBrother=pEntryFirst->pChild=NULL;
  996. m_EntryHead.pChild=pEntryFirst;
  997. m_EntryHead.pRightBrother=NULL;
  998. PLZWENCODEENTRY pPrev=pEntryFirst;
  999. for(int i=1;i<=255;i++)
  1000. {// set the brother nodes
  1001. PLZWENCODEENTRY pEntry=new LZWENCODEENTRY;
  1002. pEntry->wCode=(WORD)i;
  1003. pEntry->bLast=(BYTE)i;
  1004. pEntry->pChild=pEntry->pRightBrother=NULL;
  1005. pPrev->pRightBrother=pEntry;
  1006. pPrev=pEntry;
  1007. }
  1008. m_dwTableEntryNumber=258;
  1009. m_uNextCodeForUse=LZW_BEGIN_ENTRY;
  1010. }
  1011. PLZWENCODEENTRY CLZWEncodeTable::FindMatchChild(BYTE bChildLast,PLZWENCODEENTRY pCurrent)
  1012. {// return the find child entry
  1013. if(pCurrent->pChild==NULL)
  1014. return NULL;
  1015. PLZWENCODEENTRY pChild=pCurrent->pChild;
  1016. // pChild is the current's child
  1017. // and all pChild's brother is the current's child
  1018. while(pChild!=NULL)
  1019. {
  1020. if(pChild->bLast==bChildLast)
  1021. return pChild;
  1022. pChild=pChild->pRightBrother;
  1023. }
  1024. return NULL;
  1025. }
  1026. PLZWENCODEENTRY CLZWEncodeTable::AddToChild(BYTE bLast,PLZWENCODEENTRY pCurrent)
  1027. {
  1028. ASSERT(pCurrent);
  1029. PLZWENCODEENTRY pChild=new LZWENCODEENTRY;
  1030. if(pChild==NULL)
  1031. {
  1032. int _j=0;
  1033. }
  1034. pChild->pChild=pChild->pRightBrother=NULL;
  1035. pChild->bLast=bLast;
  1036. pChild->wCode=(WORD)m_uNextCodeForUse;
  1037. if(pChild->wCode==LZW_CLEAR_CODE)
  1038. {
  1039. int _i=0;
  1040. }
  1041. m_uNextCodeForUse++;
  1042. m_dwTableEntryNumber++;
  1043. pChild->pRightBrother=pCurrent->pChild;
  1044. pCurrent->pChild=pChild;
  1045. return pChild;
  1046. }
  1047. CDecodeBitArray::CDecodeBitArray(DWORD dwInitWidth)// width in bit
  1048. {
  1049. m_pbBits=NULL;
  1050. m_dwWidthInByte=0;
  1051. m_dwTail=m_dwHead=0;
  1052. if(dwInitWidth)
  1053. InitBits(dwInitWidth);
  1054. }
  1055. CDecodeBitArray::~CDecodeBitArray()
  1056. {
  1057. ClearBits();
  1058. }
  1059. void CDecodeBitArray::ClearBits(void)
  1060. {
  1061. if(m_pbBits)
  1062. delete m_pbBits;
  1063. m_dwTail=m_dwHead=0;
  1064. m_dwWidthInByte=0;
  1065. }
  1066. void CDecodeBitArray::InitBits(DWORD dwInitWidth)
  1067. {
  1068. ClearBits();
  1069. DWORD dwLength=dwInitWidth/8;
  1070. dwLength+=(dwInitWidth%8)?1:0;
  1071. m_pbBits=new BYTE[dwLength];
  1072. m_dwHead=m_dwTail=0;
  1073. m_dwWidthInByte=dwLength;
  1074. }
  1075. void CDecodeBitArray::InitBytes(DWORD dwInitWidth)
  1076. {
  1077. InitBits(dwInitWidth*8);
  1078. }
  1079. DWORD CDecodeBitArray::GetLeftBytes(void)
  1080. {
  1081. DWORD dwLeftBytes;
  1082. DWORD dwLeftBits=GetLeftBits();
  1083. dwLeftBytes=dwLeftBits/8;
  1084. dwLeftBytes+=(dwLeftBits%8)?1:0;
  1085. return dwLeftBytes;
  1086. }
  1087. WORD CDecodeBitArray::RemoveBits(int iWidth)
  1088. {
  1089. WORD wGet=0;
  1090. for(int i=iWidth-1;i>=0;i--)
  1091. {
  1092. if(RemoveFirstBit())
  1093. {
  1094. (wGet |= (1<<i));
  1095. }
  1096. //// SET_BIT_1(wGet,i);
  1097. }
  1098. return wGet;
  1099. }
  1100. WORD CDecodeBitArray::RemoveFirstBit(void)
  1101. {
  1102. BYTE* pbFirstByte=m_pbBits+m_dwHead/8;
  1103. BYTE bFirstByte=*pbFirstByte;
  1104. //  (b & (1<<i))
  1105. WORD wGet= bFirstByte&(1<<(7-m_dwHead%8))?1:0;
  1106. m_dwHead++;
  1107. return wGet;
  1108. }
  1109. BOOL CDecodeBitArray::AddBytes(BYTE* pbAdd,int iLength)
  1110. {
  1111. if(m_pbBits==NULL)
  1112. return FALSE;
  1113. Resort();
  1114. memcpy(m_pbBits+m_dwTail/8,pbAdd,iLength);
  1115. m_dwTail+=8*(DWORD)iLength;
  1116. return TRUE;
  1117. }
  1118. void CDecodeBitArray::Resort(void)
  1119. {// because m_dwTail%8 always equal 0
  1120. if(m_dwHead<8)
  1121. return;
  1122. if(m_dwTail==m_dwHead)
  1123. {
  1124. m_dwTail=m_dwHead=0;
  1125. return;
  1126. }
  1127. DWORD dwLength=GetLeftBytes();
  1128. DWORD dwHead=m_dwHead%8;
  1129. DWORD dwMove=m_dwHead-dwHead;
  1130. memcpy(m_pbBits,m_pbBits+(m_dwHead/8),(int)dwLength);
  1131. m_dwHead=dwHead;
  1132. m_dwTail-=dwMove;
  1133. }
  1134. /*
  1135. class CEncodeBitArray
  1136. */
  1137. CEncodeBitArray::CEncodeBitArray(DWORD dwInitWidth)
  1138. {
  1139. if(dwInitWidth==0)
  1140. m_pbBits=NULL;
  1141. else
  1142. InitBits(dwInitWidth);
  1143. }
  1144. CEncodeBitArray::~CEncodeBitArray()
  1145. {
  1146. ClearBits();
  1147. }
  1148. BOOL CEncodeBitArray::InitBits(DWORD dwInitWidth)
  1149. {
  1150. ClearBits();
  1151. ASSERT(dwInitWidth);
  1152. m_dwWidthInByte=dwInitWidth/8;
  1153. m_dwWidthInByte+=(dwInitWidth%8)?1:0;
  1154. m_pbBits=new BYTE[m_dwWidthInByte];
  1155. m_dwTail=0;
  1156. return TRUE;
  1157. }
  1158. void CEncodeBitArray::ClearBits(void)
  1159. {
  1160. if(m_pbBits)
  1161. delete m_pbBits;
  1162. m_pbBits=NULL;
  1163. }
  1164. BOOL CEncodeBitArray::AddBits(WORD wAdd,int iWidth)
  1165. {
  1166. if(m_pbBits==NULL)
  1167. return FALSE;
  1168. for(int i=iWidth-1;i>=0;i--)
  1169. {
  1170. BYTE* pbByte=m_pbBits+m_dwTail/8;
  1171. if((wAdd & (1<<i)))//  CHECK_BIT_1(wAdd,i))
  1172. ((*pbByte) |= (1<<(7-m_dwTail%8)));//SET_BIT_1(*pbByte,7-m_dwTail%8);
  1173. else
  1174. ((*pbByte) &= (~(1<<(7-m_dwTail%8))));//SET_BIT_0(*pbByte,7-m_dwTail%8);
  1175. m_dwTail++;
  1176. }
  1177. return TRUE;
  1178. }
  1179. DWORD CEncodeBitArray::GetBytesWidth(void)
  1180. {
  1181. DWORD dwBytes=m_dwTail/8;
  1182. dwBytes+=(m_dwTail%8)?1:0;
  1183. return dwBytes;
  1184. }
  1185. int CEncodeBitArray::RemoveBytes(BYTE *pbGet,int iWant)
  1186. {
  1187. if(m_pbBits==NULL)
  1188. return -1;
  1189. int iTotal=(int)GetBytesWidth();
  1190. if(iWant>iTotal)
  1191. iWant=iTotal;
  1192. if(pbGet!=NULL)
  1193. memcpy(pbGet,m_pbBits,iWant);
  1194. memcpy(m_pbBits,m_pbBits+iWant,iTotal-iWant);
  1195. int iTail=(int)m_dwTail;
  1196. iTail-=iWant*8;
  1197. if(iTail<0)
  1198. iTail=0;
  1199. m_dwTail=iTail;
  1200. return iWant;
  1201. }
  1202. HGLOBAL CLZWDecode::BeginLZWDecode(char *chZipData, int dwLength)
  1203. {
  1204. if(!AfxIsValidAddress(chZipData, dwLength))
  1205. return NULL;
  1206. //缓冲数据区大小
  1207. const int nMaxSize = 65536;
  1208. HGLOBAL hUnZip = NULL;
  1209. hUnZip = ::GlobalAlloc(GHND, nMaxSize);
  1210. if(hUnZip == NULL)
  1211. {
  1212. return NULL;
  1213. }
  1214. LPBYTE lpZipData = (LPBYTE)::GlobalLock(hUnZip);
  1215. BYTE byMaxBuf[nMaxSize];
  1216. ZeroMemory(byMaxBuf, nMaxSize);
  1217. int nZipPos = 0; //记下压缩数据的位置
  1218. int nBufPos = 0; //记下当前存储位置hZip
  1219. int nZipSize = 0; //压缩后数据的大小
  1220. iDePutPos=iDeGetPos=0;
  1221. DWORD dwBytesOnce = 1;
  1222. DWORD wBuffer = 1024;
  1223. m_dwDecodedByte=0;
  1224. BYTE *pbNewData=new BYTE[4000],*pbOutData=new BYTE[4000];
  1225. BYTE *pbBuffer=new BYTE[wBuffer];
  1226. BYTE bFirst;
  1227. m_LZWTable.InitLZWTable();
  1228. int iBitWidth=9;
  1229. m_iTotalEntry=LZW_BEGIN_ENTRY;
  1230. BYTE* pbDecodedData;
  1231. WORD wOld,wLastLen;
  1232. m_baContain.InitBits((wBuffer+20)*8);
  1233. int iR=0;
  1234. DWORD dwRead=0;
  1235. while(1)
  1236. {
  1237. if(m_baContain.GetLeftBytes()<5)
  1238. {
  1239. WORD wGetBytes= (WORD)wBuffer;
  1240. if((DWORD)wGetBytes+dwRead>(DWORD)dwLength)
  1241. wGetBytes=(WORD)(dwLength-dwRead);
  1242. if(wGetBytes!=0)
  1243. {
  1244. ///////////////////////////////////
  1245. int iGet = wGetBytes;
  1246. if(iDeGetPos + iGet > dwLength)
  1247. iGet = dwLength - iDeGetPos;
  1248. memcpy(pbBuffer, chZipData + iDeGetPos, iGet);
  1249. iDeGetPos += iGet;
  1250. ///////////////////////////////////
  1251. m_baContain.AddBytes(pbBuffer,wBuffer);
  1252. dwRead+=wGetBytes;
  1253. }
  1254. }
  1255. int iT=m_iTotalEntry+1;
  1256. iT>>=9;
  1257. iBitWidth=9;
  1258. while(iT>0)
  1259. {
  1260. iT>>=1;
  1261. iBitWidth++;
  1262. }
  1263. WORD wGet=m_baContain.RemoveBits(iBitWidth);
  1264. if(wGet==LZW_END_CODE)
  1265. {
  1266. break;
  1267. }
  1268. if(wGet==LZW_CLEAR_CODE)
  1269. {
  1270. m_LZWTable.InitLZWTable();
  1271. wGet=m_baContain.RemoveBits(9);
  1272. if(wGet==LZW_END_CODE)
  1273. break;
  1274. pbDecodedData=m_LZWTable.GetMatchData(wGet);
  1275. //////////////////////////////
  1276. BYTE *pbWrite = pbDecodedData;
  1277. if(pbWrite != NULL)
  1278. {
  1279. WORD wL=*((WORD*)pbWrite);
  1280. pbWrite+=sizeof(WORD);
  1281. for(DWORD i=0;i<wL;i++)
  1282. {
  1283. //存储这个压缩
  1284. if(nZipPos >= nMaxSize)
  1285. {
  1286. memcpy(lpZipData + nBufPos, byMaxBuf, nMaxSize);
  1287. nBufPos += nMaxSize;
  1288. nZipPos = 0;
  1289. ::GlobalUnlock(hUnZip); //重新分配内存
  1290. hUnZip = ::GlobalReAlloc(hUnZip, nBufPos + nMaxSize, 0);
  1291. lpZipData = (LPBYTE)::GlobalLock(hUnZip);
  1292. }
  1293. byMaxBuf[nZipPos] = *pbWrite;
  1294. nZipPos ++;//
  1295. nZipSize ++; //add
  1296. pbWrite++;
  1297. }
  1298. m_dwDecodedByte+=wL;
  1299. }
  1300. //////////////////////////////
  1301. wOld=wGet;
  1302. m_iTotalEntry=258;
  1303. }
  1304. else
  1305. {// not clear
  1306. pbDecodedData=m_LZWTable.GetMatchData(wGet);
  1307. if(NULL!=pbDecodedData)
  1308. {// in table
  1309. bFirst=pbDecodedData[2];
  1310. //////////////////////////////
  1311. BYTE *pbWrite = pbDecodedData;
  1312. if(pbWrite != NULL)
  1313. {
  1314. WORD wL=*((WORD*)pbWrite);
  1315. pbWrite+=sizeof(WORD);
  1316. for(DWORD i=0;i<wL;i++)
  1317. {
  1318. //存储这个压缩
  1319. if(nZipPos >= nMaxSize)
  1320. {
  1321. memcpy(lpZipData + nBufPos, byMaxBuf, nMaxSize);
  1322. nBufPos += nMaxSize;
  1323. nZipPos = 0;
  1324. ::GlobalUnlock(hUnZip); //重新分配内存
  1325. hUnZip = ::GlobalReAlloc(hUnZip, nBufPos + nMaxSize, 0);
  1326. lpZipData = (LPBYTE)::GlobalLock(hUnZip);
  1327. }
  1328. byMaxBuf[nZipPos] = *pbWrite;
  1329. nZipPos ++;//
  1330. nZipSize ++; //add
  1331. pbWrite++;
  1332. }
  1333. m_dwDecodedByte+=wL;
  1334. }
  1335. //////////////////////////////
  1336. if(wOld!=LZW_CLEAR_CODE)
  1337. {// not the first code be read in
  1338. pbDecodedData=m_LZWTable.GetMatchData(wOld);
  1339. wLastLen=*((WORD*)pbDecodedData);
  1340. memcpy(pbNewData,pbDecodedData+2,wLastLen);
  1341. pbNewData[wLastLen]=bFirst;
  1342. m_LZWTable.AddToChild((WORD)m_iTotalEntry,pbNewData,wLastLen+1);
  1343. m_iTotalEntry+=1;
  1344. }
  1345. wOld=wGet;
  1346. }
  1347. else
  1348. {
  1349. pbDecodedData=m_LZWTable.GetMatchData(wOld);
  1350. bFirst=pbDecodedData[2];
  1351. wLastLen=*((WORD*)pbDecodedData);
  1352. memcpy(pbOutData+2,pbDecodedData+2,wLastLen);
  1353. pbOutData[wLastLen+2]=bFirst;
  1354. *((WORD*)pbOutData)=wLastLen+1;
  1355. //////////////////////////////
  1356. BYTE *pbWrite = pbOutData;
  1357. if(pbWrite != NULL)
  1358. {
  1359. WORD wL=*((WORD*)pbWrite);
  1360. pbWrite+=sizeof(WORD);
  1361. for(DWORD i=0;i<wL;i++)
  1362. {
  1363. //存储这个压缩
  1364. if(nZipPos >= nMaxSize)
  1365. {
  1366. memcpy(lpZipData + nBufPos, byMaxBuf, nMaxSize);
  1367. nBufPos += nMaxSize;
  1368. nZipPos = 0;
  1369. ::GlobalUnlock(hUnZip); //重新分配内存
  1370. hUnZip = ::GlobalReAlloc(hUnZip, nBufPos + nMaxSize, 0);
  1371. lpZipData = (LPBYTE)::GlobalLock(hUnZip);
  1372. }
  1373. byMaxBuf[nZipPos] = *pbWrite;
  1374. nZipPos ++;//
  1375. nZipSize ++; //add
  1376. pbWrite++;
  1377. }
  1378. m_dwDecodedByte+=wL;
  1379. }
  1380. //////////////////////////////
  1381. if(m_iTotalEntry>=4096)
  1382. {
  1383. int _j=0;
  1384. }
  1385. m_LZWTable.AddToChild((WORD)m_iTotalEntry,pbOutData+2,wLastLen+1);
  1386. m_iTotalEntry+=1;
  1387. wOld=wGet;
  1388. }
  1389. }
  1390. }
  1391. delete pbNewData;
  1392. delete pbOutData;
  1393. delete pbBuffer;
  1394. if(dwRead != (DWORD)dwLength)
  1395. {
  1396. GlobalFree(hUnZip);
  1397. hUnZip = NULL;
  1398. }
  1399. //存储剩余的压缩数据
  1400. if(nZipPos > 0)
  1401. {
  1402. memcpy(lpZipData + nBufPos, byMaxBuf, nZipPos);
  1403. }
  1404. ::GlobalUnlock(hUnZip); //重新分配内存
  1405. hUnZip = ::GlobalReAlloc(hUnZip, nZipSize, 0);
  1406. return hUnZip;
  1407. }
  1408. /*
  1409. CLZWEncode
  1410. */
  1411. HGLOBAL CLZWEncode::BeginLZWEncode(char *chData, int dwLength)
  1412. {
  1413. if(!AfxIsValidAddress(chData, dwLength))
  1414. return NULL;
  1415. //缓冲数据区大小
  1416. const int nMaxSize = 65536;
  1417. HGLOBAL hZip = NULL;
  1418. hZip = ::GlobalAlloc(GHND, nMaxSize);
  1419. if(hZip == NULL)
  1420. {
  1421. return NULL;
  1422. }
  1423. LPBYTE lpZipData = (LPBYTE)::GlobalLock(hZip);
  1424. BYTE byMaxBuf[nMaxSize];
  1425. ZeroMemory(byMaxBuf, nMaxSize);
  1426. int nZipPos = 0; //记下压缩数据的位置
  1427. int nBufPos = 0; //记下当前存储位置hZip
  1428. int nZipSize = 0; //压缩后数据的大小
  1429. m_dwCompressedLength = 0;
  1430. WORD wBufferLen = 1024;
  1431. int iBufferLen = wBufferLen = 1024;
  1432. BYTE bGet;
  1433. int nCurPos = 0;
  1434. m_LZWTable.InitLZWTable();// init the entry table
  1435. m_baContain.InitBits((iBufferLen+8)*8);// init the bit array
  1436. m_baContain.AddBits(LZW_CLEAR_CODE,9);// add the first clear code
  1437. /// below : begin the algorithm
  1438. PLZWENCODEENTRY pCurrent = m_LZWTable.GetHead();
  1439. int iBitWidth;
  1440. DWORD dwEncoded=0;
  1441. for(DWORD i = 0; i < (DWORD)dwLength; i ++)
  1442. {// for each byte
  1443. bGet = chData[nCurPos]; // add
  1444. nCurPos ++;
  1445. PLZWENCODEENTRY pChild=m_LZWTable.FindMatchChild(bGet,pCurrent);
  1446. if(pChild)
  1447. {// has found the continue
  1448. pCurrent=pChild;
  1449. }
  1450. else
  1451. {// not find write last code & add new entry 
  1452. iBitWidth=GetBitWidth();
  1453. WORD wW=pCurrent->wCode;
  1454. m_baContain.AddBits(wW,iBitWidth);// add last code to buffer
  1455. m_LZWTable.AddToChild(bGet,pCurrent);// add last code to table
  1456. if(m_baContain.GetBytesWidth()>(DWORD)iBufferLen)
  1457. {
  1458. m_dwCompressedLength+=(DWORD)iBufferLen;
  1459. //存储这个压缩
  1460. if(nZipPos + iBufferLen >= nMaxSize)
  1461. {
  1462. memcpy(lpZipData + nBufPos, byMaxBuf, nZipPos);
  1463. nBufPos += nZipPos;
  1464. nZipPos = 0;
  1465. ::GlobalUnlock(hZip); //重新分配内存
  1466. hZip = ::GlobalReAlloc(hZip, nBufPos + nMaxSize, 0);
  1467. lpZipData = (LPBYTE)::GlobalLock(hZip);
  1468. }
  1469. memcpy(byMaxBuf + nZipPos, m_baContain.GetBits(), iBufferLen);
  1470. nZipPos += iBufferLen;
  1471. nZipSize += iBufferLen; //add
  1472. m_baContain.RemoveBytes(NULL,iBufferLen);
  1473. }
  1474. if(m_LZWTable.GetTableEntryNumber()>= (DWORD)(m_wMaxEntry-3))
  1475. {
  1476. iBitWidth=GetBitWidth();
  1477. m_baContain.AddBits(LZW_CLEAR_CODE,iBitWidth);
  1478. m_LZWTable.InitLZWTable();
  1479. }
  1480. pCurrent=m_LZWTable.FindMatchChild(bGet,m_LZWTable.GetHead());
  1481. }
  1482. dwEncoded++;
  1483. }
  1484. iBitWidth=GetBitWidth();
  1485. m_baContain.AddBits(pCurrent->wCode,iBitWidth);// add last code to buffer
  1486. iBitWidth=GetBitWidth();
  1487. m_baContain.AddBits(LZW_END_CODE,iBitWidth);
  1488. m_dwCompressedLength+=m_baContain.GetBytesWidth();
  1489. iBufferLen = m_baContain.GetBytesWidth();
  1490. //存储这个压缩
  1491. if(nZipPos + iBufferLen >= nMaxSize)
  1492. {
  1493. memcpy(lpZipData + nBufPos, byMaxBuf, nZipPos);
  1494. nBufPos += nZipPos;
  1495. nZipPos = 0;
  1496. ::GlobalUnlock(hZip); //重新分配内存
  1497. hZip = ::GlobalReAlloc(hZip, nBufPos + nMaxSize, 0);
  1498. lpZipData = (LPBYTE)::GlobalLock(hZip);
  1499. }
  1500. memcpy(byMaxBuf + nZipPos, m_baContain.GetBits(), iBufferLen);
  1501. nZipPos += iBufferLen;
  1502. nZipSize += iBufferLen; //add
  1503. m_LZWTable.ClearLZWTable();
  1504. m_baContain.ClearBits();
  1505. //存储剩余的压缩数据
  1506. if(nZipPos > 0)
  1507. {
  1508. memcpy(lpZipData + nBufPos, byMaxBuf, nZipPos);
  1509. }
  1510. ::GlobalUnlock(hZip); //重新分配内存
  1511. hZip = ::GlobalReAlloc(hZip, nZipSize, 0);
  1512. return hZip;
  1513. }
  1514. int CLZWEncode::GetBitWidth(void)
  1515. {
  1516. int iTotal=(int)m_LZWTable.GetTableEntryNumber();
  1517. iTotal>>=9;
  1518. int iBitWidth=9;
  1519. while(iTotal>0)
  1520. {
  1521. iTotal>>=1;
  1522. iBitWidth+=1;
  1523. }
  1524. return iBitWidth;
  1525. }
  1526. HGLOBAL C_LZW::Encode(char *chData, int nSize)
  1527. {
  1528. CLZWEncode cl;
  1529. return cl.BeginLZWEncode(chData, nSize);
  1530. }
  1531. HGLOBAL C_LZW::Decode(char *chZipData, int nZipSize)
  1532. {
  1533. CLZWDecode cl;
  1534. return cl.BeginLZWDecode(chZipData, nZipSize);
  1535. }
  1536. ///////////////////////////////////////////////////////////////////////////////
  1537. //LZW Part END--------------------------------------------------------------
  1538. ///////////////////////////////////////////////////////////////////////////////