1 /***************************************************************************/
5 /* FreeType support for .gz compressed files. */
7 /* This optional component relies on zlib. It should mainly be used to */
8 /* parse compressed PCF fonts, as found with many X11 server */
11 /* Copyright 2002-2006, 2009-2012 by */
12 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
14 /* This file is part of the FreeType project, and may only be used, */
15 /* modified, and distributed under the terms of the FreeType project */
16 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
17 /* this file you indicate that you have read the license and */
18 /* understand and accept it fully. */
20 /***************************************************************************/
24 #include FT_INTERNAL_MEMORY_H
25 #include FT_INTERNAL_STREAM_H
26 #include FT_INTERNAL_DEBUG_H
28 #include FT_CONFIG_STANDARD_LIBRARY_H
31 #include FT_MODULE_ERRORS_H
36 #define FT_ERR_PREFIX Gzip_Err_
37 #define FT_ERR_BASE FT_Mod_Err_Gzip
42 #ifdef FT_CONFIG_OPTION_USE_ZLIB
44 #ifdef FT_CONFIG_OPTION_PIC
45 #error "gzip code does not support PIC yet"
48 #ifdef FT_CONFIG_OPTION_SYSTEM_ZLIB
52 #else /* !FT_CONFIG_OPTION_SYSTEM_ZLIB */
54 /* In this case, we include our own modified sources of the ZLib */
55 /* within the "ftgzip" component. The modifications were necessary */
56 /* to #include all files without conflicts, as well as preventing */
57 /* the definition of "extern" functions that may cause linking */
58 /* conflicts when a program is linked with both FreeType and the */
62 #ifndef USE_ZLIB_ZCALLOC
63 #define MY_ZCALLOC /* prevent all zcalloc() & zfree() in zutils.c */
69 #define SLOW 1 /* we can't use asm-optimized sources here! */
71 /* Urgh. `inflate_mask' must not be declared twice -- C++ doesn't like
72 this. We temporarily disable it and load all necessary header files. */
73 #define NO_INFLATE_MASK
79 #undef NO_INFLATE_MASK
81 /* infutil.c must be included before infcodes.c */
90 #endif /* !FT_CONFIG_OPTION_SYSTEM_ZLIB */
93 /***************************************************************************/
94 /***************************************************************************/
96 /***** Z L I B M E M O R Y M A N A G E M E N T *****/
98 /***************************************************************************/
99 /***************************************************************************/
101 /* it is better to use FreeType memory routines instead of raw
105 ft_gzip_alloc( FT_Memory memory,
109 FT_ULong sz = (FT_ULong)size * items;
114 (void)FT_ALLOC( p, sz );
120 ft_gzip_free( FT_Memory memory,
123 FT_MEM_FREE( address );
127 #if !defined( FT_CONFIG_OPTION_SYSTEM_ZLIB ) && !defined( USE_ZLIB_ZCALLOC )
130 zcalloc ( voidpf opaque,
134 return ft_gzip_alloc( (FT_Memory)opaque, items, size );
138 zcfree( voidpf opaque,
141 ft_gzip_free( (FT_Memory)opaque, ptr );
144 #endif /* !SYSTEM_ZLIB && !USE_ZLIB_ZCALLOC */
147 /***************************************************************************/
148 /***************************************************************************/
150 /***** Z L I B F I L E D E S C R I P T O R *****/
152 /***************************************************************************/
153 /***************************************************************************/
155 #define FT_GZIP_BUFFER_SIZE 4096
157 typedef struct FT_GZipFileRec_
159 FT_Stream source; /* parent/source stream */
160 FT_Stream stream; /* embedding stream */
161 FT_Memory memory; /* memory allocator */
162 z_stream zstream; /* zlib input stream */
164 FT_ULong start; /* starting position, after .gz header */
165 FT_Byte input[FT_GZIP_BUFFER_SIZE]; /* input read buffer */
167 FT_Byte buffer[FT_GZIP_BUFFER_SIZE]; /* output buffer */
168 FT_ULong pos; /* position in output */
172 } FT_GZipFileRec, *FT_GZipFile;
176 #define FT_GZIP_ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
177 #define FT_GZIP_HEAD_CRC 0x02 /* bit 1 set: header CRC present */
178 #define FT_GZIP_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
179 #define FT_GZIP_ORIG_NAME 0x08 /* bit 3 set: original file name present */
180 #define FT_GZIP_COMMENT 0x10 /* bit 4 set: file comment present */
181 #define FT_GZIP_RESERVED 0xE0 /* bits 5..7: reserved */
184 /* check and skip .gz header - we don't support `transparent' compression */
186 ft_gzip_check_header( FT_Stream stream )
192 if ( FT_STREAM_SEEK( 0 ) ||
193 FT_STREAM_READ( head, 4 ) )
196 /* head[0] && head[1] are the magic numbers; */
197 /* head[2] is the method, and head[3] the flags */
198 if ( head[0] != 0x1f ||
200 head[2] != Z_DEFLATED ||
201 (head[3] & FT_GZIP_RESERVED) )
203 error = Gzip_Err_Invalid_File_Format;
207 /* skip time, xflags and os code */
208 (void)FT_STREAM_SKIP( 6 );
210 /* skip the extra field */
211 if ( head[3] & FT_GZIP_EXTRA_FIELD )
216 if ( FT_READ_USHORT_LE( len ) ||
217 FT_STREAM_SKIP( len ) )
221 /* skip original file name */
222 if ( head[3] & FT_GZIP_ORIG_NAME )
228 if ( FT_READ_BYTE( c ) )
235 /* skip .gz comment */
236 if ( head[3] & FT_GZIP_COMMENT )
242 if ( FT_READ_BYTE( c ) )
250 if ( head[3] & FT_GZIP_HEAD_CRC )
251 if ( FT_STREAM_SKIP( 2 ) )
260 ft_gzip_file_init( FT_GZipFile zip,
264 z_stream* zstream = &zip->zstream;
265 FT_Error error = Gzip_Err_Ok;
268 zip->stream = stream;
269 zip->source = source;
270 zip->memory = stream->memory;
272 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE;
273 zip->cursor = zip->limit;
276 /* check and skip .gz header */
280 error = ft_gzip_check_header( stream );
284 zip->start = FT_STREAM_POS();
287 /* initialize zlib -- there is no zlib header in the compressed stream */
288 zstream->zalloc = (alloc_func)ft_gzip_alloc;
289 zstream->zfree = (free_func) ft_gzip_free;
290 zstream->opaque = stream->memory;
292 zstream->avail_in = 0;
293 zstream->next_in = zip->buffer;
295 if ( inflateInit2( zstream, -MAX_WBITS ) != Z_OK ||
296 zstream->next_in == NULL )
297 error = Gzip_Err_Invalid_File_Format;
305 ft_gzip_file_done( FT_GZipFile zip )
307 z_stream* zstream = &zip->zstream;
310 inflateEnd( zstream );
313 zstream->zalloc = NULL;
314 zstream->zfree = NULL;
315 zstream->opaque = NULL;
316 zstream->next_in = NULL;
317 zstream->next_out = NULL;
318 zstream->avail_in = 0;
319 zstream->avail_out = 0;
328 ft_gzip_file_reset( FT_GZipFile zip )
330 FT_Stream stream = zip->source;
334 if ( !FT_STREAM_SEEK( zip->start ) )
336 z_stream* zstream = &zip->zstream;
339 inflateReset( zstream );
341 zstream->avail_in = 0;
342 zstream->next_in = zip->input;
343 zstream->avail_out = 0;
344 zstream->next_out = zip->buffer;
346 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE;
347 zip->cursor = zip->limit;
356 ft_gzip_file_fill_input( FT_GZipFile zip )
358 z_stream* zstream = &zip->zstream;
359 FT_Stream stream = zip->source;
365 size = stream->read( stream, stream->pos, zip->input,
366 FT_GZIP_BUFFER_SIZE );
368 return Gzip_Err_Invalid_Stream_Operation;
372 size = stream->size - stream->pos;
373 if ( size > FT_GZIP_BUFFER_SIZE )
374 size = FT_GZIP_BUFFER_SIZE;
377 return Gzip_Err_Invalid_Stream_Operation;
379 FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
383 zstream->next_in = zip->input;
384 zstream->avail_in = size;
391 ft_gzip_file_fill_output( FT_GZipFile zip )
393 z_stream* zstream = &zip->zstream;
394 FT_Error error = Gzip_Err_Ok;
397 zip->cursor = zip->buffer;
398 zstream->next_out = zip->cursor;
399 zstream->avail_out = FT_GZIP_BUFFER_SIZE;
401 while ( zstream->avail_out > 0 )
406 if ( zstream->avail_in == 0 )
408 error = ft_gzip_file_fill_input( zip );
413 err = inflate( zstream, Z_NO_FLUSH );
415 if ( err == Z_STREAM_END )
417 zip->limit = zstream->next_out;
418 if ( zip->limit == zip->cursor )
419 error = Gzip_Err_Invalid_Stream_Operation;
422 else if ( err != Z_OK )
424 error = Gzip_Err_Invalid_Stream_Operation;
433 /* fill output buffer; `count' must be <= FT_GZIP_BUFFER_SIZE */
435 ft_gzip_file_skip_output( FT_GZipFile zip,
438 FT_Error error = Gzip_Err_Ok;
444 delta = (FT_ULong)( zip->limit - zip->cursor );
445 if ( delta >= count )
448 zip->cursor += delta;
455 error = ft_gzip_file_fill_output( zip );
465 ft_gzip_file_io( FT_GZipFile zip,
474 /* Reset inflate stream if we're seeking backwards. */
475 /* Yes, that is not too efficient, but it saves memory :-) */
476 if ( pos < zip->pos )
478 error = ft_gzip_file_reset( zip );
483 /* skip unwanted bytes */
484 if ( pos > zip->pos )
486 error = ft_gzip_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
494 /* now read the data */
500 delta = (FT_ULong)( zip->limit - zip->cursor );
501 if ( delta >= count )
504 FT_MEM_COPY( buffer, zip->cursor, delta );
507 zip->cursor += delta;
514 error = ft_gzip_file_fill_output( zip );
524 /***************************************************************************/
525 /***************************************************************************/
527 /***** G Z E M B E D D I N G S T R E A M *****/
529 /***************************************************************************/
530 /***************************************************************************/
533 ft_gzip_stream_close( FT_Stream stream )
535 FT_GZipFile zip = (FT_GZipFile)stream->descriptor.pointer;
536 FT_Memory memory = stream->memory;
541 /* finalize gzip file descriptor */
542 ft_gzip_file_done( zip );
546 stream->descriptor.pointer = NULL;
552 ft_gzip_stream_io( FT_Stream stream,
557 FT_GZipFile zip = (FT_GZipFile)stream->descriptor.pointer;
560 return ft_gzip_file_io( zip, pos, buffer, count );
565 ft_gzip_get_uncompressed_size( FT_Stream stream )
572 old_pos = stream->pos;
573 if ( !FT_Stream_Seek( stream, stream->size - 4 ) )
575 result = FT_Stream_ReadULong( stream, &error );
579 (void)FT_Stream_Seek( stream, old_pos );
586 FT_EXPORT_DEF( FT_Error )
587 FT_Stream_OpenGzip( FT_Stream stream,
591 FT_Memory memory = source->memory;
592 FT_GZipFile zip = NULL;
596 * check the header right now; this prevents allocating un-necessary
597 * objects when we don't need them
599 error = ft_gzip_check_header( source );
604 stream->memory = memory;
606 if ( !FT_QNEW( zip ) )
608 error = ft_gzip_file_init( zip, stream, source );
615 stream->descriptor.pointer = zip;
619 * We use the following trick to try to dramatically improve the
620 * performance while dealing with small files. If the original stream
621 * size is less than a certain threshold, we try to load the whole font
622 * file into memory. This saves us from using the 32KB buffer needed
623 * to inflate the file, plus the two 4KB intermediate input/output
624 * buffers used in the `FT_GZipFile' structure.
627 FT_ULong zip_size = ft_gzip_get_uncompressed_size( source );
630 if ( zip_size != 0 && zip_size < 40 * 1024 )
632 FT_Byte* zip_buff = NULL;
635 if ( !FT_ALLOC( zip_buff, zip_size ) )
640 count = ft_gzip_file_io( zip, 0, zip_buff, zip_size );
641 if ( count == zip_size )
643 ft_gzip_file_done( zip );
646 stream->descriptor.pointer = NULL;
648 stream->size = zip_size;
650 stream->base = zip_buff;
652 stream->close = ft_gzip_stream_close;
657 ft_gzip_file_io( zip, 0, NULL, 0 );
664 stream->size = 0x7FFFFFFFL; /* don't know the real size! */
667 stream->read = ft_gzip_stream_io;
668 stream->close = ft_gzip_stream_close;
674 #else /* !FT_CONFIG_OPTION_USE_ZLIB */
676 FT_EXPORT_DEF( FT_Error )
677 FT_Stream_OpenGzip( FT_Stream stream,
683 return Gzip_Err_Unimplemented_Feature;
686 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */