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