You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-06 02:11:06 +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;
|
||||
while (true)
|
||||
{
|
||||
if (num2 == 0x23)
|
||||
throw new FormatException("Bad 7-bit encoded integer");
|
||||
|
||||
byte num3 = this.ReadByte();
|
||||
num1 |= (num3 & 0x7f) << num2;
|
||||
num2 += 7;
|
||||
@@ -345,22 +342,8 @@ namespace Lidgren.Network
|
||||
/// </summary>
|
||||
public int ReadVariableInt32()
|
||||
{
|
||||
int num1 = 0;
|
||||
int num2 = 0;
|
||||
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);
|
||||
}
|
||||
}
|
||||
uint n = ReadVariableUInt32();
|
||||
return (int)(n >> 1) ^ -(int)(n & 1); // decode zigzag
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -368,22 +351,8 @@ namespace Lidgren.Network
|
||||
/// </summary>
|
||||
public Int64 ReadVariableInt64()
|
||||
{
|
||||
Int64 num1 = 0;
|
||||
int num2 = 0;
|
||||
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);
|
||||
}
|
||||
}
|
||||
UInt64 n = ReadVariableUInt64();
|
||||
return (Int64)(n >> 1) ^ -(long)(n & 1); // decode zigzag
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -396,8 +365,8 @@ namespace Lidgren.Network
|
||||
int num2 = 0;
|
||||
while (true)
|
||||
{
|
||||
if (num2 == 0x23)
|
||||
throw new FormatException("Bad 7-bit encoded integer");
|
||||
//if (num2 == 0x23)
|
||||
// throw new FormatException("Bad 7-bit encoded integer");
|
||||
|
||||
byte num3 = this.ReadByte();
|
||||
num1 |= ((UInt64)num3 & 0x7f) << num2;
|
||||
@@ -511,6 +480,14 @@ namespace Lidgren.Network
|
||||
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>
|
||||
/// Pads data with the specified number of bits.
|
||||
/// </summary>
|
||||
|
||||
@@ -393,7 +393,7 @@ namespace Lidgren.Network
|
||||
//
|
||||
|
||||
/// <summary>
|
||||
/// Write Base128 encoded variable sized unsigned integer
|
||||
/// Write Base128 encoded variable sized unsigned integer of up to 32 bits
|
||||
/// </summary>
|
||||
/// <returns>number of bytes written</returns>
|
||||
[CLSCompliant(false)]
|
||||
@@ -412,43 +412,27 @@ namespace Lidgren.Network
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write Base128 encoded variable sized signed integer
|
||||
/// Write Base128 encoded variable sized signed integer of up to 32 bits
|
||||
/// </summary>
|
||||
/// <returns>number of bytes written</returns>
|
||||
public int WriteVariableInt32(int value)
|
||||
{
|
||||
int retval = 1;
|
||||
uint num1 = (uint)((value << 1) ^ (value >> 31));
|
||||
while (num1 >= 0x80)
|
||||
{
|
||||
this.Write((byte)(num1 | 0x80));
|
||||
num1 = num1 >> 7;
|
||||
retval++;
|
||||
}
|
||||
this.Write((byte)num1);
|
||||
return retval;
|
||||
uint zigzag = (uint)(value << 1) ^ (uint)(value >> 31);
|
||||
return WriteVariableUInt32(zigzag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write Base128 encoded variable sized signed integer
|
||||
/// Write Base128 encoded variable sized signed integer of up to 64 bits
|
||||
/// </summary>
|
||||
/// <returns>number of bytes written</returns>
|
||||
public int WriteVariableInt64(Int64 value)
|
||||
{
|
||||
int retval = 1;
|
||||
UInt64 num1 = (UInt64)((value << 1) ^ (value >> 31));
|
||||
while (num1 >= 0x80)
|
||||
{
|
||||
this.Write((byte)(num1 | 0x80));
|
||||
num1 = num1 >> 7;
|
||||
retval++;
|
||||
}
|
||||
this.Write((byte)num1);
|
||||
return retval;
|
||||
ulong zigzag = (ulong)(value << 1) ^ (ulong)(value >> 63);
|
||||
return WriteVariableUInt64(zigzag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write Base128 encoded variable sized unsigned integer
|
||||
/// Write Base128 encoded variable sized unsigned integer of up to 64 bits
|
||||
/// </summary>
|
||||
/// <returns>number of bytes written</returns>
|
||||
[CLSCompliant(false)]
|
||||
|
||||
@@ -9,6 +9,30 @@ namespace UnitTests
|
||||
{
|
||||
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)
|
||||
{
|
||||
NetOutgoingMessage msg = peer.CreateMessage();
|
||||
@@ -26,6 +50,7 @@ namespace UnitTests
|
||||
int bcnt = 0;
|
||||
|
||||
msg.Write(45.0f);
|
||||
msg.WriteVariableInt32(2115998022);
|
||||
msg.Write(46.0);
|
||||
bcnt += msg.WriteVariableInt32(-47);
|
||||
msg.WriteVariableInt32(470000);
|
||||
@@ -56,13 +81,14 @@ namespace UnitTests
|
||||
inc.SkipPadBits();
|
||||
|
||||
bdr.Append(inc.ReadSingle());
|
||||
bdr.Append(inc.ReadVariableInt32());
|
||||
bdr.Append(inc.ReadDouble());
|
||||
bdr.Append(inc.ReadVariableInt32());
|
||||
bdr.Append(inc.ReadVariableInt32());
|
||||
bdr.Append(inc.ReadVariableUInt32());
|
||||
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");
|
||||
else
|
||||
throw new NetException("Read/write tests FAILED!");
|
||||
|
||||
Reference in New Issue
Block a user