/
input.cpp
339 lines (296 loc) · 9.28 KB
/
input.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "input.h"
#include "buffer_pool.h"
#include "cso.h"
#include "dax.h"
#define ZLIB_CONST
#include "zlib.h"
namespace maxcso {
Input::Input(uv_loop_t *loop)
: loop_(loop), type_(UNKNOWN), paused_(false), resumeShouldRead_(false), size_(-1), cache_(nullptr),
csoIndex_(nullptr), daxSize_(nullptr), daxIsNC_(nullptr) {
}
Input::~Input() {
delete [] cache_;
cache_ = nullptr;
delete [] csoIndex_;
csoIndex_ = nullptr;
delete [] daxSize_;
daxSize_ = nullptr;
delete [] daxIsNC_;
daxIsNC_ = nullptr;
}
void Input::OnFinish(InputFinishCallback finish) {
finish_ = finish;
}
void Input::OnBegin(InputBeginCallback begin) {
begin_ = begin;
}
void Input::Pipe(uv_file file, InputCallback callback) {
file_ = file;
callback_ = callback;
pos_ = 0;
// First, we need to check what format it is in.
DetectFormat();
}
void Input::DetectFormat() {
// CSO files will start with "CISO" magic, so let's try to read a header and see what we get.
uint8_t *const headerBuf = pool.Alloc();
const uv_buf_t buf = uv_buf_init(reinterpret_cast<char *>(headerBuf), 24);
uv_.fs_read(loop_, &req_, file_, &buf, 1, 0, [this, headerBuf](uv_fs_t *req) {
if (req->result != 24) {
// ISOs are always sector aligned, and CSOs always have headers.
finish_(false, "Not able to read first 24 bytes");
uv_fs_req_cleanup(req);
pool.Release(headerBuf);
return;
}
uv_fs_req_cleanup(req);
if (!memcmp(headerBuf, CSO_MAGIC, 4)) {
type_ = CISO;
const CSOHeader *const header = reinterpret_cast<CSOHeader *>(headerBuf);
if (header->version > 1) {
finish_(false, "CISO header indicates unsupported version");
} else if (header->sector_size != SECTOR_SIZE) {
finish_(false, "CISO header indicates unsupported sector size");
} else if ((header->uncompressed_size & SECTOR_MASK) != 0) {
finish_(false, "CISO uncompressed size not aligned to sector size");
} else {
size_ = header->uncompressed_size;
csoShift_ = header->index_shift;
const uint32_t sectors = static_cast<uint32_t>(size_ >> SECTOR_SHIFT);
csoIndex_ = new uint32_t[sectors + 1];
const unsigned int bytes = (sectors + 1) * sizeof(uint32_t);
const uv_buf_t buf = uv_buf_init(reinterpret_cast<char *>(csoIndex_), bytes);
SetupCache(header->sector_size);
uv_.fs_read(loop_, &req_, file_, &buf, 1, sizeof(CSOHeader), [this, bytes](uv_fs_t *req) {
if (req->result != bytes) {
// Index wasn't all there, this file is corrupt.
finish_(false, "Unable to read entire index");
uv_fs_req_cleanup(req);
return;
}
uv_fs_req_cleanup(req);
begin_(size_);
ReadSector();
});
}
} else if (!memcmp(headerBuf, DAX_MAGIC, 4)) {
type_ = DAX;
const DAXHeader *const header = reinterpret_cast<DAXHeader *>(headerBuf);
if (header->version > 1) {
finish_(false, "DAX header indicates unsupported version");
} else if ((header->uncompressed_size & SECTOR_MASK) != 0) {
finish_(false, "DAX uncompressed size not aligned to sector size");
} else {
size_ = header->uncompressed_size;
const uint32_t frames = static_cast<uint32_t>((size_ + DAX_FRAME_SIZE - 1) >> DAX_FRAME_SHIFT);
daxIndex_ = new uint32_t[frames];
daxSize_ = new uint16_t[frames];
daxIsNC_ = new bool[frames];
memset(daxIsNC_, 0, sizeof(bool) * frames);
uv_buf_t bufs[3];
int nbufs = 2;
uint32_t bytes = frames * (sizeof(uint32_t) + sizeof(uint16_t));
bufs[0] = uv_buf_init(reinterpret_cast<char *>(daxIndex_), frames * sizeof(uint32_t));
bufs[1] = uv_buf_init(reinterpret_cast<char *>(daxSize_), frames * sizeof(uint16_t));
int nareas = header->version >= 1 ? header->nc_areas : 0;
DAXNCArea *areas = nareas > 0 ? new DAXNCArea[nareas] : nullptr;
if (areas != nullptr) {
bufs[nbufs++] = uv_buf_init(reinterpret_cast<char *>(areas), nareas * sizeof(DAXNCArea));
bytes += nareas * sizeof(DAXNCArea);
}
SetupCache(DAX_FRAME_SIZE);
uv_.fs_read(loop_, &req_, file_, bufs, nbufs, sizeof(DAXHeader), [this, bytes, areas, nareas](uv_fs_t *req) {
if (req->result != bytes) {
// Index wasn't all there, this file is corrupt.
finish_(false, "Unable to read entire index");
uv_fs_req_cleanup(req);
return;
}
uv_fs_req_cleanup(req);
// Map the areas to an index and free.
for (int i = 0; i < nareas; ++i) {
for (uint32_t frame = 0; frame < areas[i].count; ++frame) {
daxIsNC_[areas[i].start + frame] = true;
}
}
delete [] areas;
begin_(size_);
ReadSector();
});
}
} else {
type_ = ISO;
uv_.fs_fstat(loop_, &req_, file_, [this](uv_fs_t *req) {
// An ISO can't be an uneven size, must align to sectors.
if (req->result < 0 || (req->statbuf.st_size & SECTOR_MASK) != 0) {
finish_(false, "ISO file not aligned to sector size");
uv_fs_req_cleanup(req);
} else {
size_ = req->statbuf.st_size;
uv_fs_req_cleanup(req);
SetupCache(SECTOR_SIZE);
begin_(size_);
ReadSector();
}
});
}
pool.Release(headerBuf);
});
}
void Input::SetupCache(uint32_t minSize) {
const uint32_t STANDARD_SIZE = 32768;
while (minSize < STANDARD_SIZE) {
minSize <<= 1;
}
cachePos_ = size_;
cacheSize_ = minSize;
cache_ = new uint8_t[cacheSize_];
}
void Input::ReadSector() {
// At the end of the file, all done.
if (pos_ >= size_) {
finish_(true, nullptr);
return;
}
if (paused_) {
// When we resume, it'll need to call ReadSector() to resume.
resumeShouldRead_ = true;
return;
}
// Position of data in file.
int64_t pos = pos_;
// Offset into position where our data is.
uint32_t offset = 0;
unsigned int len = SECTOR_SIZE;
bool compressed = false;
switch (type_) {
case ISO:
break;
case CISO:
{
const uint32_t sector = static_cast<uint32_t>(pos_ >> SECTOR_SHIFT);
const uint32_t index = csoIndex_[sector];
const uint32_t nextIndex = csoIndex_[sector + 1];
compressed = (index & CSO_INDEX_UNCOMPRESSED) == 0;
pos = static_cast<uint64_t>(index & 0x7FFFFFFF) << csoShift_;
const int64_t nextPos = static_cast<uint64_t>(nextIndex & 0x7FFFFFFF) << csoShift_;
len = static_cast<unsigned int>(nextPos - pos);
if (!compressed && len != SECTOR_SIZE) {
finish_(false, "Uncompressed sector is not the expected size");
return;
}
}
break;
case DAX:
{
const uint32_t frame = static_cast<uint32_t>(pos_ >> DAX_FRAME_SHIFT);
pos = daxIndex_[frame];
len = daxSize_[frame];
compressed = !daxIsNC_[frame];
offset = pos_ & DAX_FRAME_MASK;
if (!compressed && offset != 0) {
pos += offset;
offset = 0;
}
}
}
// This ends up being owned by the compressor.
uint8_t *const readBuf = pool.Alloc();
if (pos >= cachePos_ && pos + len <= cachePos_ + cacheSize_) {
// Already read in, let's just reuse.
if (compressed) {
EnqueueDecompressSector(cache_ + pos - cachePos_, len, offset);
} else {
memcpy(readBuf, cache_ + pos - cachePos_, len);
callback_(pos_, readBuf);
pos_ += SECTOR_SIZE;
ReadSector();
}
} else {
const uv_buf_t buf = uv_buf_init(reinterpret_cast<char *>(cache_), cacheSize_);
cachePos_ = pos;
uv_.fs_read(loop_, &req_, file_, &buf, 1, pos, [this, readBuf, len, offset, compressed](uv_fs_t *req) {
if (req->result < len) {
finish_(false, "Unable to read entire sector");
uv_fs_req_cleanup(req);
return;
}
uv_fs_req_cleanup(req);
if (compressed) {
EnqueueDecompressSector(cache_, len, offset);
} else {
memcpy(readBuf, cache_, len);
callback_(pos_, readBuf);
pos_ += SECTOR_SIZE;
ReadSector();
}
});
}
}
void Input::EnqueueDecompressSector(uint8_t *src, uint32_t len, uint32_t offset) {
// We swap this with the compressed buf, and free the readBuf.
uint8_t *const actualBuf = pool.Alloc();
decompressError_.clear();
uv_.queue_work(loop_, &work_, [this, actualBuf, src, len](uv_work_t *req) {
if (!DecompressSector(actualBuf, src, len, type_, decompressError_)) {
if (decompressError_.empty()) {
decompressError_ = "Unknown error";
}
}
}, [this, actualBuf, offset](uv_work_t *req, int status) {
if (offset != 0) {
memmove(actualBuf, actualBuf + offset, SECTOR_SIZE);
}
if (!decompressError_.empty()) {
finish_(false, decompressError_.c_str());
pool.Release(actualBuf);
} else if (status == -1) {
finish_(false, "Decompression work failed");
pool.Release(actualBuf);
} else {
callback_(pos_, actualBuf);
pos_ += SECTOR_SIZE;
ReadSector();
}
});
}
void Input::Pause() {
paused_ = true;
}
void Input::Resume() {
paused_ = false;
if (resumeShouldRead_) {
resumeShouldRead_ = false;
ReadSector();
}
}
bool Input::DecompressSector(uint8_t *dst, const uint8_t *src, unsigned int len, FileType type, std::string &err) {
z_stream z;
memset(&z, 0, sizeof(z));
// TODO: inflateReset2?
if (inflateInit2(&z, type == CISO ? -15 : 15) != Z_OK) {
err = z.msg ? z.msg : "Unable to initialize inflate";
return false;
}
z.avail_in = len;
z.next_out = dst;
z.avail_out = BufferPool::BUFFER_SIZE;
z.next_in = src;
const int status = inflate(&z, Z_FINISH);
if (status != Z_STREAM_END) {
err = z.msg ? z.msg : "Inflate failed";
inflateEnd(&z);
return false;
}
if (type == CISO) {
if (z.avail_out != BufferPool::BUFFER_SIZE - SECTOR_SIZE) {
err = "Expected to decompress into a full sector";
inflateEnd(&z);
return false;
}
}
inflateEnd(&z);
return true;
}
};