X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FVarInt.cs;h=0f77b18d89644a94049f425d90e7633864c8740c;hb=be9d844557911f95165d2c9875c4f5b2822cfc92;hp=5fa2685c2da6c279d28ee16e3c5853d36bd4d916;hpb=89cc5043b8bb4a587aec5fce6d448204d891e3f5;p=NovacoinLibrary.git
diff --git a/Novacoin/VarInt.cs b/Novacoin/VarInt.cs
index 5fa2685..0f77b18 100644
--- a/Novacoin/VarInt.cs
+++ b/Novacoin/VarInt.cs
@@ -1,8 +1,24 @@
-using System;
+/**
+ * Novacoin classes library
+ * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+
+using System;
using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.IO;
namespace Novacoin
{
@@ -15,9 +31,9 @@ namespace Novacoin
///
/// Unsigned integer value
/// Byte sequence
- public static IList EncodeVarInt(ulong n)
+ public static byte[] EncodeVarInt(ulong n)
{
- List resultBytes = new List();
+ var resultBytes = new List();
if (n <= 0xfc)
{
@@ -52,7 +68,7 @@ namespace Novacoin
resultBytes.AddRange(valueBytes);
}
- return resultBytes;
+ return resultBytes.ToArray();
}
///
@@ -62,11 +78,31 @@ namespace Novacoin
///
/// Integer value
/// Byte sequence
- public static IList 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;
+ }
+ }
+
///
/// Decodes integer value from compact representation
///
@@ -74,13 +110,12 @@ namespace Novacoin
///
/// Byte sequence
/// Integer value
- public static ulong DecodeVarInt(IList 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)
{
@@ -95,29 +130,22 @@ namespace Novacoin
}
}
- ///
- /// Read and decode variable integer from wrapped list object.
- ///
- /// Note: Should be used only if there is some variable integer data at current position. Otherwise you will get undefined behavior, so make sure that you know what you are doing.
- ///
- ///
- ///
- public static ulong ReadVarInt(ref WrappedList wBytes)
+ public static ulong ReadVarInt(ref BinaryReader reader)
{
- byte prefix = wBytes.GetItem();
+ byte prefix = reader.ReadByte();
switch (prefix)
{
case 0xfd: // ushort
- return BitConverter.ToUInt16(wBytes.GetItems(2), 0);
+ return reader.ReadUInt16();
case 0xfe: // uint
- return BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+ return reader.ReadUInt32();
case 0xff: // ulong
- return BitConverter.ToUInt64(wBytes.GetItems(8), 0);
+ return reader.ReadUInt64();
default:
return prefix;
}
-
}
+
}
}