From 89f5e037ff6378d2f03605f7f1b078d70e24e3a7 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Thu, 3 Nov 2016 00:14:20 +0800 Subject: [PATCH] fix bugs; --- src/AntShares/Core/RegisterTransaction.cs | 2 +- src/AntShares/IO/Helper.cs | 28 +++++++++---------- .../Network/Payloads/VersionPayload.cs | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/AntShares/Core/RegisterTransaction.cs b/src/AntShares/Core/RegisterTransaction.cs index 30baecd108..c92e326518 100644 --- a/src/AntShares/Core/RegisterTransaction.cs +++ b/src/AntShares/Core/RegisterTransaction.cs @@ -75,7 +75,7 @@ protected override void DeserializeExclusiveData(BinaryReader reader) AssetType = (AssetType)reader.ReadByte(); if (!Enum.IsDefined(typeof(AssetType), AssetType) || AssetType == AssetType.CreditFlag || AssetType == AssetType.DutyFlag) throw new FormatException(); - Name = reader.ReadVarString(); + Name = reader.ReadVarString(1024); Amount = reader.ReadSerializable(); if (Amount == Fixed8.Zero || Amount < -Fixed8.Satoshi) throw new FormatException(); if (AssetType == AssetType.Invoice && Amount != -Fixed8.Satoshi) diff --git a/src/AntShares/IO/Helper.cs b/src/AntShares/IO/Helper.cs index cce446cfeb..0a28c4949f 100644 --- a/src/AntShares/IO/Helper.cs +++ b/src/AntShares/IO/Helper.cs @@ -30,7 +30,7 @@ public static ISerializable AsSerializable(this byte[] value, Type type) return serializable; } - public static int GetVarSize(this int value) + internal static int GetVarSize(this int value) { if (value < 0xFD) return sizeof(byte); @@ -40,20 +40,20 @@ public static int GetVarSize(this int value) return sizeof(byte) + sizeof(uint); } - public static string ReadFixedString(this BinaryReader reader, int length) + internal static string ReadFixedString(this BinaryReader reader, int length) { byte[] data = reader.ReadBytes(length); return Encoding.UTF8.GetString(data.TakeWhile(p => p != 0).ToArray()); } - public static T ReadSerializable(this BinaryReader reader) where T : ISerializable, new() + internal static T ReadSerializable(this BinaryReader reader) where T : ISerializable, new() { T obj = new T(); obj.Deserialize(reader); return obj; } - public static T[] ReadSerializableArray(this BinaryReader reader, int max = 0x10000000) where T : ISerializable, new() + internal static T[] ReadSerializableArray(this BinaryReader reader, int max = 0x10000000) where T : ISerializable, new() { T[] array = new T[reader.ReadVarInt((ulong)max)]; for (int i = 0; i < array.Length; i++) @@ -64,12 +64,12 @@ public static string ReadFixedString(this BinaryReader reader, int length) return array; } - public static byte[] ReadVarBytes(this BinaryReader reader, int max = 0X7fffffc7) + internal static byte[] ReadVarBytes(this BinaryReader reader, int max = 0X7fffffc7) { return reader.ReadBytes((int)reader.ReadVarInt((ulong)max)); } - public static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxValue) + internal static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxValue) { byte fb = reader.ReadByte(); ulong value; @@ -85,9 +85,9 @@ public static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxVa return value; } - public static string ReadVarString(this BinaryReader reader) + internal static string ReadVarString(this BinaryReader reader, int max = 0X7fffffc7) { - return Encoding.UTF8.GetString(reader.ReadVarBytes()); + return Encoding.UTF8.GetString(reader.ReadVarBytes(max)); } public static byte[] ToArray(this ISerializable value) @@ -101,12 +101,12 @@ public static byte[] ToArray(this ISerializable value) } } - public static void Write(this BinaryWriter writer, ISerializable value) + internal static void Write(this BinaryWriter writer, ISerializable value) { value.Serialize(writer); } - public static void Write(this BinaryWriter writer, ISerializable[] value) + internal static void Write(this BinaryWriter writer, ISerializable[] value) { writer.WriteVarInt(value.Length); for (int i = 0; i < value.Length; i++) @@ -115,7 +115,7 @@ public static void Write(this BinaryWriter writer, ISerializable[] value) } } - public static void WriteFixedString(this BinaryWriter writer, string value, int length) + internal static void WriteFixedString(this BinaryWriter writer, string value, int length) { if (value == null) throw new ArgumentNullException(nameof(value)); @@ -129,13 +129,13 @@ public static void WriteFixedString(this BinaryWriter writer, string value, int writer.Write(new byte[length - bytes.Length]); } - public static void WriteVarBytes(this BinaryWriter writer, byte[] value) + internal static void WriteVarBytes(this BinaryWriter writer, byte[] value) { writer.WriteVarInt(value.Length); writer.Write(value); } - public static void WriteVarInt(this BinaryWriter writer, long value) + internal static void WriteVarInt(this BinaryWriter writer, long value) { if (value < 0) throw new ArgumentOutOfRangeException(); @@ -160,7 +160,7 @@ public static void WriteVarInt(this BinaryWriter writer, long value) } } - public static void WriteVarString(this BinaryWriter writer, string value) + internal static void WriteVarString(this BinaryWriter writer, string value) { writer.WriteVarBytes(Encoding.UTF8.GetBytes(value)); } diff --git a/src/AntShares/Network/Payloads/VersionPayload.cs b/src/AntShares/Network/Payloads/VersionPayload.cs index 3b06caeced..ab9b5c8961 100644 --- a/src/AntShares/Network/Payloads/VersionPayload.cs +++ b/src/AntShares/Network/Payloads/VersionPayload.cs @@ -41,7 +41,7 @@ void ISerializable.Deserialize(BinaryReader reader) Timestamp = reader.ReadUInt32(); Port = reader.ReadUInt16(); Nonce = reader.ReadUInt32(); - UserAgent = reader.ReadVarString(); + UserAgent = reader.ReadVarString(1024); StartHeight = reader.ReadUInt32(); Relay = reader.ReadBoolean(); }