📄 ftgzip.c
字号:
return error;
}
static FT_Error
ft_gzip_file_fill_input( FT_GZipFile zip )
{
z_stream* zstream = &zip->zstream;
FT_Stream stream = zip->source;
FT_ULong size;
if ( stream->read )
{
size = stream->read( stream, stream->pos, zip->input,
FT_GZIP_BUFFER_SIZE );
if ( size == 0 )
return Gzip_Err_Invalid_Stream_Operation;
}
else
{
size = stream->size - stream->pos;
if ( size > FT_GZIP_BUFFER_SIZE )
size = FT_GZIP_BUFFER_SIZE;
if ( size == 0 )
return Gzip_Err_Invalid_Stream_Operation;
FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
}
stream->pos += size;
zstream->next_in = zip->input;
zstream->avail_in = size;
return Gzip_Err_Ok;
}
static FT_Error
ft_gzip_file_fill_output( FT_GZipFile zip )
{
z_stream* zstream = &zip->zstream;
FT_Error error = 0;
zip->cursor = zip->buffer;
zstream->next_out = zip->cursor;
zstream->avail_out = FT_GZIP_BUFFER_SIZE;
while ( zstream->avail_out > 0 )
{
int err;
if ( zstream->avail_in == 0 )
{
error = ft_gzip_file_fill_input( zip );
if ( error )
break;
}
err = inflate( zstream, Z_NO_FLUSH );
if ( err == Z_STREAM_END )
{
zip->limit = zstream->next_out;
if ( zip->limit == zip->cursor )
error = Gzip_Err_Invalid_Stream_Operation;
break;
}
else if ( err != Z_OK )
{
error = Gzip_Err_Invalid_Stream_Operation;
break;
}
}
return error;
}
/* fill output buffer; `count' must be <= FT_GZIP_BUFFER_SIZE */
static FT_Error
ft_gzip_file_skip_output( FT_GZipFile zip,
FT_ULong count )
{
FT_Error error = Gzip_Err_Ok;
FT_ULong delta;
for (;;)
{
delta = (FT_ULong)( zip->limit - zip->cursor );
if ( delta >= count )
delta = count;
zip->cursor += delta;
zip->pos += delta;
count -= delta;
if ( count == 0 )
break;
error = ft_gzip_file_fill_output( zip );
if ( error )
break;
}
return error;
}
static FT_ULong
ft_gzip_file_io( FT_GZipFile zip,
FT_ULong pos,
FT_Byte* buffer,
FT_ULong count )
{
FT_ULong result = 0;
FT_Error error;
/* Reset inflate stream if we're seeking backwards. */
/* Yes, that is not too efficient, but it saves memory :-) */
if ( pos < zip->pos )
{
error = ft_gzip_file_reset( zip );
if ( error )
goto Exit;
}
/* skip unwanted bytes */
if ( pos > zip->pos )
{
error = ft_gzip_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
if ( error )
goto Exit;
}
if ( count == 0 )
goto Exit;
/* now read the data */
for (;;)
{
FT_ULong delta;
delta = (FT_ULong)( zip->limit - zip->cursor );
if ( delta >= count )
delta = count;
FT_MEM_COPY( buffer, zip->cursor, delta );
buffer += delta;
result += delta;
zip->cursor += delta;
zip->pos += delta;
count -= delta;
if ( count == 0 )
break;
error = ft_gzip_file_fill_output( zip );
if ( error )
break;
}
Exit:
return result;
}
/***************************************************************************/
/***************************************************************************/
/***** *****/
/***** G Z E M B E D D I N G S T R E A M *****/
/***** *****/
/***************************************************************************/
/***************************************************************************/
static void
ft_gzip_stream_close( FT_Stream stream )
{
FT_GZipFile zip = (FT_GZipFile)stream->descriptor.pointer;
FT_Memory memory = stream->memory;
if ( zip )
{
/* finalize gzip file descriptor */
ft_gzip_file_done( zip );
FT_FREE( zip );
stream->descriptor.pointer = NULL;
}
}
static FT_ULong
ft_gzip_stream_io( FT_Stream stream,
FT_ULong pos,
FT_Byte* buffer,
FT_ULong count )
{
FT_GZipFile zip = (FT_GZipFile)stream->descriptor.pointer;
return ft_gzip_file_io( zip, pos, buffer, count );
}
static FT_ULong
ft_gzip_get_uncompressed_size( FT_Stream stream )
{
FT_Error error;
FT_ULong old_pos;
FT_ULong result = 0;
old_pos = stream->pos;
if ( !FT_Stream_Seek( stream, stream->size - 4 ) )
{
result = (FT_ULong)FT_Stream_ReadLong( stream, &error );
if ( error )
result = 0;
FT_Stream_Seek( stream, old_pos );
}
return result;
}
FT_EXPORT_DEF( FT_Error )
FT_Stream_OpenGzip( FT_Stream stream,
FT_Stream source )
{
FT_Error error;
FT_Memory memory = source->memory;
FT_GZipFile zip;
/*
* check the header right now; this prevents allocating un-necessary
* objects when we don't need them
*/
error = ft_gzip_check_header( source );
if ( error )
goto Exit;
FT_ZERO( stream );
stream->memory = memory;
if ( !FT_QNEW( zip ) )
{
error = ft_gzip_file_init( zip, stream, source );
if ( error )
{
FT_FREE( zip );
goto Exit;
}
stream->descriptor.pointer = zip;
}
/*
* We use the following trick to try to dramatically improve the
* performance while dealing with small files. If the original stream
* size is less than a certain threshold, we try to load the whole font
* file into memory. This saves us from using the 32KB buffer needed
* to inflate the file, plus the two 4KB intermediate input/output
* buffers used in the `FT_GZipFile' structure.
*/
{
FT_ULong zip_size = ft_gzip_get_uncompressed_size( source );
if ( zip_size != 0 && zip_size < 40 * 1024 )
{
FT_Byte* zip_buff;
if ( !FT_ALLOC( zip_buff, zip_size ) )
{
FT_ULong count;
count = ft_gzip_file_io( zip, 0, zip_buff, zip_size );
if ( count == zip_size )
{
ft_gzip_file_done( zip );
FT_FREE( zip );
stream->descriptor.pointer = NULL;
stream->size = zip_size;
stream->pos = 0;
stream->base = zip_buff;
stream->read = NULL;
stream->close = ft_gzip_stream_close;
goto Exit;
}
ft_gzip_file_io( zip, 0, NULL, 0 );
FT_FREE( zip_buff );
}
error = 0;
}
}
stream->size = 0x7FFFFFFFL; /* don't know the real size! */
stream->pos = 0;
stream->base = 0;
stream->read = ft_gzip_stream_io;
stream->close = ft_gzip_stream_close;
Exit:
return error;
}
#else /* !FT_CONFIG_OPTION_USE_ZLIB */
FT_EXPORT_DEF( FT_Error )
FT_Stream_OpenGzip( FT_Stream stream,
FT_Stream source )
{
FT_UNUSED( stream );
FT_UNUSED( source );
return Gzip_Err_Unimplemented_Feature;
}
#endif /* !FT_CONFIG_OPTION_USE_ZLIB */
/* END */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -