You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-15 22:56:30 +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)]
|
||||
|
||||
Reference in New Issue
Block a user