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