]> git.sesse.net Git - vlc/blob - modules/access/rar/stream.c
Added support for multiple files in RAR archives.
[vlc] / modules / access / rar / stream.c
1 /*****************************************************************************
2  * rar.c: uncompressed RAR stream filter
3  *****************************************************************************
4  * Copyright (C) 2008-2010 Laurent Aimar
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_stream.h>
34 #include <vlc_url.h>
35
36 #include <assert.h>
37 #include <limits.h>
38
39 #include "rar.h"
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open (vlc_object_t *);
45 static void Close(vlc_object_t *);
46
47 vlc_module_begin()
48     set_category(CAT_INPUT)
49     set_subcategory(SUBCAT_INPUT_STREAM_FILTER)
50     set_description(N_("Uncompressed RAR"))
51     set_capability("stream_filter", 1)
52     set_callbacks(Open, Close)
53     add_shortcut("rar")
54 vlc_module_end()
55
56 /****************************************************************************
57  * Local definitions/prototypes
58  ****************************************************************************/
59 struct stream_sys_t {
60     stream_t *payload;
61 };
62
63 static int Read(stream_t *s, void *data, unsigned size)
64 {
65     return stream_Read(s->p_sys->payload, data, size);
66 }
67
68 static int Peek( stream_t *s, const uint8_t **data, unsigned size)
69 {
70     return stream_Peek(s->p_sys->payload, data, size);
71 }
72
73 static int Control(stream_t *s, int query, va_list args)
74 {
75     switch (query) {
76     case STREAM_GET_CONTENT_TYPE: {
77         char **mime = va_arg(args, char **);
78         *mime = strdup("audio/x-mpegurl");
79         return VLC_EGENERIC;
80     }
81     default:
82         return stream_vaControl(s->p_sys->payload, query, args);
83     }
84 }
85
86 static int Open(vlc_object_t *object)
87 {
88     stream_t *s = (stream_t*)object;
89
90     if (RarProbe(s->p_source))
91         return VLC_EGENERIC;
92
93     int count;
94     rar_file_t **files;
95     if (RarParse(s->p_source, &count, &files) || count <= 0) {
96         msg_Err(s, "Invalid or unsupported RAR archive");
97         free(files);
98         return VLC_EGENERIC;
99     }
100
101     /* TODO use xspf to have node for directories
102      * Reusing WriteXSPF from the zip access is probably a good idea
103      * (becareful about '\' and '/'.
104      */
105     char *base;
106     char *encoded = encode_URI_component(s->psz_path);
107     if (!encoded || asprintf(&base, "rar://%s", encoded) < 0)
108         base = NULL;
109     free(encoded);
110
111     char *data = strdup("#EXTM3U\n");
112     for (int i = 0; i < count; i++) {
113         rar_file_t *f = files[i];
114         char *next;
115         if (base && data &&
116             asprintf(&next, "%s"
117                             "#EXTINF:,,%s\n"
118                             "%s|%s\n",
119                             data, f->name, base, f->name) >= 0) {
120             free(data);
121             data = next;
122         }
123         RarFileDelete(f);
124     }
125     free(files);
126     if (!data)
127         return VLC_EGENERIC;
128     stream_t *payload = stream_MemoryNew(s, (uint8_t*)data, strlen(data), false);
129     if (!payload) {
130         free(data);
131         return VLC_EGENERIC;
132     }
133
134     s->pf_read = Read;
135     s->pf_peek = Peek;
136     s->pf_control = Control;
137
138     stream_sys_t *sys = s->p_sys = malloc(sizeof(*sys));
139     if (!sys) {
140         stream_Delete(payload);
141         return VLC_ENOMEM;
142     }
143     sys->payload = payload;
144
145     char *tmp;
146     if (asprintf(&tmp, "%s.m3u", s->psz_path) < 0) {
147         Close(object);
148         return VLC_ENOMEM;
149     }
150     free(s->psz_path);
151     s->psz_path = tmp;
152
153     return VLC_SUCCESS;
154 }
155
156 static void Close(vlc_object_t *object)
157 {
158     stream_t *s = (stream_t*)object;
159     stream_sys_t *sys = s->p_sys;
160
161     stream_Delete(sys->payload);
162     free(sys);
163 }
164