-
Notifications
You must be signed in to change notification settings - Fork 25
/
psextract.cc
212 lines (170 loc) · 4.54 KB
/
psextract.cc
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* bcm2-utils
* Copyright (C) 2016-2018 Joseph Lehner <[email protected]>
*
* bcm2-utils 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.
*
* bcm2-utils 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 bcm2-utils. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <fstream>
#include "util.h"
#include "ps.h"
using namespace bcm2dump;
using namespace std;
namespace {
class mono_header
{
public:
struct raw
{
// 0x4d4f4e4f (MONO)
uint32_t magic;
// signature (similar to ProgramStore)
uint16_t signature;
uint16_t unk1;
// length including header
uint32_t length;
uint16_t unk2;
uint16_t unk3;
} __attribute__((packed));
mono_header& parse(const string& buf)
{
if (buf.size() < sizeof(raw)) {
throw invalid_argument("buffer too small to contain valid header");
}
memcpy(&m_raw, buf.data(), sizeof(m_raw));
return *this;
}
bool valid() const
{ return be_to_h(m_raw.magic) == 0x4d4f4e4f; }
uint16_t signature() const
{ return be_to_h(m_raw.signature); }
uint32_t length() const
{ return be_to_h(m_raw.length); }
uint16_t unk1() const
{ return be_to_h(m_raw.unk1); }
uint16_t unk2() const
{ return be_to_h(m_raw.unk2); }
uint16_t unk3() const
{ return be_to_h(m_raw.unk3); }
private:
raw m_raw;
};
void do_extract(istream& in, const ps_header& ps, size_t length = 0)
{
if (!length) {
length = ps.length();
}
auto buf = make_unique<char[]>(length);
if (!in.read(buf.get(), length)) {
throw runtime_error("read error (data)");
}
ofstream out(ps.filename());
out.write(reinterpret_cast<const char*>(ps.data()), sizeof(ps_header::raw));
out.write(buf.get(), length);
if (!out) {
throw runtime_error("write error");
}
}
string read_hbuf(istream& in)
{
string hbuf(sizeof(ps_header::raw), '\0');
if (!in.read(&hbuf[0], hbuf.size())) {
throw runtime_error("read error (header)");
}
return hbuf;
}
void extract_ps(istream& in, const ps_header& ps)
{
unsigned long off = static_cast<unsigned long>(in.tellg()) - sizeof(ps_header::raw);
logger::i("0x%07lx ", off);
logger::i() << "image: " << ps.filename() << ", " << ps.length() << " b";
logger::v(", %04x", ps.signature());
logger::i() << endl;
do_extract(in, ps);
}
bool extract_ps(istream& in)
{
ps_header ps;
if (!ps.parse(read_hbuf(in)).hcs_valid()) {
return false;
}
extract_ps(in, ps);
return true;
}
void extract_image(istream& in)
{
ps_header ps;
mono_header mono;
streamoff beg = in.tellg();
string hbuf = read_hbuf(in);
if (ps.parse(hbuf).hcs_valid()) {
extract_ps(in, ps);
} else {
logger::i("0x%07lx ", long(beg & 0xffffffff));
if (mono.parse(hbuf).valid()) {
logger::i() << "monolithic, " << mono.length() << " b";
logger::v(", %04x, ", mono.signature());
logger::v("(%04x %04x %04x)", mono.unk1(), mono.unk2(), mono.unk3());
logger::i() << endl;
streamoff end = beg + mono.length();
in.seekg(beg + sizeof(mono_header::raw));
while (!in.eof() && in.tellg() < end && extract_ps(in)) {
streamoff pos = in.tellg() - beg;
in.seekg(beg + align_right(pos, 0xffff + 1));
}
} else if (hbuf[0] == 0x30 && (hbuf[1] & 0xff) == 0x82) {
// add 7, because sizeof(type + len) is 4, and
// sizeof(end-of-data) is 2. add 1 for next data.
auto len = be_to_h(extract<uint16_t>(hbuf, 2)) + 7;
logger::i() << "asn.1 data, " << len << " b " << endl;
in.seekg(beg + len);
return extract_image(in);
} else {
logger::e() << "unknown image format" << endl;
}
}
}
int do_main(int argc, char* argv[])
{
logger::loglevel(logger::debug);
if (argc < 2) {
logger::e() << "Usage: psextract <infile> [<offset1> ...]" << endl;
return 1;
}
ifstream in(argv[1]);
if (!in.good()) {
throw user_error("failed to open input file");
}
if (argc == 2) {
extract_image(in);
} else {
for (int i = 2; i < argc; ++i) {
if (!in.seekg(lexical_cast<unsigned>(argv[i], 0))) {
throw user_error("bad offset "s + argv[i]);
}
extract_image(in);
}
}
return 0;
}
}
int main(int argc, char* argv[])
{
try {
return do_main(argc, argv);
} catch (const exception& e) {
logger::e() << "error: " << e.what() << endl;
return 1;
}
}