예제 #1
0
        protected T[] FixBPA <T>(DataReader reader, int length, bool littleEndian) where T : struct
        {
            if (length == 0)
            {
                return(new T[0]);
            }

            if ((typeof(T) == typeof(byte[])) || (typeof(T) == typeof(sbyte[])))
            {
                return((T[])(object)reader.ReadBytes(length));
            }
            else
            {
                int primSize = ParserUtils.GetPrimitiveSize(typeof(T));

                if (primSize > 0)
                {
                    int elementCount = length / primSize;
                    T[] ret          = new T[elementCount];

                    for (int i = 0; i < elementCount; ++i)
                    {
                        ret[i] = reader.ReadPrimitive <T>(littleEndian);
                    }

                    return(ret);
                }
                else
                {
                    List <T> ret = new List <T>();
                    reader = new DataReader(reader.ReadBytes(length));

                    while (reader.DataLeft > 0)
                    {
                        ret.Add(reader.ReadPrimitive <T>(littleEndian));
                    }

                    return(ret.ToArray());
                }
            }
        }
예제 #2
0
        protected long BLen(IPrimitiveValue[] arr)
        {
            int size = ParserUtils.GetPrimitiveSize(arr.GetType().GetElementType());

            // No choice, write it out
            if (size < 0)
            {
                DataWriter w = new DataWriter();

                for (int i = 0; (i < arr.Length); i++)
                {
                    arr[i].ToWriter(w, false);
                }

                return(w.BytesWritten);
            }
            else
            {
                return(arr.Length * size);
            }
        }
예제 #3
0
        /// <summary>
        /// Convert an object to a document
        /// </summary>
        /// <param name="context">The type context</param>
        /// <param name="culture">The culture of the conversion</param>
        /// <param name="value">The value to convert</param>
        /// <returns>The converted value</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            object ret = null;

            if (value.GetType() == typeof(String))
            {
                SequenceChoiceMemberEntry entry;

                if (context.Instance is SequenceChoice)
                {
                    entry = ((SequenceChoice)context.Instance).ParentEntry;
                }
                else
                {
                    entry = (SequenceChoiceMemberEntry)ParserUtils.GetCompatibleType(typeof(SequenceChoiceMemberEntry), context.Instance);
                }

                if (entry != null)
                {
                    foreach (SequenceParserType type in entry.ChoiceTypes)
                    {
                        if (type.Name.Equals((string)value))
                        {
                            ret = type;
                            break;
                        }
                    }
                }
            }
            else if (typeof(SequenceParserType).IsAssignableFrom(value.GetType()))
            {
                ret = value;
            }
            else
            {
                ret = base.ConvertFrom(context, culture, value);
            }

            return(ret);
        }
예제 #4
0
 protected int SBL(string value, BinaryStringEncoding encoding)
 {
     return(ParserUtils.GetStringByteLength(value, GeneralUtils.GetEncodingFromType(encoding)));
 }
예제 #5
0
 /// <summary>
 /// Read a string terminated with a specific character
 /// </summary>
 /// <param name="reader">DataReader to read from</param>
 /// <param name="encoding">The string encoding used</param>
 /// <param name="terminator">The terminating string</param>
 /// <param name="required">If true then the terminator must be present, if false will also terminate on EOF</param>
 /// <exception cref="EndOfStreamException">Throw if hits the end of stream before termination</exception>
 /// <returns>The read string</returns>
 public string RTS(DataReader reader, BinaryStringEncoding encoding, string terminator, bool required)
 {
     return(ParserUtils.ReadTerminatedString(reader, GeneralUtils.GetEncodingFromType(encoding), terminator, required));
 }
예제 #6
0
 /// <summary>
 /// Read a string terminated with a specific character
 /// </summary>
 /// <param name="reader">DataReader to read from</param>
 /// <param name="encoding">The string encoding used</param>
 /// <param name="terminator">The terminating character</param>
 /// <exception cref="EndOfStreamException">Throw if hits the end of stream before termination</exception>
 /// <returns>The read string</returns>
 protected string RTS(DataReader reader, BinaryStringEncoding encoding, char terminator)
 {
     return(ParserUtils.ReadTerminatedString(reader, GeneralUtils.GetEncodingFromType(encoding), terminator));
 }
예제 #7
0
 /// <summary>
 /// Get size of type
 /// </summary>
 /// <returns>The size of the type</returns>
 public override int GetSize()
 {
     return(ParserUtils.GetPrimitiveSize(DataType));
 }