1 /***************************************************************************/
5 /* FreeType support for .gz compressed fileds */
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 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 /***************************************************************************/
23 #include FT_INTERNAL_MEMORY_H
24 #include FT_INTERNAL_STREAM_H
25 #include FT_INTERNAL_DEBUG_H
28 #ifdef FT_CONFIG_OPTION_USE_ZLIB
30 #ifdef FT_CONFIG_OPTION_SYSTEM_ZLIB
34 #else /* !SYSTEM_ZLIB */
36 /* in this case, we include our own modified sources of the ZLib */
37 /* within the "ftgzip" component. The modifications were necessary */
38 /* to #include all files without conflicts, as well as preventing */
39 /* the definition of "extern" functions that may cause linking */
40 /* conflicts when a program is linked with both FreeType and the */
43 # define NO_DUMMY_DECL
44 # define BUILDFIXED /* save code size */
50 # define SLOW 1 /* we can't use asm-optimized sources here !! */
53 # include "inftrees.c"
54 # include "infcodes.c"
56 # include "infblock.c"
60 #endif /* !SYSTEM_ZLIB */
63 /***************************************************************************/
64 /***************************************************************************/
66 /***** Z L I B M E M O R Y M A N A G E M E N T *****/
68 /***************************************************************************/
69 /***************************************************************************/
71 /* it's better to use FreeType memory routines instead of raw 'malloc/free' */
75 ft_gzip_alloc( FT_Memory memory,
79 FT_ULong sz = (FT_ULong)size * items;
82 FT_MEM_ALLOC( p, sz );
89 ft_gzip_free( FT_Memory memory,
92 FT_MEM_FREE( address );
96 #ifndef FT_CONFIG_OPTION_SYSTEM_ZLIB
99 zcalloc ( /* opaque, items, size) */
104 return ft_gzip_alloc( opaque, items, size );
108 zcfree( voidpf opaque,
111 ft_gzip_free( opaque, ptr );
114 #endif /* !SYSTEM_ZLIB */
117 /***************************************************************************/
118 /***************************************************************************/
120 /***** Z L I B F I L E D E S C R I P T O R *****/
122 /***************************************************************************/
123 /***************************************************************************/
125 #define FT_GZIP_BUFFER_SIZE 4096
127 typedef struct FT_GZipFileRec_
129 FT_Stream source; /* parent/source stream */
130 FT_Stream stream; /* embedding stream */
131 FT_Memory memory; /* memory allocator */
132 z_stream zstream; /* zlib input stream */
134 FT_ULong start; /* starting position, after .gz header */
135 FT_Byte input[ FT_GZIP_BUFFER_SIZE ]; /* input read buffer */
137 FT_Byte buffer[ FT_GZIP_BUFFER_SIZE ]; /* output buffer */
138 FT_ULong pos; /* position in output */
142 } FT_GZipFileRec, *FT_GZipFile;
146 #define FT_GZIP_ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
147 #define FT_GZIP_HEAD_CRC 0x02 /* bit 1 set: header CRC present */
148 #define FT_GZIP_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
149 #define FT_GZIP_ORIG_NAME 0x08 /* bit 3 set: original file name present */
150 #define FT_GZIP_COMMENT 0x10 /* bit 4 set: file comment present */
151 #define FT_GZIP_RESERVED 0xE0 /* bits 5..7: reserved */
154 /* check and skip .gz header - we don't support "transparent" compression */
156 ft_gzip_check_header( FT_Stream stream )
161 if ( FT_STREAM_SEEK( 0 ) ||
162 FT_STREAM_READ( head, 4 ) )
165 /* head[0] && head[1] are the magic numbers */
166 /* head[2] is the method, and head[3] the flags */
167 if ( head[0] != 0x1f ||
169 head[2] != Z_DEFLATED ||
170 (head[3] & FT_GZIP_RESERVED) )
172 error = FT_Err_Invalid_File_Format;
176 /* skip time, xflags and os code */
177 (void)FT_STREAM_SKIP( 6 );
179 /* skip the extra field */
180 if ( head[3] & FT_GZIP_EXTRA_FIELD )
184 if ( FT_READ_USHORT_LE( len ) ||
185 FT_STREAM_SKIP( len ) )
189 /* skip original file name */
190 if ( head[3] & FT_GZIP_ORIG_NAME )
195 if ( FT_READ_BYTE( c) )
202 /* skip .gz comment */
203 if ( head[3] & FT_GZIP_COMMENT )
208 if ( FT_READ_BYTE( c) )
216 if ( head[3] & FT_GZIP_HEAD_CRC )
217 if ( FT_STREAM_SKIP( 2 ) )
227 ft_gzip_file_init( FT_GZipFile zip,
231 z_stream* zstream = &zip->zstream;
234 zip->stream = stream;
235 zip->source = source;
236 zip->memory = stream->memory;
238 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE;
239 zip->cursor = zip->limit;
242 /* check and skip .gz header */
246 error = ft_gzip_check_header( stream );
250 zip->start = FT_STREAM_POS();
253 /* initialize zlib - there is no zlib header in the compressed stream */
254 zstream->zalloc = (alloc_func) ft_gzip_alloc;
255 zstream->zfree = (free_func) ft_gzip_free;
256 zstream->opaque = stream->memory;
258 zstream->avail_in = 0;
259 zstream->next_in = zip->buffer;
261 if ( inflateInit2( zstream, -MAX_WBITS ) != Z_OK ||
262 zstream->next_in == NULL )
264 error = FT_Err_Invalid_File_Format;
275 ft_gzip_file_done( FT_GZipFile zip )
277 z_stream* zstream = &zip->zstream;
279 inflateEnd( zstream );
282 zstream->zalloc = NULL;
283 zstream->zfree = NULL;
284 zstream->opaque = NULL;
285 zstream->next_in = NULL;
286 zstream->next_out = NULL;
287 zstream->avail_in = 0;
288 zstream->avail_out = 0;
297 ft_gzip_file_reset( FT_GZipFile zip )
299 FT_Stream stream = zip->source;
302 if ( !FT_STREAM_SEEK( zip->start ) )
304 z_stream* zstream = &zip->zstream;
306 inflateReset( zstream );
308 zstream->avail_in = 0;
309 zstream->next_in = zip->input;
310 zstream->avail_out = 0;
311 zstream->next_out = zip->buffer;
313 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE;
314 zip->cursor = zip->limit;
322 ft_gzip_file_fill_input( FT_GZipFile zip )
324 z_stream* zstream = &zip->zstream;
325 FT_Stream stream = zip->source;
330 size = stream->read( stream, stream->pos, zip->input, FT_GZIP_BUFFER_SIZE );
332 return FT_Err_Invalid_Stream_Operation;
336 size = stream->size - stream->pos;
337 if ( size > FT_GZIP_BUFFER_SIZE )
338 size = FT_GZIP_BUFFER_SIZE;
341 return FT_Err_Invalid_Stream_Operation;
343 FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
347 zstream->next_in = zip->input;
348 zstream->avail_in = size;
356 ft_gzip_file_fill_output( FT_GZipFile zip )
358 z_stream* zstream = &zip->zstream;
361 zip->cursor = zip->buffer;
362 zstream->next_out = zip->cursor;
363 zstream->avail_out = FT_GZIP_BUFFER_SIZE;
365 while ( zstream->avail_out > 0 )
369 if ( zstream->avail_in == 0 )
371 error = ft_gzip_file_fill_input( zip );
376 err = inflate( zstream, Z_NO_FLUSH );
378 if ( err == Z_STREAM_END )
380 zip->limit = zstream->next_out;
381 error = FT_Err_Invalid_Stream_Operation;
384 else if ( err != Z_OK )
386 error = FT_Err_Invalid_Stream_Operation;
394 /* fill output buffer, 'count' must be <= FT_GZIP_BUFFER_SIZE */
396 ft_gzip_file_skip_output( FT_GZipFile zip,
404 delta = (FT_ULong)( zip->limit - zip->cursor );
405 if ( delta >= count )
408 zip->cursor += delta;
415 error = ft_gzip_file_fill_output( zip );
425 ft_gzip_file_io( FT_GZipFile zip,
433 /* reset inflate stream if we're seeking backwards */
434 /* yes, that's not too efficient, but it saves memory :-) */
435 if ( pos < zip->pos )
437 error = ft_gzip_file_reset( zip );
438 if ( error ) goto Exit;
441 /* skip unwanted bytes */
442 if ( pos > zip->pos )
444 error = ft_gzip_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
452 /* now read the data */
457 delta = (FT_ULong)( zip->limit - zip->cursor );
458 if ( delta >= count )
461 FT_MEM_COPY( buffer, zip->cursor, delta );
464 zip->cursor += delta;
471 error = ft_gzip_file_fill_output( zip );
481 /***************************************************************************/
482 /***************************************************************************/
484 /***** G Z E M B E D D I N G S T R E A M *****/
486 /***************************************************************************/
487 /***************************************************************************/
490 ft_gzip_stream_close( FT_Stream stream )
492 FT_GZipFile zip = stream->descriptor.pointer;
493 FT_Memory memory = stream->memory;
497 /* finalize gzip file descriptor */
498 ft_gzip_file_done( zip );
502 stream->descriptor.pointer = NULL;
508 ft_gzip_stream_io( FT_Stream stream,
513 FT_GZipFile zip = stream->descriptor.pointer;
515 return ft_gzip_file_io( zip, pos, buffer, count );
519 FT_EXPORT_DEF( FT_Error )
520 FT_Stream_OpenGzip( FT_Stream stream,
524 FT_Memory memory = source->memory;
528 stream->memory = memory;
530 if ( !FT_NEW( zip ) )
532 error = ft_gzip_file_init( zip, stream, source );
539 stream->descriptor.pointer = zip;
542 stream->size = 0x7FFFFFFF; /* don't know the real size !! */
545 stream->read = ft_gzip_stream_io;
546 stream->close = ft_gzip_stream_close;
552 #else /* !FT_CONFIG_OPTION_USE_ZLIB */
554 FT_EXPORT_DEF( FT_Error )
555 FT_Stream_OpenGzip( FT_Stream stream,
561 return FT_Err_Unimplemented_Feature;
564 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */