-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvfs_rar.cpp
419 lines (355 loc) · 9.4 KB
/
vfs_rar.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <cassert>
#include <vector>
#include <string>
using namespace std;
#include <deadbeef/deadbeef.h>
#include "unrar/rar.hpp"
//-----------------------------------------------------------------------------
#ifdef DEBUG
#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#else
#define trace(fmt,...)
#endif
#define min(x,y) ((x)<(y)?(x):(y))
static DB_functions_t *deadbeef;
static DB_vfs_t plugin;
typedef struct {
DB_FILE file;
Archive *arc;
ComprDataIO *dataIO;
Unpack *unp;
byte *buffer;
size_t buffer_size;
int64_t offset;
int64_t size;
} rar_file_t;
static const char *scheme_names[] = { "rar://", NULL };
void unstore_file(ComprDataIO *dataIO, int64_t size)
{
byte buffer[0x10000];
while (1) {
uint code = dataIO->UnpRead(buffer, 0x10000);
if (code == 0 || (int)code == -1)
break;
code = min(code, size);
dataIO->UnpWrite(buffer, code);
if (size >= 0)
size -= code;
}
}
//-----------------------------------------------------------------------------
const char **
vfs_rar_get_schemes (void)
{
return scheme_names;
}
int
vfs_rar_is_streaming (void)
{
return 0;
}
// fname must have form of rar://full_filepath.rar:full_filepath_in_rar
DB_FILE*
vfs_rar_open (const char *fname)
{
trace("[vfs_rar_open] %s\n", fname);
if (strncasecmp (fname, "rar://", 6))
return NULL;
// get the full path of .rar file
fname += 6;
const char *colon = strchr(fname, ':');
if (!colon) {
return NULL;
}
char rarname[colon-fname+1];
memcpy(rarname, fname, colon-fname);
rarname[colon-fname] = '\0';
// get the compressed file in this archive
fname = colon+1;
wchar_t wrarname[NM];
UtfToWide(rarname, wrarname, ASIZE(wrarname)-1);
wchar_t wfname[NM];
UtfToWide(fname, wfname, ASIZE(wfname)-1);
Archive *arc = new Archive();
trace("opening rar file: %s\n", rarname);
if (!arc->WOpen(wrarname))
return NULL;
if (!arc->IsArchive(true))
return NULL;
// find the desired file from archive
trace("searching file %s\n", fname);
bool found_file = false;
while (arc->ReadHeader() > 0) {
int hdr_type = arc->GetHeaderType();
if (hdr_type == HEAD_ENDARC)
break;
switch (hdr_type) {
case HEAD_FILE:
if (!arc->IsArcDir()) {
wchar_t warcfn[NM];
ConvertPath(arc->FileHead.FileName, warcfn, NM);
if (!wcscmp(warcfn, wfname)) {
trace("file %s found\n", fname);
found_file = true;
}
}
break;
default:
break;
}
if (found_file)
break;
else
arc->SeekToNext();
}
if (!found_file)
return NULL;
// Seek to head of the file
arc->Seek(arc->NextBlockPos - arc->FileHead.PackSize, SEEK_SET);
// Initialize the ComprDataIO and Unpack
ComprDataIO *dataIO = new ComprDataIO();
Unpack *unp = new Unpack(dataIO);
SecPassword secpwd;
byte pswcheck[SIZE_PSWCHECK];
dataIO->SetEncryption(
false,
arc->FileHead.CryptMethod,
&secpwd,
arc->FileHead.SaltSet ? arc->FileHead.Salt : NULL,
arc->FileHead.InitV,
arc->FileHead.Lg2Count,
pswcheck,
arc->FileHead.HashKey
);
dataIO->CurUnpRead = 0;
dataIO->CurUnpWrite = 0;
dataIO->UnpHash.Init(arc->FileHead.FileHash.Type, 1);
dataIO->PackedDataHash.Init(arc->FileHead.FileHash.Type, 1);
dataIO->SetPackedSizeToRead(arc->FileHead.PackSize);
dataIO->SetTestMode(true);
dataIO->SetSkipUnpCRC(true);
dataIO->SetFiles(arc, NULL); // we unpack data to memory
// Unpack the full file into memory
byte *buffer = (byte *)malloc(arc->FileHead.UnpSize);
dataIO->SetUnpackToMemory(buffer, arc->FileHead.UnpSize);
if (arc->FileHead.Method == 0)
unstore_file(dataIO, arc->FileHead.UnpSize);
else {
unp->Init(arc->FileHead.WinSize, arc->FileHead.Solid);
unp->SetDestSize(arc->FileHead.UnpSize);
if (arc->Format != RARFMT50 && arc->FileHead.UnpVer <= 15)
unp->DoUnpack(15, arc->Solid);
else
unp->DoUnpack(arc->FileHead.UnpVer, arc->FileHead.Solid);
}
rar_file_t *rf = (rar_file_t *)malloc (sizeof (rar_file_t));
memset (rf, 0, sizeof (rar_file_t));
rf->file.vfs = &plugin;
rf->arc = arc;
rf->dataIO = dataIO;
rf->unp = unp;
rf->buffer = buffer;
rf->offset = 0;
rf->size = arc->FileHead.UnpSize;
return (DB_FILE*)rf;
}
void
vfs_rar_close (DB_FILE *f)
{
rar_file_t *rf = (rar_file_t *)f;
if(rf->buffer)
free(rf->buffer);
if (rf->unp)
delete rf->unp;
if (rf->dataIO)
delete rf->dataIO;
if (rf->arc)
delete rf->arc;
free (rf);
}
size_t
vfs_rar_read (void *ptr, size_t size, size_t nmemb, DB_FILE *f)
{
trace("[vfs_rar_read]\n");
rar_file_t *rf = (rar_file_t *)f;
size_t rb = min((int64_t)(size * nmemb), (rf->size - rf->offset));
memcpy(ptr, rf->buffer + rf->offset, rb);
rf->offset += rb;
return (rb/size);
}
int
vfs_rar_seek (DB_FILE *f, int64_t offset, int whence)
{
trace("[vfs_rar_seek]");
rar_file_t *rf = (rar_file_t *)f;
if (whence == SEEK_CUR) {
offset = rf->offset + offset;
}
else if (whence == SEEK_END) {
offset = rf->size + offset;
}
if (offset < 0 || offset > rf->size)
return -1;
rf->offset = offset;
return 0;
#if 0
// reopen when seeking back
if (offset < rf->offset) {
rf->arc->Seek(
rf->arc->NextBlockPos - rf->arc->NewLhd.FullPackSize,
SEEK_SET
);
rf->offset = 0;
}
unsigned char buf[4096];
int64_t n = offset - rf->offset;
while (n > 0) {
int sz = min (n, sizeof (buf));
size_t rb = read_unpacked_data(rf, buf, sz);
n -= rb;
assert (n >= 0);
rf->offset += rb;
if (rb != sz) {
break;
}
}
return n > 0 ? -1 : 0;
#endif
}
int64_t
vfs_rar_tell (DB_FILE *f)
{
rar_file_t *rf = (rar_file_t *)f;
return rf->offset;
}
void
vfs_rar_rewind (DB_FILE *f)
{
rar_file_t *rf = (rar_file_t *)f;
rf->offset = 0;
}
int64_t
vfs_rar_getlength (DB_FILE *f)
{
rar_file_t *rf = (rar_file_t *)f;
return rf->size;
}
int
vfs_rar_scandir (
const char *dir,
struct dirent ***namelist,
int (*selector) (const struct dirent *),
int (*cmp) (const struct dirent **, const struct dirent **)
)
{
trace ("vfs_rar_scandir: %s\n", dir);
vector<string> fname_list;
Archive arc;
wchar_t wdir[NM];
UtfToWide(dir, wdir, ASIZE(wdir)-1);
if (!arc.WOpen(wdir))
return -1;
if (!arc.IsArchive(true))
return -1;
// read files from archive
while (arc.ReadHeader() > 0) {
int hdr_type = arc.GetHeaderType();
if (hdr_type == HEAD_ENDARC)
break;
switch (hdr_type) {
case HEAD_FILE:
if (!arc.IsArcDir()) {
char fname[wcslen(arc.FileHead.FileName)*2];
WideToUtf(arc.FileHead.FileName, fname, ASIZE(fname));
fname_list.push_back(string(fname));
}
break;
default:
break;
}
arc.SeekToNext();
}
// transmit files to player
int num_files = 0;
const int n = fname_list.size();
*namelist = (dirent **)malloc(sizeof(void *) * n);
for (int i = 0; i < n; i++) {
const char *nm = fname_list[i].c_str();
struct dirent entry;
strncpy(entry.d_name, nm, sizeof(entry.d_name)-1);
entry.d_name[sizeof(entry.d_name)-1] = '\0';
if (!selector || selector && selector(&entry)) {
(*namelist)[num_files] = (dirent *)calloc(1, sizeof(struct dirent));
strcpy((*namelist)[num_files]->d_name, entry.d_name);
num_files++;
trace("vfs_rar: %s\n", nm);
}
}
trace ("vfs_rar: scandir done\n");
return num_files;
}
int
vfs_rar_is_container (const char *fname)
{
const char *ext = strrchr (fname, '.');
if (ext && !strcasecmp (ext, ".rar"))
return 1;
return 0;
}
const char *
vfs_rar_get_scheme_for_name (const char *fname) {
return scheme_names[0];
}
extern "C"
DB_plugin_t *
vfs_rar_load (DB_functions_t *api)
{
deadbeef = api;
plugin.plugin.api_vmajor = DB_API_VERSION_MAJOR;
plugin.plugin.api_vminor = DB_API_VERSION_MINOR;
plugin.plugin.version_major = 1;
plugin.plugin.version_minor = 10;
plugin.plugin.type = DB_PLUGIN_VFS;
plugin.plugin.id = "vfs_rar";
plugin.plugin.name = "RAR vfs";
plugin.plugin.descr = "play files directly from rar files";
plugin.plugin.copyright =
"MIT License\n"
"\n"
"Copyright (c) 2017 Shao Hao\n"
"\n"
"Permission is hereby granted, free of charge, to any person obtaining a copy\n"
"of this software and associated documentation files (the \"Software\"), to deal\n"
"in the Software without restriction, including without limitation the rights\n"
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
"copies of the Software, and to permit persons to whom the Software is\n"
"furnished to do so, subject to the following conditions:\n"
"\n"
"The above copyright notice and this permission notice shall be included in all\n"
"copies or substantial portions of the Software.\n"
"\n"
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
"SOFTWARE.\n"
"\n"
"\n"
"UnRAR source (C) Alexander RoshalUnRAR";
plugin.plugin.website = "http://github.com/shaohao/vfs_rar";
plugin.open = vfs_rar_open;
plugin.close = vfs_rar_close;
plugin.read = vfs_rar_read;
plugin.seek = vfs_rar_seek;
plugin.tell = vfs_rar_tell;
plugin.rewind = vfs_rar_rewind;
plugin.getlength = vfs_rar_getlength;
plugin.get_schemes = vfs_rar_get_schemes;
plugin.is_streaming = vfs_rar_is_streaming;
plugin.is_container = vfs_rar_is_container;
plugin.scandir = vfs_rar_scandir;
plugin.get_scheme_for_name = vfs_rar_get_scheme_for_name;
return DB_PLUGIN(&plugin);
}