inflate.c 5.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*****************************************************************************
 * inflate.c: zlib decompression module for VLC
 *****************************************************************************
 * Copyright © 2016 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
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <zlib.h>

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_stream.h>

33
typedef struct
34 35 36 37
{
    z_stream zstream;
    bool eof;
    unsigned char buffer[16384];
38
} stream_sys_t;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

static ssize_t Read(stream_t *stream, void *buf, size_t buflen)
{
    stream_sys_t *sys = stream->p_sys;
    ssize_t val;

    if (sys->eof || unlikely(buflen == 0))
        return 0;

    sys->zstream.next_out = buf;
    sys->zstream.avail_out = buflen;

    if (sys->zstream.avail_in == 0)
        sys->zstream.next_in = sys->buffer;

    val = (sys->buffer + sizeof (sys->buffer))
        - (sys->zstream.next_in + sys->zstream.avail_in);

    if (val > 0)
    {   /* Fill input buffer if there is space left */
59
        val = vlc_stream_Read(stream->s,
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                          sys->zstream.next_in + sys->zstream.avail_in, val);
        if (val >= 0)
            sys->zstream.avail_in += val;
    }

    if (sys->zstream.avail_in == 0)
    {
        msg_Err(stream, "unexpected end of stream");
        return 0;
    }

    val = inflate(&sys->zstream, Z_SYNC_FLUSH);
    switch (val)
    {
        case Z_STREAM_END:
            msg_Dbg(stream, "end of stream");
            sys->eof = true;
            /* fall through */
        case Z_OK:
            return buflen - sys->zstream.avail_out;
        case Z_DATA_ERROR:
            msg_Err(stream, "corrupt stream");
            sys->eof = true;
            return -1;
        case Z_BUF_ERROR:
            if (sys->zstream.next_in == sys->buffer)
                break;

            memmove(sys->buffer, sys->zstream.next_in, sys->zstream.avail_in);
            sys->zstream.next_in = sys->buffer;
            return Read(stream, buf, buflen);
    }

    msg_Err(stream, "unhandled decompression error (%zd)", val);
    return -1;
}

static int Seek(stream_t *stream, uint64_t offset)
{
    (void) stream; (void) offset;
    return -1;
}

static int Control(stream_t *stream, int query, va_list args)
{
    switch (query)
    {
        case STREAM_CAN_SEEK:
        case STREAM_CAN_FASTSEEK:
            *va_arg(args, bool *) = false;
            break;
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
        case STREAM_GET_PTS_DELAY:
        case STREAM_GET_META:
        case STREAM_GET_CONTENT_TYPE:
        case STREAM_GET_SIGNAL:
        case STREAM_SET_PAUSE_STATE:
118
            return vlc_stream_vaControl(stream->s, query, args);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        case STREAM_GET_SIZE:
        case STREAM_GET_TITLE_INFO:
        case STREAM_GET_TITLE:
        case STREAM_GET_SEEKPOINT:
        case STREAM_SET_TITLE:
        case STREAM_SET_SEEKPOINT:
        case STREAM_SET_PRIVATE_ID_STATE:
        case STREAM_SET_PRIVATE_ID_CA:
        case STREAM_GET_PRIVATE_ID_STATE:
            return VLC_EGENERIC;
        default:
            msg_Err(stream, "unimplemented query (%d) in control", query);
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

static int Open(vlc_object_t *obj)
{
    stream_t *stream = (stream_t *)obj;
    const uint8_t *peek;
140
    int bits;
141 142

    /* See IETF RFC6713 */
143
    if (vlc_stream_Peek(stream->s, &peek, 2) < 2)
144 145 146 147 148
        return VLC_EGENERIC;

    if ((peek[0] & 0xF) == 8 && (peek[0] >> 4) < 8 && (U16_AT(peek) % 31) == 0)
        bits = 15; /* zlib */
    else
149
    if (!memcmp(peek, "\x1F\x8B", 2))
150 151
        bits = 15 + 32; /* gzip */
    else
152 153 154 155 156 157 158 159 160 161 162 163 164
        return VLC_EGENERIC;

    stream_sys_t *sys = malloc(sizeof (*sys));
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

    sys->zstream.next_in = sys->buffer;
    sys->zstream.avail_in = 0;
    sys->zstream.zalloc = Z_NULL;
    sys->zstream.zfree = Z_NULL;
    sys->zstream.opaque = Z_NULL;
    sys->eof = false;

165
    int ret = inflateInit2(&sys->zstream, bits);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    if (ret != Z_OK)
    {
        free(sys);
        return (ret == Z_MEM_ERROR) ? VLC_ENOMEM : VLC_EGENERIC;
    }

    stream->p_sys = sys;
    stream->pf_read = Read;
    stream->pf_seek = Seek;
    stream->pf_control = Control;
    return VLC_SUCCESS;
}

static void Close (vlc_object_t *obj)
{
    stream_t *stream = (stream_t *)obj;
    stream_sys_t *sys = stream->p_sys;

    inflateEnd(&sys->zstream);
    free(sys);
}

vlc_module_begin()
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_STREAM_FILTER)
191
    set_capability("stream_filter", 330)
192 193 194 195

    set_description(N_("Zlib decompression filter"))
    set_callbacks(Open, Close)
vlc_module_end()