private static object ConvertIfNecessary(object value, Type objectType, ObjectTypeCode typeCode, ObjectTypeCode expectedTypeCode, bool isConvertible) { if (expectedTypeCode != typeCode && isConvertible) { return(Reflector.ChangeType(value, objectType)); } return(value); }
/// <summary> /// Creates an instance of the StringMetadataComponent struct with the specified typecode and size. /// </summary> /// <param name="typeCode">The TypeCode representing this data type.</param> /// <param name="size">The size of this data element, in bytes.</param> public StringMetadataComponent(ObjectTypeCode typeCode, int size) : base(typeCode) { m_Size = size; }
public void WriteObject(object value, ObjectTypeCode typeCode, IDictionary <Type, int> typeMap) { if (value == null) { Write((byte)TypeCode.Empty); } else { Write((byte)typeCode); switch (typeCode) { case ObjectTypeCode.Boolean: Write((bool)value); break; case ObjectTypeCode.Byte: Write((byte)value); break; case ObjectTypeCode.SByte: Write((sbyte)value); break; case ObjectTypeCode.UInt16: Write((ushort)value); break; case ObjectTypeCode.UInt32: Write((uint)value); break; case ObjectTypeCode.UInt64: Write((ulong)value); break; case ObjectTypeCode.Int16: Write((short)value); break; case ObjectTypeCode.Int32: Write((int)value); break; case ObjectTypeCode.Int64: Write((long)value); break; case ObjectTypeCode.Char: Write((char)value); break; case ObjectTypeCode.String: Write((string)value); break; case ObjectTypeCode.Single: Write((float)value); break; case ObjectTypeCode.Double: Write((double)value); break; case ObjectTypeCode.Decimal: Write((decimal)value); break; case ObjectTypeCode.DateTime: Write((DateTime)value); break; case ObjectTypeCode.DBNull: break; case ObjectTypeCode.Object: { var serializer = CreateDelegate(value.GetType()); serializer(this, new[] { value }, 1, null); } break; case ObjectTypeCode.ObjectList: { var items = (IList)value; var serializer = CreateDelegate(value.GetType()); serializer(this, items, items.Count, null); } break; case ObjectTypeCode.PolymorphicObjectList: { var items = (IList)value; Write((uint)items.Count); WriteListAspectType(items); var map = items.Cast <object>().Where(t => t != null).Select(t => t.GetType()).Distinct().Select((t, i) => new { t, i }).ToDictionary(k => k.t, k => k.i); WriteTypes(map); foreach (var item in items) { WriteTypeMarker(item, map); WriteObject(item, ObjectTypeCode.Object, map); } } break; case ObjectTypeCode.SimpleList: { var items = (IList)value; WriteList(items); } break; case ObjectTypeCode.ByteArray: Write((byte[])value); break; case ObjectTypeCode.CharArray: Write((char[])value); break; case ObjectTypeCode.TimeSpan: Write((TimeSpan)value); break; case ObjectTypeCode.Guid: Write(((Guid)value).ToByteArray()); break; case ObjectTypeCode.DateTimeOffset: Write((DateTimeOffset)value); break; case ObjectTypeCode.Version: { var version = (Version)value; Write((uint)version.Major); Write((uint)version.Minor); Write((uint)version.Build); Write((uint)version.Revision); break; } case ObjectTypeCode.Uri: Write(((Uri)value).AbsoluteUri); break; case ObjectTypeCode.ObjectMap: { var map = (IDictionary)value; WriteDictionary(map); } break; } } }
private void WriteObjectTypeCode(ObjectTypeCode value) { Output.Write((byte)value); }
public object ReadObject(Type objectType, ObjectTypeCode expectedTypeCode, bool isConvertible) { ObjectTypeCode typeCode; if (_objectByte.HasValue) { typeCode = (ObjectTypeCode)_objectByte.Value; _objectByte = null; } else { typeCode = (ObjectTypeCode)ReadByte(); } switch (typeCode) { case ObjectTypeCode.Boolean: return(ConvertIfNecessary(ReadBoolean(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.Byte: return(ConvertIfNecessary(ReadByte(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.UInt16: return(ConvertIfNecessary(ReadUInt16(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.UInt32: return(ConvertIfNecessary(ReadUInt32(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.UInt64: return(ConvertIfNecessary(ReadUInt64(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.SByte: return(ConvertIfNecessary(ReadSByte(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.Int16: return(ConvertIfNecessary(ReadInt16(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.Int32: return(ConvertIfNecessary(ReadInt32(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.Int64: return(ConvertIfNecessary(ReadInt64(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.Char: return(ConvertIfNecessary(ReadChar(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.String: return(ConvertIfNecessary(ReadString(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.Single: return(ConvertIfNecessary(ReadSingle(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.Double: return(ConvertIfNecessary(ReadDouble(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.Decimal: return(ConvertIfNecessary(ReadDecimal(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.DateTime: return(ConvertIfNecessary(ReadDateTime(), objectType, typeCode, expectedTypeCode, isConvertible)); case ObjectTypeCode.ByteArray: return(ReadBytes()); case ObjectTypeCode.CharArray: return(ReadChars()); case ObjectTypeCode.TimeSpan: return(ReadTimeSpan()); case ObjectTypeCode.DateTimeOffset: return(ReadDateTimeOffset()); case ObjectTypeCode.Guid: return(ReadGuid()); case ObjectTypeCode.Version: return(ReadVersion()); case ObjectTypeCode.Uri: return(ReadUri()); case ObjectTypeCode.DBNull: return(DBNull.Value); case ObjectTypeCode.SimpleList: { var objectList = (IList)objectType.New(); ReadList(objectList, objectType.GetGenericArguments()[0]); return(objectList); } case ObjectTypeCode.ObjectMap: { var objectMap = (IDictionary)objectType.New(); var genericArgs = objectType.GetGenericArguments(); ReadDictionary(objectMap, genericArgs[0], genericArgs[1]); return(objectMap); } case ObjectTypeCode.Object: { var deserializer = CreateDelegate(objectType); var dataEntities = deserializer(this, 1); if (dataEntities != null && dataEntities.Length > 0) { return(dataEntities[0]); } return(null); } case ObjectTypeCode.ObjectList: case ObjectTypeCode.PolymorphicObjectList: { var itemCount = (int)ReadUInt32(); var listAspectType = (ListAspectType)ReadByte(); DistinctAttribute distinctAttribute = null; SortedAttribute sortedAttribute = null; if (listAspectType != ListAspectType.None) { var comparerType = GetType(ReadString()); switch (listAspectType) { case ListAspectType.Distinct: distinctAttribute = new DistinctAttribute { EqualityComparerType = comparerType }; break; case (ListAspectType.Sorted | ListAspectType.Distinct): case ListAspectType.Sorted: sortedAttribute = new SortedAttribute { ComparerType = comparerType }; if (listAspectType != ListAspectType.Sorted) { distinctAttribute = new DistinctAttribute(); } break; } } var list = List.Create(objectType, distinctAttribute, sortedAttribute); if (typeCode == ObjectTypeCode.PolymorphicObjectList) { var elementTypes = ReadList <string>().Select(t => Type.GetType(t, false)).ToArray(); for (var i = 0; i < itemCount; i++) { var pos = ReadInt32(); var dataEntity = ReadObject(elementTypes[pos], ObjectTypeCode.Object, false); list.Add(dataEntity); } } else { var deserializer = CreateDelegate(objectType); var dataEntities = deserializer(this, itemCount); for (var i = 0; i < dataEntities.Length; i++) { list.Add(dataEntities[i]); } } return(list); } default: return(null); } }
/// <summary> /// Creates an instance of the BasicMetadataComponent struct with the specified typecode. /// </summary> /// <param name="typeCode">The TypeCode representing this data type.</param> public BasicMetadataComponent(ObjectTypeCode typeCode) { m_TypeCode = typeCode; }
/// <summary> /// Constructs an instance of the metadata element. /// </summary> /// <param name="typeCode">The byte value representing the data type.</param> /// <param name="size">The size of the data (most likely used if it's a string type).</param> public MetadataElement(ObjectTypeCode typeCode, int size, int elemAlignment) { m_TypeCode = typeCode; m_Size = size; m_Alignment = elemAlignment; }
/// <summary> /// Reads the .data metadata section of the JEF file. /// </summary> /// <param name="reader">The BinaryReader instance used to read the file.</param> /// <param name="sectionSize">The size of the section, in bytes.</param> /// <returns>An IEnumerable of metadata elements that describes all elements in the .data section.</returns> private static IEnumerable <MetadataElement> ReadMetadataSection(BinaryReader reader, int sectionSize) { var elemList = new List <MetadataElement>(); int totalBytesRead = 0; int currAlignment = CommonConstants.DEFAULT_ALIGNMENT; while (totalBytesRead < sectionSize) { // read the first byte to determine what the typecode is. ObjectTypeCode typeCodeByte = (ObjectTypeCode)reader.ReadByte(); int size = 0; ++totalBytesRead; // depending on the typecode, the size may be variable. otherwise, it is fixed // to the size of the type represented by the typecode. switch (typeCodeByte) { case ObjectTypeCode.Byte: { size = 1; break; } case ObjectTypeCode.Dword: { size = 8; break; } case ObjectTypeCode.Half: { size = 2; break; } case ObjectTypeCode.Word: { size = 4; break; } // strings include a size word afterward. case ObjectTypeCode.String: { size = reader.ReadInt32(); totalBytesRead += 4; break; } // alignment changes include the new alignment word afterward. case ObjectTypeCode.AlignmentChange: { size = 0; currAlignment = reader.ReadInt32(); totalBytesRead += 4; break; } default: { throw new Exception("Unrecognized typecode \"0x" + typeCodeByte + "\" in disassembled file."); } } var dataElem = new MetadataElement(typeCodeByte, size, currAlignment); elemList.Add(dataElem); } return(elemList); }
public AlignmentChangeMetadataComponent(ObjectTypeCode typeCode, int newAlignment) : base(typeCode) { m_Alignment = newAlignment; }