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 it
7 * 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 Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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>
34 # define _POSIX_SPAWN (-1)
37 #if (_POSIX_SPAWN >= 0)
41 #include <sys/ioctl.h>
42 #if defined (__linux__) && defined (HAVE_VMSPLICE)
44 # include <sys/mman.h>
49 static int OpenGzip (vlc_object_t *);
50 static int OpenBzip2 (vlc_object_t *);
51 static int OpenXZ (vlc_object_t *);
52 static void Close (vlc_object_t *);
55 set_description (N_("Decompression"))
56 set_category (CAT_INPUT)
57 set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
58 set_capability ("stream_filter", 20)
59 set_callbacks (OpenXZ, Close)
62 set_callbacks (OpenBzip2, Close)
63 /* TODO: access shortnames for stream_UrlNew() */
66 set_callbacks (OpenGzip, Close)
75 int write_fd, read_fd;
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 const 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 if (unlikely(buf == MAP_FAILED))
107 vlc_cleanup_push (cleanup_mmap, buf);
109 unsigned char *buf = malloc (bufsize);
110 if (unlikely(buf == NULL))
112 vlc_cleanup_push (free, buf);
115 len = stream_Read (stream->p_source, buf, bufsize);
116 vlc_restorecancel (canc);
119 for (ssize_t i = 0, j; i < len; i += j)
122 if ((len - i) <= page_mask) /* incomplete last page */
123 j = write (fd, buf + i, len - i);
126 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
127 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
129 if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
131 j = write (fd, buf + i, len - i);
136 msg_Err (stream, "cannot write data (%m)");
141 vlc_cleanup_run (); /* free (buf) */
145 msg_Dbg (stream, "compressed stream at EOF");
146 /* Let child process know about EOF */
147 p_sys->write_fd = -1;
153 static int Peek (stream_t *, const uint8_t **, unsigned int);
155 #define MIN_BLOCK (1 << 10)
156 #define MAX_BLOCK (1 << 20)
158 * Reads decompressed from the decompression program
159 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
161 static int Read (stream_t *stream, void *buf, unsigned int buflen)
163 stream_sys_t *p_sys = stream->p_sys;
167 if (buf == NULL) /* caller skips data, get big enough peek buffer */
168 buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
170 if ((peeked = p_sys->peeked) != NULL)
171 { /* dequeue peeked data */
172 length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
175 memcpy (buf, peeked->p_buffer, length);
176 buf = ((char *)buf) + length;
179 peeked->p_buffer += length;
180 peeked->i_buffer -= length;
181 if (peeked->i_buffer == 0)
183 block_Release (peeked);
184 p_sys->peeked = NULL;
186 p_sys->offset += length;
189 length += Read (stream, ((char *)buf) + length, buflen - length);
192 assert ((buf != NULL) || (buflen == 0));
194 length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
197 p_sys->offset += length;
204 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
206 stream_sys_t *p_sys = stream->p_sys;
207 block_t *peeked = p_sys->peeked;
209 int fd = p_sys->read_fd;
212 peeked = block_Alloc (len);
213 else if ((curlen = peeked->i_buffer) < len)
214 peeked = block_Realloc (peeked, 0, len);
216 if ((p_sys->peeked = peeked) == NULL)
221 ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
226 peeked->i_buffer = curlen;
229 *pbuf = peeked->p_buffer;
236 static int Control (stream_t *stream, int query, va_list args)
238 stream_sys_t *p_sys = stream->p_sys;
242 case STREAM_CAN_SEEK:
243 case STREAM_CAN_FASTSEEK:
244 *(va_arg (args, bool *)) = false;
246 case STREAM_GET_POSITION:
247 *(va_arg (args, uint64_t *)) = p_sys->offset;
249 case STREAM_GET_SIZE:
250 *(va_arg (args, uint64_t *)) = 0;
259 * Pipe data through an external executable.
260 * @param stream the stream filter object.
261 * @param path path to the executable.
263 static int Open (stream_t *stream, const char *path)
265 stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
269 stream->pf_read = Read;
270 stream->pf_peek = Peek;
271 stream->pf_control = Control;
272 p_sys->peeked = NULL;
276 /* I am not a big fan of the pyramid style, but I cannot think of anything
277 * better here. There are too many failure cases. */
278 int ret = VLC_EGENERIC;
281 /* We use two pipes rather than one stream socket pair, so that we can
282 * use vmsplice() on Linux. */
283 if (vlc_pipe (comp) == 0)
285 p_sys->write_fd = comp[1];
288 if (vlc_pipe (uncomp) == 0)
290 p_sys->read_fd = uncomp[0];
292 #if (_POSIX_SPAWN >= 0)
293 posix_spawn_file_actions_t actions;
294 if (posix_spawn_file_actions_init (&actions) == 0)
296 char *const argv[] = { (char *)path, NULL };
298 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
299 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
300 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
303 if (vlc_clone (&p_sys->thread, Thread, stream,
304 VLC_THREAD_PRIORITY_INPUT) == 0)
309 msg_Err (stream, "Cannot execute %s", path);
312 posix_spawn_file_actions_destroy (&actions);
314 #else /* _POSIX_SPAWN */
315 switch (p_sys->pid = fork ())
318 msg_Err (stream, "Cannot fork (%m)");
323 execlp (path, path, (char *)NULL);
324 exit (1); /* if we get, execlp() failed! */
326 if (vlc_clone (&p_sys->thread, Thread, stream,
327 VLC_THREAD_PRIORITY_INPUT) == 0)
330 #endif /* _POSIX_SPAWN < 0 */
332 if (ret != VLC_SUCCESS)
336 if (ret != VLC_SUCCESS)
339 if (p_sys->pid != -1)
340 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
348 * Releases allocate resources.
350 static void Close (vlc_object_t *obj)
352 stream_t *stream = (stream_t *)obj;
353 stream_sys_t *p_sys = stream->p_sys;
356 vlc_cancel (p_sys->thread);
357 close (p_sys->read_fd);
358 vlc_join (p_sys->thread, NULL);
359 if (p_sys->write_fd != -1)
360 /* Killed before EOF? */
361 close (p_sys->write_fd);
363 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
364 while (waitpid (p_sys->pid, &status, 0) == -1);
365 msg_Dbg (obj, "exit status %d", status);
368 block_Release (p_sys->peeked);
374 * Detects gzip file format
376 static int OpenGzip (vlc_object_t *obj)
378 stream_t *stream = (stream_t *)obj;
381 if (stream_Peek (stream->p_source, &peek, 3) < 3)
384 if (memcmp (peek, "\x1f\x8b\x08", 3))
387 msg_Dbg (obj, "detected gzip compressed stream");
388 return Open (stream, "zcat");
393 * Detects bzip2 file format
395 static int OpenBzip2 (vlc_object_t *obj)
397 stream_t *stream = (stream_t *)obj;
400 /* (Try to) parse the bzip2 header */
401 if (stream_Peek (stream->p_source, &peek, 10) < 10)
404 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
405 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
408 msg_Dbg (obj, "detected bzip2 compressed stream");
409 return Open (stream, "bzcat");
413 * Detects xz file format
415 static int OpenXZ (vlc_object_t *obj)
417 stream_t *stream = (stream_t *)obj;
420 /* (Try to) parse the xz stream header */
421 if (stream_Peek (stream->p_source, &peek, 8) < 8)
424 if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
427 msg_Dbg (obj, "detected xz compressed stream");
428 return Open (stream, "xzcat");