public static string Decode(ByteStringReader reader,int limit) { var l = new VLong(reader).V; var bytes = reader.ReadBytes((int)l); if (l > limit) { return bytes.Slice(0,limit).GetStringUTF8(); } return bytes.GetStringUTF8(); }
public void ReadByteTest() { var arr = new byte[10]; for (int i = 0;i < 10;i++) { arr[i] = (byte)i; } var bytes = new ByteString(arr); var reader = new ByteStringReader(bytes); for (int i = 0;i < 10;i++) { var b = reader.ReadByte(); Assert.AreEqual(b,(byte)i); } }
protected Response(ByteStringReader reader) : base(reader) { _resultStatus = reader.ReadShortLE(); }
public Option<Packet> Decode(PacketCipher cipher) { if (!_encrypted) { return Option<Packet>.Some(this); } else { try { var b = cipher.Decrypt(_bytes); var src = new ByteStringReader(b); var l = (int)(new VLong(src).V); var enc = false; if (l < 0) { enc = true; l = -l; } if (l + 2 > src.Length) { throw new SystemException("Packet corrupted"); } var pkt = new Packet(new ByteString(src.ReadBytes(l)),enc); var crc = src.ReadShortLE(); if (crc != pkt.Crc) { throw new SystemException("Invalid CRC"); } return Option<Packet>.Some(pkt); } catch(Exception e) { return Option<Packet>.None; } } }