Inheritance: TypeDescription
Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <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);
        }
Beispiel #3
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);
                }
            }
        }