Remove legacy stake reward calculation code.
[NovacoinLibrary.git] / Novacoin / VarInt.cs
index 0f77b18..c3520ba 100644 (file)
@@ -59,7 +59,7 @@ namespace Novacoin
                 }
                 else
                 {
-                    // ulong flag
+                    // long flag
                     prefix = 0xff;
                     valueBytes = BitConverter.GetBytes(n);
                 }
@@ -83,7 +83,7 @@ namespace Novacoin
             return EncodeVarInt((ulong)n);
         }
 
-        public static int GetEncodedSize(long n)
+        public static uint GetEncodedSize(long n)
         {
             if (n <= 0xfc)
             {
@@ -110,27 +110,30 @@ namespace Novacoin
         /// </summary>
         /// <param name="bytes">Byte sequence</param>
         /// <returns>Integer value</returns>
-        public static ulong DecodeVarInt(byte[] bytes)
+        public static long DecodeVarInt(byte[] bytes)
         {
             var prefix = bytes[0];
-            var bytesArray = new byte[bytes.Length - 1];
 
-            bytes.CopyTo(bytesArray, 1);  // Get rid of prefix
-
-            switch (prefix)
+            if (bytes.Length > 1)
             {
-                case 0xfd: // ushort flag
-                    return BitConverter.ToUInt16(bytesArray, 0);
-                case 0xfe: // uint flag
-                    return BitConverter.ToUInt32(bytesArray, 0);
-                case 0xff: // ulong flag
-                    return BitConverter.ToUInt64(bytesArray, 0);
-                default:
-                    return prefix;
+                var bytesArray = new byte[bytes.Length - 1];
+                Array.Copy(bytes, 1, bytesArray, 0, bytesArray.Length);
+
+                switch (prefix)
+                {
+                    case 0xfd: // ushort flag
+                        return BitConverter.ToUInt16(bytesArray, 0);
+                    case 0xfe: // uint flag
+                        return BitConverter.ToUInt32(bytesArray, 0);
+                    case 0xff: // long flag
+                        return BitConverter.ToInt64(bytesArray, 0);
+                }
             }
+            
+            return prefix; // Values lower than 0xfd are stored directly
         }
 
-        public static ulong ReadVarInt(ref BinaryReader reader)
+        public static long ReadVarInt(ref BinaryReader reader)
         {
             byte prefix = reader.ReadByte();
 
@@ -140,8 +143,8 @@ namespace Novacoin
                     return reader.ReadUInt16();
                 case 0xfe: // uint
                     return reader.ReadUInt32();
-                case 0xff: // ulong
-                    return reader.ReadUInt64();
+                case 0xff: // long
+                    return reader.ReadInt64();
                 default:
                     return prefix;
             }