Exemple #1
0
    public Packet ReadPacket(BitArray data)
    {
        var version = data.ConsumeInt(3);
        var type    = data.ConsumeInt(3);

        if (type == 4)
        {
            var value = 0L;
            while (true)
            {
                var part = data.ConsumeInt(5);
                value = (value << 4) | (uint)(part & 0xF);
                if ((part & 0x10) == 0)
                {
                    break;
                }
            }
            return(new Literal(version, type, value));
        }
        var id = data.ConsumeInt(1);

        if (id == 0)
        {
            var length     = data.ConsumeInt(15);
            var subPackets = data.ConsumeArray(length);
            var packets    = new List <Packet>();
            while (subPackets.Length > 0)
            {
                packets.Add(ReadPacket(subPackets));
            }
            return(new Operator(version, type, packets));
        }
        else
        {
            var count   = data.ConsumeInt(11);
            var packets = new List <Packet>();
            for (var i = 0; i < count; i++)
            {
                packets.Add(ReadPacket(data));
            }
            return(new Operator(version, type, packets));
        }
    }