/*
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;
namespace dnSpy.Contracts.Hex {
///
/// A buffer stream with less methods
///
public abstract class HexSimpleBufferStream : IDisposable {
///
/// Constructor
///
protected HexSimpleBufferStream() { }
///
/// true if the content can change at any time
///
public abstract bool IsVolatile { get; }
///
/// true if it's a read-only stream
///
public abstract bool IsReadOnly { get; }
///
/// Gets the span
///
public abstract HexSpan Span { get; }
///
/// Gets the name. This could be the filename if the data was read from a file
///
public abstract string Name { get; }
///
/// Gets the page size of the underlying data store or 0 if it's unknown. Eg. if it's
/// memory in some process, can be returned
/// here. The returned value must be 0 or a power of 2.
///
public virtual ulong PageSize => 0;
///
/// Gets information about a position in the stream
///
/// Position
///
public abstract HexSpanInfo GetSpanInfo(HexPosition position);
///
/// Gets information about a position in the stream
///
/// Position
/// Span of all valid data
///
protected HexSpanInfo GetSpanInfo(HexPosition position, HexSpan validSpan) {
if (position >= HexPosition.MaxEndPosition)
throw new ArgumentOutOfRangeException(nameof(position));
if (position >= validSpan.End)
return new HexSpanInfo(HexSpan.FromBounds(validSpan.End, HexPosition.MaxEndPosition), HexSpanInfoFlags.None);
else if (position < validSpan.Start)
return new HexSpanInfo(HexSpan.FromBounds(HexPosition.Zero, validSpan.Start), HexSpanInfoFlags.None);
else
return new HexSpanInfo(validSpan, HexSpanInfoFlags.HasData);
}
///
/// Reads bytes. Returns number of bytes read.
///
/// Position
/// Destination array
/// Index
/// Length
///
public abstract HexPosition Read(HexPosition position, byte[] destination, long destinationIndex, long length);
///
/// Writes bytes. Returns number of bytes written.
///
/// Position
/// Data
/// Index
/// Length
///
public abstract HexPosition Write(HexPosition position, byte[] source, long sourceIndex, long length);
///
/// Clears the cache if it uses a cache
///
public virtual void ClearCache() { }
///
/// Raised after it is disposed
///
public event EventHandler? Disposed;
///
/// true if the instance has been disposed
///
public bool IsDisposed { get; private set; }
///
/// Disposes this instance
///
public void Dispose() {
if (IsDisposed)
return;
IsDisposed = true;
DisposeCore();
Disposed?.Invoke(this, EventArgs.Empty);
}
///
/// Disposes this instance
///
protected virtual void DisposeCore() { }
}
}