X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fstream_filter%2Fdecomp.c;h=36e2aee67932679311ef11528d9e4a1bdbbe38af;hb=21ecdaab62967d244e534b57832d7c202caf3f82;hp=5171ea703d8e1e326044d601e58a6815c1cc0344;hpb=be19f7facceb129ef8977ed0ad2b41c593954629;p=vlc diff --git a/modules/stream_filter/decomp.c b/modules/stream_filter/decomp.c index 5171ea703d..36e2aee679 100644 --- a/modules/stream_filter/decomp.c +++ b/modules/stream_filter/decomp.c @@ -1,7 +1,7 @@ /***************************************************************************** * decomp.c : Decompression module for vlc ***************************************************************************** - * Copyright © 2008 Rémi Denis-Courmont + * Copyright © 2008-2009 Rémi Denis-Courmont * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -26,8 +26,10 @@ #include #include #include +#include #include #include +#include #ifndef _POSIX_SPAWN # define _POSIX_SPAWN (-1) #endif @@ -46,6 +48,7 @@ static int OpenGzip (vlc_object_t *); static int OpenBzip2 (vlc_object_t *); +static int OpenXZ (vlc_object_t *); static void Close (vlc_object_t *); vlc_module_begin () @@ -53,6 +56,9 @@ vlc_module_begin () set_category (CAT_INPUT) set_subcategory (SUBCAT_INPUT_STREAM_FILTER) set_capability ("stream_filter", 20) + set_callbacks (OpenXZ, Close) + + add_submodule () set_callbacks (OpenBzip2, Close) /* TODO: access shortnames for stream_UrlNew() */ @@ -69,12 +75,6 @@ struct stream_sys_t int write_fd, read_fd; }; -static void cloexec (int fd) -{ - int flags = fcntl (fd, F_GETFD); - fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0)); -} - extern char **environ; static const size_t bufsize = 65536; @@ -90,7 +90,7 @@ static void *Thread (void *data) stream_t *stream = data; stream_sys_t *p_sys = stream->p_sys; #ifdef HAVE_VMSPLICE - ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1; + const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1; #endif int fd = p_sys->write_fd; bool error = false; @@ -102,9 +102,14 @@ static void *Thread (void *data) #ifdef HAVE_VMSPLICE unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (unlikely(buf == MAP_FAILED)) + break; vlc_cleanup_push (cleanup_mmap, buf); #else - unsigned char buf[bufsize]; + unsigned char *buf = malloc (bufsize); + if (unlikely(buf == NULL)) + break; + vlc_cleanup_push (free, buf); #endif len = stream_Read (stream->p_source, buf, bufsize); @@ -133,13 +138,14 @@ static void *Thread (void *data) break; } } -#ifdef HAVE_VMSPLICE - vlc_cleanup_run (); /* munmap (buf, bufsize) */ -#endif + vlc_cleanup_run (); /* free (buf) */ } while (!error); msg_Dbg (stream, "compressed stream at EOF"); + /* Let child process know about EOF */ + p_sys->write_fd = -1; + close (fd); return NULL; } @@ -238,10 +244,10 @@ static int Control (stream_t *stream, int query, va_list args) *(va_arg (args, bool *)) = false; break; case STREAM_GET_POSITION: - *(va_arg (args, int64_t *)) = p_sys->offset; + *(va_arg (args, uint64_t *)) = p_sys->offset; break; case STREAM_GET_SIZE: - *(va_arg (args, int64_t *)) = 0; + *(va_arg (args, uint64_t *)) = 0; break; default: return VLC_EGENERIC; @@ -274,15 +280,13 @@ static int Open (stream_t *stream, const char *path) /* We use two pipes rather than one stream socket pair, so that we can * use vmsplice() on Linux. */ - if (pipe (comp) == 0) + if (vlc_pipe (comp) == 0) { - cloexec (comp[1]); p_sys->write_fd = comp[1]; int uncomp[2]; - if (pipe (uncomp) == 0) + if (vlc_pipe (uncomp) == 0) { - cloexec (uncomp[0]); p_sys->read_fd = uncomp[0]; #if (_POSIX_SPAWN >= 0) @@ -292,9 +296,7 @@ static int Open (stream_t *stream, const char *path) char *const argv[] = { (char *)path, NULL }; if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0) - && !posix_spawn_file_actions_addclose (&actions, comp[0]) && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1) - && !posix_spawn_file_actions_addclose (&actions, uncomp[1]) && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv, environ)) { @@ -317,9 +319,7 @@ static int Open (stream_t *stream, const char *path) break; case 0: dup2 (comp[0], 0); - close (comp[0]); dup2 (uncomp[1], 1); - close (uncomp[1]); execlp (path, path, (char *)NULL); exit (1); /* if we get, execlp() failed! */ default: @@ -356,7 +356,9 @@ static void Close (vlc_object_t *obj) vlc_cancel (p_sys->thread); close (p_sys->read_fd); vlc_join (p_sys->thread, NULL); - close (p_sys->write_fd); + if (p_sys->write_fd != -1) + /* Killed before EOF? */ + close (p_sys->write_fd); msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid); while (waitpid (p_sys->pid, &status, 0) == -1); @@ -407,3 +409,21 @@ static int OpenBzip2 (vlc_object_t *obj) return Open (stream, "bzcat"); } +/** + * Detects xz file format + */ +static int OpenXZ (vlc_object_t *obj) +{ + stream_t *stream = (stream_t *)obj; + const uint8_t *peek; + + /* (Try to) parse the xz stream header */ + if (stream_Peek (stream->p_source, &peek, 8) < 8) + return VLC_EGENERIC; + + if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6)) + return VLC_EGENERIC; + + msg_Dbg (obj, "detected xz compressed stream"); + return Open (stream, "xzcat"); +}