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 (C) 2002-2020 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.
23 #include <freetype/internal/ftmemory.h>
24 #include <freetype/internal/ftstream.h>
25 #include <freetype/internal/ftdebug.h>
26 #include <freetype/ftgzip.h>
27 #include FT_CONFIG_STANDARD_LIBRARY_H
30 #include <freetype/ftmoderr.h>
35 #define FT_ERR_PREFIX Gzip_Err_
36 #define FT_ERR_BASE FT_Mod_Err_Gzip
38 #include <freetype/fterrors.h>
41 #ifdef FT_CONFIG_OPTION_USE_ZLIB
43 #ifdef FT_CONFIG_OPTION_SYSTEM_ZLIB
47 #else /* !FT_CONFIG_OPTION_SYSTEM_ZLIB */
49 /* In this case, we include our own modified sources of the ZLib */
50 /* within the `gzip' component. The modifications were necessary */
51 /* to #include all files without conflicts, as well as preventing */
52 /* the definition of `extern' functions that may cause linking */
53 /* conflicts when a program is linked with both FreeType and the */
56 #ifndef USE_ZLIB_ZCALLOC
57 #define MY_ZCALLOC /* prevent all zcalloc() & zfree() in zutil.c */
60 /* Note that our `zlib.h' includes `ftzconf.h' instead of `zconf.h'; */
61 /* the main reason is that even a global `zlib.h' includes `zconf.h' */
64 /* #include "zconf.h" */
66 /* instead of the expected */
68 /* #include <zconf.h> */
70 /* so that configuration with `FT_CONFIG_OPTION_SYSTEM_ZLIB' might */
71 /* include the wrong `zconf.h' file, leading to errors. */
75 #define SLOW 1 /* we can't use asm-optimized sources here! */
77 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */
78 /* We disable the warning `conversion from XXX to YYY, */
79 /* possible loss of data' in order to compile cleanly with */
80 /* the maximum level of warnings: zlib is non-FreeType */
82 #pragma warning( push )
83 #pragma warning( disable : 4244 )
86 /* Urgh. `inflate_mask' must not be declared twice -- C++ doesn't like
87 this. We temporarily disable it and load all necessary header files. */
88 #define NO_INFLATE_MASK
94 #undef NO_INFLATE_MASK
96 /* infutil.c must be included before infcodes.c */
100 #include "infcodes.c"
101 #include "infblock.c"
105 #if defined( _MSC_VER )
106 #pragma warning( pop )
109 #endif /* !FT_CONFIG_OPTION_SYSTEM_ZLIB */
112 /***************************************************************************/
113 /***************************************************************************/
115 /***** Z L I B M E M O R Y M A N A G E M E N T *****/
117 /***************************************************************************/
118 /***************************************************************************/
120 /* it is better to use FreeType memory routines instead of raw
124 ft_gzip_alloc( FT_Memory memory,
128 FT_ULong sz = (FT_ULong)size * items;
133 (void)FT_ALLOC( p, sz );
139 ft_gzip_free( FT_Memory memory,
142 FT_MEM_FREE( address );
146 #if !defined( FT_CONFIG_OPTION_SYSTEM_ZLIB ) && !defined( USE_ZLIB_ZCALLOC )
149 zcalloc ( voidpf opaque,
153 return ft_gzip_alloc( (FT_Memory)opaque, items, size );
157 zcfree( voidpf opaque,
160 ft_gzip_free( (FT_Memory)opaque, ptr );
163 #endif /* !SYSTEM_ZLIB && !USE_ZLIB_ZCALLOC */
166 /***************************************************************************/
167 /***************************************************************************/
169 /***** Z L I B F I L E D E S C R I P T O R *****/
171 /***************************************************************************/
172 /***************************************************************************/
174 #define FT_GZIP_BUFFER_SIZE 4096
176 typedef struct FT_GZipFileRec_
178 FT_Stream source; /* parent/source stream */
179 FT_Stream stream; /* embedding stream */
180 FT_Memory memory; /* memory allocator */
181 z_stream zstream; /* zlib input stream */
183 FT_ULong start; /* starting position, after .gz header */
184 FT_Byte input[FT_GZIP_BUFFER_SIZE]; /* input read buffer */
186 FT_Byte buffer[FT_GZIP_BUFFER_SIZE]; /* output buffer */
187 FT_ULong pos; /* position in output */
191 } FT_GZipFileRec, *FT_GZipFile;
195 #define FT_GZIP_ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
196 #define FT_GZIP_HEAD_CRC 0x02 /* bit 1 set: header CRC present */
197 #define FT_GZIP_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
198 #define FT_GZIP_ORIG_NAME 0x08 /* bit 3 set: original file name present */
199 #define FT_GZIP_COMMENT 0x10 /* bit 4 set: file comment present */
200 #define FT_GZIP_RESERVED 0xE0 /* bits 5..7: reserved */
203 /* check and skip .gz header - we don't support `transparent' compression */
205 ft_gzip_check_header( FT_Stream stream )
211 if ( FT_STREAM_SEEK( 0 ) ||
212 FT_STREAM_READ( head, 4 ) )
215 /* head[0] && head[1] are the magic numbers; */
216 /* head[2] is the method, and head[3] the flags */
217 if ( head[0] != 0x1F ||
219 head[2] != Z_DEFLATED ||
220 (head[3] & FT_GZIP_RESERVED) )
222 error = FT_THROW( Invalid_File_Format );
226 /* skip time, xflags and os code */
227 (void)FT_STREAM_SKIP( 6 );
229 /* skip the extra field */
230 if ( head[3] & FT_GZIP_EXTRA_FIELD )
235 if ( FT_READ_USHORT_LE( len ) ||
236 FT_STREAM_SKIP( len ) )
240 /* skip original file name */
241 if ( head[3] & FT_GZIP_ORIG_NAME )
247 if ( FT_READ_BYTE( c ) )
254 /* skip .gz comment */
255 if ( head[3] & FT_GZIP_COMMENT )
261 if ( FT_READ_BYTE( c ) )
269 if ( head[3] & FT_GZIP_HEAD_CRC )
270 if ( FT_STREAM_SKIP( 2 ) )
279 ft_gzip_file_init( FT_GZipFile zip,
283 z_stream* zstream = &zip->zstream;
284 FT_Error error = FT_Err_Ok;
287 zip->stream = stream;
288 zip->source = source;
289 zip->memory = stream->memory;
291 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE;
292 zip->cursor = zip->limit;
295 /* check and skip .gz header */
299 error = ft_gzip_check_header( stream );
303 zip->start = FT_STREAM_POS();
306 /* initialize zlib -- there is no zlib header in the compressed stream */
307 zstream->zalloc = (alloc_func)ft_gzip_alloc;
308 zstream->zfree = (free_func) ft_gzip_free;
309 zstream->opaque = stream->memory;
311 zstream->avail_in = 0;
312 zstream->next_in = zip->buffer;
314 if ( inflateInit2( zstream, -MAX_WBITS ) != Z_OK ||
316 error = FT_THROW( Invalid_File_Format );
324 ft_gzip_file_done( FT_GZipFile zip )
326 z_stream* zstream = &zip->zstream;
329 inflateEnd( zstream );
332 zstream->zalloc = NULL;
333 zstream->zfree = NULL;
334 zstream->opaque = NULL;
335 zstream->next_in = NULL;
336 zstream->next_out = NULL;
337 zstream->avail_in = 0;
338 zstream->avail_out = 0;
347 ft_gzip_file_reset( FT_GZipFile zip )
349 FT_Stream stream = zip->source;
353 if ( !FT_STREAM_SEEK( zip->start ) )
355 z_stream* zstream = &zip->zstream;
358 inflateReset( zstream );
360 zstream->avail_in = 0;
361 zstream->next_in = zip->input;
362 zstream->avail_out = 0;
363 zstream->next_out = zip->buffer;
365 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE;
366 zip->cursor = zip->limit;
375 ft_gzip_file_fill_input( FT_GZipFile zip )
377 z_stream* zstream = &zip->zstream;
378 FT_Stream stream = zip->source;
384 size = stream->read( stream, stream->pos, zip->input,
385 FT_GZIP_BUFFER_SIZE );
388 zip->limit = zip->cursor;
389 return FT_THROW( Invalid_Stream_Operation );
394 size = stream->size - stream->pos;
395 if ( size > FT_GZIP_BUFFER_SIZE )
396 size = FT_GZIP_BUFFER_SIZE;
400 zip->limit = zip->cursor;
401 return FT_THROW( Invalid_Stream_Operation );
404 FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
408 zstream->next_in = zip->input;
409 zstream->avail_in = size;
416 ft_gzip_file_fill_output( FT_GZipFile zip )
418 z_stream* zstream = &zip->zstream;
419 FT_Error error = FT_Err_Ok;
422 zip->cursor = zip->buffer;
423 zstream->next_out = zip->cursor;
424 zstream->avail_out = FT_GZIP_BUFFER_SIZE;
426 while ( zstream->avail_out > 0 )
431 if ( zstream->avail_in == 0 )
433 error = ft_gzip_file_fill_input( zip );
438 err = inflate( zstream, Z_NO_FLUSH );
440 if ( err == Z_STREAM_END )
442 zip->limit = zstream->next_out;
443 if ( zip->limit == zip->cursor )
444 error = FT_THROW( Invalid_Stream_Operation );
447 else if ( err != Z_OK )
449 zip->limit = zip->cursor;
450 error = FT_THROW( Invalid_Stream_Operation );
459 /* fill output buffer; `count' must be <= FT_GZIP_BUFFER_SIZE */
461 ft_gzip_file_skip_output( FT_GZipFile zip,
464 FT_Error error = FT_Err_Ok;
470 delta = (FT_ULong)( zip->limit - zip->cursor );
471 if ( delta >= count )
474 zip->cursor += delta;
481 error = ft_gzip_file_fill_output( zip );
491 ft_gzip_file_io( FT_GZipFile zip,
500 /* Reset inflate stream if we're seeking backwards. */
501 /* Yes, that is not too efficient, but it saves memory :-) */
502 if ( pos < zip->pos )
504 error = ft_gzip_file_reset( zip );
509 /* skip unwanted bytes */
510 if ( pos > zip->pos )
512 error = ft_gzip_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
520 /* now read the data */
526 delta = (FT_ULong)( zip->limit - zip->cursor );
527 if ( delta >= count )
530 FT_MEM_COPY( buffer, zip->cursor, delta );
533 zip->cursor += delta;
540 error = ft_gzip_file_fill_output( zip );
550 /***************************************************************************/
551 /***************************************************************************/
553 /***** G Z E M B E D D I N G S T R E A M *****/
555 /***************************************************************************/
556 /***************************************************************************/
559 ft_gzip_stream_close( FT_Stream stream )
561 FT_GZipFile zip = (FT_GZipFile)stream->descriptor.pointer;
562 FT_Memory memory = stream->memory;
567 /* finalize gzip file descriptor */
568 ft_gzip_file_done( zip );
572 stream->descriptor.pointer = NULL;
576 FT_FREE( stream->base );
581 ft_gzip_stream_io( FT_Stream stream,
582 unsigned long offset,
583 unsigned char* buffer,
584 unsigned long count )
586 FT_GZipFile zip = (FT_GZipFile)stream->descriptor.pointer;
589 return ft_gzip_file_io( zip, offset, buffer, count );
594 ft_gzip_get_uncompressed_size( FT_Stream stream )
601 old_pos = stream->pos;
602 if ( !FT_Stream_Seek( stream, stream->size - 4 ) )
604 result = FT_Stream_ReadULongLE( stream, &error );
608 (void)FT_Stream_Seek( stream, old_pos );
615 /* documentation is in ftgzip.h */
617 FT_EXPORT_DEF( FT_Error )
618 FT_Stream_OpenGzip( FT_Stream stream,
623 FT_GZipFile zip = NULL;
626 if ( !stream || !source )
628 error = FT_THROW( Invalid_Stream_Handle );
632 memory = source->memory;
635 * check the header right now; this prevents allocating un-necessary
636 * objects when we don't need them
638 error = ft_gzip_check_header( source );
643 stream->memory = memory;
645 if ( !FT_QNEW( zip ) )
647 error = ft_gzip_file_init( zip, stream, source );
654 stream->descriptor.pointer = zip;
658 * We use the following trick to try to dramatically improve the
659 * performance while dealing with small files. If the original stream
660 * size is less than a certain threshold, we try to load the whole font
661 * file into memory. This saves us from using the 32KB buffer needed
662 * to inflate the file, plus the two 4KB intermediate input/output
663 * buffers used in the `FT_GZipFile' structure.
666 FT_ULong zip_size = ft_gzip_get_uncompressed_size( source );
669 if ( zip_size != 0 && zip_size < 40 * 1024 )
671 FT_Byte* zip_buff = NULL;
674 if ( !FT_ALLOC( zip_buff, zip_size ) )
679 count = ft_gzip_file_io( zip, 0, zip_buff, zip_size );
680 if ( count == zip_size )
682 ft_gzip_file_done( zip );
685 stream->descriptor.pointer = NULL;
687 stream->size = zip_size;
689 stream->base = zip_buff;
691 stream->close = ft_gzip_stream_close;
696 ft_gzip_file_io( zip, 0, NULL, 0 );
703 stream->size = zip_size;
705 stream->size = 0x7FFFFFFFL; /* don't know the real size! */
710 stream->read = ft_gzip_stream_io;
711 stream->close = ft_gzip_stream_close;
718 /* documentation is in ftgzip.h */
720 FT_EXPORT_DEF( FT_Error )
721 FT_Gzip_Uncompress( FT_Memory memory,
723 FT_ULong* output_len,
724 const FT_Byte* input,
731 /* check for `input' delayed to `inflate' */
733 if ( !memory || !output_len || !output )
734 return FT_THROW( Invalid_Argument );
736 /* this function is modeled after zlib's `uncompress' function */
738 stream.next_in = (Bytef*)input;
739 stream.avail_in = (uInt)input_len;
741 stream.next_out = output;
742 stream.avail_out = (uInt)*output_len;
744 stream.zalloc = (alloc_func)ft_gzip_alloc;
745 stream.zfree = (free_func) ft_gzip_free;
746 stream.opaque = memory;
748 /* This is a temporary fix and will be removed once the internal
749 * copy of zlib is updated to the newest version. The `|32' flag
750 * is only supported in the new versions of zlib to enable gzip
753 #ifdef FT_CONFIG_OPTION_SYSTEM_ZLIB
754 err = inflateInit2( &stream, MAX_WBITS|32 );
756 err = inflateInit2( &stream, MAX_WBITS );
760 return FT_THROW( Invalid_Argument );
762 err = inflate( &stream, Z_FINISH );
763 if ( err != Z_STREAM_END )
765 inflateEnd( &stream );
771 *output_len = stream.total_out;
773 err = inflateEnd( &stream );
776 if ( err == Z_MEM_ERROR )
777 return FT_THROW( Out_Of_Memory );
779 if ( err == Z_BUF_ERROR )
780 return FT_THROW( Array_Too_Large );
782 if ( err == Z_DATA_ERROR )
783 return FT_THROW( Invalid_Table );
789 #else /* !FT_CONFIG_OPTION_USE_ZLIB */
791 FT_EXPORT_DEF( FT_Error )
792 FT_Stream_OpenGzip( FT_Stream stream,
798 return FT_THROW( Unimplemented_Feature );
802 FT_EXPORT_DEF( FT_Error )
803 FT_Gzip_Uncompress( FT_Memory memory,
805 FT_ULong* output_len,
806 const FT_Byte* input,
811 FT_UNUSED( output_len );
813 FT_UNUSED( input_len );
815 return FT_THROW( Unimplemented_Feature );
818 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */