]> git.sesse.net Git - vlc/blob - modules/stream_filter/decomp.c
5171ea703d8e1e326044d601e58a6815c1cc0344
[vlc] / modules / stream_filter / decomp.c
1 /*****************************************************************************
2  * decomp.c : Decompression module for vlc
3  *****************************************************************************
4  * Copyright © 2008 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_stream.h>
28 #include <vlc_network.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #ifndef _POSIX_SPAWN
32 # define _POSIX_SPAWN (-1)
33 #endif
34 #include <fcntl.h>
35 #if (_POSIX_SPAWN >= 0)
36 # include <spawn.h>
37 #endif
38 #include <sys/wait.h>
39 #include <sys/ioctl.h>
40 #if defined (__linux__) && defined (HAVE_VMSPLICE)
41 # include <sys/uio.h>
42 # include <sys/mman.h>
43 #else
44 # undef HAVE_VMSPLICE
45 #endif
46
47 static int  OpenGzip (vlc_object_t *);
48 static int  OpenBzip2 (vlc_object_t *);
49 static void Close (vlc_object_t *);
50
51 vlc_module_begin ()
52     set_description (N_("Decompression"))
53     set_category (CAT_INPUT)
54     set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
55     set_capability ("stream_filter", 20)
56     set_callbacks (OpenBzip2, Close)
57     /* TODO: access shortnames for stream_UrlNew() */
58
59     add_submodule ()
60     set_callbacks (OpenGzip, Close)
61 vlc_module_end ()
62
63 struct stream_sys_t
64 {
65     block_t      *peeked;
66     uint64_t     offset;
67     vlc_thread_t thread;
68     pid_t        pid;
69     int          write_fd, read_fd;
70 };
71
72 static void cloexec (int fd)
73 {
74     int flags = fcntl (fd, F_GETFD);
75     fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
76 }
77
78 extern char **environ;
79
80 static const size_t bufsize = 65536;
81 #ifdef HAVE_VMSPLICE
82 static void cleanup_mmap (void *addr)
83 {
84     munmap (addr, bufsize);
85 }
86 #endif
87
88 static void *Thread (void *data)
89 {
90     stream_t *stream = data;
91     stream_sys_t *p_sys = stream->p_sys;
92 #ifdef HAVE_VMSPLICE
93     ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
94 #endif
95     int fd = p_sys->write_fd;
96     bool error = false;
97
98     do
99     {
100         ssize_t len;
101         int canc = vlc_savecancel ();
102 #ifdef HAVE_VMSPLICE
103         unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
104                                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
105         vlc_cleanup_push (cleanup_mmap, buf);
106 #else
107         unsigned char buf[bufsize];
108 #endif
109
110         len = stream_Read (stream->p_source, buf, bufsize);
111         vlc_restorecancel (canc);
112         error = len <= 0;
113
114         for (ssize_t i = 0, j; i < len; i += j)
115         {
116 #ifdef HAVE_VMSPLICE
117             if ((len - i) <= page_mask) /* incomplete last page */
118                 j = write (fd, buf + i, len - i);
119             else
120             {
121                 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
122                 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
123             }
124             if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
125 #endif
126             j = write (fd, buf + i, len - i);
127             if (j <= 0)
128             {
129                 if (j == 0)
130                     errno = EPIPE;
131                 msg_Err (stream, "cannot write data (%m)");
132                 error = true;
133                 break;
134             }
135         }
136 #ifdef HAVE_VMSPLICE
137         vlc_cleanup_run (); /* munmap (buf, bufsize) */
138 #endif
139     }
140     while (!error);
141
142     msg_Dbg (stream, "compressed stream at EOF");
143     return NULL;
144 }
145
146
147 static int Peek (stream_t *, const uint8_t **, unsigned int);
148
149 #define MIN_BLOCK (1 << 10)
150 #define MAX_BLOCK (1 << 20)
151 /**
152  * Reads decompressed from the decompression program
153  * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
154  */
155 static int Read (stream_t *stream, void *buf, unsigned int buflen)
156 {
157     stream_sys_t *p_sys = stream->p_sys;
158     block_t *peeked;
159     ssize_t length;
160
161     if (buf == NULL) /* caller skips data, get big enough peek buffer */
162         buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
163
164     if ((peeked = p_sys->peeked) != NULL)
165     {   /* dequeue peeked data */
166         length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
167         if (buf != NULL)
168         {
169             memcpy (buf, peeked->p_buffer, length);
170             buf = ((char *)buf) + length;
171         }
172         buflen -= length;
173         peeked->p_buffer += length;
174         peeked->i_buffer -= length;
175         if (peeked->i_buffer == 0)
176         {
177             block_Release (peeked);
178             p_sys->peeked = NULL;
179         }
180         p_sys->offset += length;
181
182         if (buflen > 0)
183             length += Read (stream, ((char *)buf) + length, buflen - length);
184         return length;
185     }
186     assert ((buf != NULL) || (buflen == 0));
187
188     length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
189     if (length < 0)
190         return 0;
191     p_sys->offset += length;
192     return length;
193 }
194
195 /**
196  *
197  */
198 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
199 {
200     stream_sys_t *p_sys = stream->p_sys;
201     block_t *peeked = p_sys->peeked;
202     size_t curlen = 0;
203     int fd = p_sys->read_fd;
204
205     if (peeked == NULL)
206         peeked = block_Alloc (len);
207     else if ((curlen = peeked->i_buffer) < len)
208         peeked = block_Realloc (peeked, 0, len);
209
210     if ((p_sys->peeked = peeked) == NULL)
211         return 0;
212
213     if (curlen < len)
214     {
215         ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
216                                 len - curlen, true);
217         if (val >= 0)
218         {
219             curlen += val;
220             peeked->i_buffer = curlen;
221         }
222     }
223     *pbuf = peeked->p_buffer;
224     return curlen;
225 }
226
227 /**
228  *
229  */
230 static int Control (stream_t *stream, int query, va_list args)
231 {
232     stream_sys_t *p_sys = stream->p_sys;
233
234     switch (query)
235     {
236         case STREAM_CAN_SEEK:
237         case STREAM_CAN_FASTSEEK:
238             *(va_arg (args, bool *)) = false;
239             break;
240         case STREAM_GET_POSITION:
241             *(va_arg (args, int64_t *)) = p_sys->offset;
242             break;
243         case STREAM_GET_SIZE:
244             *(va_arg (args, int64_t *)) = 0;
245             break;
246         default:
247             return VLC_EGENERIC;
248     }
249     return VLC_SUCCESS;
250 }
251
252 /**
253  * Pipe data through an external executable.
254  * @param stream the stream filter object.
255  * @param path path to the executable.
256  */
257 static int Open (stream_t *stream, const char *path)
258 {
259     stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
260     if (p_sys == NULL)
261         return VLC_ENOMEM;
262
263     stream->pf_read = Read;
264     stream->pf_peek = Peek;
265     stream->pf_control = Control;
266     p_sys->peeked = NULL;
267     p_sys->offset = 0;
268     p_sys->pid = -1;
269
270     /* I am not a big fan of the pyramid style, but I cannot think of anything
271      * better here. There are too many failure cases. */
272     int ret = VLC_EGENERIC;
273     int comp[2];
274
275     /* We use two pipes rather than one stream socket pair, so that we can
276      * use vmsplice() on Linux. */
277     if (pipe (comp) == 0)
278     {
279         cloexec (comp[1]);
280         p_sys->write_fd = comp[1];
281
282         int uncomp[2];
283         if (pipe (uncomp) == 0)
284         {
285             cloexec (uncomp[0]);
286             p_sys->read_fd = uncomp[0];
287
288 #if (_POSIX_SPAWN >= 0)
289             posix_spawn_file_actions_t actions;
290             if (posix_spawn_file_actions_init (&actions) == 0)
291             {
292                 char *const argv[] = { (char *)path, NULL };
293
294                 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
295                  && !posix_spawn_file_actions_addclose (&actions, comp[0])
296                  && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
297                  && !posix_spawn_file_actions_addclose (&actions, uncomp[1])
298                  && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
299                                    environ))
300                 {
301                     if (vlc_clone (&p_sys->thread, Thread, stream,
302                                    VLC_THREAD_PRIORITY_INPUT) == 0)
303                         ret = VLC_SUCCESS;
304                 }
305                 else
306                 {
307                     msg_Err (stream, "Cannot execute %s", path);
308                     p_sys->pid = -1;
309                 }
310                 posix_spawn_file_actions_destroy (&actions);
311             }
312 #else /* _POSIX_SPAWN */
313             switch (p_sys->pid = fork ())
314             {
315                 case -1:
316                     msg_Err (stream, "Cannot fork (%m)");
317                     break;
318                 case 0:
319                     dup2 (comp[0], 0);
320                     close (comp[0]);
321                     dup2 (uncomp[1], 1);
322                     close (uncomp[1]);
323                     execlp (path, path, (char *)NULL);
324                     exit (1); /* if we get, execlp() failed! */
325                 default:
326                     if (vlc_clone (&p_sys->thread, Thread, stream,
327                                    VLC_THREAD_PRIORITY_INPUT) == 0)
328                         ret = VLC_SUCCESS;
329             }
330 #endif /* _POSIX_SPAWN < 0 */
331             close (uncomp[1]);
332             if (ret != VLC_SUCCESS)
333                 close (uncomp[0]);
334         }
335         close (comp[0]);
336         if (ret != VLC_SUCCESS)
337         {
338             close (comp[1]);
339             if (p_sys->pid != -1)
340                 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
341         }
342     }
343     return ret;
344 }
345
346
347 /**
348  * Releases allocate resources.
349  */
350 static void Close (vlc_object_t *obj)
351 {
352     stream_t *stream = (stream_t *)obj;
353     stream_sys_t *p_sys = stream->p_sys;
354     int status;
355
356     vlc_cancel (p_sys->thread);
357     close (p_sys->read_fd);
358     vlc_join (p_sys->thread, NULL);
359     close (p_sys->write_fd);
360
361     msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
362     while (waitpid (p_sys->pid, &status, 0) == -1);
363     msg_Dbg (obj, "exit status %d", status);
364
365     if (p_sys->peeked)
366         block_Release (p_sys->peeked);
367     free (p_sys);
368 }
369
370
371 /**
372  * Detects gzip file format
373  */
374 static int OpenGzip (vlc_object_t *obj)
375 {
376     stream_t      *stream = (stream_t *)obj;
377     const uint8_t *peek;
378
379     if (stream_Peek (stream->p_source, &peek, 3) < 3)
380         return VLC_EGENERIC;
381
382     if (memcmp (peek, "\x1f\x8b\x08", 3))
383         return VLC_EGENERIC;
384
385     msg_Dbg (obj, "detected gzip compressed stream");
386     return Open (stream, "zcat");
387 }
388
389
390 /**
391  * Detects bzip2 file format
392  */
393 static int OpenBzip2 (vlc_object_t *obj)
394 {
395     stream_t      *stream = (stream_t *)obj;
396     const uint8_t *peek;
397
398     /* (Try to) parse the bzip2 header */
399     if (stream_Peek (stream->p_source, &peek, 10) < 10)
400         return VLC_EGENERIC;
401
402     if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
403      || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
404         return VLC_EGENERIC;
405
406     msg_Dbg (obj, "detected bzip2 compressed stream");
407     return Open (stream, "bzcat");
408 }
409