]> git.sesse.net Git - vlc/blob - modules/access/rar/stream.c
LGPL
[vlc] / modules / access / rar / stream.c
1 /*****************************************************************************
2  * stream.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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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_SUCCESS;
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     const int64_t position = stream_Tell(s->p_source);
96     if (RarParse(s->p_source, &count, &files)) {
97         stream_Seek(s->p_source, position);
98         msg_Err(s, "Invalid or unsupported RAR archive");
99         free(files);
100         return VLC_EGENERIC;
101     }
102
103     /* TODO use xspf to have node for directories
104      * Reusing WriteXSPF from the zip access is probably a good idea
105      * (becareful about '\' and '/'.
106      */
107     char *mrl;
108     if (asprintf(&mrl, "%s://%s", s->psz_access, s->psz_path)< 0)
109         mrl = NULL;
110     char *base;
111     char *encoded = mrl ? encode_URI_component(mrl) : NULL;
112     free(mrl);
113
114     if (!encoded || asprintf(&base, "rar://%s", encoded) < 0)
115         base = NULL;
116     free(encoded);
117
118     char *data = strdup("#EXTM3U\n");
119     for (int i = 0; i < count; i++) {
120         rar_file_t *f = files[i];
121         char *next;
122         if (base && data &&
123             asprintf(&next, "%s"
124                             "#EXTINF:,,%s\n"
125                             "%s|%s\n",
126                             data, f->name, base, f->name) >= 0) {
127             free(data);
128             data = next;
129         }
130         RarFileDelete(f);
131     }
132     free(base);
133     free(files);
134     if (!data)
135         return VLC_EGENERIC;
136     stream_t *payload = stream_MemoryNew(s, (uint8_t*)data, strlen(data), false);
137     if (!payload) {
138         free(data);
139         return VLC_EGENERIC;
140     }
141
142     s->pf_read = Read;
143     s->pf_peek = Peek;
144     s->pf_control = Control;
145
146     stream_sys_t *sys = s->p_sys = malloc(sizeof(*sys));
147     if (!sys) {
148         stream_Delete(payload);
149         return VLC_ENOMEM;
150     }
151     sys->payload = payload;
152
153     char *tmp;
154     if (asprintf(&tmp, "%s.m3u", s->psz_path) < 0) {
155         Close(object);
156         return VLC_ENOMEM;
157     }
158     free(s->psz_path);
159     s->psz_path = tmp;
160
161     return VLC_SUCCESS;
162 }
163
164 static void Close(vlc_object_t *object)
165 {
166     stream_t *s = (stream_t*)object;
167     stream_sys_t *sys = s->p_sys;
168
169     stream_Delete(sys->payload);
170     free(sys);
171 }
172