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

📄 lz77压缩算法(c语言版).mht

📁 gzip 压缩解压缩 和zlib的核心算法一样
💻 MHT
📖 第 1 页 / 共 5 页
字号:
algorithm.<BR>*<BR>******************************************************=
***************/<BR><BR><BR>#include=20
            &lt;windows.h&gt;<BR>#include &lt;conio.h&gt;<BR>#include=20
            &lt;stdio.h&gt;<BR>#include &lt;assert.h&gt;<BR><BR>#define=20
            OFFSET_CODING_LENGTH &nbsp; &nbsp;(10)<BR>#define =
MAX_WND_SIZE=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1024<BR>//#define=20
            MAX_WND_SIZE &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;(1&lt;&lt;OFFSET_CODING_LENGTH)<BR>#define =
OFFSET_MASK_CODE=20
            &nbsp; &nbsp; &nbsp; &nbsp;(MAX_WND_SIZE-1)<BR><BR>const =
ULONG=20
            &nbsp; &nbsp; m=3D3;<BR><BR><BR>UCHAR &nbsp;=20
            __buffer1__[0x200000];<BR>UCHAR &nbsp;=20
            =
__buffer2__[0x200000];<BR><BR><BR>///////////////////////////////////////=
/////////////////////////////////////////<BR><BR>void<BR>Write1ToBitStrea=
m(<BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp;pBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulBitOffset<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG =
&nbsp;=20
            ulByteBoundary;<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulOffsetInByte;<BR><BR>&nbsp; &nbsp;ulByteBoundary =3D=20
            ulBitOffset&gt;&gt;3 ;<BR>&nbsp; &nbsp;ulOffsetInByte =3D=20
            ulBitOffset&amp;7;<BR><BR>&nbsp; =
&nbsp;*(pBuffer+ulByteBoundary) |=3D=20
            =
(1&lt;&lt;ulOffsetInByte);<BR>}<BR><BR>void<BR>Write0ToBitStream(<BR>&nbs=
p;=20
            &nbsp;PUCHAR &nbsp;pBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulBitOffset<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG =
&nbsp;=20
            ulByteBoundary;<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulOffsetInByte;<BR><BR>&nbsp; &nbsp;ulByteBoundary =3D=20
            ulBitOffset&gt;&gt;3 ;<BR>&nbsp; &nbsp;ulOffsetInByte =3D=20
            ulBitOffset&amp;7;<BR><BR>&nbsp; =
&nbsp;*(pBuffer+ulByteBoundary)=20
            &amp;=3D=20
            =
(~(1&lt;&lt;ulOffsetInByte));<BR>}<BR><BR>ULONG<BR>ReadBitFromBitStream(<=
BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp;pBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulBitOffset<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG =
&nbsp;=20
            ulByteBoundary;<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulOffsetInByte;<BR><BR>&nbsp; &nbsp;ulByteBoundary =3D=20
            ulBitOffset&gt;&gt;3 ;<BR>&nbsp; &nbsp;ulOffsetInByte =3D=20
            ulBitOffset&amp;7;<BR><BR>&nbsp; &nbsp;return=20
            =
((*(PULONG)(pBuffer+ulByteBoundary))&gt;&gt;ulOffsetInByte)&amp;1=20
            ;<BR>}<BR><BR><BR>ULONG WINAPI<BR>WriteGolombCode(<BR>&nbsp; =

            &nbsp;ULONG &nbsp; x,<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;pBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp; =
ulBitOffset<BR>&nbsp;=20
            &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; q, r;<BR>&nbsp; &nbsp;int &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; i;<BR><BR>&nbsp; &nbsp;q =3D =
(x-1)&gt;&gt;m;<BR>&nbsp; &nbsp;r=20
            =3D x-(q&lt;&lt;m)-1;<BR><BR>&nbsp; &nbsp;for(i=3D0; =
(ULONG)i&lt;q; i++,=20
            ulBitOffset++)<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;Write1ToBitStream(pBuffer, ulBitOffset);<BR>&nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp;Write0ToBitStream(pBuffer,=20
            ulBitOffset);<BR>&nbsp; &nbsp;ulBitOffset++;<BR><BR>&nbsp;=20
            &nbsp;for(i=3D0; i&lt;m; i++, ulBitOffset++)<BR>&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;if( (r&gt;&gt;i)&amp;1 =

            )<BR>&nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;Write1ToBitStream(pBuffer, =
ulBitOffset);<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;else<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;Write0ToBitStream(pBuffer, ulBitOffset);<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;}<BR>&nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp;return=20
            m+q+1;<BR>}<BR><BR><BR>ULONG<BR>ReadGolombCode(<BR>&nbsp;=20
            &nbsp;PULONG &nbsp;pulCodingLength,<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;pBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp; =
ulBitOffset<BR>&nbsp;=20
            &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG &nbsp; q, r;<BR>&nbsp;=20
            &nbsp;ULONG &nbsp; bit;<BR>&nbsp; &nbsp;int i;<BR><BR>&nbsp; =

            &nbsp;for(q=3D0; ;q++)<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;bit =3D (ULONG)ReadBitFromBitStream(pBuffer,=20
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;ulBitOffset++;<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;if( !bit )<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;break;<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp;}<BR><BR><BR>&nbsp;=20
            &nbsp;for(i=3D0, r=3D0; (ULONG)i&lt;m; i++, =
ulBitOffset++)<BR>&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;bit =3D=20
            (ULONG)ReadBitFromBitStream(pBuffer, ulBitOffset);<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;bit &lt;&lt;=3D i;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;r |=3D=20
            bit;<BR>&nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp;*pulCodingLength =
=3D m + q=20
            + 1;<BR><BR>&nbsp; &nbsp;return=20
            =
r+(q&lt;&lt;m)+1;<BR>}<BR><BR><BR>ULONG<BR>CompareStrings(<BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp;string1,<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;string2,<BR>&nbsp; &nbsp;ULONG &nbsp; length<BR>&nbsp; =

            &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG &nbsp; &nbsp; &nbsp; =
i;<BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp; &nbsp; &nbsp;p1, p2;<BR><BR>&nbsp; =
&nbsp;p1 =3D=20
            string1;<BR>&nbsp; &nbsp;p2 =3D string2;<BR><BR>&nbsp; =
&nbsp;for(i=3D0;=20
            i&lt;length; i++)<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;if( *p1=3D=3D*p2 )<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;{<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p1++;<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp;p2++;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;}<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;else<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;{<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp;return=20
            p1-string1;<BR>}<BR><BR><BR>void=20
            WINAPI<BR>FindLongestSubstring(<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;pSourceString,<BR>&nbsp; &nbsp;PUCHAR =
&nbsp;pString,<BR>&nbsp;=20
            &nbsp;ULONG &nbsp; ulSourceStringLength,<BR>&nbsp; =
&nbsp;PULONG=20
            &nbsp;pulSubstringOffset,<BR>&nbsp; &nbsp;PULONG=20
            &nbsp;pulSubstringLength<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp;pSrc;<BR><BR>&nbsp; &nbsp;ULONG &nbsp; =
offset,=20
            length;<BR><BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulMaxLength;<BR><BR><BR>&nbsp; &nbsp;*pulSubstringOffset =3D =
offset =3D=20
            0;<BR>&nbsp; &nbsp;*pulSubstringLength =3D 0;<BR><BR>&nbsp; =
&nbsp;if(=20
            NULL=3D=3DpSourceString || NULL=3D=3DpString )<BR>&nbsp; =
&nbsp;{<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;return;<BR>&nbsp; &nbsp;}<BR><BR>&nbsp;=20
            &nbsp;ulMaxLength =3D ulSourceStringLength;<BR>&nbsp; =
&nbsp;pSrc =3D=20
            pSourceString;<BR><BR>&nbsp; &nbsp;while( ulMaxLength&gt;0=20
            )<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;length =3D =

            CompareStrings(pSrc, pString, ulMaxLength);<BR><BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;if( length&gt;*pulSubstringLength )<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;*pulSubstringLength =3D length;<BR>&nbsp; &nbsp; =
&nbsp; &nbsp;=20
            &nbsp; &nbsp;*pulSubstringOffset =3D offset;<BR>&nbsp; =
&nbsp; &nbsp;=20
            &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;pSrc++;<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;offset++;<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;ulMaxLength--;<BR>&nbsp;=20
            =
&nbsp;}<BR>}<BR><BR><BR>/*<BR>void<BR>FindLongestSubstring(<BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp;pSourceString,<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;pString,<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulSourceStringLength,<BR>&nbsp; &nbsp;PULONG=20
            &nbsp;pulSubstringOffset,<BR>&nbsp; &nbsp;PULONG=20
            &nbsp;pulSubstringLength<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp;pCurrentOffset;<BR>&nbsp; &nbsp;PUCHAR =
&nbsp;p1,=20
            p2;<BR><BR>&nbsp; &nbsp;ULONG &nbsp; offset, =
length;<BR><BR>&nbsp;=20
            &nbsp;pCurrentOffset =3D pSourceString;<BR><BR>&nbsp;=20
            &nbsp;*pulSubstringOffset =3D offset =3D 0;<BR>&nbsp;=20
            &nbsp;*pulSubstringLength =3D length =3D 0;<BR><BR>&nbsp; =
&nbsp;while(=20
            pCurrentOffset&lt;pSourceString+ulSourceStringLength =
)<BR>&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;p1 =3D =
pCurrentOffset;<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;p2 =3D pString;<BR><BR><BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;if( *p1=3D=3D*p2 )<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;{<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(=20
            p1&lt;pSourceString+ulSourceStringLength &amp;&amp; =
*p1=3D=3D*p2=20
            )<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p1++;<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p2++;<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;length =3D p1 - pCurrentOffset;<BR>&nbsp; =
&nbsp; &nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;length =
=3D=20
            0;<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;if( length&gt;*pulSubstringLength )<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;*pulSubstringLength =3D length;<BR>&nbsp; &nbsp; =
&nbsp; &nbsp;=20
            &nbsp; &nbsp;*pulSubstringOffset =3D (ULONG)pCurrentOffset - =

            (ULONG)pSourceString;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;}<BR><BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;pCurrentOffset++;<BR>&nbsp;=20
            &nbsp;}<BR>}<BR>*/<BR><BR><BR>void<BR>WriteBits(<BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp;pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp; =

            ulOffsetToWrite,<BR>&nbsp; &nbsp;ULONG &nbsp; =
ulBits,<BR>&nbsp;=20
            &nbsp;ULONG &nbsp; ulBitLength<BR>&nbsp; =
&nbsp;)<BR>{<BR>&nbsp;=20
            &nbsp;ULONG &nbsp; ulDwordsOffset;<BR>&nbsp; &nbsp;ULONG =
&nbsp;=20
            ulBitsOffset, ulBitsRemained;<BR><BR>&nbsp; =
&nbsp;ulDwordsOffset =3D=20
            ulOffsetToWrite&gt;&gt;5;<BR>&nbsp; &nbsp;ulBitsOffset =3D=20
            ulOffsetToWrite&amp;31;<BR>&nbsp; &nbsp;ulBitsRemained =3D =
32 -=20
            ulBitsOffset;<BR><BR>&nbsp; &nbsp;if( 0=3D=3DulBitsOffset =
)<BR>&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;*((PULONG)pDataBuffer+ulDwordsOffset) =3D =
ulBits;<BR>&nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp;else if( =
ulBitsRemained&gt;=3DulBitLength=20
            )<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;*((PULONG)pDataBuffer+ulDwordsOffset) |=3D=20
            (ulBits&lt;&lt;ulBitsOffset);<BR>&nbsp; &nbsp;}<BR>&nbsp;=20
            &nbsp;else<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;*((PULONG)pDataBuffer+ulDwordsOffset) |=3D=20
            (ulBits&lt;&lt;ulBitsOffset);<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;*((PULONG)pDataBuffer+ulDwordsOffset+1) =3D=20
            ulBits&gt;&gt;ulBitsRemained;<BR>&nbsp;=20
            &nbsp;}<BR>}<BR><BR>void<BR>ReadBits(<BR>&nbsp; &nbsp;PUCHAR =

            &nbsp;pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulOffsetToRead,<BR>&nbsp; &nbsp;PULONG =
&nbsp;pulBits<BR>&nbsp;=20
            &nbsp;)<BR>{<BR>&nbsp; &nbsp;ULONG &nbsp; =
ulDwordsOffset;<BR>&nbsp;=20
            &nbsp;ULONG &nbsp; ulBitsOffset, ulBitsLength;<BR><BR>&nbsp; =

            &nbsp;ulDwordsOffset =3D ulOffsetToRead&gt;&gt;5;<BR>&nbsp;=20
            &nbsp;ulBitsOffset =3D ulOffsetToRead&amp;31;<BR>&nbsp;=20
            &nbsp;ulBitsLength =3D 32 - ulBitsOffset;<BR><BR><BR>&nbsp;=20
            &nbsp;*pulBits =3D =
*((PULONG)pDataBuffer+ulDwordsOffset);<BR>&nbsp;=20
            &nbsp;if( 0!=3DulBitsOffset )<BR>&nbsp; &nbsp;{<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;(*pulBits) &gt;&gt;=3D ulBitsOffset;<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;(*pulBits) |=3D=20
            =
(*((PULONG)pDataBuffer+ulDwordsOffset+1))&lt;&lt;ulBitsLength;<BR>&nbsp; =

            &nbsp;}<BR>}<BR><BR>void<BR>lz77compress(<BR>&nbsp; =
&nbsp;PUCHAR=20
            &nbsp;pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulDataLength,<BR>&nbsp; &nbsp;PUCHAR =
&nbsp;pOutputBuffer,<BR>&nbsp;=20
            &nbsp;PULONG &nbsp;pulNumberOfBits<BR>&nbsp; =
&nbsp;)<BR>{<BR>&nbsp;=20
            &nbsp;LONG &nbsp; &nbsp; &nbsp; =
&nbsp;iSlideWindowPtr;<BR>&nbsp;=20
            &nbsp;ULONG &nbsp; &nbsp; &nbsp; ulBytesCoded;<BR>&nbsp; =
&nbsp;ULONG=20
            &nbsp; &nbsp; &nbsp; ulMaxlength;<BR>&nbsp; &nbsp;PUCHAR =

⌨️ 快捷键说明

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