private int ReadInBlockMode(byte[] buffer, int offset, int size)
        {
            while (this.dataSizeForRead <= 0)
            {
                if (!base.ReadHeader())
                {
                    return(0);
                }
                for (int i = 0; i < (int)this.dataSizeForRead; i += base.Read(this.dataBufferForRead, i, (int)this.dataSizeForRead - i))
                {
                }
                for (int j = 0; j < (int)this.tokenSizeForRead; j += base.Read(this.tokenBufferForRead, j, (int)this.tokenSizeForRead - j))
                {
                }
                this.securityBuffers[0].offset = 0;
                this.securityBuffers[0].size   = (int)this.dataSizeForRead;
                this.securityBuffers[0].token  = this.dataBufferForRead;
                this.securityBuffers[0].type   = 1;
                this.securityBuffers[1].offset = 0;
                this.securityBuffers[1].size   = (int)this.tokenSizeForRead;
                this.securityBuffers[1].token  = this.tokenBufferForRead;
                this.securityBuffers[1].type   = 2;
                int num = SSPIWrapper.DecryptMessage(ref this.securityContext.Handle, this.securityBuffers, ++this.sequenceNumberForRead);
                if (num != 0)
                {
                    throw new XmlaStreamException(new Win32Exception(num).Message);
                }
                SecurityBufferClass securityBufferClass = this.securityBuffers[0];
                if (securityBufferClass.offset + securityBufferClass.size > 65535)
                {
                    throw new XmlaStreamException(XmlaSR.InternalError);
                }
                this.dataOffsetForRead = (ushort)securityBufferClass.offset;
                this.dataSizeForRead   = (ushort)securityBufferClass.size;
            }
            ushort num2 = 0;
            int    num3 = (int)this.dataOffsetForRead;
            int    num4 = (int)(this.dataOffsetForRead + this.dataSizeForRead);
            int    num5 = offset;
            int    num6 = offset + size;

            while (num3 < num4 && num5 < num6)
            {
                buffer[num5] = this.dataBufferForRead[num3];
                num3++;
                num5++;
                num2 += 1;
            }
            this.dataSizeForRead   -= num2;
            this.dataOffsetForRead += num2;
            return((int)num2);
        }
        private int ReadInStreamMode(byte[] buffer, int offset, int size)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(null);
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer can't be null!");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset can't be negative!");
            }
            if (size < 0)
            {
                throw new ArgumentOutOfRangeException("size can't be negative!");
            }
            if (size + offset > buffer.Length)
            {
                throw new ArgumentException(XmlaSR.InvalidArgument, "buffer is smaller than offset + size!");
            }
            if (this.streamDecryptedDataForRead.Count > 0)
            {
                return(this.ReturnAvailableDecryptedData(buffer, offset, size));
            }
            this.ReleaseEncryptedDataAccumulatorForReadFreeBuffers();
            int i = 0;

            for (int j = 0; j < this.streamEncryptedDataAccumulatorForRead.Count; j++)
            {
                i += this.streamEncryptedDataAccumulatorForRead[j].Count;
            }
            bool value;

            if (i < this.streamSizes.cbMaxMessage)
            {
                ArraySegment <byte> orCreateFreeEncryptedBuffer = this.GetOrCreateFreeEncryptedBuffer(this.streamSizes.cbMaxMessage);
                int num = base.Read(orCreateFreeEncryptedBuffer.Array, orCreateFreeEncryptedBuffer.Offset, orCreateFreeEncryptedBuffer.Count);
                if (num > 0)
                {
                    orCreateFreeEncryptedBuffer = new ArraySegment <byte>(orCreateFreeEncryptedBuffer.Array, orCreateFreeEncryptedBuffer.Offset, num);
                    this.streamEncryptedDataAccumulatorForRead.Add(orCreateFreeEncryptedBuffer);
                    i += num;
                }
                else
                {
                    this.streamEncryptedDataAccumulatorForReadFreeBuffers.Add(orCreateFreeEncryptedBuffer);
                }
                value = true;
                if (TcpStream.TRACESWITCH.TraceVerbose)
                {
                    StackTrace stackTrace = new StackTrace();
                    stackTrace.GetFrame(1).GetMethod();
                }
            }
            else
            {
                value = false;
                if (TcpStream.TRACESWITCH.TraceVerbose)
                {
                    StackTrace stackTrace2 = new StackTrace();
                    stackTrace2.GetFrame(1).GetMethod();
                }
            }
            while (i > 0)
            {
                TcpEncryptedStream.CleanSecurityBuffers(this.securityBuffers);
                byte[] array;
                if (this.contiguosEncryptedByteArrayCache != null && this.contiguosEncryptedByteArrayCache.Length >= i)
                {
                    array = this.contiguosEncryptedByteArrayCache;
                }
                else
                {
                    array = (this.contiguosEncryptedByteArrayCache = new byte[i]);
                }
                int k    = 0;
                int num2 = 0;
                while (k < this.streamEncryptedDataAccumulatorForRead.Count)
                {
                    ArraySegment <byte> arraySegment = this.streamEncryptedDataAccumulatorForRead[k];
                    Array.Copy(arraySegment.Array, arraySegment.Offset, array, num2, arraySegment.Count);
                    num2 += arraySegment.Count;
                    this.streamEncryptedDataAccumulatorForReadFreeBuffers.Add(new ArraySegment <byte>(arraySegment.Array));
                    k++;
                }
                this.streamEncryptedDataAccumulatorForRead.Clear();
                this.securityBuffers[0].token  = array;
                this.securityBuffers[0].offset = 0;
                this.securityBuffers[0].size   = i;
                this.securityBuffers[0].type   = 1;
                int num3 = SSPIWrapper.DecryptMessage(ref this.securityContext.Handle, this.securityBuffers, ++this.sequenceNumberForRead);
                if (num3 == -2146893032)
                {
                    if (TcpStream.TRACESWITCH.TraceVerbose)
                    {
                        StackTrace stackTrace3 = new StackTrace();
                        stackTrace3.GetFrame(1).GetMethod();
                    }
                    this.streamEncryptedDataAccumulatorForRead.Add(new ArraySegment <byte>(array, 0, i));
                    byte[] array2 = this.contiguosEncryptedByteArrayCache = null;
                }
                else
                {
                    if (num3 != 0)
                    {
                        throw new XmlaStreamException(new StringBuilder().Append("readFromTcp=").Append(value).Append("; ").Append("encryptedDataSz=").Append(i).ToString(), new Win32Exception(num3));
                    }
                    int num4 = this.ExtractDecryptedAndExtraData(this.securityBuffers, buffer, offset, size);
                    if (num4 > 0)
                    {
                        return(num4);
                    }
                }
                ArraySegment <byte> orCreateFreeEncryptedBuffer2 = this.GetOrCreateFreeEncryptedBuffer(this.streamSizes.cbMaxMessage);
                int num5 = base.Read(orCreateFreeEncryptedBuffer2.Array, orCreateFreeEncryptedBuffer2.Offset, orCreateFreeEncryptedBuffer2.Count);
                if (num5 > 0)
                {
                    orCreateFreeEncryptedBuffer2 = new ArraySegment <byte>(orCreateFreeEncryptedBuffer2.Array, orCreateFreeEncryptedBuffer2.Offset, num5);
                    this.streamEncryptedDataAccumulatorForRead.Add(orCreateFreeEncryptedBuffer2);
                }
                else
                {
                    this.streamEncryptedDataAccumulatorForReadFreeBuffers.Add(orCreateFreeEncryptedBuffer2);
                    if (num3 == -2146893032)
                    {
                        throw new XmlaStreamException("Not complete Encrypted stream received from underlying layer of type " + base.GetType().Name + "! DecryptMessage returned SEC_E_INCOMPLETE_MESSAGE while underlying stream reported all data was read.");
                    }
                }
                i = 0;
                for (int l = 0; l < this.streamEncryptedDataAccumulatorForRead.Count; l++)
                {
                    i += this.streamEncryptedDataAccumulatorForRead[l].Count;
                }
                if (TcpStream.TRACESWITCH.TraceVerbose)
                {
                    StackTrace stackTrace4 = new StackTrace();
                    stackTrace4.GetFrame(1).GetMethod();
                }
            }
            return(0);
        }