]> git.sesse.net Git - vlc/blob - modules/stream_filter/decomp.c
Missing #include <errno.h>
[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 #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 void Close (vlc_object_t *);
51
52 vlc_module_begin ()
53     set_description (N_("Decompression"))
54     set_category (CAT_INPUT)
55     set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
56     set_capability ("stream_filter", 20)
57     set_callbacks (OpenBzip2, Close)
58     /* TODO: access shortnames for stream_UrlNew() */
59
60     add_submodule ()
61     set_callbacks (OpenGzip, Close)
62 vlc_module_end ()
63
64 struct stream_sys_t
65 {
66     block_t      *peeked;
67     uint64_t     offset;
68     vlc_thread_t thread;
69     pid_t        pid;
70     int          write_fd, read_fd;
71 };
72
73 static void cloexec (int fd)
74 {
75     int flags = fcntl (fd, F_GETFD);
76     fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
77 }
78
79 extern char **environ;
80
81 static const size_t bufsize = 65536;
82 #ifdef HAVE_VMSPLICE
83 static void cleanup_mmap (void *addr)
84 {
85     munmap (addr, bufsize);
86 }
87 #endif
88
89 static void *Thread (void *data)
90 {
91     stream_t *stream = data;
92     stream_sys_t *p_sys = stream->p_sys;
93 #ifdef HAVE_VMSPLICE
94     ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
95 #endif
96     int fd = p_sys->write_fd;
97     bool error = false;
98
99     do
100     {
101         ssize_t len;
102         int canc = vlc_savecancel ();
103 #ifdef HAVE_VMSPLICE
104         unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
105                                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
106         vlc_cleanup_push (cleanup_mmap, buf);
107 #else
108         unsigned char buf[bufsize];
109 #endif
110
111         len = stream_Read (stream->p_source, buf, bufsize);
112         vlc_restorecancel (canc);
113         error = len <= 0;
114
115         for (ssize_t i = 0, j; i < len; i += j)
116         {
117 #ifdef HAVE_VMSPLICE
118             if ((len - i) <= page_mask) /* incomplete last page */
119                 j = write (fd, buf + i, len - i);
120             else
121             {
122                 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
123                 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
124             }
125             if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
126 #endif
127             j = write (fd, buf + i, len - i);
128             if (j <= 0)
129             {
130                 if (j == 0)
131                     errno = EPIPE;
132                 msg_Err (stream, "cannot write data (%m)");
133                 error = true;
134                 break;
135             }
136         }
137 #ifdef HAVE_VMSPLICE
138         vlc_cleanup_run (); /* munmap (buf, bufsize) */
139 #endif
140     }
141     while (!error);
142
143     msg_Dbg (stream, "compressed stream at EOF");
144     return NULL;
145 }
146
147
148 static int Peek (stream_t *, const uint8_t **, unsigned int);
149
150 #define MIN_BLOCK (1 << 10)
151 #define MAX_BLOCK (1 << 20)
152 /**
153  * Reads decompressed from the decompression program
154  * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
155  */
156 static int Read (stream_t *stream, void *buf, unsigned int buflen)
157 {
158     stream_sys_t *p_sys = stream->p_sys;
159     block_t *peeked;
160     ssize_t length;
161
162     if (buf == NULL) /* caller skips data, get big enough peek buffer */
163         buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
164
165     if ((peeked = p_sys->peeked) != NULL)
166     {   /* dequeue peeked data */
167         length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
168         if (buf != NULL)
169         {
170             memcpy (buf, peeked->p_buffer, length);
171             buf = ((char *)buf) + length;
172         }
173         buflen -= length;
174         peeked->p_buffer += length;
175         peeked->i_buffer -= length;
176         if (peeked->i_buffer == 0)
177         {
178             block_Release (peeked);
179             p_sys->peeked = NULL;
180         }
181         p_sys->offset += length;
182
183         if (buflen > 0)
184             length += Read (stream, ((char *)buf) + length, buflen - length);
185         return length;
186     }
187     assert ((buf != NULL) || (buflen == 0));
188
189     length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
190     if (length < 0)
191         return 0;
192     p_sys->offset += length;
193     return length;
194 }
195
196 /**
197  *
198  */
199 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
200 {
201     stream_sys_t *p_sys = stream->p_sys;
202     block_t *peeked = p_sys->peeked;
203     size_t curlen = 0;
204     int fd = p_sys->read_fd;
205
206     if (peeked == NULL)
207         peeked = block_Alloc (len);
208     else if ((curlen = peeked->i_buffer) < len)
209         peeked = block_Realloc (peeked, 0, len);
210
211     if ((p_sys->peeked = peeked) == NULL)
212         return 0;
213
214     if (curlen < len)
215     {
216         ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
217                                 len - curlen, true);
218         if (val >= 0)
219         {
220             curlen += val;
221             peeked->i_buffer = curlen;
222         }
223     }
224     *pbuf = peeked->p_buffer;
225     return curlen;
226 }
227
228 /**
229  *
230  */
231 static int Control (stream_t *stream, int query, va_list args)
232 {
233     stream_sys_t *p_sys = stream->p_sys;
234
235     switch (query)
236     {
237         case STREAM_CAN_SEEK:
238         case STREAM_CAN_FASTSEEK:
239             *(va_arg (args, bool *)) = false;
240             break;
241         case STREAM_GET_POSITION:
242             *(va_arg (args, int64_t *)) = p_sys->offset;
243             break;
244         case STREAM_GET_SIZE:
245             *(va_arg (args, int64_t *)) = 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