/
input.cpp
402 lines (355 loc) · 11.1 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <cstring>
#include "input.h"
#include "buffer_pool.h"
#include "cso.h"
#include "dax.h"
#include "lz4.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);
const bool isZSO = !memcmp(headerBuf, ZSO_MAGIC, 4);
if (isZSO || !memcmp(headerBuf, CSO_MAGIC, 4)) {
const CSOHeader *const header = reinterpret_cast<CSOHeader *>(headerBuf);
if (isZSO) {
type_ = ZSO;
} else {
type_ = header->version == 2 ? CSO2 : CSO1;
}
if (header->version > 2) {
finish_(false, "CSO header indicates unsupported version");
} else if (header->sector_size < SECTOR_SIZE || header->sector_size > pool.bufferSize) {
finish_(false, "CSO header indicates unsupported sector size");
} else if ((header->uncompressed_size & SECTOR_MASK) != 0) {
finish_(false, "CSO uncompressed size not aligned to sector size");
} else {
size_ = header->uncompressed_size;
csoIndexShift_ = header->index_shift;
csoBlockSize_ = header->sector_size;
csoBlockShift_ = 0;
for (uint32_t i = header->sector_size; i > 1; i >>= 1) {
++csoBlockShift_;
}
const uint32_t sectors = static_cast<uint32_t>(SizeAligned() >> csoBlockShift_);
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 compressedDeflate = false;
bool compressedLZ4 = false;
switch (type_) {
case ISO:
break;
case CSO1:
case CSO2:
case ZSO:
{
const uint32_t block = static_cast<uint32_t>(pos_ >> csoBlockShift_);
const uint32_t index = csoIndex_[block];
const uint32_t nextIndex = csoIndex_[block + 1];
pos = static_cast<uint64_t>(index & 0x7FFFFFFF) << csoIndexShift_;
const int64_t nextPos = static_cast<uint64_t>(nextIndex & 0x7FFFFFFF) << csoIndexShift_;
len = static_cast<unsigned int>(nextPos - pos);
offset = pos_ & static_cast<uint64_t>(csoBlockSize_ - 1);
switch (type_) {
case CSO1:
compressedDeflate = (index & CSO_INDEX_UNCOMPRESSED) == 0;
break;
case CSO2:
// In v2, only smaller than csoBlockSize_ is compressed. Flags means how.
if (index & CSO2_INDEX_LZ4) {
compressedLZ4 = len < csoBlockSize_;
} else {
compressedDeflate = len < csoBlockSize_;
}
break;
case ZSO:
compressedLZ4 = (index & CSO_INDEX_UNCOMPRESSED) == 0;
break;
case ISO:
case DAX:
case UNKNOWN:
finish_(false, "Unexpected input file type");
break;
}
if (!compressedDeflate && !compressedLZ4 && offset != 0) {
pos += offset;
len -= offset;
offset = 0;
}
}
break;
case DAX:
{
const uint32_t frame = static_cast<uint32_t>(pos_ >> DAX_FRAME_SHIFT);
pos = daxIndex_[frame];
len = daxSize_[frame];
compressedDeflate = !daxIsNC_[frame];
offset = pos_ & DAX_FRAME_MASK;
if (!compressedDeflate && offset != 0) {
pos += offset;
len -= offset;
offset = 0;
}
}
break;
case UNKNOWN:
finish_(false, "Unexpected input file type");
break;
}
// This ends up being owned by the compressor.
if (pos >= cachePos_ && pos + len <= cachePos_ + cacheSize_) {
// Already read in, let's just reuse.
if (compressedDeflate || compressedLZ4) {
EnqueueDecompressSector(cache_ + pos - cachePos_, len, offset, compressedLZ4);
} else {
uint8_t *readBuf = pool.Alloc();
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, len, offset, compressedDeflate, compressedLZ4](uv_fs_t *req) {
if (req->result < static_cast<ssize_t>(len)) {
finish_(false, "Unable to read entire sector");
uv_fs_req_cleanup(req);
return;
}
uv_fs_req_cleanup(req);
if (compressedDeflate || compressedLZ4) {
EnqueueDecompressSector(cache_, len, offset, compressedLZ4);
} else {
uint8_t *readBuf = pool.Alloc();
memcpy(readBuf, cache_, len);
callback_(pos_, readBuf);
pos_ += SECTOR_SIZE;
ReadSector();
}
});
}
}
void Input::EnqueueDecompressSector(uint8_t *src, uint32_t len, uint32_t offset, bool isLZ4) {
// 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, isLZ4](uv_work_t *req) {
bool result;
if (isLZ4) {
result = DecompressSectorLZ4(actualBuf, src, csoBlockSize_, decompressError_);
} else {
result = DecompressSectorDeflate(actualBuf, src, len, type_, decompressError_);
}
if (!result) {
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::DecompressSectorDeflate(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 == DAX ? 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 = pool.bufferSize;
// ZLIB_CONST doesn't seem to work on all platforms.
z.next_in = const_cast<uint8_t *>(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 (z.total_out < SECTOR_SIZE) {
err = "Expected to decompress into at least a full sector";
inflateEnd(&z);
return false;
}
inflateEnd(&z);
return true;
}
bool Input::DecompressSectorLZ4(uint8_t *dst, const uint8_t *src, int dstSize, std::string &err) {
// Must use fast, because we don't know the size of the input data. It could include padding.
if (LZ4_decompress_fast(reinterpret_cast<const char *>(src), reinterpret_cast<char *>(dst), dstSize) < 0) {
err = "LZ4 decompression failed.";
return false;
}
return true;
}
inline int64_t Input::SizeAligned() {
return size_ + csoBlockSize_ - 1;
}
};