/// <inheritdoc /> public override void Write(IBinaryStreamWriter writer) { foreach (var member in _members) { ulong value = member.IsImportByName ? _hintNameTable.GetHintNameRva(member) : member.Ordinal | (1UL << (Is32Bit ? 31 : 63)); if (Is32Bit) { writer.WriteUInt32((uint)value); } else { writer.WriteUInt64(value); } } if (Is32Bit) { writer.WriteUInt32(0); } else { writer.WriteUInt64(0); } }
/// <summary> /// Writes a specified amount of zero bytes to the stream. /// </summary> /// <param name="writer">The writer to use.</param> /// <param name="count">The amount of zeroes to write.</param> public static void WriteZeroes(this IBinaryStreamWriter writer, int count) { while (count >= sizeof(ulong)) { writer.WriteUInt64(0); count -= sizeof(ulong); } while (count >= sizeof(uint)) { writer.WriteUInt32(0); count -= sizeof(uint); } while (count >= sizeof(ushort)) { writer.WriteUInt16(0); count -= sizeof(ushort); } while (count >= sizeof(byte)) { writer.WriteByte(0); count -= sizeof(byte); } }
+ sizeof(ulong); // FileDate /// <inheritdoc /> public override void Write(IBinaryStreamWriter writer) { writer.WriteUInt32(Signature); writer.WriteUInt32(DefaultStructVersion); WriteVersion(writer, FileVersion); WriteVersion(writer, ProductVersion); writer.WriteUInt32((uint)FileFlagsMask); writer.WriteUInt32((uint)FileFlags); writer.WriteUInt32((uint)FileOS); writer.WriteUInt32((uint)FileType); writer.WriteUInt32((uint)FileSubType); writer.WriteUInt64(FileDate); }
/// <inheritdoc /> public void Write(IBinaryStreamWriter writer) { foreach (var token in Items) { if (Type.HasFlag(VTableType.VTable32Bit)) { writer.WriteUInt32(token.ToUInt32()); } else { writer.WriteUInt64(token.ToUInt32()); } } }
/// <inheritdoc /> public override void Write(IBinaryStreamWriter writer) { ulong start = writer.Offset; writer.WriteUInt16((ushort)Magic); writer.WriteByte(MajorLinkerVersion); writer.WriteByte(MinorLinkerVersion); writer.WriteUInt32(SizeOfCode); writer.WriteUInt32(SizeOfInitializedData); writer.WriteUInt32(SizeOfUninitializedData); writer.WriteUInt32(AddressOfEntrypoint); writer.WriteUInt32(BaseOfCode); switch (Magic) { case OptionalHeaderMagic.Pe32: writer.WriteUInt32(BaseOfData); writer.WriteUInt32((uint)ImageBase); break; case OptionalHeaderMagic.Pe32Plus: writer.WriteUInt64(ImageBase); break; default: throw new BadImageFormatException("Unrecognized or unsupported optional header format."); } writer.WriteUInt32(SectionAlignment); writer.WriteUInt32(FileAlignment); writer.WriteUInt16(MajorOperatingSystemVersion); writer.WriteUInt16(MinorOperatingSystemVersion); writer.WriteUInt16(MajorImageVersion); writer.WriteUInt16(MinorImageVersion); writer.WriteUInt16(MajorSubsystemVersion); writer.WriteUInt16(MinorSubsystemVersion); writer.WriteUInt32(Win32VersionValue); writer.WriteUInt32(SizeOfImage); writer.WriteUInt32(SizeOfHeaders); writer.WriteUInt32(CheckSum); writer.WriteUInt16((ushort)SubSystem); writer.WriteUInt16((ushort)DllCharacteristics); if (Magic == OptionalHeaderMagic.Pe32) { writer.WriteUInt32((uint)SizeOfStackReserve); writer.WriteUInt32((uint)SizeOfStackCommit); writer.WriteUInt32((uint)SizeOfHeapReserve); writer.WriteUInt32((uint)SizeOfHeapCommit); } else { writer.WriteUInt64(SizeOfStackReserve); writer.WriteUInt64(SizeOfStackCommit); writer.WriteUInt64(SizeOfHeapReserve); writer.WriteUInt64(SizeOfHeapCommit); } writer.WriteUInt32(LoaderFlags); writer.WriteUInt32(NumberOfRvaAndSizes); foreach (var directory in DataDirectories) { directory.Write(writer); } writer.WriteZeroes((int)(GetPhysicalSize() - (writer.Offset - start))); }
private void WriteValue(IBinaryStreamWriter writer, TypeSignature argumentType, ITypeCodedIndexProvider provider, object value) { if (argumentType.IsTypeOf("System", "Type")) { writer.WriteSerString(TypeNameBuilder.GetAssemblyQualifiedName((TypeSignature)value)); return; } switch (argumentType.ElementType) { case ElementType.Boolean: writer.WriteByte((byte)((bool)value ? 1 : 0)); break; case ElementType.Char: writer.WriteUInt16((char)value); break; case ElementType.I1: writer.WriteSByte((sbyte)value); break; case ElementType.U1: writer.WriteByte((byte)value); break; case ElementType.I2: writer.WriteInt16((short)value); break; case ElementType.U2: writer.WriteUInt16((ushort)value); break; case ElementType.I4: writer.WriteInt32((int)value); break; case ElementType.U4: writer.WriteUInt32((uint)value); break; case ElementType.I8: writer.WriteInt64((long)value); break; case ElementType.U8: writer.WriteUInt64((ulong)value); break; case ElementType.R4: writer.WriteSingle((float)value); break; case ElementType.R8: writer.WriteDouble((double)value); break; case ElementType.String: writer.WriteSerString(value as string); break; case ElementType.Object: var valueType = value.GetType(); var innerTypeSig = argumentType.Module.CorLibTypeFactory.FromName(valueType.Namespace, valueType.Name); TypeSignature.WriteFieldOrPropType(writer, innerTypeSig); WriteValue(writer, innerTypeSig, provider, value); break; case ElementType.Class: case ElementType.Enum: case ElementType.ValueType: var enumTypeDef = argumentType.Resolve(); if (enumTypeDef != null && enumTypeDef.IsEnum) { WriteValue(writer, enumTypeDef.GetEnumUnderlyingType(), provider, Value); } else { throw new NotImplementedException(); } break; default: throw new ArgumentOutOfRangeException(); } }
/// <inheritdoc /> public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer) { if (Value == null) { writer.WriteSerString(null); return; } switch (Type.GetTypeCode(Value.GetType())) { case TypeCode.Boolean: writer.WriteByte((byte)((bool)Value ? 1 : 0)); break; case TypeCode.Byte: writer.WriteByte((byte)Value); break; case TypeCode.Char: writer.WriteUInt16((char)Value); break; case TypeCode.Double: writer.WriteDouble((double)Value); break; case TypeCode.Int16: writer.WriteInt16((short)Value); break; case TypeCode.Int32: writer.WriteInt32((int)Value); break; case TypeCode.Int64: writer.WriteInt64((long)Value); break; case TypeCode.SByte: writer.WriteSByte((sbyte)Value); break; case TypeCode.Single: writer.WriteSingle((float)Value); break; case TypeCode.String: writer.WriteSerString((string)Value); break; case TypeCode.UInt16: writer.WriteUInt16((ushort)Value); break; case TypeCode.UInt32: writer.WriteUInt32((uint)Value); break; case TypeCode.UInt64: writer.WriteUInt64((ulong)Value); break; default: if (Value is TypeSignature typeSignature) { writer.WriteSerString(TypeNameBuilder.GetAssemblyQualifiedName(typeSignature)); } else { throw new NotSupportedException(); } break; } }