1 /*****************************************************************************
2 * decomp.c : Decompression module for vlc
3 *****************************************************************************
4 * Copyright © 2008 Rémi Denis-Courmont
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.
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.
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 *****************************************************************************/
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_stream.h>
28 #include <vlc_network.h>
32 # define _POSIX_SPAWN (-1)
35 #if (_POSIX_SPAWN >= 0)
39 #include <sys/ioctl.h>
40 #if defined (__linux__) && defined (HAVE_VMSPLICE)
42 # include <sys/mman.h>
47 static int OpenGzip (vlc_object_t *);
48 static int OpenBzip2 (vlc_object_t *);
49 static void Close (vlc_object_t *);
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() */
60 set_callbacks (OpenGzip, Close)
69 int write_fd, read_fd;
72 static void cloexec (int fd)
74 int flags = fcntl (fd, F_GETFD);
75 fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
78 extern char **environ;
80 static const size_t bufsize = 65536;
82 static void cleanup_mmap (void *addr)
84 munmap (addr, bufsize);
88 static void *Thread (void *data)
90 stream_t *stream = data;
91 stream_sys_t *p_sys = stream->p_sys;
93 ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
95 int fd = p_sys->write_fd;
101 int canc = vlc_savecancel ();
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);
107 unsigned char buf[bufsize];
110 len = stream_Read (stream->p_source, buf, bufsize);
111 vlc_restorecancel (canc);
114 for (ssize_t i = 0, j; i < len; i += j)
117 if ((len - i) <= page_mask) /* incomplete last page */
118 j = write (fd, buf + i, len - i);
121 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
122 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
124 if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
126 j = write (fd, buf + i, len - i);
131 msg_Err (stream, "cannot write data (%m)");
137 vlc_cleanup_run (); /* munmap (buf, bufsize) */
142 msg_Dbg (stream, "compressed stream at EOF");
147 static int Peek (stream_t *, const uint8_t **, unsigned int);
149 #define MIN_BLOCK (1 << 10)
150 #define MAX_BLOCK (1 << 20)
152 * Reads decompressed from the decompression program
153 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
155 static int Read (stream_t *stream, void *buf, unsigned int buflen)
157 stream_sys_t *p_sys = stream->p_sys;
161 if (buf == NULL) /* caller skips data, get big enough peek buffer */
162 buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
164 if ((peeked = p_sys->peeked) != NULL)
165 { /* dequeue peeked data */
166 length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
169 memcpy (buf, peeked->p_buffer, length);
170 buf = ((char *)buf) + length;
173 peeked->p_buffer += length;
174 peeked->i_buffer -= length;
175 if (peeked->i_buffer == 0)
177 block_Release (peeked);
178 p_sys->peeked = NULL;
180 p_sys->offset += length;
183 length += Read (stream, ((char *)buf) + length, buflen - length);
186 assert ((buf != NULL) || (buflen == 0));
188 length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
191 p_sys->offset += length;
198 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
200 stream_sys_t *p_sys = stream->p_sys;
201 block_t *peeked = p_sys->peeked;
203 int fd = p_sys->read_fd;
206 peeked = block_Alloc (len);
207 else if ((curlen = peeked->i_buffer) < len)
208 peeked = block_Realloc (peeked, 0, len);
210 if ((p_sys->peeked = peeked) == NULL)
215 ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
220 peeked->i_buffer = curlen;
223 *pbuf = peeked->p_buffer;
230 static int Control (stream_t *stream, int query, va_list args)
232 stream_sys_t *p_sys = stream->p_sys;
236 case STREAM_CAN_SEEK:
237 case STREAM_CAN_FASTSEEK:
238 *(va_arg (args, bool *)) = false;
240 case STREAM_GET_POSITION:
241 *(va_arg (args, int64_t *)) = p_sys->offset;
243 case STREAM_GET_SIZE:
244 *(va_arg (args, int64_t *)) = 0;
247 *(va_arg (args, int *)) = 0;
256 * Pipe data through an external executable.
257 * @param stream the stream filter object.
258 * @param path path to the executable.
260 static int Open (stream_t *stream, const char *path)
262 stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
266 stream->pf_read = Read;
267 stream->pf_peek = Peek;
268 stream->pf_control = Control;
269 p_sys->peeked = NULL;
273 /* I am not a big fan of the pyramid style, but I cannot think of anything
274 * better here. There are too many failure cases. */
275 int ret = VLC_EGENERIC;
278 /* We use two pipes rather than one stream socket pair, so that we can
279 * use vmsplice() on Linux. */
280 if (pipe (comp) == 0)
283 p_sys->write_fd = comp[1];
286 if (pipe (uncomp) == 0)
289 p_sys->read_fd = uncomp[0];
291 #if (_POSIX_SPAWN >= 0)
292 posix_spawn_file_actions_t actions;
293 if (posix_spawn_file_actions_init (&actions) == 0)
295 char *const argv[] = { (char *)path, NULL };
297 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
298 && !posix_spawn_file_actions_addclose (&actions, comp[0])
299 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
300 && !posix_spawn_file_actions_addclose (&actions, uncomp[1])
301 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
304 if (vlc_clone (&p_sys->thread, Thread, stream,
305 VLC_THREAD_PRIORITY_INPUT) == 0)
310 msg_Err (stream, "Cannot execute %s", path);
313 posix_spawn_file_actions_destroy (&actions);
315 #else /* _POSIX_SPAWN */
316 switch (p_sys->pid = fork ())
319 msg_Err (stream, "Cannot fork (%m)");
326 execlp (path, path, (char *)NULL);
327 exit (1); /* if we get, execlp() failed! */
329 if (vlc_clone (&p_sys->thread, Thread, stream,
330 VLC_THREAD_PRIORITY_INPUT) == 0)
333 #endif /* _POSIX_SPAWN < 0 */
335 if (ret != VLC_SUCCESS)
339 if (ret != VLC_SUCCESS)
342 if (p_sys->pid != -1)
343 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
351 * Releases allocate resources.
353 static void Close (vlc_object_t *obj)
355 stream_t *stream = (stream_t *)obj;
356 stream_sys_t *p_sys = stream->p_sys;
359 vlc_cancel (p_sys->thread);
360 close (p_sys->read_fd);
361 vlc_join (p_sys->thread, NULL);
362 close (p_sys->write_fd);
364 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
365 while (waitpid (p_sys->pid, &status, 0) == -1);
366 msg_Dbg (obj, "exit status %d", status);
369 block_Release (p_sys->peeked);
375 * Detects gzip file format
377 static int OpenGzip (vlc_object_t *obj)
379 stream_t *stream = (stream_t *)obj;
382 if (stream_Peek (stream->p_source, &peek, 3) < 3)
385 if (memcmp (peek, "\x1f\x8b\x08", 3))
388 msg_Dbg (obj, "detected gzip compressed stream");
389 return Open (stream, "zcat");
394 * Detects bzip2 file format
396 static int OpenBzip2 (vlc_object_t *obj)
398 stream_t *stream = (stream_t *)obj;
401 /* (Try to) parse the bzip2 header */
402 if (stream_Peek (stream->p_source, &peek, 10) < 10)
405 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
406 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
409 msg_Dbg (obj, "detected bzip2 compressed stream");
410 return Open (stream, "bzcat");