summaryrefslogtreecommitdiff
path: root/plugins/vfs_zip/vfs_zip.c
blob: f8ad50a17468ca6a4e45e0f44441c36dab177447 (plain)
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
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009-2012 Alexey Yakovenko <waker@users.sourceforge.net>

    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 2
    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 <string.h>
#include <zip.h>
#include <stdlib.h>
#include <assert.h>
#include "../../deadbeef.h"

#define trace(...) { fprintf(stderr, __VA_ARGS__); }
//#define trace(fmt,...)

#define min(x,y) ((x)<(y)?(x):(y))

static DB_functions_t *deadbeef;
static DB_vfs_t plugin;

typedef struct {
    DB_FILE file;
    struct zip* z;
    struct zip_file *zf;
    int64_t offset;
    int index;
    int64_t size;
} zip_file_t;

static const char *scheme_names[] = { "zip://", NULL };

const char **
vfs_zip_get_schemes (void) {
    return scheme_names;
}

int
vfs_zip_is_streaming (void) {
    return 0;
}

// fname must have form of zip://full_filepath.zip:full_filepath_in_zip
DB_FILE*
vfs_zip_open (const char *fname) {
    if (strncasecmp (fname, "zip://", 6)) {
        return NULL;
    }

    fname += 6;
    const char *colon = strchr (fname, ':');
    if (!colon) {
        return NULL;
    }


    char zipname[colon-fname+1];
    memcpy (zipname, fname, colon-fname);
    zipname[colon-fname] = 0;

    fname = colon+1;

    struct zip *z = zip_open (zipname, 0, NULL);
    if (!z) {
        return NULL;
    }
    struct zip_stat st;
    memset (&st, 0, sizeof (st));

    int res = zip_stat(z, fname, 0, &st);
    if (res != 0) {
        zip_close (z);
        return NULL;
    }

    struct zip_file *zf = zip_fopen_index (z, st.index, 0);
    if (!zf) {
        zip_close (z);
        return NULL;
    }

    zip_file_t *f = malloc (sizeof (zip_file_t));
    memset (f, 0, sizeof (zip_file_t));
    f->file.vfs = &plugin;
    f->z = z;
    f->zf = zf;
    f->index = st.index;
    f->size = st.size;
    return (DB_FILE*)f;
}

void
vfs_zip_close (DB_FILE *f) {
    zip_file_t *zf = (zip_file_t *)f;
    if (zf->zf) {
        zip_fclose (zf->zf);
    }
    if (zf->z) {
        zip_close (zf->z);
    }
    free (zf);
}

size_t
vfs_zip_read (void *ptr, size_t size, size_t nmemb, DB_FILE *f) {
    zip_file_t *zf = (zip_file_t *)f;
    ssize_t rb = zip_fread (zf->zf, ptr, size * nmemb);
    zf->offset += rb;
    return rb / size;
}

int
vfs_zip_seek (DB_FILE *f, int64_t offset, int whence) {
    zip_file_t *zf = (zip_file_t *)f;

    if (whence == SEEK_CUR) {
        offset = zf->offset + offset;
    }
    else if (whence == SEEK_END) {
        offset = zf->size + offset;
    }

    if (offset < zf->offset) {
        // reopen
        zip_fclose (zf->zf);
        zf->zf = zip_fopen_index (zf->z, zf->index, 0);
        if (!zf->zf) {
            return -1;
        }
        zf->offset = 0;
    }
    char buf[4096];
    int64_t n = offset - zf->offset;
    while (n > 0) {
        int sz = min (n, sizeof (buf));
        ssize_t rb = zip_fread (zf->zf, buf, sz);
        n -= rb;
        assert (n >= 0);
        zf->offset += rb;
        if (rb != sz) {
            break;
        }
    }
    if (n > 0) {
        return -1;
    }
    return 0;
}

int64_t
vfs_zip_tell (DB_FILE *f) {
    zip_file_t *zf = (zip_file_t *)f;
    return zf->offset;
}

void
vfs_zip_rewind (DB_FILE *f) {
    zip_file_t *zf = (zip_file_t *)f;
    zip_fclose (zf->zf);
    zf->zf = zip_fopen_index (zf->z, zf->index, 0);
    assert (zf->zf); // FIXME: better error handling?
    zf->offset = 0;
}

int64_t
vfs_zip_getlength (DB_FILE *f) {
    zip_file_t *zf = (zip_file_t *)f;
    return zf->size;
}

int
vfs_zip_scandir (const char *dir, struct dirent ***namelist, int (*selector) (const struct dirent *), int (*cmp) (const struct dirent **, const struct dirent **)) {
    struct zip *z = zip_open (dir, ZIP_CHECKCONS, NULL);
    if (!z) {
        return -1;
    }

    int n = zip_get_num_files (z);
    *namelist = malloc (sizeof (void *) * n);
    for (int i = 0; i < n; i++) {
        (*namelist)[i] = malloc (sizeof (struct dirent));
        memset ((*namelist)[i], 0, sizeof (struct dirent));
        const char *nm = zip_get_name (z, i, 0);
        snprintf ((*namelist)[i]->d_name, sizeof ((*namelist)[i]->d_name), "zip://%s:%s", dir, nm);
    }

    zip_close (z);
    return n;
}

int
vfs_zip_is_container (const char *fname) {
    const char *ext = strrchr (fname, '.');
    if (ext && !strcasecmp (ext, ".zip")) {
        return 1;
    }
    return 0;
}

static DB_vfs_t plugin = {
    .plugin.api_vmajor = 1,
    .plugin.api_vminor = 0,
    .plugin.version_major = 1,
    .plugin.version_minor = 0,
    .plugin.type = DB_PLUGIN_VFS,
    .plugin.id = "vfs_zip",
    .plugin.name = "ZIP vfs",
    .plugin.descr = "play files directly from zip files",
    .plugin.copyright = 
        "Copyright (C) 2009-2012 Alexey Yakovenko <waker@users.sourceforge.net>\n"
        "\n"
        "This program is free software; you can redistribute it and/or\n"
        "modify it under the terms of the GNU General Public License\n"
        "as published by the Free Software Foundation; either version 2\n"
        "of the License, or (at your option) any later version.\n"
        "\n"
        "This program is distributed in the hope that it will be useful,\n"
        "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
        "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
        "GNU General Public License for more details.\n"
        "\n"
        "You should have received a copy of the GNU General Public License\n"
        "along with this program; if not, write to the Free Software\n"
        "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n"
    ,
    .plugin.website = "http://deadbeef.sf.net",
    .open = vfs_zip_open,
    .close = vfs_zip_close,
    .read = vfs_zip_read,
    .seek = vfs_zip_seek,
    .tell = vfs_zip_tell,
    .rewind = vfs_zip_rewind,
    .getlength = vfs_zip_getlength,
    .get_schemes = vfs_zip_get_schemes,
    .is_streaming = vfs_zip_is_streaming,
    .is_container = vfs_zip_is_container,
    .scandir = vfs_zip_scandir,
};

DB_plugin_t *
vfs_zip_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}