Example #1
0
        /// <summary>
        /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
        /// </summary>
        /// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
        /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source. </param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream.</param>
        /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
        public override int Read(byte[] buffer, int offset, int count)
        {
            ValidateSize(count);

            //get the current sector
            long currentSector = CurrentSector;

            //read the sector from the base stream
            int ret = base.Read(_tempBuffer, 0, count);

            if (ret == 0)
            {
                return(0);
            }

            if (_decryptor == null)
            {
                _decryptor = _xts.CreateDecryptor();
            }

            //decrypt the sector
            int retV = _decryptor.TransformBlock(_tempBuffer, 0, ret, buffer, offset, (ulong)currentSector);

            //Console.WriteLine("Decrypting sector {0} == {1} bytes", currentSector, retV);

            return(retV);
        }