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>
33 #include <sys/ioctl.h>
36 # include <sys/mman.h>
41 static int OpenGzip (vlc_object_t *);
42 static int OpenBzip2 (vlc_object_t *);
43 static void Close (vlc_object_t *);
46 set_description (N_("Decompression"))
47 set_category (CAT_INPUT)
48 set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
49 set_capability ("stream_filter", 20)
50 set_callbacks (OpenBzip2, Close)
51 /* TODO: access shortnames for stream_UrlNew() */
54 set_callbacks (OpenGzip, Close)
63 int write_fd, read_fd;
66 static void cloexec (int fd)
68 int flags = fcntl (fd, F_GETFD);
69 fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
72 extern char **environ;
74 static const size_t bufsize = 65536;
75 static void cleanup_mmap (void *addr)
77 munmap (addr, bufsize);
81 static void *Thread (void *data)
83 stream_t *stream = data;
84 stream_sys_t *p_sys = stream->p_sys;
86 uintptr_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
88 int fd = p_sys->write_fd;
94 int canc = vlc_savecancel ();
96 unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
97 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
98 vlc_cleanup_push (cleanup_mmap, buf);
100 unsigned char buf[bufsize];
103 len = stream_Read (stream->p_source, buf, bufsize);
104 vlc_restorecancel (canc);
109 for (ssize_t i = 0, j = 0; i < len; i += j)
111 struct iovec iov[1] = { { buf + i, len - i, } };
114 if (((len | i) & page_mask) == 0)
115 j = vmsplice (fd, iov, 1, SPLICE_F_GIFT);
118 j = writev (fd, iov, 1);
124 msg_Err (stream, "cannot write data (%m)");
130 vlc_cleanup_run (); /* munmap (buf, bufsize) */
135 msg_Dbg (stream, "compressed stream at EOF");
140 #define MIN_BLOCK (1 << 10)
141 #define MAX_BLOCK (1 << 20)
143 * Reads decompressed from the decompression program
144 * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
146 static int Read (stream_t *stream, void *buf, unsigned int buflen)
148 stream_sys_t *p_sys = stream->p_sys;
153 if ((peeked = p_sys->peeked) != NULL)
155 bonus = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
156 memcpy (buf, peeked->p_buffer, bonus);
157 peeked->p_buffer += bonus;
158 peeked->i_buffer -= bonus;
159 if (peeked->i_buffer == 0)
161 block_Release (peeked);
162 p_sys->peeked = NULL;
166 length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
170 p_sys->offset += length;
177 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
179 stream_sys_t *p_sys = stream->p_sys;
180 block_t *peeked = p_sys->peeked;
182 int fd = p_sys->read_fd;
185 peeked = block_Alloc (len);
186 else if ((curlen = peeked->i_buffer) < len)
187 peeked = block_Realloc (peeked, 0, len);
189 if ((p_sys->peeked = peeked) == NULL)
194 ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
199 peeked->i_buffer = curlen;
202 *pbuf = peeked->p_buffer;
209 static int Control (stream_t *stream, int query, va_list args)
211 stream_sys_t *p_sys = stream->p_sys;
215 case STREAM_CAN_SEEK:
216 case STREAM_CAN_FASTSEEK:
217 *(va_arg (args, bool *)) = false;
219 case STREAM_GET_POSITION:
220 *(va_arg (args, int64_t *)) = p_sys->offset;
222 case STREAM_GET_SIZE:
223 *(va_arg (args, int64_t *)) = 0;
226 *(va_arg (args, int *)) = 0;
235 * Pipe data through an external executable.
236 * @param stream the stream filter object.
237 * @param path path to the executable.
239 static int Open (stream_t *stream, const char *path)
241 stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
245 stream->pf_read = Read;
246 stream->pf_peek = Peek;
247 stream->pf_control = Control;
248 p_sys->peeked = NULL;
252 /* I am not a big fan of the pyramid style, but I cannot think of anything
253 * better here. There are too many failure cases. */
254 int ret = VLC_EGENERIC;
257 if (pipe (comp) == 0)
260 p_sys->write_fd = comp[1];
263 if (pipe (uncomp) == 0)
266 p_sys->read_fd = uncomp[0];
268 posix_spawn_file_actions_t actions;
269 if (posix_spawn_file_actions_init (&actions) == 0)
271 char *const argv[] = { (char *)path, NULL };
273 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
274 && !posix_spawn_file_actions_addclose (&actions, comp[0])
275 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
276 && !posix_spawn_file_actions_addclose (&actions, uncomp[1])
277 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
280 if (vlc_clone (&p_sys->thread, Thread, stream,
281 VLC_THREAD_PRIORITY_INPUT) == 0)
286 msg_Err (stream, "Cannot execute %s", path);
289 posix_spawn_file_actions_destroy (&actions);
292 if (ret != VLC_SUCCESS)
296 if (ret != VLC_SUCCESS)
299 if (p_sys->pid != -1)
300 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
308 * Releases allocate resources.
310 static void Close (vlc_object_t *obj)
312 stream_t *stream = (stream_t *)obj;
313 stream_sys_t *p_sys = stream->p_sys;
316 vlc_cancel (p_sys->thread);
317 close (p_sys->read_fd);
318 vlc_join (p_sys->thread, NULL);
319 close (p_sys->write_fd);
321 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
322 while (waitpid (p_sys->pid, &status, 0) == -1);
323 msg_Dbg (obj, "exit status %d", status);
326 block_Release (p_sys->peeked);
332 * Detects gzip file format
334 static int OpenGzip (vlc_object_t *obj)
336 stream_t *stream = (stream_t *)obj;
339 if (stream_Peek (stream->p_source, &peek, 3) < 3)
342 if (memcmp (peek, "\x1f\x8b\x08", 3))
345 msg_Dbg (obj, "detected gzip compressed stream");
346 return Open (stream, "zcat");
351 * Detects bzip2 file format
353 static int OpenBzip2 (vlc_object_t *obj)
355 stream_t *stream = (stream_t *)obj;
358 /* (Try to) parse the bzip2 header */
359 if (stream_Peek (stream->p_source, &peek, 10) < 10)
362 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
363 || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
366 msg_Dbg (obj, "detected bzip2 compressed stream");
367 return Open (stream, "bzcat");