⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csdn技术中心 lz77压缩算法(c语言版).htm

📁 是一个压缩文件的算法 用c语言写的
💻 HTM
📖 第 1 页 / 共 4 页
字号:
            (ULONG)ReadBitFromBitStream(pBuffer, ulBitOffset);<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;bit &lt;&lt;= i;<BR>&nbsp; &nbsp; &nbsp; &nbsp;r |= 
            bit;<BR>&nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp;*pulCodingLength = m + q 
            + 1;<BR><BR>&nbsp; &nbsp;return 
            r+(q&lt;&lt;m)+1;<BR>}<BR><BR><BR>ULONG<BR>CompareStrings(<BR>&nbsp; 
            &nbsp;PUCHAR &nbsp;string1,<BR>&nbsp; &nbsp;PUCHAR 
            &nbsp;string2,<BR>&nbsp; &nbsp;ULONG &nbsp; length<BR>&nbsp; 
            &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG &nbsp; &nbsp; &nbsp; i;<BR>&nbsp; 
            &nbsp;PUCHAR &nbsp; &nbsp; &nbsp;p1, p2;<BR><BR>&nbsp; &nbsp;p1 = 
            string1;<BR>&nbsp; &nbsp;p2 = string2;<BR><BR>&nbsp; &nbsp;for(i=0; 
            i&lt;length; i++)<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;if( *p1==*p2 )<BR>&nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p1++;<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp;p2++;<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;else<BR>&nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp;return 
            p1-string1;<BR>}<BR><BR><BR>void 
            WINAPI<BR>FindLongestSubstring(<BR>&nbsp; &nbsp;PUCHAR 
            &nbsp;pSourceString,<BR>&nbsp; &nbsp;PUCHAR &nbsp;pString,<BR>&nbsp; 
            &nbsp;ULONG &nbsp; ulSourceStringLength,<BR>&nbsp; &nbsp;PULONG 
            &nbsp;pulSubstringOffset,<BR>&nbsp; &nbsp;PULONG 
            &nbsp;pulSubstringLength<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp; 
            &nbsp;PUCHAR &nbsp;pSrc;<BR><BR>&nbsp; &nbsp;ULONG &nbsp; offset, 
            length;<BR><BR>&nbsp; &nbsp;ULONG &nbsp; 
            ulMaxLength;<BR><BR><BR>&nbsp; &nbsp;*pulSubstringOffset = offset = 
            0;<BR>&nbsp; &nbsp;*pulSubstringLength = 0;<BR><BR>&nbsp; &nbsp;if( 
            NULL==pSourceString || NULL==pString )<BR>&nbsp; &nbsp;{<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;return;<BR>&nbsp; &nbsp;}<BR><BR>&nbsp; 
            &nbsp;ulMaxLength = ulSourceStringLength;<BR>&nbsp; &nbsp;pSrc = 
            pSourceString;<BR><BR>&nbsp; &nbsp;while( ulMaxLength&gt;0 
            )<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;length = 
            CompareStrings(pSrc, pString, ulMaxLength);<BR><BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;if( length&gt;*pulSubstringLength )<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;*pulSubstringLength = length;<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;*pulSubstringOffset = offset;<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;pSrc++;<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;offset++;<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;ulMaxLength--;<BR>&nbsp; 
            &nbsp;}<BR>}<BR><BR><BR>/*<BR>void<BR>FindLongestSubstring(<BR>&nbsp; 
            &nbsp;PUCHAR &nbsp;pSourceString,<BR>&nbsp; &nbsp;PUCHAR 
            &nbsp;pString,<BR>&nbsp; &nbsp;ULONG &nbsp; 
            ulSourceStringLength,<BR>&nbsp; &nbsp;PULONG 
            &nbsp;pulSubstringOffset,<BR>&nbsp; &nbsp;PULONG 
            &nbsp;pulSubstringLength<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp; 
            &nbsp;PUCHAR &nbsp;pCurrentOffset;<BR>&nbsp; &nbsp;PUCHAR &nbsp;p1, 
            p2;<BR><BR>&nbsp; &nbsp;ULONG &nbsp; offset, length;<BR><BR>&nbsp; 
            &nbsp;pCurrentOffset = pSourceString;<BR><BR>&nbsp; 
            &nbsp;*pulSubstringOffset = offset = 0;<BR>&nbsp; 
            &nbsp;*pulSubstringLength = length = 0;<BR><BR>&nbsp; &nbsp;while( 
            pCurrentOffset&lt;pSourceString+ulSourceStringLength )<BR>&nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;p1 = pCurrentOffset;<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;p2 = pString;<BR><BR><BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;if( *p1==*p2 )<BR>&nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while( 
            p1&lt;pSourceString+ulSourceStringLength &amp;&amp; *p1==*p2 
            )<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p1++;<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p2++;<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;length = p1 - pCurrentOffset;<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;length = 
            0;<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;if( length&gt;*pulSubstringLength )<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;*pulSubstringLength = length;<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;*pulSubstringOffset = (ULONG)pCurrentOffset - 
            (ULONG)pSourceString;<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;pCurrentOffset++;<BR>&nbsp; 
            &nbsp;}<BR>}<BR>*/<BR><BR><BR>void<BR>WriteBits(<BR>&nbsp; 
            &nbsp;PUCHAR &nbsp;pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp; 
            ulOffsetToWrite,<BR>&nbsp; &nbsp;ULONG &nbsp; ulBits,<BR>&nbsp; 
            &nbsp;ULONG &nbsp; ulBitLength<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp; 
            &nbsp;ULONG &nbsp; ulDwordsOffset;<BR>&nbsp; &nbsp;ULONG &nbsp; 
            ulBitsOffset, ulBitsRemained;<BR><BR>&nbsp; &nbsp;ulDwordsOffset = 
            ulOffsetToWrite&gt;&gt;5;<BR>&nbsp; &nbsp;ulBitsOffset = 
            ulOffsetToWrite&amp;31;<BR>&nbsp; &nbsp;ulBitsRemained = 32 - 
            ulBitsOffset;<BR><BR>&nbsp; &nbsp;if( 0==ulBitsOffset )<BR>&nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;*((PULONG)pDataBuffer+ulDwordsOffset) = ulBits;<BR>&nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp;else if( ulBitsRemained&gt;=ulBitLength 
            )<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;*((PULONG)pDataBuffer+ulDwordsOffset) |= 
            (ulBits&lt;&lt;ulBitsOffset);<BR>&nbsp; &nbsp;}<BR>&nbsp; 
            &nbsp;else<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;*((PULONG)pDataBuffer+ulDwordsOffset) |= 
            (ulBits&lt;&lt;ulBitsOffset);<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;*((PULONG)pDataBuffer+ulDwordsOffset+1) = 
            ulBits&gt;&gt;ulBitsRemained;<BR>&nbsp; 
            &nbsp;}<BR>}<BR><BR>void<BR>ReadBits(<BR>&nbsp; &nbsp;PUCHAR 
            &nbsp;pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp; 
            ulOffsetToRead,<BR>&nbsp; &nbsp;PULONG &nbsp;pulBits<BR>&nbsp; 
            &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG &nbsp; ulDwordsOffset;<BR>&nbsp; 
            &nbsp;ULONG &nbsp; ulBitsOffset, ulBitsLength;<BR><BR>&nbsp; 
            &nbsp;ulDwordsOffset = ulOffsetToRead&gt;&gt;5;<BR>&nbsp; 
            &nbsp;ulBitsOffset = ulOffsetToRead&amp;31;<BR>&nbsp; 
            &nbsp;ulBitsLength = 32 - ulBitsOffset;<BR><BR><BR>&nbsp; 
            &nbsp;*pulBits = *((PULONG)pDataBuffer+ulDwordsOffset);<BR>&nbsp; 
            &nbsp;if( 0!=ulBitsOffset )<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;(*pulBits) &gt;&gt;= ulBitsOffset;<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;(*pulBits) |= 
            (*((PULONG)pDataBuffer+ulDwordsOffset+1))&lt;&lt;ulBitsLength;<BR>&nbsp; 
            &nbsp;}<BR>}<BR><BR>void<BR>lz77compress(<BR>&nbsp; &nbsp;PUCHAR 
            &nbsp;pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp; 
            ulDataLength,<BR>&nbsp; &nbsp;PUCHAR &nbsp;pOutputBuffer,<BR>&nbsp; 
            &nbsp;PULONG &nbsp;pulNumberOfBits<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp; 
            &nbsp;LONG &nbsp; &nbsp; &nbsp; &nbsp;iSlideWindowPtr;<BR>&nbsp; 
            &nbsp;ULONG &nbsp; &nbsp; &nbsp; ulBytesCoded;<BR>&nbsp; &nbsp;ULONG 
            &nbsp; &nbsp; &nbsp; ulMaxlength;<BR>&nbsp; &nbsp;PUCHAR &nbsp; 
            &nbsp; &nbsp;pSlideWindowPtr;<BR>&nbsp; &nbsp;PUCHAR &nbsp; &nbsp; 
            &nbsp;pUnprocessedDataPtr;<BR><BR>&nbsp; &nbsp;ULONG &nbsp; 
            offset;<BR>&nbsp; &nbsp;ULONG &nbsp; length;<BR>&nbsp; &nbsp;ULONG 
            &nbsp; ulCodingLength;<BR><BR>&nbsp; &nbsp;ULONG &nbsp; 
            ulBitOffset;<BR>&nbsp; &nbsp;UCHAR &nbsp; cc;<BR><BR>&nbsp; 
            &nbsp;int &nbsp; &nbsp; i;<BR><BR>&nbsp; &nbsp;iSlideWindowPtr = 
            -MAX_WND_SIZE;<BR>&nbsp; &nbsp;pSlideWindowPtr = NULL;<BR>&nbsp; 
            &nbsp;ulBitOffset = 0;<BR>&nbsp; &nbsp;ulBytesCoded = 
            0;<BR><BR>&nbsp; &nbsp;while( ulBytesCoded&lt;ulDataLength 
            )<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;if( 
            iSlideWindowPtr&gt;=0 )<BR>&nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pSlideWindowPtr = 
            pDataBuffer+iSlideWindowPtr;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;ulMaxlength = MAX_WND_SIZE;<BR><BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else if( 
            iSlideWindowPtr&gt;=-MAX_WND_SIZE )<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pSlideWindowPtr 
            = pDataBuffer;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;ulMaxlength = MAX_WND_SIZE + iSlideWindowPtr;<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;pSlideWindowPtr = NULL;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;ulMaxlength = 0;<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; 
            &nbsp; &nbsp; &nbsp;pUnprocessedDataPtr = pDataBuffer + 
            ulBytesCoded;<BR>&nbsp; &nbsp; &nbsp; &nbsp;if( 
            ulMaxlength&gt;ulDataLength-ulBytesCoded )<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ulMaxlength = 
            ulDataLength-ulBytesCoded;<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;FindLongestSubstring(<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;pSlideWindowPtr,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;pUnprocessedDataPtr,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;ulMaxlength,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;&amp;offset,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;&amp;length<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;);<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;assert( 
            length&lt;=MAX_WND_SIZE );<BR>&nbsp; &nbsp; &nbsp; &nbsp;assert( 
            offset&lt;MAX_WND_SIZE );<BR><BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;if(length&gt;1)<BR>&nbsp; &nbsp; &nbsp; &nbsp;{<BR><BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Write1ToBitStream(pOutputBuffer, 
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;ulBitOffset++;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;for(i=0; i&lt;OFFSET_CODING_LENGTH; i++, 
            ulBitOffset++)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;if( (offset&gt;&gt;i)&amp;1 )<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Write1ToBitStream(pOutputBuffer, 
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;else<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;Write0ToBitStream(pOutputBuffer, 
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ulCodingLength = 
            WriteGolombCode(length, pOutputBuffer, ulBitOffset);<BR><BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ulBitOffset += 
            ulCodingLength;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;iSlideWindowPtr += length;<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;ulBytesCoded += length;<BR><BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;Write0ToBitStream(pOutputBuffer, ulBitOffset);<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ulBitOffset++;<BR><BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cc = 
            (*pUnprocessedDataPtr);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;for(i=0; i&lt;8; i++, ulBitOffset++)<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;if( (cc&gt;&gt;i)&amp;1 )<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;Write1ToBitStream(pOutputBuffer, ulBitOffset);<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;Write0ToBitStream(pOutputBuffer, ulBitOffset);<BR>&nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;iSlideWindowPtr++;<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp; &nbsp;ulBytesCoded++;<BR>&nbsp; &nbsp; &nbsp; 
            &nbsp;}<BR><BR>&nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp;if( 
            ulBytesCoded!=ulDataLength )<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; 
            &nbsp; &nbsp;assert(ulBytesCoded==ulDataLength);<BR>&nbsp; 
            &nbsp;}<BR><BR>&nbsp; &nbsp;*pulNumberOfBits = 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -