Exemple #1
0
        public void ItDoesLookahead(string input, int count, bool eHas, char eChar)
        {
            var result = new LookAheadStream <char>(input).LookAhead(count);

            Assert.AreEqual(eHas, result.HasCharactersUpTo);
            Assert.AreEqual(eChar, result.Value);
        }
Exemple #2
0
 private void Dispose(bool disposing)
 {
     if (_disposed)
     {
         return;
     }
     if (disposing)
     {
         if (_inputStream != null)
         {
             _inputStream.Dispose();
             _inputStream = null;
         }
         if (_hmacBufferStream != null)
         {
             _hmacBufferStream.Dispose();
             _hmacBufferStream = null;
         }
         if (_hmacStream != null)
         {
             _hmacStream.Dispose();
             _hmacStream = null;
         }
         _disposed = true;
     }
 }
 public void ItEnumeratesAfterLookAhead(string @in, int lookTo)
 {
     var lookAhead = new LookAheadStream<char>(@in);
     lookAhead.LookAhead(lookTo);
     var enumeratedLookAhead = lookAhead.ToArray();
     Assert.That(enumeratedLookAhead.SequenceEqual(@in));
 }
Exemple #4
0
        private void DisposeInternal()
        {
            if (_outStream != null)
            {
                if (!_hasFinalFlushed)
                {
                    FinalFlush();
                }

                _outStream.Dispose();
                _outStream = null;
            }

            if (_inStream != null)
            {
                _inStream.Dispose();
                _inStream = null;
            }

            if (_transform != null)
            {
                _transform.Dispose();
                _transform = null;
            }
        }
Exemple #5
0
        public override CryptoStreamBase Initialize(Stream stream, ICryptoTransform transform, CryptoStreamMode mode)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (transform == null)
            {
                throw new ArgumentNullException("transform");
            }
            if (transform.InputBlockSize != transform.OutputBlockSize)
            {
                throw new ArgumentException("The transform must have the same input and output block size.", "transform");
            }

            _transform = transform;

            if (mode == CryptoStreamMode.Read)
            {
                _inStream = new LookAheadStream(stream);
            }
            else
            {
                _outStream = stream;
            }

            _blockBuffer = new ByteBuffer(new byte[_transform.InputBlockSize]);
            _blockBuffer.AvailableForRead = 0;

            return(this);
        }
Exemple #6
0
 /// <summary>
 /// Implement an AxCryptReader based on a Stream.
 /// </summary>
 /// <param name="inputStream">The stream. Will be disposed when this instance is disposed.</param>
 protected AxCryptReaderBase(LookAheadStream inputStream)
     : this()
 {
     if (inputStream == null)
     {
         throw new ArgumentNullException("inputStream");
     }
     InputStream = inputStream;
 }
Exemple #7
0
        public AxCryptReader CreateReader(LookAheadStream inputStream)
        {
            IList <HeaderBlock> headers = LoadUnversionedHeaders(inputStream);
            AxCryptReader       reader  = CreateVersionedReader(inputStream, headers);

            reader.Reinterpret(headers, HeaderBlocks);

            return(reader);
        }
Exemple #8
0
        public void ItEnumeratesAfterLookAhead(string @in, int lookTo)
        {
            var lookAhead = new LookAheadStream <char>(@in);

            lookAhead.LookAhead(lookTo);
            var enumeratedLookAhead = lookAhead.ToArray();

            Assert.That(enumeratedLookAhead.SequenceEqual(@in));
        }
 public void ATotallyCrazyLookAheadDoesNotHurtTraversal(string @in)
 {
     var rand = new Random();
     var lookAhead = new LookAheadStream<char>(@in);
     foreach (char t in @in)
     {
         lookAhead.LookAhead(rand.Next(10));
         Assert.IsTrue(lookAhead.MoveNext());
         Assert.AreEqual(t, lookAhead.Current);
     }
 }
Exemple #10
0
        public void ATotallyCrazyLookAheadDoesNotHurtTraversal(string @in)
        {
            var rand      = new Random();
            var lookAhead = new LookAheadStream <char>(@in);

            foreach (char t in @in)
            {
                lookAhead.LookAhead(rand.Next(10));
                Assert.IsTrue(lookAhead.MoveNext());
                Assert.AreEqual(t, lookAhead.Current);
            }
        }
Exemple #11
0
        /// <summary>
        /// Implement an AxCryptReader based on a Stream.
        /// </summary>
        /// <param name="inputStream">The stream. Will be disposed when this instance is disposed.</param>
        public AxCryptStreamReader(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            LookAheadStream lookAheadStream = inputStream as LookAheadStream;

            if (lookAheadStream == null)
            {
                lookAheadStream = new LookAheadStream(inputStream);
            }
            SetInputStream(lookAheadStream);
        }
Exemple #12
0
 public void ItDiesOnIllegalStatements(string code)
 {
     var llstream = new LookAheadStream<Token>(new Lexer(code.ToStream()).Lex());
     var parser = new DaisyParser(llstream);
     try
     {
         parser.Parse();
     }
     catch (Exception)
     {
         //throw;
         return;
     }
     Assert.Fail("Expected " + code + " to not parse");
 }
Exemple #13
0
        public void ItParsesLanguages(string code, string expectedTree)
        {
            var llstream = new LookAheadStream <Token>(new Lexer(code.ToStream()).Lex());
            var parser   = new DaisyParser(llstream);
            var tree     = parser.Parse();

            Assert.IsNotNull(tree);
            var actualTree = DaisyAstPrinter.Print(tree.Root);

            if (expectedTree != actualTree)
            {
                Console.WriteLine(expectedTree);
                Console.WriteLine("----------------");
                Console.WriteLine(actualTree);
            }
            Assert.AreEqual(expectedTree, actualTree);
        }
Exemple #14
0
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                if (_inputStream != null)
                {
                    _inputStream.Dispose();
                    _inputStream = null;
                }
            }

            _disposed = true;
        }
Exemple #15
0
        private static AxCryptReader CreateVersionedReader(LookAheadStream inputStream, IList <HeaderBlock> headers)
        {
            AxCryptReader      reader;
            VersionHeaderBlock versionHeaderBlock = FindHeaderBlock <VersionHeaderBlock>(headers);

            switch (versionHeaderBlock.FileVersionMajor)
            {
            case 1:
            case 2:
            case 3:
                reader = new V1AxCryptReader(inputStream);
                break;

            case 4:
                reader = new V2AxCryptReader(inputStream);
                break;

            default:
                throw new FileFormatException("Too new file format. You need a more recent version.");
            }
            return(reader);
        }
 public MineDefineParser(IEnumerable<LexToken> tokens)
 {
     _tokens = new LookAheadStream<LexToken>(tokens);
 }
Exemple #17
0
 public void ItParsesLanguages(string code, string expectedTree)
 {
     var llstream = new LookAheadStream<Token>(new Lexer(code.ToStream()).Lex());
     var parser = new DaisyParser(llstream);
     var tree = parser.Parse();
     Assert.IsNotNull(tree);
     var actualTree = DaisyAstPrinter.Print(tree.Root);
     if(expectedTree != actualTree)
     {
         Console.WriteLine(expectedTree);
         Console.WriteLine("----------------");
         Console.WriteLine(actualTree);
     }
     Assert.AreEqual(expectedTree, actualTree);
 }
 public void ItDoesLookahead(string input, int count, bool eHas, char eChar)
 {
     var result =  new LookAheadStream<char>(input).LookAhead(count);
     Assert.AreEqual(eHas, result.HasCharactersUpTo);
     Assert.AreEqual(eChar, result.Value);
 }
Exemple #19
0
        private static IList <HeaderBlock> LoadUnversionedHeaders(LookAheadStream inputStream)
        {
            UnversionedAxCryptReader vxReader = new UnversionedAxCryptReader(inputStream);

            return(LoadFromReader(vxReader));
        }
Exemple #20
0
 protected void SetInputStream(LookAheadStream inputStream)
 {
     _inputStream = inputStream;
 }
Exemple #21
0
 public FormatIntegrityChecker(Stream inputStream, string fileName)
 {
     _inputStream = new LookAheadStream(inputStream ?? throw new ArgumentNullException(nameof(inputStream), "inputStream"));
     _fileName    = fileName ?? throw new ArgumentNullException(nameof(fileName), "fileName");
 }
Exemple #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnversionedAxCryptReader"/> class.
 /// </summary>
 /// <param name="inputStream">The stream. Will NOT be disposed when this instance is disposed.</param>
 public UnversionedAxCryptReader(LookAheadStream inputStream)
     : base(inputStream)
 {
 }
Exemple #23
0
        public static void TestLookAheadStream()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                using (Stream stream = new LookAheadStream(null)) { }
            }, "Input stream cannot be null.");

            Assert.Throws <ArgumentException>(() =>
            {
                using (Stream writeOnlyStream = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
                {
                    using (Stream stream = new LookAheadStream(writeOnlyStream)) { }
                }
            }, "The input stream must support reading.");

            using (Stream inputStream = new MemoryStream())
            {
                byte[] inData = Encoding.UTF8.GetBytes("0123456789");
                inputStream.Write(inData, 0, inData.Length);
                inputStream.Position = 0;
                using (LookAheadStream lookAheadStream = new LookAheadStream(inputStream))
                {
                    Assert.That(lookAheadStream.CanRead, Is.True, "The stream always supports reading.");
                    Assert.That(lookAheadStream.CanSeek, Is.False, "The stream does not support seeking.");
                    Assert.That(lookAheadStream.CanWrite, Is.False, "The stream does not support writing.");
                    Assert.Throws <NotSupportedException>(() =>
                    {
                        long length = lookAheadStream.Length;

                        // Make FxCop not complain
                        Object.Equals(length, null);
                    });
                    Assert.Throws <NotSupportedException>(() =>
                    {
                        long position = lookAheadStream.Position;

                        // Make FxCop not complain
                        Object.Equals(position, null);
                    });
                    Assert.Throws <NotSupportedException>(() =>
                    {
                        lookAheadStream.Position = 0;
                    });
                    Assert.Throws <NotSupportedException>(() =>
                    {
                        lookAheadStream.Seek(0, SeekOrigin.Begin);
                    });
                    Assert.Throws <NotSupportedException>(() =>
                    {
                        lookAheadStream.SetLength(0);
                    });
                    Assert.Throws <NotSupportedException>(() =>
                    {
                        lookAheadStream.Write(new byte[1], 0, 1);
                    });

                    int    count;
                    byte[] buffer;

                    lookAheadStream.Pushback(new byte[] { 0x99 }, 0, 1);
                    buffer = new byte[1];
                    count  = lookAheadStream.Read(buffer, 0, 1);
                    Assert.That(count, Is.EqualTo(1), "One byte was read.");
                    Assert.That(buffer[0], Is.EqualTo(0x99), "A byte with value 0x99 was pushed back.");

                    buffer = new byte[5];
                    count  = lookAheadStream.Read(buffer, 0, buffer.Length);
                    Assert.That(count, Is.EqualTo(5), "Five bytes were read.");
                    Assert.That(buffer, Is.EquivalentTo(new byte[] { 48, 49, 50, 51, 52 }), "The string '01234' was read.");

                    lookAheadStream.Pushback(new byte[] { 52 }, 0, 1);
                    lookAheadStream.Pushback(new byte[] { 51 }, 0, 1);
                    lookAheadStream.Pushback(new byte[] { 48, 49, 50 }, 0, 3);

                    // Nothing should happen, this is a pure dummy call.
                    lookAheadStream.Flush();

                    buffer = new byte[10];
                    bool exactWasRead = lookAheadStream.ReadExact(buffer);
                    Assert.That(exactWasRead, Is.True, "Ten bytes were read.");
                    Assert.That(buffer, Is.EquivalentTo(new byte[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 }), "The string '0123456789' was read.");

                    // This also implicitly tests double-dispose since we're in a using block
                    lookAheadStream.Dispose();
                    Assert.Throws <ObjectDisposedException>(() =>
                    {
                        buffer = new byte[3];
                        lookAheadStream.Read(buffer, 0, 3);
                    });

                    Assert.Throws <ObjectDisposedException>(() =>
                    {
                        buffer = new byte[5];
                        lookAheadStream.ReadExact(buffer);
                    });

                    Assert.Throws <ObjectDisposedException>(() =>
                    {
                        buffer = new byte[5];
                        lookAheadStream.Pushback(buffer, 0, buffer.Length);
                    });
                }
            }
        }
Exemple #24
0
 public DaisyParser(LookAheadStream<Token> tokenStream)
 {
     this.tokenStream = tokenStream;
 }
Exemple #25
0
 /// <summary>
 /// Instantiate an AxCryptReader from a stream.
 /// </summary>
 /// <param name="inputStream">The stream to read from, will be disposed when this instance is disposed.</param>
 /// <returns></returns>
 public V2AxCryptReader(LookAheadStream inputStream)
     : base(inputStream)
 {
 }
 public AxCryptReaderForTest(LookAheadStream inputStream)
     : base(inputStream)
 {
 }
Exemple #27
0
 public static DaisyAst Parse(string code)
 {
     var llstream = new LookAheadStream<Token>(new Lexer(code.ToStream()).Lex());
     var parser = new DaisyParser(llstream);
     return parser.Parse();
 }
Exemple #28
0
 protected AxCryptReader(LookAheadStream inputStream)
     : base(inputStream)
 {
 }
 public TestingAxCryptReader(LookAheadStream inputStream)
     : base(inputStream)
 {
 }
        private static void ReadMultiPartFormData(Connection con, WebRequest request, string contentType, Stream bodyStream)
        {
            request.PostVars = new NameValueCollection();
            int boundaryOffset = contentType.IndexOf("boundary=");

            if (boundaryOffset < 0)
            {
                return;
            }
            string          boundary        = contentType.Substring(boundaryOffset + 9);
            LookAheadStream lookAheadStream = new LookAheadStream(bodyStream, 200);

            // Read and discard the preamble.
            MultiPartMIMEStream multiPartStream = new MultiPartMIMEStream(lookAheadStream, "--" + boundary);

            for (; ;)
            {
                byte[] discardBuffer = new byte[100];
                if (multiPartStream.Read(discardBuffer, 0, discardBuffer.Length) == 0)
                {
                    break;
                }
            }
            multiPartStream.SkipBoundary();

            // Read and save any following parts.
            for (; ;)
            {
                if (multiPartStream.LastPartRead || multiPartStream.TotalBytesRead == 0)
                {
                    break;
                }
                multiPartStream = new MultiPartMIMEStream(lookAheadStream, "\r\n--" + boundary);
                bool connectionBroken;
                NameValueCollection headers = ReadAllHeaders(multiPartStream, out connectionBroken);
                if (connectionBroken)
                {
                    con.ConnectionBroken = true;
                }
                using (MemoryStream memStream = new MemoryStream())
                {
                    using (BufferedStream partBody = new BufferedStream(memStream, 1024))
                    {
                        multiPartStream.CopyTo(partBody, 1024);
                        multiPartStream.SkipBoundary();
                        partBody.Flush();
                        partBody.Seek(0, SeekOrigin.Begin);
                        string contentDisposition = headers.Get(HttpHeaders.ContentDisposition);
                        if (contentDisposition != null)
                        {
                            // save uploaded files in data structures compatible with the way ASP.NET exposes them
                            string[] parts         = contentDisposition.Split(';');
                            bool     formDataFound = false;
                            string   name          = null;
                            string   filename      = null;
                            foreach (string part in parts)
                            {
                                string trimmedPart = part.Trim();
                                if (trimmedPart == "form-data")
                                {
                                    formDataFound = true;
                                }
                                else if (trimmedPart.StartsWith("name=\""))
                                {
                                    name = trimmedPart.Substring(6, trimmedPart.Length - 7);
                                }
                                else if (trimmedPart.StartsWith("filename=\""))
                                {
                                    filename = trimmedPart.Substring(10, trimmedPart.Length - 11);
                                }
                            }
                            if (!formDataFound)
                            {
                                throw new InvalidDataException();
                            }
                            if (filename != null)
                            {
                                AddNameValuePair(request.PostVars, name, filename);
                                partBody.Seek(0, SeekOrigin.Begin);
                                string fileContentType = headers.Get(HttpHeaders.ContentType);
                                if (string.IsNullOrEmpty(fileContentType))
                                {
                                    fileContentType = HttpHeaders.ContentType_TextPlain;
                                }
                                WebUploadedFile file = new WebUploadedFile(filename, partBody, partBody.Length, fileContentType);
                                request.UploadedFiles.Add(name, file);
                            }
                            else
                            {
                                string value;
                                bool   ignoreConnectionBroken;
                                Connection.ReadAsciiUntilEnd(partBody, out value, out ignoreConnectionBroken);
                                AddNameValuePair(request.PostVars, name, value);
                            }
                        }
                    }
                }
            }

            // Discard the epilogue
            for (; ;)
            {
                byte[] discardBuffer = new byte[100];
                if (lookAheadStream.Read(discardBuffer, 0, discardBuffer.Length) == 0)
                {
                    break;
                }
            }
        }