Example #1
0
        /// <summary>
        /// Parses a packet
        /// </summary>
        public static ParsedPacket ParsePacket(JSONPacketWrapper wrapper, Packet p)
        {
            var pack = new ParsedPacket();

            pack.PacketID      = p.ID;
            pack.PacketName    = wrapper.PacketName;
            pack.PayloadLength = p.DecryptedPayload.Length;

            var parsedFields = new List <ParsedField <object> >();

            using (var br = new BinaryReader(new MemoryStream(p.DecryptedPayload)))
            {
                foreach (var field in wrapper.Fields)
                {
                    try
                    {
                        if (field.FieldType == FieldType.Bytes)
                        {
                            int    toRead   = 0;
                            string replaced = ReplaceWildcards(field.BytesToRead, parsedFields);

                            if (replaced != null)
                            {
                                if (IsNumber(replaced))
                                {
                                    toRead = Convert.ToInt32(replaced);
                                }
                            }
                            else
                            {
                                if (IsNumber(field.BytesToRead))
                                {
                                    toRead = Convert.ToInt32(field.BytesToRead);
                                }
                            }
                            if (toRead > 0)
                            {
                                var value = br.ReadBytes(toRead);

                                parsedFields.Add(new ParsedField <object>()
                                {
                                    FieldLength = value.Length,
                                    FieldName   = field.FieldName,
                                    FieldType   = FieldType.Bytes,
                                    FieldValue  = BitConverter.ToString(value)
                                });
                            }
                        }
                        else
                        {
                            var value = br.ReadField(field.FieldType);
                            parsedFields.Add(new ParsedField <object>
                            {
                                FieldLength = GetObjectSize(value),
                                FieldName   = field.FieldName,
                                FieldType   = field.FieldType,
                                FieldValue  = value
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Log("Exception occured while Parsing Packet: " + ex);
                    }
                }
            }
            pack.ParsedFields = parsedFields.Count > 0 ? parsedFields : null;
            return(pack);
        }