You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-16 15:16:33 +09:00
Read/Write variable sized Int32/Int64 bug fixed; very large numbers were decoded incorrectly
This commit is contained in:
@@ -329,9 +329,6 @@ namespace Lidgren.Network
|
|||||||
int num2 = 0;
|
int num2 = 0;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (num2 == 0x23)
|
|
||||||
throw new FormatException("Bad 7-bit encoded integer");
|
|
||||||
|
|
||||||
byte num3 = this.ReadByte();
|
byte num3 = this.ReadByte();
|
||||||
num1 |= (num3 & 0x7f) << num2;
|
num1 |= (num3 & 0x7f) << num2;
|
||||||
num2 += 7;
|
num2 += 7;
|
||||||
@@ -345,22 +342,8 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int ReadVariableInt32()
|
public int ReadVariableInt32()
|
||||||
{
|
{
|
||||||
int num1 = 0;
|
uint n = ReadVariableUInt32();
|
||||||
int num2 = 0;
|
return (int)(n >> 1) ^ -(int)(n & 1); // decode zigzag
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (num2 == 0x23)
|
|
||||||
throw new FormatException("Bad 7-bit encoded integer");
|
|
||||||
|
|
||||||
byte num3 = this.ReadByte();
|
|
||||||
num1 |= (num3 & 0x7f) << (num2 & 0x1f);
|
|
||||||
num2 += 7;
|
|
||||||
if ((num3 & 0x80) == 0)
|
|
||||||
{
|
|
||||||
int sign = (num1 << 31) >> 31;
|
|
||||||
return sign ^ (num1 >> 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -368,22 +351,8 @@ namespace Lidgren.Network
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public Int64 ReadVariableInt64()
|
public Int64 ReadVariableInt64()
|
||||||
{
|
{
|
||||||
Int64 num1 = 0;
|
UInt64 n = ReadVariableUInt64();
|
||||||
int num2 = 0;
|
return (Int64)(n >> 1) ^ -(long)(n & 1); // decode zigzag
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (num2 == 0x23)
|
|
||||||
throw new FormatException("Bad 7-bit encoded integer");
|
|
||||||
|
|
||||||
byte num3 = this.ReadByte();
|
|
||||||
num1 |= (Int64)((Int64)(num3 & 0x7f) << (num2 & 0x1f));
|
|
||||||
num2 += 7;
|
|
||||||
if ((num3 & 0x80) == 0)
|
|
||||||
{
|
|
||||||
Int64 sign = (num1 << 63) >> 63;
|
|
||||||
return sign ^ (num1 >> 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -396,8 +365,8 @@ namespace Lidgren.Network
|
|||||||
int num2 = 0;
|
int num2 = 0;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (num2 == 0x23)
|
//if (num2 == 0x23)
|
||||||
throw new FormatException("Bad 7-bit encoded integer");
|
// throw new FormatException("Bad 7-bit encoded integer");
|
||||||
|
|
||||||
byte num3 = this.ReadByte();
|
byte num3 = this.ReadByte();
|
||||||
num1 |= ((UInt64)num3 & 0x7f) << num2;
|
num1 |= ((UInt64)num3 & 0x7f) << num2;
|
||||||
@@ -511,6 +480,14 @@ namespace Lidgren.Network
|
|||||||
m_readPosition = ((m_readPosition + 7) >> 3) * 8;
|
m_readPosition = ((m_readPosition + 7) >> 3) * 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Pads data with enough bits to reach a full byte. Decreases cpu usage for subsequent byte writes.
|
||||||
|
/// </summary>
|
||||||
|
public void ReadPadBits()
|
||||||
|
{
|
||||||
|
m_readPosition = ((m_readPosition + 7) >> 3) * 8;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pads data with the specified number of bits.
|
/// Pads data with the specified number of bits.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -393,7 +393,7 @@ namespace Lidgren.Network
|
|||||||
//
|
//
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write Base128 encoded variable sized unsigned integer
|
/// Write Base128 encoded variable sized unsigned integer of up to 32 bits
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>number of bytes written</returns>
|
/// <returns>number of bytes written</returns>
|
||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
@@ -412,43 +412,27 @@ namespace Lidgren.Network
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write Base128 encoded variable sized signed integer
|
/// Write Base128 encoded variable sized signed integer of up to 32 bits
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>number of bytes written</returns>
|
/// <returns>number of bytes written</returns>
|
||||||
public int WriteVariableInt32(int value)
|
public int WriteVariableInt32(int value)
|
||||||
{
|
{
|
||||||
int retval = 1;
|
uint zigzag = (uint)(value << 1) ^ (uint)(value >> 31);
|
||||||
uint num1 = (uint)((value << 1) ^ (value >> 31));
|
return WriteVariableUInt32(zigzag);
|
||||||
while (num1 >= 0x80)
|
|
||||||
{
|
|
||||||
this.Write((byte)(num1 | 0x80));
|
|
||||||
num1 = num1 >> 7;
|
|
||||||
retval++;
|
|
||||||
}
|
|
||||||
this.Write((byte)num1);
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write Base128 encoded variable sized signed integer
|
/// Write Base128 encoded variable sized signed integer of up to 64 bits
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>number of bytes written</returns>
|
/// <returns>number of bytes written</returns>
|
||||||
public int WriteVariableInt64(Int64 value)
|
public int WriteVariableInt64(Int64 value)
|
||||||
{
|
{
|
||||||
int retval = 1;
|
ulong zigzag = (ulong)(value << 1) ^ (ulong)(value >> 63);
|
||||||
UInt64 num1 = (UInt64)((value << 1) ^ (value >> 31));
|
return WriteVariableUInt64(zigzag);
|
||||||
while (num1 >= 0x80)
|
|
||||||
{
|
|
||||||
this.Write((byte)(num1 | 0x80));
|
|
||||||
num1 = num1 >> 7;
|
|
||||||
retval++;
|
|
||||||
}
|
|
||||||
this.Write((byte)num1);
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write Base128 encoded variable sized unsigned integer
|
/// Write Base128 encoded variable sized unsigned integer of up to 64 bits
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>number of bytes written</returns>
|
/// <returns>number of bytes written</returns>
|
||||||
[CLSCompliant(false)]
|
[CLSCompliant(false)]
|
||||||
|
|||||||
@@ -9,6 +9,30 @@ namespace UnitTests
|
|||||||
{
|
{
|
||||||
public static class ReadWriteTests
|
public static class ReadWriteTests
|
||||||
{
|
{
|
||||||
|
public static string ToBinaryString(ulong value, int bits, bool includeSpaces)
|
||||||
|
{
|
||||||
|
int numSpaces = Math.Max(0, (bits / 8) - 1);
|
||||||
|
if (includeSpaces == false)
|
||||||
|
numSpaces = 0;
|
||||||
|
|
||||||
|
StringBuilder bdr = new StringBuilder(bits + numSpaces);
|
||||||
|
for (int i = 0; i < bits + numSpaces; i++)
|
||||||
|
bdr.Append(' ');
|
||||||
|
|
||||||
|
for (int i = 0; i < bits; i++)
|
||||||
|
{
|
||||||
|
ulong shifted = (ulong)(value >> i);
|
||||||
|
bool isSet = ((shifted & 1) != 0);
|
||||||
|
|
||||||
|
int pos = bits - 1 - i;
|
||||||
|
if (includeSpaces)
|
||||||
|
pos += Math.Max(0, (pos / 8));
|
||||||
|
|
||||||
|
bdr[pos] = (isSet ? '1' : '0');
|
||||||
|
}
|
||||||
|
return bdr.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
public static void Run(NetPeer peer)
|
public static void Run(NetPeer peer)
|
||||||
{
|
{
|
||||||
NetOutgoingMessage msg = peer.CreateMessage();
|
NetOutgoingMessage msg = peer.CreateMessage();
|
||||||
@@ -26,6 +50,7 @@ namespace UnitTests
|
|||||||
int bcnt = 0;
|
int bcnt = 0;
|
||||||
|
|
||||||
msg.Write(45.0f);
|
msg.Write(45.0f);
|
||||||
|
msg.WriteVariableInt32(2115998022);
|
||||||
msg.Write(46.0);
|
msg.Write(46.0);
|
||||||
bcnt += msg.WriteVariableInt32(-47);
|
bcnt += msg.WriteVariableInt32(-47);
|
||||||
msg.WriteVariableInt32(470000);
|
msg.WriteVariableInt32(470000);
|
||||||
@@ -56,13 +81,14 @@ namespace UnitTests
|
|||||||
inc.SkipPadBits();
|
inc.SkipPadBits();
|
||||||
|
|
||||||
bdr.Append(inc.ReadSingle());
|
bdr.Append(inc.ReadSingle());
|
||||||
|
bdr.Append(inc.ReadVariableInt32());
|
||||||
bdr.Append(inc.ReadDouble());
|
bdr.Append(inc.ReadDouble());
|
||||||
bdr.Append(inc.ReadVariableInt32());
|
bdr.Append(inc.ReadVariableInt32());
|
||||||
bdr.Append(inc.ReadVariableInt32());
|
bdr.Append(inc.ReadVariableInt32());
|
||||||
bdr.Append(inc.ReadVariableUInt32());
|
bdr.Append(inc.ReadVariableUInt32());
|
||||||
bdr.Append(inc.ReadVariableInt64());
|
bdr.Append(inc.ReadVariableInt64());
|
||||||
|
|
||||||
if (bdr.ToString().Equals("False-342duke of earl4344True4546-4747000048-49"))
|
if (bdr.ToString().Equals("False-342duke of earl4344True45211599802246-4747000048-49"))
|
||||||
Console.WriteLine("Read/write tests OK");
|
Console.WriteLine("Read/write tests OK");
|
||||||
else
|
else
|
||||||
throw new NetException("Read/write tests FAILED!");
|
throw new NetException("Read/write tests FAILED!");
|
||||||
|
|||||||
Reference in New Issue
Block a user