/*
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 dnSpy.Contracts.Hex.Text;
namespace dnSpy.Contracts.Hex.Files.DotNet {
///
/// A
///
public class DateTimeData : SimpleData {
///
/// Constructor
///
/// Data span
public DateTimeData(HexBufferSpan span)
: base(span) {
if (span.Length != 8)
throw new ArgumentOutOfRangeException(nameof(span));
}
///
/// Constructor
///
/// Buffer
/// Position
public DateTimeData(HexBuffer buffer, HexPosition position)
: this(new HexBufferSpan(buffer, new HexSpan(position, 8))) {
}
static DateTime ReadDateTime(HexBuffer buffer, HexPosition position) =>
DateTime.FromBinary(buffer.ReadInt64(position));
///
/// Reads the value
///
///
public DateTime ReadValue() => ReadDateTime(Span.Buffer, Span.Start);
///
/// Writes the value
///
/// Formatter
public override void WriteValue(HexFieldFormatter formatter) {
formatter.WriteUInt64(Span.Buffer.ReadUInt64(Span.Start));
formatter.WriteSpace();
formatter.Write("(", PredefinedClassifiedTextTags.Punctuation);
formatter.Write(ReadValue().ToString(), PredefinedClassifiedTextTags.Text);
formatter.Write(")", PredefinedClassifiedTextTags.Punctuation);
}
}
///
/// A
///
public class TimeSpanData : SimpleData {
///
/// Constructor
///
/// Data span
public TimeSpanData(HexBufferSpan span)
: base(span) {
if (span.Length != 8)
throw new ArgumentOutOfRangeException(nameof(span));
}
///
/// Constructor
///
/// Buffer
/// Position
public TimeSpanData(HexBuffer buffer, HexPosition position)
: this(new HexBufferSpan(buffer, new HexSpan(position, 8))) {
}
static TimeSpan ReadTimeSpan(HexBuffer buffer, HexPosition position) =>
new TimeSpan(buffer.ReadInt64(position));
///
/// Reads the value
///
///
public TimeSpan ReadValue() => ReadTimeSpan(Span.Buffer, Span.Start);
///
/// Writes the value
///
/// Formatter
public override void WriteValue(HexFieldFormatter formatter) {
formatter.WriteUInt64(Span.Buffer.ReadUInt64(Span.Start));
formatter.WriteSpace();
formatter.Write("(", PredefinedClassifiedTextTags.Punctuation);
formatter.Write(ReadValue().ToString(), PredefinedClassifiedTextTags.Text);
formatter.Write(")", PredefinedClassifiedTextTags.Punctuation);
}
}
///
/// Portable PDB Id (20 bytes)
///
public sealed class PortablePdbIdData : SimpleData {
///
/// Constructor
///
/// Data span
public PortablePdbIdData(HexBufferSpan span)
: base(span) {
if (span.Length != 20)
throw new ArgumentOutOfRangeException();
}
///
/// Constructor
///
/// Buffer
/// Position
public PortablePdbIdData(HexBuffer buffer, HexPosition position)
: this(new HexBufferSpan(buffer, new HexSpan(position, 20))) {
}
///
/// Writes the value
///
/// Formatter
public override void WriteValue(HexFieldFormatter formatter) {
var buffer = Span.Buffer;
var pos = Span.Span.Start;
var end = Span.Span.End;
while (pos < end)
formatter.Write(buffer.ReadByte(pos++).ToString("X2"), PredefinedClassifiedTextTags.Number);
}
}
}