/* Copyright (C) 2014-2019 de4dot@gmail.com This file is part of dnSpy dnSpy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. dnSpy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ using System; using System.Globalization; using dnSpy.Contracts.Hex.Text; namespace dnSpy.Contracts.Hex.Files.PE { /// /// 32-bit Unix (epoch) time /// public class UnixTime32Data : UInt32Data { /// /// Constructor /// /// Data span public UnixTime32Data(HexBufferSpan span) : base(span) { if (span.Length != 4) throw new ArgumentOutOfRangeException(nameof(span)); } /// /// Constructor /// /// Buffer /// Position public UnixTime32Data(HexBuffer buffer, HexPosition position) : this(new HexBufferSpan(buffer, new HexSpan(position, 4))) { } /// /// Reads the value as a /// /// public DateTime ReadDateTime() => Epoch.AddSeconds(ReadValue()); static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); /// /// Writes the value /// /// Formatter public override void WriteValue(HexFieldFormatter formatter) { formatter.WriteUInt32(ReadValue()); formatter.WriteSpace(); formatter.Write("(", PredefinedClassifiedTextTags.Punctuation); formatter.Write(ReadDateTime().ToLocalTime().ToString(CultureInfo.CurrentCulture.DateTimeFormat), PredefinedClassifiedTextTags.Text); formatter.Write(")", PredefinedClassifiedTextTags.Punctuation); } } /// /// RVA data /// public class RvaData : UInt32Data { /// /// Constructor /// /// Data span public RvaData(HexBufferSpan span) : base(span) { } /// /// Constructor /// /// Buffer /// Position public RvaData(HexBuffer buffer, HexPosition position) : base(buffer, position) { } /// /// Returns the span the field value references or null. The span can be empty. /// /// File /// public override HexSpan? GetFieldReferenceSpan(HexBufferFile file) { var peHeaders = file.GetHeaders(); if (peHeaders is null) return null; var rva = ReadValue(); if (rva == 0) return null; return new HexSpan(peHeaders.RvaToBufferPosition(rva), 0); } } /// /// 32-bit file offset /// public class FileOffsetData : UInt32Data { /// /// Constructor /// /// Data span public FileOffsetData(HexBufferSpan span) : base(span) { } /// /// Constructor /// /// Buffer /// Position public FileOffsetData(HexBuffer buffer, HexPosition position) : base(buffer, position) { } /// /// Returns the span the field value references or null. The span can be empty. /// /// File /// public override HexSpan? GetFieldReferenceSpan(HexBufferFile file) { var offset = ReadValue(); if (offset == 0) return null; var pos = file.GetHeaders()?.FilePositionToBufferPosition(offset) ?? HexPosition.Min(HexPosition.MaxEndPosition - 1, file.Span.Start + offset); return new HexSpan(pos, 0); } } }