Computes Adler32 checksum for a stream of data. An Adler32 checksum is not as reliable as a CRC32 checksum, but a lot faster to compute. The specification for Adler32 may be found in RFC 1950. ZLIB Compressed Data Format Specification version 3.3) From that document: "ADLER32 (Adler-32 checksum) This contains a checksum value of the uncompressed data (excluding any dictionary data) computed according to Adler-32 algorithm. This algorithm is a 32-bit extension and improvement of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073 standard. Adler-32 is composed of two sums accumulated per byte: s1 is the sum of all bytes, s2 is the sum of all s1 values. Both sums are done modulo 65521. s1 is initialized to 1, s2 to zero. The Adler-32 checksum is stored as s2*65536 + s1 in most- significant-byte first (network) order." "8.2. The Adler-32 algorithm The Adler-32 algorithm is much faster than the CRC32 algorithm yet still provides an extremely low probability of undetected errors. The modulo on unsigned long accumulators can be delayed for 5552 bytes, so the modulo operation time is negligible. If the bytes are a, b, c, the second sum is 3a + 2b + c + 3, and so is position and order sensitive, unlike the first sum, which is just a checksum. That 65521 is prime is important to avoid a possible large class of two-byte errors that leave the check unchanged. (The Fletcher checksum uses 255, which is not prime and which also makes the Fletcher check insensitive to single byte changes 0 - 255.) The sum s1 is initialized to 1 instead of zero to make the length of the sequence part of s2, so that the length does not have to be checked separately. (Any sequence of zeroes has a Fletcher checksum of zero.)"
Inheritance: IChecksum
Example #1
0
        public static byte[] Compress(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            using (var output = new MemoryStream())
            {
                // ZLib Header 0x78 0x9C
                output.WriteByte(0x78);
                output.WriteByte(0x9C);
                using (var input = new MemoryStream(data))
                {
                    using (var compressionStream = new DeflateStream(output, CompressionMode.Compress, true))
                    {
                        input.CopyTo(compressionStream);
                        compressionStream.Close();

                        // Adler32 hash of the uncompressed data
                        var adler32 = new Adler32();
                        adler32.Update(data);
                        byte[] hash = BitConverter.GetBytes((int) adler32.Value);
                        Array.Reverse(hash);
                        output.Write(hash, 0, hash.Length);
                        return output.ToArray();
                    }
                }
            }
        }
Example #2
0
 public Inflater(bool noHeader)
 {
     this.noHeader = noHeader;
     this.adler = new Adler32();
     this.input = new StreamManipulator();
     this.outputWindow = new OutputWindow();
     this.mode = noHeader ? 2 : 0;
 }
Example #3
0
 public Inflater(bool noHeader)
 {
     this.noHeader = noHeader;
     this.adler = new Adler32();
     input = new StreamManipulator();
     outputWindow = new OutputWindow();
     mode = noHeader ? DECODE_BLOCKS : DECODE_HEADER;
 }
Example #4
0
 public DeflaterEngine(DeflaterPending pending)
 {
     this.pending = pending;
     this.huffman = new DeflaterHuffman(pending);
     this.adler = new Adler32();
     this.window = new byte[65536];
     this.head = new short[32768];
     this.prev = new short[32768];
     this.blockStart = (this.strstart = 1);
 }
Example #5
0
		/// <summary>
		/// Construct instance with pending buffer
		/// </summary>
		/// <param name="pending">
		/// Pending buffer to use
		/// </param>>
		public DeflaterEngine(DeflaterPending pending) 
		{
			this.pending = pending;
			huffman = new DeflaterHuffman(pending);
			adler = new Adler32();
			
			window = new byte[2 * WSIZE];
			head   = new short[HASH_SIZE];
			prev   = new short[WSIZE];
			blockStart = strstart = 1;
		}
		/// <summary>
		/// Construct instance with pending buffer
		/// </summary>
		/// <param name="pending">
		/// Pending buffer to use
		/// </param>>
		public DeflaterEngine(DeflaterPending pending) 
		{
			this.pending = pending;
			huffman = new DeflaterHuffman(pending);
			adler = new Adler32();
			
			window = new byte[2 * WSIZE];
			head   = new short[HASH_SIZE];
			prev   = new short[WSIZE];
			
			// We start at index 1, to avoid an implementation deficiency, that
			// we cannot build a repeat pattern at index 0.
			blockStart = strstart = 1;
		}
Example #7
0
        private static void WriteDataChunksFast()
        {
            byte[] pixels = _image.ToByteArray();

            // Convert the pixel array to a new array for adding
            // the filter byte.
            // --------------------------------------------------
            byte[] data = new byte[_image.PixelWidth * _image.PixelHeight * 4 + _image.PixelHeight];

            int rowLength = _image.PixelWidth * 4 + 1;

            for (int y = 0; y < _image.PixelHeight; y++)
            {
                data[y * rowLength] = 0;

                Array.Copy(pixels, y * _image.PixelWidth * 4, data, y * rowLength + 1, _image.PixelWidth * 4);
            }
            // --------------------------------------------------

            Adler32 adler32 = new Adler32();
            adler32.Update(data);

            using (MemoryStream tempStream = new MemoryStream())
            {
                int remainder = data.Length;

                int blockCount;
                if ((data.Length % MaxBlockSize) == 0)
                {
                    blockCount = data.Length / MaxBlockSize;
                }
                else
                {
                    blockCount = (data.Length / MaxBlockSize) + 1;
                }

                // Write headers
                tempStream.WriteByte(0x78);
                tempStream.WriteByte(0xDA);

                for (int i = 0; i < blockCount; i++)
                {
                    // Write the length
                    ushort length = (ushort)((remainder < MaxBlockSize) ? remainder : MaxBlockSize);

                    if (length == remainder)
                    {
                        tempStream.WriteByte(0x01);
                    }
                    else
                    {
                        tempStream.WriteByte(0x00);
                    }

                    tempStream.Write(BitConverter.GetBytes(length), 0, 2);

                    // Write one's compliment of length
                    tempStream.Write(BitConverter.GetBytes((ushort)~length), 0, 2);

                    // Write blocks
                    tempStream.Write(data, (int)(i * MaxBlockSize), length);

                    // Next block
                    remainder -= MaxBlockSize;
                }

                WriteInteger(tempStream, (int)adler32.Value);

                tempStream.Seek(0, SeekOrigin.Begin);

                byte[] zipData = new byte[tempStream.Length];
                tempStream.Read(zipData, 0, (int)tempStream.Length);

                WriteChunk(PngChunkTypes.Data, zipData);
            }
        }
Example #8
0
        internal static void Run()
        {
            const int iterations = 0x20000;
            const int testSize = 0x10000;

            List<long> timeMurmur = new List<long>(iterations);
            List<long> timeAdler = new List<long>(iterations);
            List<long> timeMd5 = new List<long>(iterations);

            byte[] randBytes = new byte[testSize];
            var rnd = new Random();

            var watchMurmur = new Stopwatch();
            var watchAdler = new Stopwatch();
            var watchMd5 = new Stopwatch();

            bool dry = true;
            DoRun:
            for (int i = 0; i < iterations; i++)
            {
                rnd.NextBytes(randBytes);

                {
                    var murmur = Murmur128.CreateMurmur();
                    watchMurmur.Restart();
                    murmur.ComputeHash(randBytes);
                    watchMurmur.Stop();

                    if (!dry)
                        timeMurmur.Add(watchMurmur.ElapsedTicks);
                }

                {
                    var adler = new Adler32();
                    watchAdler.Restart();
                    adler.Update(randBytes);
                    watchAdler.Stop();

                    if (!dry)
                        timeAdler.Add(watchAdler.ElapsedTicks);
                }

                {
                    var md5 = MD5.Create();
                    watchMd5.Restart();
                    md5.ComputeHash(randBytes);
                    watchMd5.Stop();

                    if (!dry)
                        timeMd5.Add(watchMd5.ElapsedTicks);
                }

                if (i == 0x1000 && dry)
                {
                    dry = false;
                    goto DoRun;
                }
            }

            var avgMurmur = TimeSpan.FromTicks(Convert.ToInt64(timeMurmur.Average()));
            var avgAdler = TimeSpan.FromTicks(Convert.ToInt64(timeAdler.Average()));
            var avgMd5 = TimeSpan.FromTicks(Convert.ToInt64(timeMd5.Average()));

            Console.WriteLine("Average murmur:\t" + avgMurmur);
            Console.WriteLine("Average adler:\t" + avgAdler);
            Console.WriteLine("Average md5:\t" + avgMd5);
            Console.ReadKey();
        }