Пример #1
0
        internal static MemoryStream ZLibCompress(IntPtr memoryAddress, int memoryLength)
        {
            MemoryStream compressedStream = new MemoryStream();

            compressedStream.Write(new byte[] { 0x78, 0x01 }, 0, 2);

            byte[] buffer = new byte[1024];

            AdlerHolder adlerHolder = new AdlerHolder();

            using (DeflateStream deflate = new DeflateStream(compressedStream, CompressionLevel.Optimal, true))
            {
                while (memoryLength > 0)
                {
                    int currentBytes = Math.Min(memoryLength, 1024);
                    Marshal.Copy(memoryAddress, buffer, 0, currentBytes);
                    deflate.Write(buffer, 0, currentBytes);
                    memoryLength -= currentBytes;
                    memoryAddress = IntPtr.Add(memoryAddress, currentBytes);
                    Adler32(buffer, currentBytes, adlerHolder);
                }
            }


            uint checksum = (adlerHolder.s2 << 16) + adlerHolder.s1;

            compressedStream.Write(new byte[] { (byte)((checksum >> 24) & 255), (byte)((checksum >> 16) & 255), (byte)((checksum >> 8) & 255), (byte)(checksum & 255) }, 0, 4);

            compressedStream.Seek(0, SeekOrigin.Begin);

            return(compressedStream);
        }
Пример #2
0
 internal static void Adler32(byte[] buffer, int length, AdlerHolder holder)
 {
     for (int i = 0; i < length; i++)
     {
         holder.s1 = (holder.s1 + buffer[i]) % 65521U;
         holder.s2 = (holder.s2 + holder.s1) % 65521U;
     }
 }