Ejemplo n.º 1
0
 public static void WriteCommonHeader(Signature signature)
 {
     // Writing the initial header
     signature.Output.Write(RDiffBinary.SIGNATURE_MAGIC, 0, RDiffBinary.SIGNATURE_MAGIC.Length);
     signature.Output.Write(RDiffBinary.FixEndian(BitConverter.GetBytes(signature.BlockLength)), 0, 4);
     signature.Output.Write(RDiffBinary.FixEndian(BitConverter.GetBytes(signature.StrongLength)), 0, 4);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a chunck of data to checksum list
        /// </summary>
        /// <param name="buffer">The data to add a checksum entry for</param>
        /// <param name="index">The index in the buffer to start reading from</param>
        /// <param name="count">The number of bytes to extract from the array</param>
        private static void AddChunk(Signature signature, byte[] buffer, int index, int count)
        {
            //Add weak checksum (Adler-32) to the chunk
            signature.Output.Write(RDiffBinary.FixEndian(BitConverter.GetBytes(Adler32Checksum.Calculate(buffer, index, count))), 0, 4);

            //Add strong checksum
            signature.Output.Write(Utility.Hash.ComputeHash(buffer, index, count), 0, signature.StrongLength);
        }
Ejemplo n.º 3
0
        private int ReadStrongLength(Stream input, byte[] buffer)
        {
            int response;

            Utility.ReadChunk(input, buffer);

            response = BitConverter.ToInt32(RDiffBinary.FixEndian(buffer), 0);

            if (response < 1 || response > (Utility.Hash.HashSize / 8))
            {
                throw new Exception(string.Format(Strings.ChecksumFile.InvalidStrongsizeError, response));
            }

            return(response);
        }
Ejemplo n.º 4
0
        private int ReadBlockLength(Stream input, byte[] buffer)
        {
            int response;

            // Reading block lenght (stram must be in the right position
            Utility.ReadChunk(input, buffer);

            // Getting the block length
            response = BitConverter.ToInt32(RDiffBinary.FixEndian(buffer), 0);

            // Validating the block length
            if (response < 1 || response > int.MaxValue / 2)
            {
                throw new Exception(string.Format(Strings.ChecksumFile.InvalidBlocksizeError, response));
            }

            return(response);
        }
Ejemplo n.º 5
0
        public ChecksumGenerator(Stream inputFileStream)
        {
            byte[] tempBuffer;
            int    byteCounter;

            _signatureByteList = new List <byte>();
            _signatureByteList.AddRange(RDiffBinary.SIGNATURE_MAGIC);
            _signatureByteList.AddRange(RDiffBinary.FixEndian(BitConverter.GetBytes(DEFAULT_BLOCK_SIZE)));
            _signatureByteList.AddRange(RDiffBinary.FixEndian(BitConverter.GetBytes(DEFAULT_STRONG_LEN)));

            tempBuffer = new byte[DEFAULT_BLOCK_SIZE];

            while ((byteCounter = inputFileStream.Read(tempBuffer, 0, tempBuffer.Length)) != 0)
            {
                AddSignatureChunk(tempBuffer, 0, byteCounter);
            }

            _signature = _signatureByteList.ToArray();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Adds a chunck of data to checksum list
        /// </summary>
        /// <param name="buffer">The data to add a checksum entry for</param>
        /// <param name="index">The index in the buffer to start reading from</param>
        /// <param name="count">The number of bytes to extract from the array</param>
        private void AddSignatureChunk(byte[] buffer, int index, int count)
        {
            byte[] tempBuffer;

            //if (!hashAlgorithm.CanReuseTransform)
            //{
            //    hashAlgorithm = MD5.Create();
            //}

            _signatureByteList.AddRange(
                RDiffBinary.FixEndian(
                    BitConverter.GetBytes(
                        Adler32Checksum.Calculate(buffer, index, count))));

            //Add first half of the computed hash
            tempBuffer = Utility.Hash.ComputeHash(buffer, index, count);

            for (int i = 0; i < DEFAULT_STRONG_LEN; i++)
            {
                _signatureByteList.Add(tempBuffer[i]);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Reads a ChecksumFile from a stream
        /// </summary>
        /// <param name="input">The stream to read from</param>
        public ChecksumFileReader(Stream input)
        {
            #region Variables

            byte[]       readBuffer;
            int          strongLength;
            long         chunkCount;
            uint         weak;
            byte[]       strongBuffer;
            List <ulong> tempStrongIndex;

            #endregion


            readBuffer = new byte[4];

            Utility.ValidateSignature(input, readBuffer, RDiffBinary.SIGNATURE_MAGIC);

            // Saving the block length for future reference
            _checksumBlockLenght = ReadBlockLength(input, readBuffer);

            /// The number of bytes used for storing a single strong signature
            strongLength = ReadBlockLength(input, readBuffer);

            //Prepare the data structures
            _weakLookup     = new HashSet <uint>(); //new bool[ushort.MaxValue + 1]; //bool[0x10000];
            _longsPerStrong = (strongLength + (BYTES_PER_LONG - 1)) / BYTES_PER_LONG;
            strongBuffer    = new byte[_longsPerStrong * BYTES_PER_LONG];

            //We would like to use static allocation for these lists, but unfortunately
            // the zip stream does not report the correct length
            _weakKeysIndex  = new Dictionary <uint, List <long> >();
            tempStrongIndex = new List <ulong>();

            chunkCount = 0;

            //Repeat until the stream is exhausted
            while (Utility.ForceStreamRead(input, readBuffer, 4) == 4)
            {
                weak = BitConverter.ToUInt32(RDiffBinary.FixEndian(readBuffer), 0);

                if (Utility.ForceStreamRead(input, strongBuffer, strongLength) != strongLength)
                {
                    throw new Exception(Strings.ChecksumFile.EndofstreamInStrongSignatureError);
                }

                // Ensure key existance
                if (!_weakKeysIndex.ContainsKey(weak))
                {
                    _weakKeysIndex.Add(weak, new List <long>());
                }

                //Record the entries
                _weakKeysIndex[weak].Add(chunkCount);

                for (int i = 0; i < _longsPerStrong; i++)
                {
                    tempStrongIndex.Add(BitConverter.ToUInt64(strongBuffer, i * BYTES_PER_LONG));
                }

                chunkCount++;
            }

            // Storing the indexes in permanent arrays
            _strongIndex = tempStrongIndex.ToArray();
        }