/// <summary>
        /// Validates a field in a structured type description.
        /// </summary>
        private void ValidateField(StructuredType description, Dictionary<string,FieldType> fields, FieldType field)
        {
            if (field == null || String.IsNullOrEmpty(field.Name))
            {
                throw Exception("The structured type '{0}' has an unnamed field.", description.Name);
            }

            if (fields.ContainsKey(field.Name))
            {
                throw Exception("The structured type '{0}' has a duplicate field name '{1}'.", description.Name, field.Name);
            }
            
            if (IsNull(field.TypeName))
            {     
                throw Exception("Field '{1}' in structured type '{0}' has no type specified.", description.Name, field.Name);
            }

            if (!m_descriptions.ContainsKey(field.TypeName))
            {
                throw Exception("Field '{1}' in structured type '{0}' has an unrecognized type '{2}'.", description.Name, field.Name, field.TypeName);
            }
                                
            if (!String.IsNullOrEmpty(field.LengthField))
            {
                if (!fields.ContainsKey(field.LengthField))
                {
                    throw Exception("Field '{1}' in structured type '{0}' references an unknownn length field '{2}'.", description.Name, field.Name, field.LengthField);
                }

                if (!IsIntegerType(fields[field.LengthField]))
                {
                    throw Exception("Field '{1}' in structured type '{0}' references a length field '{2}' which is not an integer value.", description.Name, field.Name, field.SwitchField);
                }
            }
            
            if (!String.IsNullOrEmpty(field.SwitchField))
            {
                if (!fields.ContainsKey(field.SwitchField))
                {
                    throw Exception("Field '{1}' in structured type '{0}' references an unknownn switch field '{2}'.", description.Name, field.Name, field.SwitchField);
                }

                if (!IsIntegerType(fields[field.SwitchField]))
                {
                    throw Exception("Field '{1}' in structured type '{0}' references a switch field '{2}' which is not an integer value.", description.Name, field.Name, field.SwitchField);
                }
            }
        }
        /// <summary>
        /// Returns the length of field in bits (-1 if the length is not fixed).
        /// </summary>
        private int GetFieldLength(FieldType field)
        {
            TypeDescription description = null;

            if (!m_descriptions.TryGetValue(field.TypeName, out description))
            {
                return -1;
            }

            uint count = 1;

            if (field.LengthSpecified)
            {
                count = field.Length;

                if (field.IsLengthInBytes)
                {
                    count *= 8;
                }
            }

            EnumeratedType enumerated = description as EnumeratedType;

            if (enumerated != null)
            {
                if (enumerated.LengthInBitsSpecified)
                {
                    return enumerated.LengthInBits * ((int)count);
                }
            }
            else
            {
                OpaqueType opaque = description as OpaqueType;

                if (opaque != null)
                {
                    if (opaque.LengthInBitsSpecified)
                    {
                        return opaque.LengthInBits * ((int)count);
                    }
                }
            }

            return -1;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Validates a field in a structured type description.
        /// </summary>
        private void ValidateField(StructuredType description, Dictionary <string, FieldType> fields, FieldType field)
        {
            if (field == null || String.IsNullOrEmpty(field.Name))
            {
                throw Exception("The structured type '{0}' has an unnamed field.", description.Name);
            }

            if (fields.ContainsKey(field.Name))
            {
                throw Exception("The structured type '{0}' has a duplicate field name '{1}'.", description.Name, field.Name);
            }

            if (IsNull(field.TypeName))
            {
                throw Exception("Field '{1}' in structured type '{0}' has no type specified.", description.Name, field.Name);
            }

            if (!m_descriptions.ContainsKey(field.TypeName))
            {
                throw Exception("Field '{1}' in structured type '{0}' has an unrecognized type '{2}'.", description.Name, field.Name, field.TypeName);
            }

            if (!String.IsNullOrEmpty(field.LengthField))
            {
                if (!fields.ContainsKey(field.LengthField))
                {
                    throw Exception("Field '{1}' in structured type '{0}' references an unknownn length field '{2}'.", description.Name, field.Name, field.LengthField);
                }

                if (!IsIntegerType(fields[field.LengthField]))
                {
                    throw Exception("Field '{1}' in structured type '{0}' references a length field '{2}' which is not an integer value.", description.Name, field.Name, field.SwitchField);
                }
            }

            if (!String.IsNullOrEmpty(field.SwitchField))
            {
                if (!fields.ContainsKey(field.SwitchField))
                {
                    throw Exception("Field '{1}' in structured type '{0}' references an unknownn switch field '{2}'.", description.Name, field.Name, field.SwitchField);
                }

                if (!IsIntegerType(fields[field.SwitchField]))
                {
                    throw Exception("Field '{1}' in structured type '{0}' references a switch field '{2}' which is not an integer value.", description.Name, field.Name, field.SwitchField);
                }
            }
        }
        /// <summary>
        /// Returns true if field is an integer type.
        /// </summary>
        private bool IsIntegerType(FieldType field)
        {
            TypeDescription description = null;

            if (!m_descriptions.TryGetValue(field.TypeName, out description))
            {
                return false;
            }

            if (description is EnumeratedType)
            {
                return true;
            }

            OpaqueType opaqueType = description as OpaqueType;

            if (opaqueType != null)
            {
                if (opaqueType.LengthInBitsSpecified)
                {
                    return true;
                }
            }

            return false;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Validates a type description.
        /// </summary>
        private void ValidateDescription(TypeDescription description)
        {
            OpaqueType opaque = description as OpaqueType;

            if (opaque != null)
            {
                if (!opaque.LengthInBitsSpecified)
                {
                    m_warnings.Add(String.Format(CultureInfo.InvariantCulture, "Warning: The opaque type '{0}' does not have a length specified.", description.Name));
                }

                if (IsNull(opaque.Documentation))
                {
                    m_warnings.Add(String.Format(CultureInfo.InvariantCulture, "Warning: The opaque type '{0}' does not have any documentation.", description.Name));
                }
            }

            EnumeratedType enumerated = description as EnumeratedType;

            if (enumerated != null)
            {
                if (!enumerated.LengthInBitsSpecified)
                {
                    throw Exception("The enumerated type '{0}' does not have a length specified.", description.Name);
                }
            }

            StructuredType structure = description as StructuredType;

            if (structure != null)
            {
                if (structure.Field == null || structure.Field.Length == 0)
                {
                    structure.Field = new FieldType[0];
                }

                int bitCount = 0;

                Dictionary <string, FieldType> fields = new Dictionary <string, FieldType>();

                for (int ii = 0; ii < structure.Field.Length; ii++)
                {
                    FieldType field = structure.Field[ii];

                    ValidateField(structure, fields, field);

                    int fieldLength = GetFieldLength(field);

                    if (fieldLength == -1)
                    {
                        if (bitCount % 8 != 0)
                        {
                            throw Exception("Field '{1}' in structured type '{0}' is not aligned on a byte boundary .", description.Name, field.Name);
                        }

                        bitCount = 0;
                    }
                    else
                    {
                        bitCount += fieldLength;
                    }

                    fields.Add(field.Name, field);
                }
            }
        }