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 #define MIN_BLOCK (1 << 10)
147 #define MAX_BLOCK (1 << 20)
149 * Reads decompressed from the decompression program
150 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
152 static int Read (stream_t *stream, void *buf, unsigned int buflen)
154 stream_sys_t *p_sys = stream->p_sys;
158 if ((peeked = p_sys->peeked) != NULL)
159 { /* dequeue peeked data */
160 length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
161 memcpy (buf, peeked->p_buffer, length);
162 buf = ((char *)buf) + length;
164 peeked->p_buffer += length;
165 peeked->i_buffer -= length;
166 if (peeked->i_buffer == 0)
168 block_Release (peeked);
169 p_sys->peeked = NULL;
171 p_sys->offset += length;
174 length += Read (stream, ((char *)buf) + length, buflen - length);
178 length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
181 p_sys->offset += length;
188 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
190 stream_sys_t *p_sys = stream->p_sys;
191 block_t *peeked = p_sys->peeked;
193 int fd = p_sys->read_fd;
196 peeked = block_Alloc (len);
197 else if ((curlen = peeked->i_buffer) < len)
198 peeked = block_Realloc (peeked, 0, len);
200 if ((p_sys->peeked = peeked) == NULL)
205 ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
210 peeked->i_buffer = curlen;
213 *pbuf = peeked->p_buffer;
220 static int Control (stream_t *stream, int query, va_list args)
222 stream_sys_t *p_sys = stream->p_sys;
226 case STREAM_CAN_SEEK:
227 case STREAM_CAN_FASTSEEK:
228 *(va_arg (args, bool *)) = false;
230 case STREAM_GET_POSITION:
231 *(va_arg (args, int64_t *)) = p_sys->offset;
233 case STREAM_GET_SIZE:
234 *(va_arg (args, int64_t *)) = 0;
237 *(va_arg (args, int *)) = 0;
246 * Pipe data through an external executable.
247 * @param stream the stream filter object.
248 * @param path path to the executable.
250 static int Open (stream_t *stream, const char *path)
252 stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
256 stream->pf_read = Read;
257 stream->pf_peek = Peek;
258 stream->pf_control = Control;
259 p_sys->peeked = NULL;
263 /* I am not a big fan of the pyramid style, but I cannot think of anything
264 * better here. There are too many failure cases. */
265 int ret = VLC_EGENERIC;
268 /* We use two pipes rather than one stream socket pair, so that we can
269 * use vmsplice() on Linux. */
270 if (pipe (comp) == 0)
273 p_sys->write_fd = comp[1];
276 if (pipe (uncomp) == 0)
279 p_sys->read_fd = uncomp[0];
281 #if (_POSIX_SPAWN >= 0)
282 posix_spawn_file_actions_t actions;
283 if (posix_spawn_file_actions_init (&actions) == 0)
285 char *const argv[] = { (char *)path, NULL };
287 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
288 && !posix_spawn_file_actions_addclose (&actions, comp[0])
289 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
290 && !posix_spawn_file_actions_addclose (&actions, uncomp[1])
291 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
294 if (vlc_clone (&p_sys->thread, Thread, stream,
295 VLC_THREAD_PRIORITY_INPUT) == 0)
300 msg_Err (stream, "Cannot execute %s", path);
303 posix_spawn_file_actions_destroy (&actions);
305 #else /* _POSIX_SPAWN */
306 switch (p_sys->pid = fork ())
309 msg_Err (stream, "Cannot fork (%m)");
316 execlp (path, path, (char *)NULL);
317 exit (1); /* if we get, execlp() failed! */
319 if (vlc_clone (&p_sys->thread, Thread, stream,
320 VLC_THREAD_PRIORITY_INPUT) == 0)
323 #endif /* _POSIX_SPAWN < 0 */
325 if (ret != VLC_SUCCESS)
329 if (ret != VLC_SUCCESS)
332 if (p_sys->pid != -1)
333 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
341 * Releases allocate resources.
343 static void Close (vlc_object_t *obj)
345 stream_t *stream = (stream_t *)obj;
346 stream_sys_t *p_sys = stream->p_sys;
349 vlc_cancel (p_sys->thread);
350 close (p_sys->read_fd);
351 vlc_join (p_sys->thread, NULL);
352 close (p_sys->write_fd);
354 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
355 while (waitpid (p_sys->pid, &status, 0) == -1);
356 msg_Dbg (obj, "exit status %d", status);
359 block_Release (p_sys->peeked);
365 * Detects gzip file format
367 static int OpenGzip (vlc_object_t *obj)
369 stream_t *stream = (stream_t *)obj;
372 if (stream_Peek (stream->p_source, &peek, 3) < 3)
375 if (memcmp (peek, "\x1f\x8b\x08", 3))
378 msg_Dbg (obj, "detected gzip compressed stream");
379 return Open (stream, "zcat");
384 * Detects bzip2 file format
386 static int OpenBzip2 (vlc_object_t *obj)
388 stream_t *stream = (stream_t *)obj;
391 /* (Try to) parse the bzip2 header */
392 if (stream_Peek (stream->p_source, &peek, 10) < 10)
395 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
396 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
399 msg_Dbg (obj, "detected bzip2 compressed stream");
400 return Open (stream, "bzcat");