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>
31 # define _POSIX_SPAWN (-1)
34 #if (_POSIX_SPAWN >= 0)
38 #include <sys/ioctl.h>
39 #if defined (__linux__) && defined (HAVE_VMSPLICE)
41 # include <sys/mman.h>
46 static int OpenGzip (vlc_object_t *);
47 static int OpenBzip2 (vlc_object_t *);
48 static void Close (vlc_object_t *);
51 set_description (N_("Decompression"))
52 set_category (CAT_INPUT)
53 set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
54 set_capability ("stream_filter", 20)
55 set_callbacks (OpenBzip2, Close)
56 /* TODO: access shortnames for stream_UrlNew() */
59 set_callbacks (OpenGzip, Close)
68 int write_fd, read_fd;
71 static void cloexec (int fd)
73 int flags = fcntl (fd, F_GETFD);
74 fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
77 extern char **environ;
79 static const size_t bufsize = 65536;
81 static void cleanup_mmap (void *addr)
83 munmap (addr, bufsize);
87 static void *Thread (void *data)
89 stream_t *stream = data;
90 stream_sys_t *p_sys = stream->p_sys;
92 ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
94 int fd = p_sys->write_fd;
100 int canc = vlc_savecancel ();
102 unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
103 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
104 vlc_cleanup_push (cleanup_mmap, buf);
106 unsigned char buf[bufsize];
109 len = stream_Read (stream->p_source, buf, bufsize);
110 vlc_restorecancel (canc);
113 for (ssize_t i = 0, j; i < len; i += j)
116 if ((len - i) <= page_mask) /* incomplete last page */
117 j = write (fd, buf + i, len - i);
120 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
121 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
123 if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
125 j = write (fd, buf + i, len - i);
130 msg_Err (stream, "cannot write data (%m)");
136 vlc_cleanup_run (); /* munmap (buf, bufsize) */
141 msg_Dbg (stream, "compressed stream at EOF");
146 static int Peek (stream_t *, const uint8_t **, unsigned int);
148 #define MIN_BLOCK (1 << 10)
149 #define MAX_BLOCK (1 << 20)
151 * Reads decompressed from the decompression program
152 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
154 static int Read (stream_t *stream, void *buf, unsigned int buflen)
156 stream_sys_t *p_sys = stream->p_sys;
160 if (buf == NULL) /* caller skips data, get big enough peek buffer */
161 buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
163 if ((peeked = p_sys->peeked) != NULL)
164 { /* dequeue peeked data */
165 length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
168 memcpy (buf, peeked->p_buffer, length);
169 buf = ((char *)buf) + length;
172 peeked->p_buffer += length;
173 peeked->i_buffer -= length;
174 if (peeked->i_buffer == 0)
176 block_Release (peeked);
177 p_sys->peeked = NULL;
179 p_sys->offset += length;
182 length += Read (stream, ((char *)buf) + length, buflen - length);
186 length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
189 p_sys->offset += length;
196 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
198 stream_sys_t *p_sys = stream->p_sys;
199 block_t *peeked = p_sys->peeked;
201 int fd = p_sys->read_fd;
204 peeked = block_Alloc (len);
205 else if ((curlen = peeked->i_buffer) < len)
206 peeked = block_Realloc (peeked, 0, len);
208 if ((p_sys->peeked = peeked) == NULL)
213 ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
218 peeked->i_buffer = curlen;
221 *pbuf = peeked->p_buffer;
228 static int Control (stream_t *stream, int query, va_list args)
230 stream_sys_t *p_sys = stream->p_sys;
234 case STREAM_CAN_SEEK:
235 case STREAM_CAN_FASTSEEK:
236 *(va_arg (args, bool *)) = false;
238 case STREAM_GET_POSITION:
239 *(va_arg (args, int64_t *)) = p_sys->offset;
241 case STREAM_GET_SIZE:
242 *(va_arg (args, int64_t *)) = 0;
245 *(va_arg (args, int *)) = 0;
254 * Pipe data through an external executable.
255 * @param stream the stream filter object.
256 * @param path path to the executable.
258 static int Open (stream_t *stream, const char *path)
260 stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
264 stream->pf_read = Read;
265 stream->pf_peek = Peek;
266 stream->pf_control = Control;
267 p_sys->peeked = NULL;
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;
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)
281 p_sys->write_fd = comp[1];
284 if (pipe (uncomp) == 0)
287 p_sys->read_fd = uncomp[0];
289 #if (_POSIX_SPAWN >= 0)
290 posix_spawn_file_actions_t actions;
291 if (posix_spawn_file_actions_init (&actions) == 0)
293 char *const argv[] = { (char *)path, NULL };
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,
302 if (vlc_clone (&p_sys->thread, Thread, stream,
303 VLC_THREAD_PRIORITY_INPUT) == 0)
308 msg_Err (stream, "Cannot execute %s", path);
311 posix_spawn_file_actions_destroy (&actions);
313 #else /* _POSIX_SPAWN */
314 switch (p_sys->pid = fork ())
317 msg_Err (stream, "Cannot fork (%m)");
324 execlp (path, path, (char *)NULL);
325 exit (1); /* if we get, execlp() failed! */
327 if (vlc_clone (&p_sys->thread, Thread, stream,
328 VLC_THREAD_PRIORITY_INPUT) == 0)
331 #endif /* _POSIX_SPAWN < 0 */
333 if (ret != VLC_SUCCESS)
337 if (ret != VLC_SUCCESS)
340 if (p_sys->pid != -1)
341 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
349 * Releases allocate resources.
351 static void Close (vlc_object_t *obj)
353 stream_t *stream = (stream_t *)obj;
354 stream_sys_t *p_sys = stream->p_sys;
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);
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);
367 block_Release (p_sys->peeked);
373 * Detects gzip file format
375 static int OpenGzip (vlc_object_t *obj)
377 stream_t *stream = (stream_t *)obj;
380 if (stream_Peek (stream->p_source, &peek, 3) < 3)
383 if (memcmp (peek, "\x1f\x8b\x08", 3))
386 msg_Dbg (obj, "detected gzip compressed stream");
387 return Open (stream, "zcat");
392 * Detects bzip2 file format
394 static int OpenBzip2 (vlc_object_t *obj)
396 stream_t *stream = (stream_t *)obj;
399 /* (Try to) parse the bzip2 header */
400 if (stream_Peek (stream->p_source, &peek, 10) < 10)
403 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
404 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
407 msg_Dbg (obj, "detected bzip2 compressed stream");
408 return Open (stream, "bzcat");