using ProcessHacker.Native.Api;
using ProcessHacker.Native.Objects;
using ProcessHacker.Native.Security;
namespace ProcessHacker.Native
{
///
/// Represents a section, a memory mapping.
///
public sealed class Section : NativeObject
{
private MemoryProtection _originalProtection = MemoryProtection.ReadWrite;
///
/// Opens an existing section.
///
/// The name of an existing section.
/// The desired access to the section.
public Section(string name, SectionAccess access)
{
this.Handle = new SectionHandle(name, access);
}
///
/// Creates a section backed by a file.
///
/// A file handle.
public Section(FileHandle fileHandle)
: this(fileHandle, MemoryProtection.ReadWrite)
{ }
///
/// Creates a section backed by a file.
///
/// A file handle.
/// The page protection to apply to mappings.
public Section(FileHandle fileHandle, MemoryProtection protection)
: this(fileHandle, false, protection)
{ }
///
/// Creates a section backed by a file.
///
/// A file handle.
/// Whether to treat the file as an executable image.
/// The page protection to apply to mappings.
public Section(FileHandle fileHandle, bool image, MemoryProtection protection)
: this(null, fileHandle, image, protection)
{ }
///
/// Creates a section backed by a file.
///
/// The name of the section.
/// A file handle.
/// Whether to treat the file as an executable image.
/// The page protection to apply to mappings.
public Section(string name, FileHandle fileHandle, bool image, MemoryProtection protection)
{
_originalProtection = protection;
this.Handle = SectionHandle.Create(
SectionAccess.All,
name,
ObjectFlags.OpenIf,
null,
fileHandle.GetSize(),
image ? SectionAttributes.Image : SectionAttributes.Commit,
protection,
fileHandle
);
}
///
/// Creates a section backed by the page file (i.e. in memory).
///
/// The maximum size of the section.
public Section(long maximumSize)
: this(maximumSize, MemoryProtection.ReadWrite)
{ }
///
/// Creates a section backed by the page file (i.e. in memory).
///
/// The maximum size of the section.
/// The page protection to apply to mappings.
public Section(long maximumSize, MemoryProtection protection)
: this(null, maximumSize, protection)
{ }
///
/// Creates a section backed by the page file (i.e. in memory).
///
/// The name of the section.
/// The maximum size of the section.
/// The page protection to apply to mappings.
public Section(string name, long maximumSize, MemoryProtection protection)
{
_originalProtection = protection;
this.Handle = SectionHandle.Create(
SectionAccess.All,
name,
ObjectFlags.OpenIf,
null,
maximumSize,
SectionAttributes.Commit,
protection,
null
);
}
///
/// Extends the size of the section.
///
/// The new size of the section.
public void Extend(long newSize)
{
this.Handle.Extend(newSize);
}
///
/// Creates a view of the section.
///
///
/// The number of bytes to map. This value will be rounded up to the
/// page size.
///
/// A view of the section.
public SectionView MapView(int size)
{
return this.MapView(size, _originalProtection);
}
///
/// Creates a view of the section.
///
///
/// The number of bytes to map. This value will be rounded up to the
/// page size.
///
/// The page protection to apply to the mapping.
/// A view of the section.
public SectionView MapView(int size, MemoryProtection protection)
{
return this.Handle.MapView(0, size, protection);
}
///
/// Creates a view of the section.
///
///
/// The offset from the beginning of the section to map. This value
/// must be a multiple of 0x10000 (65536).
///
///
/// The number of bytes to map. This value will be rounded up to the
/// page size.
///
/// The page protection to apply to the mapping.
/// A view of the section.
public SectionView MapView(int offset, int size, MemoryProtection protection)
{
return this.Handle.MapView(offset, size, protection);
}
}
}