예제 #1
0
        /// <summary>
        /// Parses an unsigned integer value, storing it in the smallest available unsigned integer type.
        /// </summary>
        /// <remarks>
        /// This method is called internally by GetTypedValue.
        /// </remarks>
        /// <param name="show">Value to parse.</param>
        /// <param name="typedType">Receives the parsed type.</param>
        /// <param name="typedValue">Receives the parsed value.</param>
        /// <param name="field">The field descriptor.</param>
        private static void ParseUnsigned(string show, ref Type typedType, ref object typedValue, TSharkField field)
        {
            if (field.DisplayBase.Equals("BASE_NONE"))
            {
                typedType  = typeof(string);
                typedValue = show;
                return;
            }

            // oct values seem to be stored in decimal form...?

            ulong v;

            if (field.DisplayBase.StartsWith("BASE_HEX"))
            {
                v = BitConverter.ToUInt64(TSharkTypeParser.Reverse(TSharkTypeParser.ParseHexBytes(show, 8)), 0);
            }

            /*else if (field.DisplayBase.StartsWith("BASE_OCT"))
             * {
             *  v = BitConverter.ToUInt64(TSharkTypeParser.Reverse(TSharkTypeParser.ParseOctBytes(show, 8)), 0);
             * }*/
            else //if (field.DisplayBase.StartsWith("BASE_DEC"))
            {
                v = ulong.Parse(show);
            }

            if (v >= byte.MinValue && v <= byte.MaxValue)
            {
                typedType  = typeof(byte);
                typedValue = (byte)v;
            }
            else if (v >= ushort.MinValue && v <= ushort.MaxValue)
            {
                typedType  = typeof(ushort);
                typedValue = (ushort)v;
            }
            else if (v >= uint.MinValue && v <= uint.MaxValue)
            {
                typedType  = typeof(uint);
                typedValue = (uint)v;
            }
            else //if (v >= ulong.MinValue && v <= ulong.MaxValue)
            {
                typedType  = typeof(ulong);
                typedValue = (ulong)v;
            }
        }
예제 #2
0
        /// <summary>
        /// Converts a string value to an instance of a runtime type.
        /// </summary>
        /// <param name="state">The parse state.</param>
        /// <param name="name">Field name.</param>
        /// <param name="showname">Node's 'showname' attribute.</param>
        /// <param name="show">Node's 'show' attribute.</param>
        /// <param name="value">Node's 'value' attribute.</param>
        /// <param name="typedType">Receives the runtime type of the value.</param>
        /// <param name="typedValue">Receives the typed value.</param>
        /// <param name="typedValueString">Receives the string corresponding to the typed value.</param>
        private static void GetTypedValue(ParseState state, string name, string showname, string show, string value, out Type typedType, out object typedValue, out string typedValueString)
        {
            typedType        = null;
            typedValue       = null;
            typedValueString = null;

            try
            {
                TSharkField field;
                if (state.Schema.Fields.TryGetValue(name, out field))
                {
                    typedType = field.Type;

                    if (typedType == typeof(bool))
                    {
                        bool?t;
                        switch (show)
                        {
                        case "0":
                            t = false;
                            break;

                        case "1":
                            t = true;
                            break;

                        default:
                            t = null;
                            break;
                        }

                        if (t == null)
                        {
                            typedType  = typeof(string);
                            typedValue = show;
                        }
                        else
                        {
                            typedValue = (bool)t;
                        }
                    }
                    else if (typedType == typeof(byte[]))
                    {
                        if (value == null)
                        {
                            typedValue = new byte[0];
                        }
                        else
                        {
                            typedValue = TSharkTypeParser.ParseHexBytes(value);
                        }
                    }
                    else if (typedType == typeof(byte) || typedType == typeof(ushort) || typedType == typeof(uint) || typedType == typeof(ulong))
                    {
                        ParseUnsigned(show, ref typedType, ref typedValue, field);
                    }
                    else if (typedType == typeof(sbyte) || typedType == typeof(short) || typedType == typeof(int) || typedType == typeof(long))
                    {
                        ParseSigned(show, ref typedType, ref typedValue, field);
                    }
                    else if (typedType == typeof(float))
                    {
                        typedValue = float.Parse(show);
                    }
                    else if (typedType == typeof(double))
                    {
                        typedValue = double.Parse(show);
                    }
                    else if (typedType == typeof(DateTime))
                    {
                        typedValue = TSharkTypeParser.ParseDateTime(show);
                    }
                    else if (typedType == typeof(TimeSpan))
                    {
                        // loss of precision here
                        typedValue = TimeSpan.FromSeconds(double.Parse(show));
                    }
                    else if (typedType == typeof(IPAddress))
                    {
                        IPAddress ipAddress;
                        if (!IPAddress.TryParse(show, out ipAddress))
                        {
                            ipAddress = new IPAddress(TSharkTypeParser.ParseHexBytes(value));
                        }
                        typedValue = ipAddress;
                    }
                    else if (typedType == typeof(Guid))
                    {
                        Guid t;
                        if (!Guid.TryParse(show, out t))
                        {
                            throw new Exception(string.Format("Badly formatted Guid value: {0}", show));
                        }
                        typedValue = t;
                    }
                    else if (typedType == typeof(string) || typedType == null)
                    {
                        typedType  = typeof(string);
                        typedValue = show;
                    }

                    //Log.WriteInfo("Type: {0} Value: {1}", typedType, show);
                }

                if (field != null)
                {
                    if (typedValue == null)
                    {
                        Log.WriteWarning("Unable to convert field value.\nName: {0}\nValue: {1}", name, show);
                    }
                    else
                    {
                        typedValueString = field.FindString(typedValue);
                    }
                }
                else
                {
                    //Log.WriteInfo("No field definition for field.\nName: {0}\nValue: {1}", name, show);
                }
            }
            catch (Exception ex)
            {
                typedValue = null;

                Log.WriteWarning("Unable to convert field value.\nName: {0}\nValue: {1}\nError: {2}", name, show, ex.Message);
            }

            if (typedValue == null)
            {
                typedType = typeof(string);

                if (show == null || (value != null && name.IndexOf(show, 0, StringComparison.OrdinalIgnoreCase) >= 0))
                {
                    typedValue = value;
                }
                else
                {
                    typedValue = show;
                }

                typedValueString = null;
            }
        }
예제 #3
0
        /// <summary>
        /// Reads TShark formatted schema files.
        /// </summary>
        /// <param name="protocolsFile">Protocols file.</param>
        /// <param name="fieldsFile">Fields file.</param>
        /// <param name="valuesFile">Values file.</param>
        /// <param name="decodesFile">Decodes file.</param>
        /// <returns>Data structure containing the schema that was read.</returns>
        public static TSharkDataSchema Read(string protocolsFile, string fieldsFile, string valuesFile, string decodesFile)
        {
            TSharkDataSchema schema = new TSharkDataSchema();

            int lineIndex = 0;

            // read protocols
            using (StreamReader reader = new StreamReader(protocolsFile, Encoding.UTF8))
            {
                while (true)
                {
                    try
                    {
                        string line = reader.ReadLine();
                        if (line == null)
                        {
                            break;
                        }

                        lineIndex++;

                        if (line.Length == 0)
                        {
                            continue;
                        }

                        string[] parts = SplitParts(line, 3);
                        if (parts.Length != 3)
                        {
                            throw new Exception(string.Format("Badly formatted protocols file. Line: {0}", lineIndex));
                        }

                        schema.AddProtocol(parts[0], parts[1], parts[2]);
                    }
                    catch (Exception ex)
                    {
                        Log.WriteWarning(ex.Message);
                    }
                }
            }

            // read fields
            using (StreamReader reader = new StreamReader(fieldsFile, Encoding.UTF8))
            {
                while (true)
                {
                    try
                    {
                        string line = reader.ReadLine();
                        if (line == null)
                        {
                            break;
                        }

                        lineIndex++;

                        if (line.Length == 0)
                        {
                            continue;
                        }

                        string[] parts;
                        switch (line[0])
                        {
                        case 'F':
                            parts = SplitParts(line, 8);
                            if (parts.Length != 8)
                            {
                                throw new Exception(string.Format("Badly formatted fields file. Line: {0}", lineIndex));
                            }
                            schema.AddField(parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]);
                            break;

                        case 'P':
                            parts = SplitParts(line, 3);
                            if (parts.Length != 3)
                            {
                                throw new Exception(string.Format("Badly formatted fields file. Line: {0}", lineIndex));
                            }
                            if (!schema.Protocols.ContainsKey(parts[2].ToLowerInvariant()))
                            {
                                schema.AddProtocol(parts[1], parts[2], parts[2].ToLowerInvariant());
                            }
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteWarning(ex.Message);
                    }
                }
            }

            // read values
            using (StreamReader reader = new StreamReader(valuesFile, Encoding.UTF8))
            {
                while (true)
                {
                    try
                    {
                        string line = reader.ReadLine();
                        if (line == null)
                        {
                            break;
                        }

                        lineIndex++;

                        if (line.Length == 0)
                        {
                            continue;
                        }

                        string[] parts;
                        switch (line[0])
                        {
                        case 'V':
                            parts = SplitParts(line, 4);
                            long value;
                            if (parts.Length != 4 || !TSharkTypeParser.TryParseInt64(parts[2], out value))
                            {
                                throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex));
                            }
                            schema.AddValueString(parts[1], value, parts[3]);
                            break;

                        case 'R':
                            parts = SplitParts(line, 5);
                            long lowerBound, upperBound;
                            if (parts.Length != 5 || !TSharkTypeParser.TryParseInt64(parts[2], out lowerBound) || !TSharkTypeParser.TryParseInt64(parts[3], out upperBound))
                            {
                                throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex));
                            }
                            schema.AddRangeString(parts[1], lowerBound, upperBound, parts[4]);
                            break;

                        case 'T':
                            parts = SplitParts(line, 4);
                            if (parts.Length != 4)
                            {
                                throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex));
                            }
                            schema.AddTrueFalseString(parts[1], parts[2], parts[3]);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteWarning(ex.Message);
                    }
                }
            }

            return(schema);
        }