// dnlib: See LICENSE.txt for more info
using System;
using dnlib.IO;
using dnlib.PE;
namespace dnlib.DotNet.Writer {
///
/// Stores a byte array
///
public sealed class ByteArrayChunk : IReuseChunk {
readonly byte[] array;
readonly uint alignment;
FileOffset offset;
RVA rva;
///
public FileOffset FileOffset => offset;
///
public RVA RVA => rva;
///
/// Gets the data
///
public byte[] Data => array;
///
/// Constructor
///
/// The data. It will be owned by this instance and can't be modified by
/// other code if this instance is inserted as a key in a dictionary (because
/// return value will be different if you modify the array). If
/// it's never inserted as a key in a dictionary, then the contents can be modified,
/// but shouldn't be resized after has been called.
/// The alignment of the data
public ByteArrayChunk(byte[] array, uint alignment = 0) {
this.array = array ?? Array2.Empty();
this.alignment = alignment;
}
bool IReuseChunk.CanReuse(RVA origRva, uint origSize) => (uint)array.Length <= origSize;
///
public void SetOffset(FileOffset offset, RVA rva) {
this.offset = offset;
this.rva = rva;
}
///
public uint GetFileLength() => (uint)array.Length;
///
public uint GetVirtualSize() => GetFileLength();
///
public uint CalculateAlignment() => alignment;
///
public void WriteTo(DataWriter writer) => writer.WriteBytes(array);
///
public override int GetHashCode() => Utils.GetHashCode(array);
///
public override bool Equals(object obj) {
var other = obj as ByteArrayChunk;
return other is not null && Utils.Equals(array, other.array);
}
}
}