Skip to content

Commit

Permalink
Merge branch 'master' into skip_optional_default_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Rich committed Feb 12, 2025
2 parents c8cb06a + 5959b14 commit e01d5f4
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions CodeGenerator/ProtocolParser/ProtocolParserVarInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,41 @@ public static ulong ReadUInt64(Stream stream)
throw new ProtocolBufferException("Got larger VarInt than 64 bit unsigned");
}

/// <summary>
/// Unsigned VarInt format
/// </summary>
public static ulong ReadUInt64(byte[] array, int pos, out int length)
{
ulong val = 0;
length = 0;

for (byte n = 0; n < 10; n++)
{
length++;

if(pos >= array.Length)
{
break;
}

byte b = array[pos++];
if (b < 0)
throw new IOException("Stream ended too early");

//Check that it fits in 64 bits
if ((n == 9) && (b & 0xFE) != 0)
throw new ProtocolBufferException("Got larger VarInt than 64 bit unsigned");
//End of check

if ((b & 0x80) == 0)
return val | (ulong) b << (7 * n);

val |= (ulong) (b & 0x7F) << (7 * n);
}

throw new ProtocolBufferException("Got larger VarInt than 64 bit unsigned");
}

/// <summary>
/// Unsigned VarInt format
/// </summary>
Expand All @@ -289,6 +324,31 @@ public static void WriteUInt64(Stream stream, ulong val)
}
}

/// <summary>
/// Unsigned VarInt format
/// </summary>
public static int WriteUInt64(ulong val, byte[] buffer, int pos)
{
int len = 0;
while (true)
{
len++;
byte b = (byte)(val & 0x7F);
val = val >> 7;
if (val == 0)
{
buffer[pos] = b;
break;
}
else
{
b |= 0x80;
buffer[pos++] = b;
}
}
return len;
}

#endregion

#region Varint: bool
Expand Down

0 comments on commit e01d5f4

Please sign in to comment.