示例#1
0
        public byte[] GetPayload()
        {
            uint headerChecksum   = 0u;
            uint bodyChecksum     = 0u;
            uint fragmentChecksum = 0u;

            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Seek((int)PacketHeader.HeaderSize, SeekOrigin.Begin);

                    if (Data.Length > 0)
                    {
                        var body = Data.ToArray();
                        writer.Write(body);
                        bodyChecksum = Hash32.Calculate(body, body.Length);
                    }
                    foreach (ServerPacketFragment fragment in Fragments)
                    {
                        fragmentChecksum += fragment.GetPayload(writer);
                    }

                    Header.Size    = (ushort)(stream.Length - PacketHeader.HeaderSize);
                    headerChecksum = Header.CalculateHash32();
                    uint payloadChecksum = bodyChecksum + fragmentChecksum;
                    Header.Checksum = headerChecksum + (payloadChecksum ^ issacXor);
                    writer.Seek(0, SeekOrigin.Begin);
                    writer.Write(Header.GetRaw());
                    writer.Flush();
                    return(stream.ToArray());
                }
            }
        }
示例#2
0
        /// <summary>
        /// Returns the Hash32 of the payload added to buffer
        /// </summary>
        public uint AddPayloadToBuffer(byte[] buffer, ref int offset)
        {
            buffer[offset++] = (byte)Sequence;
            buffer[offset++] = (byte)(Sequence >> 8);
            buffer[offset++] = (byte)(Sequence >> 16);
            buffer[offset++] = (byte)(Sequence >> 24);

            buffer[offset++] = (byte)Id;
            buffer[offset++] = (byte)(Id >> 8);
            buffer[offset++] = (byte)(Id >> 16);
            buffer[offset++] = (byte)(Id >> 24);

            buffer[offset++] = (byte)Count;
            buffer[offset++] = (byte)(Count >> 8);

            buffer[offset++] = (byte)Size;
            buffer[offset++] = (byte)(Size >> 8);

            buffer[offset++] = (byte)Index;
            buffer[offset++] = (byte)(Index >> 8);

            buffer[offset++] = (byte)Queue;
            buffer[offset++] = (byte)(Queue >> 8);

            return(Hash32.Calculate(buffer, offset - HeaderSize, HeaderSize));
        }
示例#3
0
        public uint CalculateHash32()
        {
            byte[] fragmentHeaderBytes = Header.GetRaw();
            uint   fragmentChecksum    = Hash32.Calculate(fragmentHeaderBytes, fragmentHeaderBytes.Length) + Hash32.Calculate(Data, Data.Length);

            return(fragmentChecksum);
        }
示例#4
0
文件: Packet.cs 项目: sbennington/ACE
        private uint CalculatePayloadChecksum()
        {
            uint checksum = 0u;

            if (Header.HasFlag(PacketHeaderFlags.BlobFragments))
            {
                if (HeaderOptional != null)
                {
                    // FIXME: packets with both optional headers and fragments fail to verify
                    byte[] optionalHeader = new byte[HeaderOptional.Size];
                    Buffer.BlockCopy(Data.ToArray(), 0, optionalHeader, 0, (int)HeaderOptional.Size);

                    checksum += Hash32.Calculate(optionalHeader, (int)HeaderOptional.Size);
                }

                foreach (var fragment in Fragments)
                {
                    checksum += Hash32.Calculate(fragment.Header.GetRaw(), (int)PacketFragmentHeader.HeaderSize) + Hash32.Calculate(fragment.Data.ToArray(), fragment.Header.Size - (int)PacketFragmentHeader.HeaderSize);
                }
            }
            else
            {
                checksum += Hash32.Calculate(Data.ToArray(), (int)Data.Length);
            }

            return(checksum);
        }
示例#5
0
        public void CalculateHash32(out uint checksum)
        {
            uint original = Checksum;

            Checksum = 0x0BADD70DD;
            byte[] rawHeader = GetRaw();
            checksum = Hash32.Calculate(rawHeader, rawHeader.Length);
            Checksum = original;
        }
示例#6
0
        public uint GetPayload(BinaryWriter writer)
        {
            Header.Size = (ushort)(PacketFragmentHeader.HeaderSize + Content.Length);
            byte[] fragmentHeaderBytes = Header.GetRaw();
            uint   fragmentChecksum    = Hash32.Calculate(fragmentHeaderBytes, fragmentHeaderBytes.Length) + Hash32.Calculate(Content, Content.Length);

            writer.Write(fragmentHeaderBytes);
            writer.Write(Content);
            return(fragmentChecksum);
        }
示例#7
0
        /// <summary>
        /// Returns the Hash32 of the payload added to buffer
        /// </summary>
        public uint PackAndReturnHash32(byte[] buffer, ref int offset)
        {
            Header.Size = (ushort)(PacketFragmentHeader.HeaderSize + Data.Length);

            var headerHash32 = Header.PackAndReturnHash32(buffer, ref offset);

            Buffer.BlockCopy(Data, 0, buffer, offset, Data.Length);
            offset += Data.Length;

            return(headerHash32 + Hash32.Calculate(Data, Data.Length));
        }
示例#8
0
        public uint CalculateHash32()
        {
            uint checksum = 0;
            uint original = Checksum;

            Checksum = 0xBADD70DD;
            byte[] rawHeader = GetRaw();
            checksum = Hash32.Calculate(rawHeader, rawHeader.Length);
            Checksum = original;
            return(checksum);
        }
示例#9
0
        public uint CalculateHash32()
        {
            byte[] buffer = ArrayPool <byte> .Shared.Rent(PacketFragmentHeader.HeaderSize);

            try
            {
                Header.Pack(buffer);

                uint fragmentChecksum = Hash32.Calculate(buffer, buffer.Length) + Hash32.Calculate(Data, Data.Length);

                return(fragmentChecksum);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
示例#10
0
        public uint CalculateHash32()
        {
            byte[] buffer = ArrayPool <byte> .Shared.Rent(HeaderSize);

            try
            {
                uint original = Checksum;
                Checksum = 0xBADD70DD;

                AddPayloadToBuffer(buffer);

                var checksum = Hash32.Calculate(buffer, HeaderSize);
                Checksum = original;
                return(checksum);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
示例#11
0
 public uint CalculateHash32()
 {
     return(Hash32.Calculate(Bytes, Bytes.Length));
 }
示例#12
0
        /// <summary>
        /// Returns the Hash32 of the payload added to buffer
        /// </summary>
        public uint PackAndReturnHash32(byte[] buffer, ref int offset)
        {
            Pack(buffer, ref offset);

            return(Hash32.Calculate(buffer, offset - HeaderSize, HeaderSize));
        }
示例#13
0
        public uint CalculateHash32()
        {
            var bytes = headerBytes.GetBuffer();

            return(Hash32.Calculate(bytes, (int)headerBytes.Length));
        }