From 26d381d0d31e947f99f9c1dec0266f38b13422be Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Sun, 6 Sep 2015 01:07:00 +0300 Subject: [PATCH] Bugfix --- Novacoin/VarInt.cs | 27 +++++++++++++++------------ 1 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Novacoin/VarInt.cs b/Novacoin/VarInt.cs index 0f77b18..614e3c6 100644 --- a/Novacoin/VarInt.cs +++ b/Novacoin/VarInt.cs @@ -113,21 +113,24 @@ namespace Novacoin public static ulong 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: // ulong flag + return BitConverter.ToUInt64(bytesArray, 0); + } } + + return prefix; // Values lower than 0xfd are stored directly } public static ulong ReadVarInt(ref BinaryReader reader) -- 1.7.1