Turn ByteQueue into MemoryStream wrapper, use MemoryStream for serialization of COutP...
[NovacoinLibrary.git] / Novacoin / VarInt.cs
index d376cae..0f77b18 100644 (file)
@@ -18,6 +18,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.IO;
 
 namespace Novacoin
 {
@@ -82,6 +83,26 @@ namespace Novacoin
             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
         /// 
@@ -108,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;
+            }
+        }
+
     }
 }