-
Notifications
You must be signed in to change notification settings - Fork 4
/
PESectionList.cs
115 lines (100 loc) · 3.46 KB
/
PESectionList.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
namespace PETools
{
public class PESectionList
{
OrderedDictionary sectionDict;
public PESectionList()
{
sectionDict = new OrderedDictionary();
}
public void Add(PESection section)
{
string groupName = section.Name;
if (groupName.Contains("$"))
{
groupName = groupName.Substring(0, groupName.IndexOf('$'));
}
ArrayList sectionGroup = null;
if (sectionDict.Contains(groupName))
{
sectionGroup = (ArrayList)sectionDict[groupName];
}
else
{
sectionGroup = new ArrayList();
sectionDict.Add(groupName, sectionGroup);
}
int index = 0;
foreach (PESection s in sectionGroup)
{
if (PESection.Compare(section, s) < 0)
{
sectionGroup.Insert(index, section);
return;
}
index++;
}
sectionGroup.Add(section);
}
public void AddRange(List<PESection> range)
{
foreach (PESection s in range)
this.Add(s);
}
public List<PESection> MergeSections()
{
List<PESection> mergedSections = new List<PESection>();
IDictionaryEnumerator group = sectionDict.GetEnumerator();
String str = String.Empty;
while (group.MoveNext())
{
PESection s = MergeSectionGroup((ArrayList)group.Value);
s.Name = (String)group.Key;
mergedSections.Add(s);
}
return mergedSections;
}
PESection MergeSectionGroup(ArrayList group)
{
// Create new section, copy the header from the first
// section to get us started.
PESection section = new PESection(((PESection)group[0]).Header);
// Calculate total raw section size
int sectionSize = 0;
foreach (PESection s in group)
sectionSize += s.Data.Length;
byte[] sectionData = new byte[sectionSize];
int offset = 0;
foreach (PESection s in group)
{
Array.Copy(s.Data, 0, sectionData, offset, s.Data.Length);
offset += s.Data.Length;
}
section.Data = sectionData;
section.RawSize = (uint)sectionSize;
section.PhysicalAddress = 0;
return section;
}
public override string ToString()
{
IDictionaryEnumerator group = sectionDict.GetEnumerator();
String str = String.Empty;
while (group.MoveNext())
{
str += String.Format("Section Group: {0}:\n", group.Key);
ArrayList sections = (ArrayList)group.Value;
foreach (PESection section in sections)
{
str += String.Format("\tSource: {0}\t\tSection: {1}\n",
section.SourceCoff.SourceFile, section.Name);
}
}
return str;
}
}
}