Beispiel #1
0
        public static uint ComputeBE(ulong value, uint adler32 = 1)
        {
            var bc = new BitComputer(adler32);

            bc.ComputeBE(value);
            return(bc.ComputeFinish());
        }
Beispiel #2
0
        public static uint Compute(System.IO.Stream stream, int length, uint adler32 = 1,
                                   bool restorePosition = false)
        {
            Contract.Requires <ArgumentNullException>(stream != null);
            Contract.Requires <ArgumentOutOfRangeException>(length >= 0);
            Contract.Requires <InvalidOperationException>(stream.CanRead);
            Contract.Requires(!restorePosition || stream.CanSeek);

            long prev_position = restorePosition
                                ? stream.Position
                                : -1;

            var computer = new BitComputer(adler32);

            int buffer_size = System.Math.Min(length, 1024);

            byte[] buffer = new byte[buffer_size];

            for (int bytes_remaining = length; bytes_remaining > 0;)
            {
                int num_bytes_to_read = System.Math.Min(bytes_remaining, buffer_size);
                int num_bytes_read    = 0;
                do
                {
                    int n = stream.Read(buffer, num_bytes_read, num_bytes_to_read);
                    if (n == 0)
                    {
                        break;
                    }

                    num_bytes_read    += n;
                    num_bytes_to_read -= n;
                } while (num_bytes_to_read > 0);

                if (num_bytes_read > 0)
                {
                    computer.Compute(buffer, 0, num_bytes_read);
                }
                else
                {
                    break;
                }

                bytes_remaining -= num_bytes_read;
            }

            adler32 = computer.ComputeFinish();

            if (prev_position != -1)
            {
                stream.Seek(prev_position, System.IO.SeekOrigin.Begin);
            }

            return(adler32);
        }
Beispiel #3
0
        public static uint Compute(byte[] buffer, int offset, int length, uint adler32 = 1)
        {
            Contract.Requires <ArgumentNullException>(buffer != null);
            Contract.Requires <ArgumentOutOfRangeException>(offset >= 0 && length >= 0);
            Contract.Requires <ArgumentOutOfRangeException>(offset + length <= buffer.Length);

            var computer = new BitComputer(adler32);

            computer.Compute(buffer, offset, length);
            adler32 = computer.ComputeFinish();
            return(adler32);
        }