This repository was archived by the owner on May 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPatchTypes.cs
More file actions
152 lines (132 loc) · 5.13 KB
/
PatchTypes.cs
File metadata and controls
152 lines (132 loc) · 5.13 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
namespace CloudFix
{
internal record PatchEntry(int Offset, byte[] Original, byte[] Replacement);
internal enum PatchState
{
NotInstalled,
Unpatched,
Patched,
PartiallyPatched,
UnknownVersion,
PayloadCorrupt,
OutOfDate,
}
internal class PatchResult
{
public bool Succeeded { get; set; }
public bool DllPatched { get; set; }
public bool CachePatched { get; set; }
public string Error { get; set; }
public PatchResult Fail(string error)
{
Succeeded = false;
Error = error;
return this;
}
}
internal readonly struct PeSection
{
public readonly string Name;
public readonly int VirtualAddress;
public readonly int VirtualSize;
public readonly int RawOffset;
public readonly int RawSize;
public PeSection(string name, int va, int vsize, int raw, int rawSize)
{
Name = name;
VirtualAddress = va;
VirtualSize = vsize;
RawOffset = raw;
RawSize = rawSize;
}
public static PeSection[] Parse(byte[] pe)
{
if (pe.Length < 64) return Array.Empty<PeSection>();
int peOff = BitConverter.ToInt32(pe, 0x3C);
if (peOff < 0 || peOff + 24 > pe.Length) return Array.Empty<PeSection>();
if (pe[peOff] != 'P' || pe[peOff + 1] != 'E') return Array.Empty<PeSection>();
int numSections = BitConverter.ToUInt16(pe, peOff + 6);
if (numSections > 96) return Array.Empty<PeSection>();
int optSize = BitConverter.ToUInt16(pe, peOff + 20);
int firstSection = peOff + 24 + optSize;
if (firstSection > pe.Length) return Array.Empty<PeSection>();
var result = new PeSection[numSections];
int populated = 0;
for (int i = 0; i < numSections; i++)
{
int off = firstSection + i * 40;
if (off + 40 > pe.Length) break;
int nameEnd = 0;
for (int j = 0; j < 8 && pe[off + j] != 0; j++) nameEnd = j + 1;
string name = System.Text.Encoding.ASCII.GetString(pe, off, nameEnd);
int vsize = BitConverter.ToInt32(pe, off + 8);
int va = BitConverter.ToInt32(pe, off + 12);
int rawSize = BitConverter.ToInt32(pe, off + 16);
int rawPtr = BitConverter.ToInt32(pe, off + 20);
result[i] = new PeSection(name, va, vsize, rawPtr, rawSize);
populated++;
}
if (populated < numSections)
Array.Resize(ref result, populated);
return result;
}
public static PeSection? Find(PeSection[] sections, string name)
{
for (int i = 0; i < sections.Length; i++)
if (sections[i].Name == name) return sections[i];
return null;
}
// file offset -> RVA, returns -1 if outside any section
public static int FileOffsetToRva(PeSection[] sections, int fileOffset)
{
for (int i = 0; i < sections.Length; i++)
{
var s = sections[i];
if (fileOffset >= s.RawOffset && fileOffset < s.RawOffset + s.RawSize)
return s.VirtualAddress + (fileOffset - s.RawOffset);
}
return -1;
}
// RVA -> file offset, returns -1 if in BSS or outside any section
public static int RvaToFileOffset(PeSection[] sections, int rva)
{
for (int i = 0; i < sections.Length; i++)
{
var s = sections[i];
int size = Math.Max(s.VirtualSize, s.RawSize);
if (rva >= s.VirtualAddress && rva < s.VirtualAddress + size)
{
int offsetInSection = rva - s.VirtualAddress;
if (offsetInSection >= s.RawSize)
return -1; // in BSS / zero-fill region, no file backing
return s.RawOffset + offsetInSection;
}
}
return -1;
}
public static PeSection? FindByFileOffset(PeSection[] sections, int fileOffset)
{
for (int i = 0; i < sections.Length; i++)
{
var s = sections[i];
if (fileOffset >= s.RawOffset && fileOffset < s.RawOffset + s.RawSize)
return s;
}
return null;
}
public static long GetImageBase(byte[] pe)
{
if (pe.Length < 64) return 0;
int peOff = BitConverter.ToInt32(pe, 0x3C);
if (peOff < 0 || peOff + 24 > pe.Length) return 0;
if (pe[peOff] != 'P' || pe[peOff + 1] != 'E') return 0;
int magic = BitConverter.ToUInt16(pe, peOff + 24);
if (magic == 0x20B) // PE32+
return BitConverter.ToInt64(pe, peOff + 24 + 24);
if (magic == 0x10B) // PE32
return BitConverter.ToInt32(pe, peOff + 24 + 28);
return 0;
}
}
}