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

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

📁 gzip 压缩解压缩 和zlib的核心算法一样
💻 MHT
📖 第 1 页 / 共 5 页
字号:
 =20
            &nbsp; &nbsp;pSlideWindowPtr;<BR>&nbsp; &nbsp;PUCHAR &nbsp; =
&nbsp;=20
            &nbsp;pUnprocessedDataPtr;<BR><BR>&nbsp; &nbsp;ULONG &nbsp;=20
            offset;<BR>&nbsp; &nbsp;ULONG &nbsp; length;<BR>&nbsp; =
&nbsp;ULONG=20
            &nbsp; ulCodingLength;<BR><BR>&nbsp; &nbsp;ULONG &nbsp;=20
            ulBitOffset;<BR>&nbsp; &nbsp;UCHAR &nbsp; cc;<BR><BR>&nbsp;=20
            &nbsp;int &nbsp; &nbsp; i;<BR><BR>&nbsp; =
&nbsp;iSlideWindowPtr =3D=20
            -MAX_WND_SIZE;<BR>&nbsp; &nbsp;pSlideWindowPtr =3D =
NULL;<BR>&nbsp;=20
            &nbsp;ulBitOffset =3D 0;<BR>&nbsp; &nbsp;ulBytesCoded =3D=20
            0;<BR><BR>&nbsp; &nbsp;while( ulBytesCoded&lt;ulDataLength=20
            )<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;if(=20
            iSlideWindowPtr&gt;=3D0 )<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;{<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pSlideWindowPtr =3D=20
            pDataBuffer+iSlideWindowPtr;<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;ulMaxlength =3D MAX_WND_SIZE;<BR><BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else if(=20
            iSlideWindowPtr&gt;=3D-MAX_WND_SIZE )<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;pSlideWindowPtr=20
            =3D pDataBuffer;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;ulMaxlength =3D MAX_WND_SIZE + =
iSlideWindowPtr;<BR>&nbsp; &nbsp;=20
            &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;pSlideWindowPtr =3D NULL;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;=20
            &nbsp;ulMaxlength =3D 0;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;}<BR><BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp;pUnprocessedDataPtr =3D pDataBuffer +=20
            ulBytesCoded;<BR>&nbsp; &nbsp; &nbsp; &nbsp;if(=20
            ulMaxlength&gt;ulDataLength-ulBytesCoded )<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;ulMaxlength =3D=20
            ulDataLength-ulBytesCoded;<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;FindLongestSubstring(<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;pSlideWindowPtr,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =

            &nbsp;pUnprocessedDataPtr,<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;ulMaxlength,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;&amp;offset,<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;&amp;length<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;);<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;assert(=20
            length&lt;=3DMAX_WND_SIZE );<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;assert(=20
            offset&lt;MAX_WND_SIZE );<BR><BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;if(length&gt;1)<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;{<BR><BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;Write1ToBitStream(pOutputBuffer,=20
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;ulBitOffset++;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;for(i=3D0; i&lt;OFFSET_CODING_LENGTH; i++,=20
            ulBitOffset++)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;if( (offset&gt;&gt;i)&amp;1 )<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;Write1ToBitStream(pOutputBuffer,=20
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;else<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;Write0ToBitStream(pOutputBuffer,=20
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;}<BR><BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ulCodingLength =3D=20
            WriteGolombCode(length, pOutputBuffer, =
ulBitOffset);<BR><BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ulBitOffset +=3D=20
            ulCodingLength;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;iSlideWindowPtr +=3D length;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;ulBytesCoded +=3D length;<BR><BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;else<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;Write0ToBitStream(pOutputBuffer, =
ulBitOffset);<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;ulBitOffset++;<BR><BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cc =3D=20
            (*pUnprocessedDataPtr);<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;for(i=3D0; i&lt;8; i++, ulBitOffset++)<BR>&nbsp; =
&nbsp; &nbsp;=20
            &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;if( (cc&gt;&gt;i)&amp;1 )<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;Write1ToBitStream(pOutputBuffer, =
ulBitOffset);<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;Write0ToBitStream(pOutputBuffer, =
ulBitOffset);<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;iSlideWindowPtr++;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;ulBytesCoded++;<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;}<BR><BR>&nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp;if(=20
            ulBytesCoded!=3DulDataLength )<BR>&nbsp; &nbsp;{<BR>&nbsp; =
&nbsp;=20
            &nbsp; =
&nbsp;assert(ulBytesCoded=3D=3DulDataLength);<BR>&nbsp;=20
            &nbsp;}<BR><BR>&nbsp; &nbsp;*pulNumberOfBits =3D=20
            ulBitOffset;<BR><BR>}<BR><BR><BR>void =
lz77decompress(<BR>&nbsp;=20
            &nbsp;PUCHAR &nbsp;pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp; =

            ulNumberOfBits,<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;pOutputBuffer,<BR>&nbsp; &nbsp;PULONG=20
            &nbsp;pulNumberOfBytes<BR>&nbsp; &nbsp;)<BR>{<BR>&nbsp; =
&nbsp;LONG=20
            &nbsp; &nbsp; &nbsp; &nbsp;iSlideWindowPtr;<BR>&nbsp; =
&nbsp;PUCHAR=20
            &nbsp; &nbsp; &nbsp;pSlideWindowPtr;<BR><BR>&nbsp; =
&nbsp;ULONG=20
            &nbsp; length, offset;<BR>&nbsp; &nbsp;ULONG &nbsp; =
bit;<BR>&nbsp;=20
            &nbsp;UCHAR &nbsp; cc;<BR>&nbsp; &nbsp;int &nbsp; &nbsp;=20
            i;<BR><BR>&nbsp; &nbsp;ULONG &nbsp; =
ulBytesDecoded;<BR>&nbsp;=20
            &nbsp;ULONG &nbsp; ulBitOffset;<BR><BR>&nbsp; &nbsp;ULONG =
&nbsp;=20
            ulCodingLength;<BR>&nbsp; &nbsp;PUCHAR =
&nbsp;pWrite;<BR><BR>&nbsp;=20
            &nbsp;iSlideWindowPtr =3D -MAX_WND_SIZE;<BR>&nbsp; =
&nbsp;pWrite =3D=20
            (PUCHAR)pOutputBuffer;<BR>&nbsp; &nbsp;ulBitOffset =3D =
0;<BR>&nbsp;=20
            &nbsp;ulBytesDecoded =3D 0;<BR><BR><BR>&nbsp; &nbsp;while(=20
            ulBitOffset&lt;ulNumberOfBits )<BR>&nbsp; &nbsp;{<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp;bit =3D ReadBitFromBitStream(pDataBuffer,=20
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;ulBitOffset++;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp;if( =
bit=20
            )<BR>&nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;if( iSlideWindowPtr&gt;=3D0 )<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;pSlideWindowPtr =3D pOutputBuffer +=20
            iSlideWindowPtr;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if( =

            iSlideWindowPtr&gt;=3D-MAX_WND_SIZE )<BR>&nbsp; &nbsp; =
&nbsp; &nbsp;=20
            &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;pSlideWindowPtr =3D pOutputBuffer;<BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;else<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;{<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;pSlideWindowPtr =3D=20
            NULL;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;}<BR><BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;for(i=3D0,=20
            offset=3D0; i&lt;OFFSET_CODING_LENGTH; i++, =
ulBitOffset++)<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp;bit =3D =
ReadBitFromBitStream(pDataBuffer,=20
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;offset |=3D (bit&lt;&lt;i);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;length=3D ReadGolombCode(&amp;ulCodingLength, =
pDataBuffer,=20
            ulBitOffset);<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;assert(offset&lt;MAX_WND_SIZE);<BR><BR>&nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp;if( length&gt;MAX_WND_SIZE )<BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp; =
&nbsp;assert(length&lt;=3DMAX_WND_SIZE);<BR>&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;ulBitOffset +=3D ulCodingLength;<BR><BR>&nbsp; =
&nbsp;=20
            &nbsp; &nbsp; &nbsp; &nbsp;RtlMoveMemory(pWrite,=20
            pSlideWindowPtr+offset, length);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;pWrite+=3Dlength;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;=20
            &nbsp;iSlideWindowPtr+=3Dlength;<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;=20
            &nbsp;ulBytesDecoded+=3Dlength;<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;for(i=3D0, cc=3D0;=20
            i&lt;8 ; i++, ulBitOffset++)<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;bit =3D ReadBitFromBitStream(pDataBuffer,=20
            ulBitOffset);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;cc |=3D ((UCHAR)bit&lt;&lt;i);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=20
            &nbsp;*pWrite++ =3D cc;<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;iSlideWindowPtr++;<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;=20
            &nbsp;ulBytesDecoded++;<BR>&nbsp; &nbsp; &nbsp;=20
            &nbsp;}<BR><BR>&nbsp; &nbsp;}<BR><BR>&nbsp; =
&nbsp;*pulNumberOfBytes=20
            =3D ulBytesDecoded;<BR>}<BR><BR>extern "C"<BR>void=20
            WINAPI<BR>LZ77Compress(<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;__pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            __ulDataLength,<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;__pOutputBuffer,<BR>&nbsp; &nbsp;PULONG=20
            &nbsp;__pulNumberOfBits<BR>&nbsp; &nbsp;);<BR><BR>extern =
"C"<BR>void=20
            WINAPI<BR>LZ77Decompress(<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;__pDataBuffer,<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            __ulNumberOfBits,<BR>&nbsp; &nbsp;PUCHAR=20
            &nbsp;__pOutputBuffer,<BR>&nbsp; &nbsp;PULONG=20
            &nbsp;__pulNumberOfBytes<BR>&nbsp;=20
            &nbsp;);<BR><BR>int<BR>main(<BR>&nbsp; &nbsp;int &nbsp; =
&nbsp;=20
            argc,<BR>&nbsp; &nbsp;char &nbsp; &nbsp;*argv[]<BR>&nbsp;=20
            &nbsp;)<BR>{<BR>&nbsp; &nbsp;FILE &nbsp; =
&nbsp;*fp=3DNULL;<BR>&nbsp;=20
            &nbsp;FILE &nbsp; &nbsp;*fp1;<BR>&nbsp; &nbsp;ULONG &nbsp;=20
            fsize;<BR>&nbsp; &nbsp;ULONG &nbsp; =
ulNumberOfBits;<BR>&nbsp;=20
            &nbsp;ULONG &nbsp; ulFileCompressedSize;<BR>&nbsp; =
&nbsp;ULONG=20
            &nbsp; ulFileDecompressedSize;<BR>&nbsp; &nbsp;SYSTEMTIME =
&nbsp;=20
            &nbsp; &nbsp;t1, t2;<BR><BR>&nbsp; &nbsp;if( 3!=3Dargc =
)<BR>&nbsp;=20
            &nbsp;{<BR>&nbsp; &nbsp; &nbsp; &nbsp;printf("Usage: lz77 =
[/c | /d]=20
            filename\n");<BR>&nbsp; &nbsp; &nbsp; &nbsp;return =
-1;<BR>&nbsp;=20
            &nbsp;}<BR><BR><BR><BR>// &nbsp;char &nbsp;=20
            &nbsp;s1[]=3D"abcdabcdefgabcdefaffasda";<BR>// &nbsp;ULONG =

⌨️ 快捷键说明

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