-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdol.cpp
155 lines (143 loc) · 3.36 KB
/
dol.cpp
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
153
154
#include "dol.hpp"
void FixDolHeaderEndian(dolheader * header)
{
for(int ii=0; ii<7; ii++)
{
header->offsetText[ii] = be32(header->offsetText[ii]);
header->addressText[ii] =be32(header->addressText[ii]);
header->sizeText[ii] = be32(header->sizeText[ii]);
}
for(int ii=0; ii<11; ii++)
{
header->offsetData[ii] = be32(header->offsetData[ii]);
header->addressData[ii] =be32(header->addressData[ii]);
header->sizeData[ii] = be32(header->sizeData[ii]);
}
header->addressBSS = be32(header->addressBSS);
header->sizeBSS = be32(header->sizeBSS);
header->entrypoint = be32(header->entrypoint);
}
u32 GetMemoryAddressDol( char* buffer, u32 offset)
{
dolheader header;
memcpy(&header, buffer, sizeof(header));
FixDolHeaderEndian(&header);
u32 addresses[18];
u32 offsets[18];
memset(addresses, 0, 18);
memset(offsets, 0, 18);
u32 counter = 0;
for(int ii=0; ii<7; ii++) {
if(header.addressText[ii] == 0)
continue;
offsets[counter] = header.offsetText[ii];
addresses[counter] = header.addressText[ii];
counter++;
}
for(int ii=0; ii<11; ii++) {
if(header.addressText[ii] == 0)
continue;
offsets[counter] = header.offsetData[ii];
addresses[counter] = header.addressData[ii];
counter++;
}
for(u32 ii=0; ii<counter; ii++) {
for(u32 jj=1; jj<counter; jj++) {
if((offsets[jj] < offsets[jj-1]) ||
(offsets[jj-1] == 0)) {
u32 temp = offsets[jj-1];
offsets[jj-1] = offsets[jj];
offsets[jj] = temp;
temp = addresses[jj-1];
addresses[jj-1] = addresses[jj];
addresses[jj] = temp;
}
}
}
u32 mem = 0;
u32 j = 0;
for(u32 i=0; i<counter; i++, j++)
{
#ifdef DEBUG
cout << "offsetText[" << i << "]: "
<< offsets[i];
cout << "\t";
cout << "addressText[" << i << "]: "
<< addresses[i] << endl;
#endif
if((offsets[i] > offset) ||
(offsets[i] == 0))
break;
}
j--;
offset += addresses[j];
offset -= offsets[j];
#ifdef DEBUG
cout << "Memory address: 0x"
<< hex << offset <<endl;
#endif
mem = offset;
return mem;
}
u32 GetFileOffsetDol( char* buffer, u32 address)
{
dolheader header;
memcpy(&header, buffer, sizeof(header));
FixDolHeaderEndian(&header);
u32 addresses[18];
u32 offsets[18];
memset(addresses, 0, 18);
memset(offsets, 0, 18);
u32 counter = 0;
for(int ii=0; ii<7; ii++) {
if(header.addressText[ii] == 0)
continue;
offsets[counter] = header.offsetText[ii];
addresses[counter] = header.addressText[ii];
counter++;
}
for(int ii=0; ii<11; ii++) {
if(header.addressText[ii] == 0)
continue;
offsets[counter] = header.offsetData[ii];
addresses[counter] = header.addressData[ii];
counter++;
}
for(u32 ii=0; ii<counter; ii++) {
for(u32 jj=1; jj<counter; jj++) {
if((addresses[jj] < addresses[jj-1]) ||
(addresses[jj-1] == 0)) {
u32 temp = offsets[jj-1];
offsets[jj-1] = offsets[jj];
offsets[jj] = temp;
temp = addresses[jj-1];
addresses[jj-1] = addresses[jj];
addresses[jj] = temp;
}
}
}
u32 mem = 0;
u32 j = 0;
for(u32 i=0; i<counter; i++, j++)
{
#ifdef DEBUG
cout << "offsetText[" << i << "]: "
<< offsets[i];
cout << "\t";
cout << "addressText[" << i << "]: "
<< addresses[i] << endl;
#endif
if((addresses[i] > address) ||
(addresses[i] == 0))
break;
}
j--;
address -= addresses[j];
address += offsets[j];
#ifdef DEBUG
cout << "File offset: 0x"
<< hex << address <<endl;
#endif
mem = address;
return mem;
}