Turn ByteQueue into MemoryStream wrapper, use MemoryStream for serialization of COutP...
[NovacoinLibrary.git] / Novacoin / VarInt.cs
index edca418..0f77b18 100644 (file)
@@ -18,7 +18,7 @@
 
 using System;
 using System.Collections.Generic;
-using System.Linq;
+using System.IO;
 
 namespace Novacoin
 {
@@ -31,9 +31,9 @@ namespace Novacoin
         /// </summary>
         /// <param name="n">Unsigned integer value</param>
         /// <returns>Byte sequence</returns>
-        public static IList<byte> EncodeVarInt(ulong n)
+        public static byte[] EncodeVarInt(ulong n)
         {
-            List<byte> resultBytes = new List<byte>();
+            var resultBytes = new List<byte>();
 
             if (n <= 0xfc)
             {
@@ -68,7 +68,7 @@ namespace Novacoin
                 resultBytes.AddRange(valueBytes);
             }
 
-            return resultBytes;
+            return resultBytes.ToArray();
         }
 
         /// <summary>
@@ -78,11 +78,31 @@ namespace Novacoin
         /// </summary>
         /// <param name="n">Integer value</param>
         /// <returns>Byte sequence</returns>
-        public static IList<byte> EncodeVarInt(long n)
+        public static byte[] EncodeVarInt(long n)
         {
             return EncodeVarInt((ulong)n);
         }
 
+        public static int GetEncodedSize(long n)
+        {
+            if (n <= 0xfc)
+            {
+                return 1;
+            }
+            else if (n <= ushort.MaxValue)
+            {
+                return 3;
+            }
+            else if (n <= uint.MaxValue)
+            {
+                return 5;
+            }
+            else
+            {
+                return 9;
+            }
+        }
+
         /// <summary>
         /// Decodes integer value from compact representation
         /// 
@@ -90,13 +110,12 @@ namespace Novacoin
         /// </summary>
         /// <param name="bytes">Byte sequence</param>
         /// <returns>Integer value</returns>
-        public static ulong DecodeVarInt(IList<byte> bytes)
+        public static ulong DecodeVarInt(byte[] bytes)
         {
-            byte prefix = bytes[0];
-
-            bytes.RemoveAt(0); // Remove prefix
+            var prefix = bytes[0];
+            var bytesArray = new byte[bytes.Length - 1];
 
-            byte[] bytesArray = bytes.ToArray();
+            bytes.CopyTo(bytesArray, 1);  // Get rid of prefix
 
             switch (prefix)
             {
@@ -110,5 +129,23 @@ namespace Novacoin
                     return prefix;
             }
         }
+
+        public static ulong ReadVarInt(ref BinaryReader reader)
+        {
+            byte prefix = reader.ReadByte();
+
+            switch (prefix)
+            {
+                case 0xfd: // ushort
+                    return reader.ReadUInt16();
+                case 0xfe: // uint
+                    return reader.ReadUInt32();
+                case 0xff: // ulong
+                    return reader.ReadUInt64();
+                default:
+                    return prefix;
+            }
+        }
+
     }
 }