1
0

MC uses VarInts only up to 32-bits.

This commit is contained in:
madmaxoft 2013-10-28 20:57:03 +01:00
parent c9b6c3bc2e
commit dfefdcf7f1
2 changed files with 13 additions and 13 deletions

View File

@ -39,11 +39,11 @@ public:
{ {
cByteBuffer buf(50); cByteBuffer buf(50);
buf.Write("\x05\xac\x02\x00", 4); buf.Write("\x05\xac\x02\x00", 4);
UInt64 v1; UInt32 v1;
ASSERT(buf.ReadVarInt(v1) && (v1 == 5)); ASSERT(buf.ReadVarInt(v1) && (v1 == 5));
UInt64 v2; UInt32 v2;
ASSERT(buf.ReadVarInt(v2) && (v2 == 300)); ASSERT(buf.ReadVarInt(v2) && (v2 == 300));
UInt64 v3; UInt32 v3;
ASSERT(buf.ReadVarInt(v3) && (v3 == 0)); ASSERT(buf.ReadVarInt(v3) && (v3 == 0));
} }
@ -374,11 +374,11 @@ bool cByteBuffer::ReadBEUTF16String16(AString & a_Value)
bool cByteBuffer::ReadVarInt(UInt64 & a_Value) bool cByteBuffer::ReadVarInt(UInt32 & a_Value)
{ {
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
UInt64 Value = 0; UInt32 Value = 0;
int Shift = 0; int Shift = 0;
unsigned char b = 0; unsigned char b = 0;
do do
@ -400,7 +400,7 @@ bool cByteBuffer::ReadVarUTF8String(AString & a_Value)
{ {
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
UInt64 Size = 0; UInt32 Size = 0;
if (!ReadVarInt(Size)) if (!ReadVarInt(Size))
{ {
return false; return false;
@ -534,13 +534,13 @@ bool cByteBuffer::WriteBEUTF16String16(const AString & a_Value)
bool cByteBuffer::WriteVarInt(UInt64 a_Value) bool cByteBuffer::WriteVarInt(UInt32 a_Value)
{ {
CHECK_THREAD; CHECK_THREAD;
CheckValid(); CheckValid();
// A 64-bit integer can be encoded by at most 10 bytes: // A 32-bit integer can be encoded by at most 5 bytes:
unsigned char b[10]; unsigned char b[5];
int idx = 0; int idx = 0;
do do
{ {

View File

@ -58,13 +58,13 @@ public:
bool ReadBEDouble (double & a_Value); bool ReadBEDouble (double & a_Value);
bool ReadBool (bool & a_Value); bool ReadBool (bool & a_Value);
bool ReadBEUTF16String16(AString & a_Value); // string length as BE short, then string as UTF-16BE bool ReadBEUTF16String16(AString & a_Value); // string length as BE short, then string as UTF-16BE
bool ReadVarInt (UInt64 & a_Value); bool ReadVarInt (UInt32 & a_Value);
bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8 bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8
/// Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned int, short, char, Byte, double, ...) /// Reads VarInt, assigns it to anything that can be assigned from an UInt32 (unsigned short, char, Byte, double, ...)
template <typename T> bool ReadVarUInt(T & a_Value) template <typename T> bool ReadVarUInt(T & a_Value)
{ {
UInt64 v; UInt32 v;
bool res = ReadVarInt(v); bool res = ReadVarInt(v);
if (res) if (res)
{ {
@ -83,7 +83,7 @@ public:
bool WriteBEDouble (double a_Value); bool WriteBEDouble (double a_Value);
bool WriteBool (bool a_Value); bool WriteBool (bool a_Value);
bool WriteBEUTF16String16(const AString & a_Value); // string length as BE short, then string as UTF-16BE bool WriteBEUTF16String16(const AString & a_Value); // string length as BE short, then string as UTF-16BE
bool WriteVarInt (UInt64 a_Value); bool WriteVarInt (UInt32 a_Value);
bool WriteVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8 bool WriteVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8
/// Reads a_Count bytes into a_Buffer; returns true if successful /// Reads a_Count bytes into a_Buffer; returns true if successful