Ejemplo n.º 1
0
        public Packet(EQStream stream, byte[] packet, bool combined = false)
        {
            Baked = packet;

            Opcode = packet.NetU16(0);
            var off = 2;

            switch ((SessionOp)Opcode)
            {
            case SessionOp.Ack:
            case SessionOp.OutOfOrder:
            case SessionOp.Single:
            case SessionOp.Fragment:
            case SessionOp.Combined:
                var plen = packet.Length - off;
                if (!combined && stream.Validating)
                {
                    plen -= 2;
                    var mcrc = CalculateCRC(packet.Sub(0, packet.Length - 2), stream.CRCKey);
                    var pcrc = packet.NetU16(packet.Length - 2);
                    Valid = mcrc == pcrc;
                }
                if (!combined && stream.Compressing)
                {
                    if (packet[off] == 0x5a)
                    {
                        using (var ms = new MemoryStream(packet, 3, packet.Length - 3 - 2)) {
                            using (var ds = new ZlibStream(ms, CompressionMode.Decompress)) {
                                using (var tms = new MemoryStream()) {
                                    ds.CopyTo(tms);
                                    packet = tms.ToArray();
                                    plen   = packet.Length;
                                    off    = 0;
                                }
                            }
                        }
                    }
                    else if (packet[off] == 0xa5)
                    {
                        off++;
                        plen--;
                    }
                }
                if ((SessionOp)Opcode != SessionOp.Combined)
                {
                    Sequence = packet.NetU16(off);
                    off     += 2;
                    plen    -= 2;
                }
                Data = packet.Sub(off, off + plen);
                Bare = false;
                break;

            default:
                Data = packet.Sub(2);
                Bare = true;
                break;
            }
        }
Ejemplo n.º 2
0
        public virtual byte[] Bake(EQStream stream)
        {
            if (Baked != null)
            {
                return(Baked);
            }

            if (Bare)
            {
                Baked    = new byte[Data.Length + 2];
                Baked[0] = (byte)(Opcode >> 8);
                Baked[1] = (byte)Opcode;
                Array.Copy(Data, 0, Baked, 2, Data.Length);
            }
            else
            {
                var len = Data.Length + 2 + 2;
                if (stream.Compressing)
                {
                    len++;
                }
                if (sequenced)
                {
                    len += 2;
                }
                Baked    = new byte[len];
                Baked[0] = (byte)(Opcode >> 8);
                Baked[1] = (byte)Opcode;
                var off = 2;

                var doCompress = false;

                if (!doCompress && stream.Compressing)
                {
                    Baked[off++] = 0xa5;
                }

                if (sequenced)
                {
                    Baked[off++] = (byte)(sequence >> 8);
                    Baked[off++] = (byte)sequence;
                }
                Array.Copy(Data, 0, Baked, off, Data.Length);
                off += Data.Length;

                if (doCompress && stream.Compressing)
                {
                    using (var ms = new MemoryStream()) {
                        using (var ds = new ZlibStream(ms, CompressionMode.Compress)) {
                            ds.Write(Baked, 2, off - 2);
                            ds.Flush();
                        }
                        var temp  = ms.ToArray();
                        var temp2 = new byte[3 + temp.Length + 2];
                        Array.Copy(Baked, temp2, 3);
                        temp2[2] = 0x5a;
                        Array.Copy(temp, 0, temp2, 3, temp.Length);
                        Baked = temp2;
                        off   = temp.Length + 3;
                    }
                }
                if (stream.Validating)
                {
                    var crc = CalculateCRC(Baked.Sub(0, off), stream.CRCKey);
                    Baked[off++] = (byte)(crc >> 8);
                    Baked[off++] = (byte)crc;
                }
                else
                {
                    Baked[off++] = 0;
                    Baked[off++] = 0;
                }
            }
            return(Baked);
        }