forked from lordhoto/macloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data00.cpp
185 lines (154 loc) · 4.93 KB
/
data00.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright (c) 2011 Johannes Schickel (LordHoto)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "data00.h"
#include <boost/lexical_cast.hpp>
Data00Loader::Data00Loader(Executable &exe) throw(std::exception)
: StaticDataLoader(exe), _resFork(exe.getResourceFork()), _data00(nullptr) {
}
Data00Loader::~Data00Loader() {
destroy(_data00);
}
void Data00Loader::reset() throw(std::exception) {
destroy(_data00);
}
bool Data00Loader::isSupported(const CodeSegment &code, const uint32 offset, const uint32 size) throw() {
const byte *memory = _executable.getMemory();
const uint32 memorySize = _executable.getMemorySize();
// TODO: This detection heuristic is probably all wrong...
// Chechk whether the segment is big enough
if (memorySize < offset + 0x210)
return false;
// Check whether the offset into the jump table is 0
if (READ_UINT16_BE(memory + offset + 0) != 0)
return false;
// Check whether just one function is exported
if (READ_UINT16_BE(memory + offset + 2) != 1)
return false;
// Check whether a "CODE" tag is at 0xA
if (READ_UINT32_BE(memory + offset + 0x0A) != 0x434F4445)
return false;
// Check whether a "DATA" tag is at 0x44
if (READ_UINT32_BE(memory + offset + 0x44) != 0x44415441)
return false;
// Check whether we have an DATA00 resource
_data00 = _resFork.getResource(0x44415441, 0x0000);
if (_data00 == nullptr)
return false;
return true;
}
void Data00Loader::load(const CodeSegment &code, const uint32 offset, const uint32 size, std::ostream &out) throw(std::exception) {
assert(_data00->data != nullptr);
// Uncompress the data
const byte *src = uncompress(offset, out);
}
const byte *Data00Loader::uncompress(const uint32 offset, std::ostream &out) throw(std::exception) {
byte * const memory = _executable.getMemory();
Code0Segment &code0 = _executable.getCode0Segment();
byte * const a5Base = memory + code0.getApplicationGlobalsSize();
const byte *src = _data00->data + 4;
// Whether data was written to the jump table
bool dataWrittenToJumpTable = false;
for (uint i = 0; i < 3; ++i) {
// Read the offset
const int32 offset = (int32)READ_UINT32_BE(src);
src += 4;
uint8 *dst = a5Base + offset;
// Whether we uncompress data onto the uninitialized part of the jump table
if (offset >= int32(code0.getApplicationParametersSize() + 8)) {
out << "\tData write to jump table offset: " << offset << std::endl;
dataWrittenToJumpTable = true;
}
while (true) {
uint8 code = *src++;
if (code & 0x80) {
code &= 0x7F;
code += 1;
std::memcpy(dst, src, code);
src += code;
dst += code;
} else if (code & 0x40) {
code &= 0x3F;
code += 1;
std::memset(dst, 0, code);
dst += code;
} else if (code & 0x20) {
code &= 0x1F;
code += 2;
const uint8 data = *src++;
std::memset(dst, data, code);
dst += code;
} else if (code & 0x10) {
code &= 0x0F;
code += 1;
std::memset(dst, 0xFF, code);
dst += code;
} else {
if (code == 0) {
break;
} else if (code == 1) {
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0xFF;
*dst++ = 0xFF;
*dst++ = *src++;
*dst++ = *src++;
} else if (code == 2) {
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = 0xFF;
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
} else if (code == 3) {
*dst++ = 0xA9;
*dst++ = 0xF0;
*dst++ = 0x00;
*dst++ = 0x00;
*dst++ = *src++;
*dst++ = *src++;
*dst++ = 0x0;
*dst++ = *src++;
} else if (code == 4) {
*dst++ = 0xA9;
*dst++ = 0xF0;
*dst++ = 0x00;
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
*dst++ = 0x0;
*dst++ = *src++;
} else {
throw std::runtime_error("DATA00 Loader: Invalid code " + boost::lexical_cast<std::string>(int(code)) + " encountered");
}
}
}
}
// Check whether data was written to the jump table
if (dataWrittenToJumpTable) {
// Load the data from entry 1 to the last entry into our jump table structure
for (uint i = 1, end = code0.getJumpTableEntryCount(); i < end; ++i)
std::memcpy(code0.getJumpTableEntry(i).rawData, memory + code0.getJumpTableOffset() + i * 8, 8);
code0.outputJumptable(out);
}
return src;
}