1 /*****************************************************************************
2 * decomp.c : Decompression module for vlc
3 *****************************************************************************
4 * Copyright © 2008-2009 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>
33 # define _POSIX_SPAWN (-1)
36 #if (_POSIX_SPAWN >= 0)
40 #include <sys/ioctl.h>
41 #if defined (__linux__) && defined (HAVE_VMSPLICE)
43 # include <sys/mman.h>
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 *);
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)
61 set_callbacks (OpenBzip2, Close)
62 /* TODO: access shortnames for stream_UrlNew() */
65 set_callbacks (OpenGzip, Close)
74 int write_fd, read_fd;
77 static void cloexec (int fd)
79 int flags = fcntl (fd, F_GETFD);
80 fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
83 extern char **environ;
85 static const size_t bufsize = 65536;
87 static void cleanup_mmap (void *addr)
89 munmap (addr, bufsize);
93 static void *Thread (void *data)
95 stream_t *stream = data;
96 stream_sys_t *p_sys = stream->p_sys;
98 const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
100 int fd = p_sys->write_fd;
106 int canc = vlc_savecancel ();
108 unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
109 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
110 if (unlikely(buf == MAP_FAILED))
112 vlc_cleanup_push (cleanup_mmap, buf);
114 unsigned char *buf = malloc (bufsize);
115 if (unlikely(buf == NULL))
117 vlc_cleanup_push (free, buf);
120 len = stream_Read (stream->p_source, buf, bufsize);
121 vlc_restorecancel (canc);
124 for (ssize_t i = 0, j; i < len; i += j)
127 if ((len - i) <= page_mask) /* incomplete last page */
128 j = write (fd, buf + i, len - i);
131 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
132 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
134 if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
136 j = write (fd, buf + i, len - i);
141 msg_Err (stream, "cannot write data (%m)");
146 vlc_cleanup_run (); /* free (buf) */
150 msg_Dbg (stream, "compressed stream at EOF");
155 static int Peek (stream_t *, const uint8_t **, unsigned int);
157 #define MIN_BLOCK (1 << 10)
158 #define MAX_BLOCK (1 << 20)
160 * Reads decompressed from the decompression program
161 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
163 static int Read (stream_t *stream, void *buf, unsigned int buflen)
165 stream_sys_t *p_sys = stream->p_sys;
169 if (buf == NULL) /* caller skips data, get big enough peek buffer */
170 buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
172 if ((peeked = p_sys->peeked) != NULL)
173 { /* dequeue peeked data */
174 length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
177 memcpy (buf, peeked->p_buffer, length);
178 buf = ((char *)buf) + length;
181 peeked->p_buffer += length;
182 peeked->i_buffer -= length;
183 if (peeked->i_buffer == 0)
185 block_Release (peeked);
186 p_sys->peeked = NULL;
188 p_sys->offset += length;
191 length += Read (stream, ((char *)buf) + length, buflen - length);
194 assert ((buf != NULL) || (buflen == 0));
196 length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
199 p_sys->offset += length;
206 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
208 stream_sys_t *p_sys = stream->p_sys;
209 block_t *peeked = p_sys->peeked;
211 int fd = p_sys->read_fd;
214 peeked = block_Alloc (len);
215 else if ((curlen = peeked->i_buffer) < len)
216 peeked = block_Realloc (peeked, 0, len);
218 if ((p_sys->peeked = peeked) == NULL)
223 ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
228 peeked->i_buffer = curlen;
231 *pbuf = peeked->p_buffer;
238 static int Control (stream_t *stream, int query, va_list args)
240 stream_sys_t *p_sys = stream->p_sys;
244 case STREAM_CAN_SEEK:
245 case STREAM_CAN_FASTSEEK:
246 *(va_arg (args, bool *)) = false;
248 case STREAM_GET_POSITION:
249 *(va_arg (args, uint64_t *)) = p_sys->offset;
251 case STREAM_GET_SIZE:
252 *(va_arg (args, uint64_t *)) = 0;
261 * Pipe data through an external executable.
262 * @param stream the stream filter object.
263 * @param path path to the executable.
265 static int Open (stream_t *stream, const char *path)
267 stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
271 stream->pf_read = Read;
272 stream->pf_peek = Peek;
273 stream->pf_control = Control;
274 p_sys->peeked = NULL;
278 /* I am not a big fan of the pyramid style, but I cannot think of anything
279 * better here. There are too many failure cases. */
280 int ret = VLC_EGENERIC;
283 /* We use two pipes rather than one stream socket pair, so that we can
284 * use vmsplice() on Linux. */
285 if (pipe (comp) == 0)
288 p_sys->write_fd = comp[1];
291 if (pipe (uncomp) == 0)
294 p_sys->read_fd = uncomp[0];
296 #if (_POSIX_SPAWN >= 0)
297 posix_spawn_file_actions_t actions;
298 if (posix_spawn_file_actions_init (&actions) == 0)
300 char *const argv[] = { (char *)path, NULL };
302 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
303 && !posix_spawn_file_actions_addclose (&actions, comp[0])
304 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
305 && !posix_spawn_file_actions_addclose (&actions, uncomp[1])
306 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
309 if (vlc_clone (&p_sys->thread, Thread, stream,
310 VLC_THREAD_PRIORITY_INPUT) == 0)
315 msg_Err (stream, "Cannot execute %s", path);
318 posix_spawn_file_actions_destroy (&actions);
320 #else /* _POSIX_SPAWN */
321 switch (p_sys->pid = fork ())
324 msg_Err (stream, "Cannot fork (%m)");
331 execlp (path, path, (char *)NULL);
332 exit (1); /* if we get, execlp() failed! */
334 if (vlc_clone (&p_sys->thread, Thread, stream,
335 VLC_THREAD_PRIORITY_INPUT) == 0)
338 #endif /* _POSIX_SPAWN < 0 */
340 if (ret != VLC_SUCCESS)
344 if (ret != VLC_SUCCESS)
347 if (p_sys->pid != -1)
348 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
356 * Releases allocate resources.
358 static void Close (vlc_object_t *obj)
360 stream_t *stream = (stream_t *)obj;
361 stream_sys_t *p_sys = stream->p_sys;
364 vlc_cancel (p_sys->thread);
365 close (p_sys->read_fd);
366 vlc_join (p_sys->thread, NULL);
367 close (p_sys->write_fd);
369 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
370 while (waitpid (p_sys->pid, &status, 0) == -1);
371 msg_Dbg (obj, "exit status %d", status);
374 block_Release (p_sys->peeked);
380 * Detects gzip file format
382 static int OpenGzip (vlc_object_t *obj)
384 stream_t *stream = (stream_t *)obj;
387 if (stream_Peek (stream->p_source, &peek, 3) < 3)
390 if (memcmp (peek, "\x1f\x8b\x08", 3))
393 msg_Dbg (obj, "detected gzip compressed stream");
394 return Open (stream, "zcat");
399 * Detects bzip2 file format
401 static int OpenBzip2 (vlc_object_t *obj)
403 stream_t *stream = (stream_t *)obj;
406 /* (Try to) parse the bzip2 header */
407 if (stream_Peek (stream->p_source, &peek, 10) < 10)
410 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
411 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
414 msg_Dbg (obj, "detected bzip2 compressed stream");
415 return Open (stream, "bzcat");
419 * Detects xz file format
421 static int OpenXZ (vlc_object_t *obj)
423 stream_t *stream = (stream_t *)obj;
426 /* (Try to) parse the xz stream header */
427 if (stream_Peek (stream->p_source, &peek, 8) < 8)
430 if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
433 msg_Dbg (obj, "detected xz compressed stream");
434 return Open (stream, "xzcat");