Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlobReader"/> class.
        /// </summary>
        /// <param name="input">The input stream to process.</param>
        protected BlobReader(Stream input)
            : this()
        {
            if (!input.CanRead || !input.CanSeek)
            {
                throw new ArgumentException("Input stream must be readable and seekable.");
            }

            this.input  = new PeekableStream(input);
            this.length = input.Length;
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlobReader"/> class.
        /// </summary>
        /// <param name="input">The input stream to process.</param>
        protected BlobReader(Stream input)
            : this()
        {
            if ( !input.CanRead || !input.CanSeek )
                throw new ArgumentException( "Input stream must be readable and seekable." );

            this.input = new PeekableStream(input);
            this.length = input.Length;
        }
Exemple #3
0
        private bool TryReadBlob()
        {
            if (!CanRead(BlobHeaderLength))
                return false;

            ECacheState cachestate;
            EAutoPreprocessCode process;
            Int32 serialized, spare;

            Mark(BlobHeaderLength);

            try
            {
                cachestate = (ECacheState)input.ReadByte();
                process = (EAutoPreprocessCode)input.ReadByte();

                serialized = input.ReadInt32();
                spare = input.ReadInt32();
            }
            catch (Exception)
            {
                Reset();
                throw new InvalidDataException( "Corrupted blob" );
            }

            if (!BlobUtil.IsValidCacheState(cachestate) || !BlobUtil.IsValidProcess(process) ||
                    serialized < 0 || spare < 0 ||
                    !CanReadMarked(serialized))
            {
                Reset();
                return false;
            }

            serialized -= BlobHeaderLength;

            if (process != EAutoPreprocessCode.eAutoPreprocessCodePlaintext)
            {
                PeekableStream originalStream = input;
                object extendedData = null;

                input = HandleProcessCode(process, ref serialized, out extendedData);
                bool result = TryReadBlob();

                input.ReadBytes( 40 ); // read and dispose HMACs
                // todo: validate them?

                input.Close();
                input = originalStream;

                return result;
            }

            if(Blob != null)
                Blob(process, cachestate);

            ProcessFields(serialized);

            if (spare > 0)
            {
                byte[] spareData = new byte[spare];
                input.Read(spareData, 0, spare);

                if(Spare != null)
                    Spare(spareData);
            }

            if(EndBlob != null)
                EndBlob();

            return true;
        }
Exemple #4
0
        private bool TryReadBlob(UInt32 lengthHint)
        {
            if (!CanRead(BlobHeaderLength) || lengthHint < BlobHeaderLength)
            {
                return(false);
            }

            ECacheState         cachestate;
            EAutoPreprocessCode process;
            Int32 serialized, spare;

            Mark(BlobHeaderLength);

            try
            {
                cachestate = (ECacheState)input.ReadByte();
                process    = (EAutoPreprocessCode)input.ReadByte();

                serialized = input.ReadInt32();
                spare      = input.ReadInt32();
            }
            catch (Exception)
            {
                Reset();
                throw new InvalidDataException("Corrupted blob");
            }

            if (!BlobUtil.IsValidCacheState(cachestate) || !BlobUtil.IsValidProcess(process) ||
                serialized < 0 || spare < 0 ||
                !CanReadMarked(serialized))
            {
                Reset();
                return(false);
            }

            serialized -= BlobHeaderLength;


            if (process != EAutoPreprocessCode.eAutoPreprocessCodePlaintext)
            {
                PeekableStream originalStream = input;
                object         extendedData   = null;

                input = HandleProcessCode(process, ref serialized, out extendedData);

                bool result = TryReadBlob((uint)serialized);

                input.ReadBytes(40);   // read and dispose HMACs
                // todo: validate them?

                input.Close();
                input = originalStream;

                return(result);
            }

            if (Blob != null)
            {
                Blob(process, cachestate);
            }

            ProcessFields(serialized);

            if (spare > 0)
            {
                byte[] spareData = new byte[spare];
                input.Read(spareData, 0, spare);

                if (Spare != null)
                {
                    Spare(spareData);
                }
            }

            if (EndBlob != null)
            {
                EndBlob();
            }

            return(true);
        }