Transaction script verification, unserealize exceptions
[NovacoinLibrary.git] / Novacoin / ByteQueue.cs
index a1ba14c..9752c02 100644 (file)
 
 using System;
 using System.Collections.Generic;
+using System.Diagnostics.Contracts;
 
 namespace Novacoin
 {
+    [Serializable]
     public class ByteQueueException : Exception
     {
         public ByteQueueException()
@@ -38,69 +40,122 @@ namespace Novacoin
         }
     }
 
+    /// <summary>
+    /// Stream of bytes.
+    /// 
+    /// TODO: rewrite using MemoryStream
+    /// </summary>
     public class ByteQueue
     {
-        private int Index;
-        private List<byte> Elements;
+        private int _Index;
+        private List<byte> _Elements;
 
-        public ByteQueue(IList<byte> List, int Start)
+        public ByteQueue(byte[] List, int Start)
         {
-            Elements = new List<byte>(List);
-            Index = Start;
+            _Elements = new List<byte>(List);
+            _Index = Start;
         }
 
-        public ByteQueue(IList<byte> List)
+        public ByteQueue(byte[] List)
         {
-            Elements = new List<byte>(List);
-            Index = 0;
+            _Elements = new List<byte>(List);
+            _Index = 0;
+        }
+
+        public ByteQueue(List<byte> List, int Start)
+        {
+            _Elements = new List<byte>(List);
+            _Index = Start;
+        }
+
+        public ByteQueue(List<byte> List)
+        {
+            _Elements = new List<byte>(List);
+            _Index = 0;
         }
 
         public byte Get()
         {
-            if (Elements.Count <= Index)
+            if (_Elements.Count <= _Index)
             {
                 throw new ByteQueueException("No elements left.");
             }
 
-            return Elements[Index++];
+            return _Elements[_Index++];
+        }
+
+        public bool TryGet(ref byte Element)
+        {
+            if (_Elements.Count <= _Index)
+            {
+                return false;
+            }
+
+            Element = _Elements[_Index++];
+
+            return true;
         }
 
         public byte GetCurrent()
         {
-            return Elements[Index];
+            return _Elements[_Index];
+        }
+
+        public byte[] Get(int nCount)
+        {
+            Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of elements.");
+
+            var result = _Elements.GetRange(_Index, nCount).ToArray();
+            _Index += nCount;
+
+            return result;
         }
 
-        public byte[] Get(int Count)
+        public bool TryGet(int nCount, ref byte[] Elements)
         {
-            if (Elements.Count - Index < Count)
+            if (Count - Index < nCount)
             {
-                throw new ByteQueueException("Unable to read requested amount of data.");
+                return false;
             }
 
-            var result = Elements.GetRange(Index, Count).ToArray();
-            Index += Count;
+            Elements = _Elements.GetRange(_Index, nCount).ToArray();
+            _Index += nCount;
+
+            return true;
+        }
+
+        public byte[] GetCurrent(int nCount)
+        {
+            Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of elements.");
+
+            var result = _Elements.GetRange(_Index, nCount).ToArray();
 
             return result;
         }
 
-        public byte[] GetCurrent(int Count)
+        public bool TryGetCurrent(int nCount, ref byte[] Elements)
         {
-            if (Elements.Count - Index < Count)
+            if (Count - Index < nCount)
             {
-                throw new ByteQueueException("Unable to read requested amount of data.");
+                return false;
             }
 
-            var result = Elements.GetRange(Index, Count).ToArray();
+            Elements = _Elements.GetRange(_Index, nCount).ToArray();
 
-            return result;
+            return true;
         }
 
         /// <summary>
         /// Current index value
         /// </summary>
-        public int CurrentIndex
+        public int Index
+        {
+            get { return _Index; }
+        }
+
+        public int Count
         {
-            get { return Index; }
+            get { return _Elements.Count; }
         }
 
         public ulong GetVarInt()