//} //} //using System; //using System.IO; //namespace SilentOrbit.ProtocolBuffers //{ //public static partial class ProtocolParser //{ /// <summary> /// Reads past a varint for an unknown field. /// </summary> public static void ReadSkipVarInt(CitoStream stream) { while (true) { int b = stream.ReadByte(); if (b < 0) #if !CITO { throw new IOException("Stream ended too early"); } #else { return; } #endif if ((b & 0x80) == 0) { return; //end of varint } } }
/// <summary> /// Unsigned VarInt format /// </summary> public static int ReadUInt64(CitoStream stream) { int b; int val = 0; for (int n = 0; n < 10; n++) { b = stream.ReadByte(); if (b < 0) #if !CITO { throw new IOException("Stream ended too early"); } #else { return(0); } #endif //Check that it fits in 64 bits if ((n == 9) && (b & 0xFE) != 0) #if !CITO { throw new InvalidDataException("Got larger VarInt than 64 bit unsigned"); } #else { return(0); } #endif //End of check if ((b & 0x80) == 0) { //return val | (ulong)b << (7 * n); return(val | b << (7 * n)); } //val |= (ulong)(b & 0x7F) << (7 * n); val |= (b & 0x7F) << (7 * n); } #if !CITO throw new InvalidDataException("Got larger VarInt than 64 bit unsigned"); #else return(0); #endif }
public static byte[] ReadVarIntBytes(CitoStream stream) { byte[] buffer = new byte[10]; int offset = 0; while (true) { int b = stream.ReadByte(); if (b < 0) #if !CITO { throw new IOException("Stream ended too early"); } #else { return(null); } #endif #if !CITO buffer[offset] = (byte)b; #else buffer[offset] = b.LowByte; #endif offset += 1; if ((b & 0x80) == 0) { break; //end of varint } if (offset >= ProtoPlatform.ArrayLength(buffer)) #if !CITO { throw new InvalidDataException("VarInt too long, more than 10 bytes"); } #else { return(null); } #endif } byte[] ret = new byte[offset]; for (int i = 0; i < offset; i++) { ret[i] = buffer[i]; } return(ret); }
//#endregion //#region Varint: bool public static bool ReadBool(CitoStream stream) { int b = stream.ReadByte(); if (b < 0) #if !CITO { throw new IOException("Stream ended too early"); } #else { return(false); } #endif if (b == 1) { return(true); } if (b == 0) { return(false); } #if !CITO throw new InvalidDataException("Invalid boolean value"); #else return(false); #endif }
public static byte[] ReadVarIntBytes(CitoStream stream) { byte[] buffer = new byte[10]; int offset = 0; while (true) { int b = stream.ReadByte(); if (b < 0) #if !CITO throw new IOException("Stream ended too early"); #else return null; #endif #if !CITO buffer[offset] = (byte)b; #else buffer[offset] = b.LowByte; #endif offset += 1; if ((b & 0x80) == 0) break; //end of varint if (offset >= ProtoPlatform.ArrayLength(buffer)) #if !CITO throw new InvalidDataException("VarInt too long, more than 10 bytes"); #else return null; #endif } byte[] ret = new byte[offset]; for (int i = 0; i < offset; i++) { ret[i] = buffer[i]; } return ret; }
/// <summary> /// Unsigned VarInt format /// </summary> public static int ReadUInt64(CitoStream stream) { int b; int val = 0; for (int n = 0; n < 10; n++) { b = stream.ReadByte(); if (b < 0) #if !CITO throw new IOException("Stream ended too early"); #else return 0; #endif //Check that it fits in 64 bits if ((n == 9) && (b & 0xFE) != 0) #if !CITO throw new InvalidDataException("Got larger VarInt than 64 bit unsigned"); #else return 0; #endif //End of check if ((b & 0x80) == 0) //return val | (ulong)b << (7 * n); return val | b << (7 * n); //val |= (ulong)(b & 0x7F) << (7 * n); val |= (b & 0x7F) << (7 * n); } #if !CITO throw new InvalidDataException("Got larger VarInt than 64 bit unsigned"); #else return 0; #endif }
//} //} //using System; //using System.IO; //namespace SilentOrbit.ProtocolBuffers //{ //public static partial class ProtocolParser //{ /// <summary> /// Reads past a varint for an unknown field. /// </summary> public static void ReadSkipVarInt(CitoStream stream) { while (true) { int b = stream.ReadByte(); if (b < 0) #if !CITO throw new IOException("Stream ended too early"); #else return; #endif if ((b & 0x80) == 0) return; //end of varint } }
//#endregion //#region Varint: bool public static bool ReadBool(CitoStream stream) { int b = stream.ReadByte(); if (b < 0) #if !CITO throw new IOException("Stream ended too early"); #else return false; #endif if (b == 1) return true; if (b == 0) return false; #if !CITO throw new InvalidDataException("Invalid boolean value"); #else return false; #endif }