《LZ77压缩算法C语言实现》由会员分享,可在线阅读,更多相关《LZ77压缩算法C语言实现(15页珍藏版)》请在金锄头文库上搜索。
1、/* Project description:* Lz77 compression/decompression algorithm.*/#include #include #include #include #define OFFSET_CODING_LENGTH (10)#define MAX WND SIZE1024/#define MAX WND SIZE#define OFFSET MASK CODE(13 ;ulOffsetInByte = ulBitOffset&7;*(pBuffer+ulByteBoundary) |= (13 ;ulOffsetInByte = ulBitOf
2、fset&7;*(pBuffer+ulByteBoundary) &= (13 ;ulOffsetInByte = ulBitOffset&7;return (*(PULONG)(pBuffer+ulByteBoundary)ulOffsetInByte)&1 ; ULONG WINAPIWriteGolombCode(ULONG x,PUCHAR pBuffer, ULONG ulBitOffset )ULONGq, r;inti;q = (x-1)m;r = x-(qm)-1;for(i=0; (ULONG)iq; i+, ulBitOffset+) Write1ToBitStream(p
3、Buffer, ulBitOffset);Write0ToBitStream(pBuffer, ulBitOffset);ulBitOffset+;for(i=0; ii)&1 )Write1ToBitStream(pBuffer, ulBitOffset);elseWrite0ToBitStream(pBuffer, ulBitOffset);return m+q+1;ULONGReadGolombCode(PULONG pulCodingLength,PUCHAR pBuffer,ULONG ulBitOffset)ULONG q, r;ULONG bit;int i;for(q=0; ;
4、q+)bit = (ULONG)ReadBitFromBitStream(pBuffer, ulBitOffset);ulBitOffset+;if( !bit )break;for(i=0, r=0; (ULONG)im; i+, ulBitOffset+) bit = (ULONG)ReadBitFromBitStream(pBuffer, ulBitOffset); bit = i;r |= bit;*pulCodingLength = m + q + 1;return r+(qm)+1;ULONGCompareStrings(PUCHAR string1,PUCHAR string2,
5、ULONG length)ULONGi;PUCHARp1, p2;p1 = string1;p2 = string2;for(i=0; i0 )length = CompareStrings(pSrc, pString, ulMaxLength);if( length*pulSubstringLength )*pulSubstringLength = length;*pulSubstringOffset = offset;pSrc+;offset+;ulMaxLength-;/* voidFindLongestSubstring(PUCHAR pSourceString,PUCHAR pStr
6、ing,ULONG ulSourceStringLength,PULONG pulSubstringOffset,PULONG pulSubstringLength)PUCHAR pCurrentOffset;PUCHAR p1, p2;ULONG offset, length;pCurrentOffset = pSourceString;*pulSubstringOffset = offset = 0;*pulSubstringLength = length = 0;while( pCurrentOffsetpSourceString+ulSourceStringLength )p1 = p
7、CurrentOffset;p2 = pString;if( *p1=*p2 )while( p1*pulSubstringLength )*pulSubstringLength = length;*pulSubstringOffset = (ULONG)pCurrentOffset - (ULONG)pSourceString;pCurrentOffset+;*/ voidWriteBits(PUCHAR pDataBuffer,ULONG ulOffsetToWrite,ULONG ulBits,ULONG ulBitLength)ULONG ulDwordsOffset;ULONG ul
8、BitsOffset, ulBitsRemained;ulDwordsOffset = ulOffsetToWrite5;ulBitsOffset = ulOffsetToWrite&31;ulBitsRemained = 32 - ulBitsOffset;if( 0=ulBitsOffset )*(PULONG)pDataBuffer+ulDwordsOffset) = ulBits;else if( ulBitsRemained=ulBitLength )*(PULONG)pDataBuffer+ulDwordsOffset) |= (ulBitsulBitsOffset);else*(
9、PULONG)pDataBuffer+ulDwordsOffset) |= (ulBitsulBitsRemained;voidReadBits(PUCHAR pDataBuffer,ULONG ulOffsetToRead,PULONG pulBits)ULONG ulDwordsOffset;ULONG ulBitsOffset, ulBitsLength;ulDwordsOffset = ulOffsetToRead5;ulBitsOffset = ulOffsetToRead&31;ulBitsLength = 32 - ulBitsOffset;*pulBits = *(PULONG)pDataBuffer+ulDwordsOffset);if( 0!=ulBitsOffset )(*pulBits) = ulBitsOffset;(*pulBits) |= (*(PULONG)pDataBuffer+ulDwordsOffset+1)ulBitsLength;voidlz77compress(PUCHAR pDataBuffer,ULONG ulDataLength,PUCHAR pOutputBuffer,PULONG pulNumberO