X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FVarInt.cs;h=0f77b18d89644a94049f425d90e7633864c8740c;hb=be9d844557911f95165d2c9875c4f5b2822cfc92;hp=d376caea89da3b10d28d2db8bf5884efa55d42e1;hpb=a12e065afeedd315e80bd0b928f46dbdea762ec8;p=NovacoinLibrary.git diff --git a/Novacoin/VarInt.cs b/Novacoin/VarInt.cs index d376cae..0f77b18 100644 --- a/Novacoin/VarInt.cs +++ b/Novacoin/VarInt.cs @@ -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; + } + } + /// /// 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; + } + } + } }