forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fixheader.cs
53 lines (44 loc) · 1.49 KB
/
fixheader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.IO;
namespace fixheader
{
class fixheader
{
static byte[] data;
static int peOffset;
static void Main(string[] args)
{
Console.WriteLine("fixheader {0}", args[0]);
data = File.ReadAllBytes(args[0]);
peOffset = BitConverter.ToInt32(data, 0x3c);
var corHeaderRva = BitConverter.ToInt32(data, peOffset + 20 + 100 + 14 * 8);
var corHeaderOffset = RvaToOffset(corHeaderRva);
data[corHeaderOffset + 16] |= 2;
File.WriteAllBytes(args[0], data);
}
static int RvaToOffset(int va)
{
var numSections = BitConverter.ToInt16(data, peOffset + 6);
var numDataDirectories = BitConverter.ToInt32(data, peOffset + 24 + 92);
var sectionTableStart = peOffset + 24 + 96 + 8 * numDataDirectories;
for (var i = 0; i < numSections; i++)
{
var virtualSize = BitConverter.ToInt32(data, sectionTableStart + 40 * i + 8);
var virtualAddr = BitConverter.ToInt32(data, sectionTableStart + 40 * i + 12);
var fileOffset = BitConverter.ToInt32(data, sectionTableStart + 40 * i + 20);
if (va >= virtualAddr && va < virtualAddr + virtualSize)
return va - virtualAddr + fileOffset;
}
return 0;
}
}
}