FT_Stream_OpenBzip2( FT_Stream stream, FT_Stream source ) { FT_Error error; FT_Memory memory; FT_BZip2File zip = NULL; if ( !stream || !source ) { error = FT_THROW( Invalid_Stream_Handle ); goto Exit; } memory = source->memory; /* * check the header right now; this prevents allocating unnecessary * objects when we don't need them */ error = ft_bzip2_check_header( source ); if ( error ) goto Exit; FT_ZERO( stream ); stream->memory = memory; if ( !FT_QNEW( zip ) ) { error = ft_bzip2_file_init( zip, stream, source ); if ( error ) { FT_FREE( zip ); goto Exit; } stream->descriptor.pointer = zip; } stream->size = 0x7FFFFFFFL; /* don't know the real size! */ stream->pos = 0; stream->base = 0; stream->read = ft_bzip2_stream_io; stream->close = ft_bzip2_stream_close; Exit: return error; }
static FT_Error ft_bzip2_file_init( FT_BZip2File zip, FT_Stream stream, FT_Stream source ) { bz_stream* bzstream = &zip->bzstream; FT_Error error = FT_Err_Ok; zip->stream = stream; zip->source = source; zip->memory = stream->memory; zip->limit = zip->buffer + FT_BZIP2_BUFFER_SIZE; zip->cursor = zip->limit; zip->pos = 0; /* check .bz2 header */ { stream = source; error = ft_bzip2_check_header( stream ); if ( error ) goto Exit; if ( FT_STREAM_SEEK( 0 ) ) goto Exit; } /* initialize bzlib */ bzstream->bzalloc = (alloc_func)ft_bzip2_alloc; bzstream->bzfree = (free_func) ft_bzip2_free; bzstream->opaque = zip->memory; bzstream->avail_in = 0; bzstream->next_in = (char*)zip->buffer; if ( BZ2_bzDecompressInit( bzstream, 0, 0 ) != BZ_OK || bzstream->next_in == NULL ) error = FT_THROW( Invalid_File_Format ); Exit: return error; }