/// <summary> /// Decode a StreamFrame from a raw byte array /// </summary> /// <param name="content">The raw byte array</param> /// <param name="begin">The bit index of the byte array where the AckFrame is located</param> /// <returns>The number of bits read</returns> public override int Decode(byte[] content, int begin) { if (content.Length < 1 + (begin / 8)) { throw new ArgumentException(); } _writableType = content[begin / 8]; if (Type < _minType || Type > _maxType) { throw new ArgumentException("Wrong frame type created"); } OFF = (Type & (1 << 2)) != 0; LEN = (Type & (1 << 1)) != 0; FIN = (Type & (1 << 0)) != 0; int cursor = (begin / 8) + 1; cursor += StreamID.Decode(cursor * 8, content) / 8; if (OFF) { cursor += Offset.Decode(cursor * 8, content) / 8; } if (LEN) { cursor += _length.Decode(cursor * 8, content) / 8; } else { _length = new VariableLengthInteger(Convert.ToUInt64(content.Length) - Convert.ToUInt64(cursor)); } Data = new byte[_length.Value]; // TODO: error handling if source packet is not long enough Array.Copy(content, cursor, Data, 0, Convert.ToInt32(_length.Value)); return((cursor + Convert.ToInt32(_length.Value)) * 8 - begin); }
public void TestDecode() { VariableLengthInteger n = new VariableLengthInteger(0); byte[] b = new byte[] { 0, 0, 3, 0 }; Assert.AreEqual(8, n.Decode(16, b)); Assert.AreEqual(8, n.Size); Assert.AreEqual((UInt64)3, n.Value); b = new byte[] { 0, 128, 1, 0, 0, 0, 0 }; Assert.AreEqual(32, n.Decode(8, b)); Assert.AreEqual(32, n.Size); Assert.AreEqual((UInt64)65536, n.Value); b = new byte[] { 0x9d, 0x7f, 0x3e, 0x7d }; Assert.AreEqual(32, n.Decode(0, b)); Assert.AreEqual(32, n.Size); Assert.AreEqual((UInt64)494878333, n.Value); b = new byte[] { 0x7b, 0xbd }; Assert.AreEqual(16, n.Decode(0, b)); Assert.AreEqual(16, n.Size); Assert.AreEqual((UInt64)15293, n.Value); b = new byte[] { 0x25 }; Assert.AreEqual(8, n.Decode(0, b)); Assert.AreEqual(8, n.Size); Assert.AreEqual((UInt64)37, n.Value); b = new byte[] { 0x40, 0x25 }; Assert.AreEqual(16, n.Decode(0, b)); Assert.AreEqual(16, n.Size); Assert.AreEqual((UInt64)37, n.Value); b = new byte[] { 0xc2, 0x19, 0x7c, 0x5e, 0xff, 0x14, 0xe8, 0x8c }; Assert.AreEqual(64, n.Decode(0, b)); Assert.AreEqual(64, n.Size); Assert.AreEqual((UInt64)151288809941952652, n.Value); }