summaryrefslogtreecommitdiffstats
path: root/stream/stream_rar.c
blob: 1cab4a8f48977ec13e7e5ab76678e056ce67628d (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
// Major parts based on:
/*****************************************************************************
 * access.c: uncompressed RAR access
 *****************************************************************************
 * Copyright (C) 2008-2010 Laurent Aimar
 * $Id: dcd973529e0029abe326d31f8d58cd13bbcc276c $
 *
 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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 "config.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "osdep/io.h"

#include "mpvcore/mp_msg.h"
#include "stream.h"
#include "options/m_option.h"
#include "rar.h"

/*
This works as follows:

- stream_open() with file01.rar
    - is opened as normal file (stream_file.c or others) first
    - stream_info_rar_filter stream filter applies
    - leads to rar_filter_open()
    - if multi-part, opens file02.rar, file03.rar, etc. as actual streams
      (recursive opening is prevented with the STREAM_NO_FILTERS flag)
    - read accesses return a m3u playlist with entries like:
        rar://bla01.rar|subfile.mkv
      (one such entry for each file contained in the rar)
- stream_open() with the playlist entry, e.g. rar://bla01.rar|subfile.mkv
    - leads to rar_entry_open()
    - opens bla01.rar etc. again as actual streams
      (again, STREAM_NO_FILTERS to open the actual files)
    - read accesses go into subfile.mkv contained in the rar file(s)
*/

static int rar_entry_fill_buffer(stream_t *s, char *buffer, int max_len)
{
    rar_file_t *rar_file = s->priv;
    return RarRead(rar_file, buffer, max_len);
}

static int rar_entry_seek(stream_t *s, int64_t newpos)
{
    rar_file_t *rar_file = s->priv;
    return RarSeek(rar_file, newpos);
}

static void rar_entry_close(stream_t *s)
{
    rar_file_t *rar_file = s->priv;
    RarFileDelete(rar_file);
}

static int rar_entry_control(stream_t *s, int cmd, void *arg)
{
    rar_file_t *rar_file = s->priv;
    switch (cmd) {
    case STREAM_CTRL_GET_BASE_FILENAME:
        *(char **)arg = talloc_strdup(NULL, rar_file->s->url);
        return STREAM_OK;
    }
    return STREAM_UNSUPPORTED;
}

static int rar_entry_open(stream_t *stream, int mode)
{
    if (!strchr(stream->path, '|'))
        return STREAM_ERROR;

    char *base = talloc_strdup(stream, stream->path);
    char *name = strchr(base, '|');
    *name++ = '\0';
    mp_url_unescape_inplace(base);

    struct stream *rar =
        stream_create(base, STREAM_READ | STREAM_NO_FILTERS, stream->opts);
    if (!rar)
        return STREAM_ERROR;

    int count;
    rar_file_t **files;
    if (RarProbe(rar) || RarParse(rar, &count, &files)) {
        free_stream(rar);
        return STREAM_ERROR;
    }

    rar_file_t *file = NULL;
    for (int i = 0; i < count; i++) {
        if (!file && strcmp(files[i]->name, name) == 0)
            file = files[i];
        else
            RarFileDelete(files[i]);
    }
    talloc_free(files);
    if (!file) {
        free_stream(rar);
        return STREAM_ERROR;
    }

    rar_file_chunk_t dummy = {
        .mrl = base,
    };
    file->current_chunk = &dummy;
    file->s = rar; // transfer ownership
    file->opts = stream->opts;
    RarSeek(file, 0);

    stream->priv = file;
    stream->end_pos = file->size;
    stream->fill_buffer = rar_entry_fill_buffer;
    stream->seek = rar_entry_seek;
    stream->close = rar_entry_close;
    stream->control = rar_entry_control;

    return STREAM_OK;
}

static int rar_filter_fill_buffer(stream_t *s, char *buffer, int max_len)
{
    struct stream *m = s->priv;
    return stream_read_partial(m, buffer, max_len);
}

static int rar_filter_seek(stream_t *s, int64_t newpos)
{
    struct stream *m = s->priv;
    return stream_seek(m, newpos);
}

static void rar_filter_close(stream_t *s)
{
    struct stream *m = s->priv;
    free_stream(m);
}

static int rar_filter_open(stream_t *stream, int mode)
{
    if (mode != STREAM_READ)
        return STREAM_UNSUPPORTED;

    struct stream *rar = stream->source;
    if (!rar)
        return STREAM_UNSUPPORTED;

    int count;
    rar_file_t **files;
    if (!rar || RarProbe(rar) || RarParse(rar, &count, &files))
        return STREAM_UNSUPPORTED;

    void *tmp = talloc_new(NULL);

    // Create a playlist containing all entries of the .rar file. The URLs
    // link to rar_entry_open().
    char *prefix = mp_url_escape(tmp, stream->url, "~|");
    char *pl = talloc_strdup(tmp, "#EXTM3U\n");
    for (int n = 0; n < count; n++) {
        pl = talloc_asprintf_append_buffer(pl, "rar://%s|%s\n",
                                           prefix, files[n]->name);
        RarFileDelete(files[n]);
    }
    talloc_free(files);

    struct stream *m = open_memory_stream(pl, strlen(pl));

    stream->priv = m;
    stream->end_pos = m->end_pos;
    stream->fill_buffer = rar_filter_fill_buffer;
    stream->seek = rar_filter_seek;
    stream->close = rar_filter_close;
    stream->safe_origin = true;

    talloc_free(tmp);
    return STREAM_OK;
}

const stream_info_t stream_info_rar_entry = {
    .name = "rar_entry",
    .open = rar_entry_open,
    .protocols = (const char*[]){ "rar", NULL },
};

const stream_info_t stream_info_rar_filter = {
    .name = "rar_filter",
    .open = rar_filter_open,
    .stream_filter = true,
};