/// <inheritdoc/> public virtual void SetOffset(FileOffset offset, RVA rva) { setOffsetCalled = true; this.offset = offset; this.rva = rva; length = 0; virtualSize = 0; foreach (var elem in chunks) { uint paddingF = (uint)offset.AlignUp(elem.alignment) - (uint)offset; uint paddingV = (uint)rva.AlignUp(elem.alignment) - (uint)rva; offset += paddingF; rva += paddingV; elem.chunk.SetOffset(offset, rva); if (elem.chunk.GetVirtualSize() == 0) { offset -= paddingF; rva -= paddingV; } else { uint chunkLenF = elem.chunk.GetFileLength(); uint chunkLenV = elem.chunk.GetVirtualSize(); offset += chunkLenF; rva += chunkLenV; length += paddingF + chunkLenF; virtualSize += paddingV + chunkLenV; } } }
/// <summary> /// Writes all chunks to <paramref name="writer"/> /// </summary> /// <param name="writer">The writer</param> /// <param name="chunks">All chunks</param> /// <param name="offset">File offset of first chunk</param> /// <param name="fileAlignment">File alignment</param> protected void WriteChunks(BinaryWriter writer, List <IChunk> chunks, FileOffset offset, uint fileAlignment) { foreach (var chunk in chunks) { chunk.VerifyWriteTo(writer); offset += chunk.GetFileLength(); var newOffset = offset.AlignUp(fileAlignment); writer.WriteZeros((int)(newOffset - offset)); offset = newOffset; } }
/// <summary> /// Calculates <see cref="RVA"/> and <see cref="FileOffset"/> of all <see cref="IChunk"/>s /// </summary> /// <param name="chunks">All chunks</param> /// <param name="offset">Starting file offset</param> /// <param name="rva">Starting RVA</param> /// <param name="fileAlignment">File alignment</param> /// <param name="sectionAlignment">Section alignment</param> protected void CalculateRvasAndFileOffsets(List <IChunk> chunks, FileOffset offset, RVA rva, uint fileAlignment, uint sectionAlignment) { foreach (var chunk in chunks) { chunk.SetOffset(offset, rva); offset += chunk.GetFileLength(); rva += chunk.GetVirtualSize(); offset = offset.AlignUp(fileAlignment); rva = rva.AlignUp(sectionAlignment); } }
/// <inheritdoc/> public void WriteTo(BinaryWriter writer) { FileOffset offset2 = offset; foreach (var elem in chunks) { int paddingF = (int)offset2.AlignUp(elem.alignment) - (int)offset2; writer.WriteZeros(paddingF); elem.chunk.VerifyWriteTo(writer); offset2 += (uint)paddingF + elem.chunk.GetFileLength(); } }
/// <summary> /// Writes all chunks to <paramref name="writer"/> /// </summary> /// <param name="writer">The writer</param> /// <param name="chunks">All chunks</param> /// <param name="offset">File offset of first chunk</param> /// <param name="fileAlignment">File alignment</param> protected void WriteChunks(BinaryWriter writer, List <IChunk> chunks, FileOffset offset, uint fileAlignment) { foreach (var chunk in chunks) { chunk.VerifyWriteTo(writer); // If it has zero size, it's not present in the file (eg. a section that wasn't needed) if (chunk.GetVirtualSize() != 0) { offset += chunk.GetFileLength(); var newOffset = offset.AlignUp(fileAlignment); writer.WriteZeros((int)(newOffset - offset)); offset = newOffset; } } }
/// <summary> /// Calculates <see cref="RVA"/> and <see cref="FileOffset"/> of all <see cref="IChunk"/>s /// </summary> /// <param name="chunks">All chunks</param> /// <param name="offset">Starting file offset</param> /// <param name="rva">Starting RVA</param> /// <param name="fileAlignment">File alignment</param> /// <param name="sectionAlignment">Section alignment</param> protected void CalculateRvasAndFileOffsets(List <IChunk> chunks, FileOffset offset, RVA rva, uint fileAlignment, uint sectionAlignment) { foreach (var chunk in chunks) { chunk.SetOffset(offset, rva); // If it has zero size, it's not present in the file (eg. a section that wasn't needed) if (chunk.GetVirtualSize() != 0) { offset += chunk.GetFileLength(); rva += chunk.GetVirtualSize(); offset = offset.AlignUp(fileAlignment); rva = rva.AlignUp(sectionAlignment); } } }
/// <inheritdoc /> public virtual void SetOffset(FileOffset offset, RVA rva) { setOffsetCalled = true; FileOffset = offset; RVA = rva; length = 0; virtualSize = 0; foreach (var elem in chunks) { var paddingF = (uint)offset.AlignUp(elem.alignment) - (uint)offset; var paddingV = (uint)rva.AlignUp(elem.alignment) - (uint)rva; offset += paddingF; rva += paddingV; elem.chunk.SetOffset(offset, rva); var chunkLenF = elem.chunk.GetFileLength(); var chunkLenV = elem.chunk.GetVirtualSize(); offset += chunkLenF; rva += chunkLenV; length += paddingF + chunkLenF; virtualSize += paddingV + chunkLenV; } }
/// <summary> /// Writes all chunks to <paramref name="writer"/> /// </summary> /// <param name="writer">The writer</param> /// <param name="chunks">All chunks</param> /// <param name="offset">File offset of first chunk</param> /// <param name="fileAlignment">File alignment</param> protected void WriteChunks(BinaryWriter writer, List<IChunk> chunks, FileOffset offset, uint fileAlignment) { foreach (var chunk in chunks) { chunk.VerifyWriteTo(writer); offset += chunk.GetFileLength(); var newOffset = offset.AlignUp(fileAlignment); writer.WriteZeros((int)(newOffset - offset)); offset = newOffset; } }
/// <summary> /// Calculates <see cref="RVA"/> and <see cref="FileOffset"/> of all <see cref="IChunk"/>s /// </summary> /// <param name="chunks">All chunks</param> /// <param name="offset">Starting file offset</param> /// <param name="rva">Starting RVA</param> /// <param name="fileAlignment">File alignment</param> /// <param name="sectionAlignment">Section alignment</param> protected void CalculateRvasAndFileOffsets(List<IChunk> chunks, FileOffset offset, RVA rva, uint fileAlignment, uint sectionAlignment) { foreach (var chunk in chunks) { chunk.SetOffset(offset, rva); offset += chunk.GetFileLength(); rva += chunk.GetVirtualSize(); offset = offset.AlignUp(fileAlignment); rva = rva.AlignUp(sectionAlignment); } }
static void Align(ref FileOffset offset, ref RVA rva) { offset = offset.AlignUp(HH_ALIGNMENT); rva = rva.AlignUp(HH_ALIGNMENT); }