Exemplo n.º 1
0
static MojoInput *make_gzip_input(MojoInput *origio)
{
    MojoInput *io = NULL;
    GZIPinfo *info = (GZIPinfo *) xmalloc(sizeof (GZIPinfo));

    initializeZStream(&info->stream);
    if (inflateInit2(&info->stream, 31) != Z_OK)
    {
        free(info);
        return NULL;
    } // if

    info->origio = origio;

    io = (MojoInput *) xmalloc(sizeof (MojoInput));
    io->ready = MojoInput_gzip_ready;
    io->read = MojoInput_gzip_read;
    io->seek = MojoInput_gzip_seek;
    io->tell = MojoInput_gzip_tell;
    io->length = MojoInput_gzip_length;
    io->duplicate = MojoInput_gzip_duplicate;
    io->close = MojoInput_gzip_close;
    io->opaque = info;
    return io;
} // make_gzip_input
Exemplo n.º 2
0
static int HHA_seek(fvoid *opaque, PHYSFS_uint64 offset)
{
    HHAfileinfo *finfo = (HHAfileinfo *) opaque;
    HHAentry *entry = finfo->entry;
    void *in = finfo->handle;

    BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
    BAIL_IF_MACRO(offset >= entry->uncompressed_size, ERR_PAST_EOF, 0);
    if (entry->compress == HHA_COMPRESS_NONE)
    {
        PHYSFS_sint64 newpos = offset + entry->offset;
        BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, newpos), NULL, 0);
        finfo->uncompressed_position = (PHYSFS_uint32) offset;
    }
    else if (entry->compress == HHA_COMPRESS_ZLIB)
    {
        /*
         * If seeking backwards, we need to redecode the file
         *  from the start and throw away the compressed bits until we hit
         *  the offset we need. If seeking forward, we still need to
         *  decode, but we don't rewind first.
         */
        if (offset < finfo->uncompressed_position)
        {
            /* we do a copy so state is sane if inflateInit2() fails. */
            z_stream str;
            initializeZStream(&str);
            if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
                return(0);

            if (!__PHYSFS_platformSeek(in, entry->offset))
                return(0);

            inflateEnd(&finfo->zlib_stream);
            memcpy(&finfo->zlib_stream, &str, sizeof (z_stream));
            finfo->uncompressed_position = finfo->compressed_position = 0;
        } /* if */

        while (finfo->uncompressed_position != offset)
        {
            PHYSFS_uint8 buf[512];
            PHYSFS_uint32 maxread;

            maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position);
            if (maxread > sizeof (buf))
                maxread = sizeof (buf);

            if (HHA_read(finfo, buf, maxread, 1) != 1)
                return(0);
        } /* while */
    } /* else */
    else
        printf("LZMA file: %s/%s\n", entry->dir, entry->name);
    return(1);
} /* HHA_seek */
Exemplo n.º 3
0
static int ZIP_seek(fvoid *opaque, PHYSFS_uint64 offset)
{
    ZIPfileinfo *finfo = (ZIPfileinfo *) opaque;
    ZIPentry *entry = finfo->entry;
    void *in = finfo->handle;

    BAIL_IF_MACRO(offset > entry->uncompressed_size, ERR_PAST_EOF, 0);

    if (entry->compression_method == COMPMETH_NONE)
    {
        PHYSFS_sint64 newpos = offset + entry->offset;
        BAIL_IF_MACRO(!__PHYSFS_platformSeek(in, newpos), NULL, 0);
        finfo->uncompressed_position = (PHYSFS_uint32) offset;
    } /* if */

    else
    {
        /*
         * If seeking backwards, we need to redecode the file
         *  from the start and throw away the compressed bits until we hit
         *  the offset we need. If seeking forward, we still need to
         *  decode, but we don't rewind first.
         */
        if (offset < finfo->uncompressed_position)
        {
            /* we do a copy so state is sane if inflateInit2() fails. */
            z_stream str;
            initializeZStream(&str);
            if (zlib_err(inflateInit2(&str, -MAX_WBITS)) != Z_OK)
                return(0);

            if (!__PHYSFS_platformSeek(in, entry->offset))
                return(0);

            inflateEnd(&finfo->stream);
            memcpy(&finfo->stream, &str, sizeof (z_stream));
            finfo->uncompressed_position = finfo->compressed_position = 0;
        } /* if */

        while (finfo->uncompressed_position != offset)
        {
            PHYSFS_uint8 buf[512];
            PHYSFS_uint32 maxread;

            maxread = (PHYSFS_uint32) (offset - finfo->uncompressed_position);
            if (maxread > sizeof (buf))
                maxread = sizeof (buf);

            if (ZIP_read(finfo, buf, maxread, 1) != 1)
                return(0);
        } /* while */
    } /* else */

    return(1);
} /* ZIP_seek */
Exemplo n.º 4
0
static fvoid *HHA_openRead(dvoid *opaque, const char *fnm, int *fileExists)
{
    HHAinfo *info = (HHAinfo *) opaque;
    HHAfileinfo *finfo;
    HHAentry *entry;

    entry = HHA_find_entry(info, fnm);
    *fileExists = (entry != NULL);
    BAIL_IF_MACRO(entry == NULL, NULL, NULL);

    finfo = (HHAfileinfo *) allocator.Malloc(sizeof (HHAfileinfo));
    BAIL_IF_MACRO(finfo == NULL, ERR_OUT_OF_MEMORY, NULL);
    memset(finfo, '\0', sizeof (HHAfileinfo));
    finfo->handle = __PHYSFS_platformOpenRead(info->filename);
    if ( (finfo->handle == NULL) ||
         (!__PHYSFS_platformSeek(finfo->handle, entry->offset)) )
    {
        allocator.Free(finfo);
        return(NULL);
    } /* if */
    
    finfo->entry = entry;
    
    if (finfo->entry->compress == HHA_COMPRESS_ZLIB)
    {
        initializeZStream(&finfo->zlib_stream);
        if (zlib_err(inflateInit2(&finfo->zlib_stream, -MAX_WBITS)) != Z_OK)
        {
            HHA_fileClose(finfo);
            return(NULL);
        } /* if */

        finfo->buffer = (PHYSFS_uint8 *) allocator.Malloc(ZIP_READBUFSIZE);
        if (finfo->buffer == NULL)
        {
            HHA_fileClose(finfo);
            BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
        } /* if */
    } /* if */
    
    return(finfo);
} /* HHA_openRead */
Exemplo n.º 5
0
static boolean MojoInput_gzip_seek(MojoInput *io, uint64 offset)
{
    // This is all really expensive.
    GZIPinfo *info = (GZIPinfo *) io->opaque;

    /*
     * If seeking backwards, we need to redecode the file
     *  from the start and throw away the compressed bits until we hit
     *  the offset we need. If seeking forward, we still need to
     *  decode, but we don't rewind first.
     */
    if (offset < info->uncompressed_position)
    {
        if (!info->origio->seek(info->origio, 0))
            return false;
        inflateEnd(&info->stream);
        initializeZStream(&info->stream);
        if (inflateInit2(&info->stream, 31) != Z_OK)
            return false;
        info->uncompressed_position = 0;
    } // if

    while (info->uncompressed_position != offset)
    {
        uint8 buf[512];
        uint32 maxread;
        int64 br;

        maxread = (uint32) (offset - info->uncompressed_position);
        if (maxread > sizeof (buf))
            maxread = sizeof (buf);

        br = io->read(io, buf, maxread);
        if (br != maxread)
            return false;
    } /* while */

    return true;
} // MojoInput_gzip_seek