1 /****************************************************************************
5 * FreeType support for .bz2 compressed files.
7 * This optional component relies on libbz2. It should mainly be used to
8 * parse compressed PCF fonts, as found with many X11 server
11 * Copyright (C) 2010-2019 by
14 * based on `src/gzip/ftgzip.c'
16 * This file is part of the FreeType project, and may only be used,
17 * modified, and distributed under the terms of the FreeType project
18 * license, LICENSE.TXT. By continuing to use, modify, or distribute
19 * this file you indicate that you have read the license and
20 * understand and accept it fully.
26 #include FT_INTERNAL_MEMORY_H
27 #include FT_INTERNAL_STREAM_H
28 #include FT_INTERNAL_DEBUG_H
30 #include FT_CONFIG_STANDARD_LIBRARY_H
33 #include FT_MODULE_ERRORS_H
38 #define FT_ERR_PREFIX Bzip2_Err_
39 #define FT_ERR_BASE FT_Mod_Err_Bzip2
44 #ifdef FT_CONFIG_OPTION_USE_BZIP2
46 #define BZ_NO_STDIO /* Do not need FILE */
50 /***************************************************************************/
51 /***************************************************************************/
53 /***** B Z I P 2 M E M O R Y M A N A G E M E N T *****/
55 /***************************************************************************/
56 /***************************************************************************/
58 /* it is better to use FreeType memory routines instead of raw
61 typedef void *(* alloc_func)(void*, int, int);
62 typedef void (* free_func)(void*, void*);
65 ft_bzip2_alloc( FT_Memory memory,
69 FT_ULong sz = (FT_ULong)size * (FT_ULong)items;
74 (void)FT_ALLOC( p, sz );
80 ft_bzip2_free( FT_Memory memory,
83 FT_MEM_FREE( address );
87 /***************************************************************************/
88 /***************************************************************************/
90 /***** B Z I P 2 F I L E D E S C R I P T O R *****/
92 /***************************************************************************/
93 /***************************************************************************/
95 #define FT_BZIP2_BUFFER_SIZE 4096
97 typedef struct FT_BZip2FileRec_
99 FT_Stream source; /* parent/source stream */
100 FT_Stream stream; /* embedding stream */
101 FT_Memory memory; /* memory allocator */
102 bz_stream bzstream; /* bzlib input stream */
104 FT_Byte input[FT_BZIP2_BUFFER_SIZE]; /* input read buffer */
106 FT_Byte buffer[FT_BZIP2_BUFFER_SIZE]; /* output buffer */
107 FT_ULong pos; /* position in output */
111 } FT_BZip2FileRec, *FT_BZip2File;
114 /* check and skip .bz2 header - we don't support `transparent' compression */
116 ft_bzip2_check_header( FT_Stream stream )
118 FT_Error error = FT_Err_Ok;
122 if ( FT_STREAM_SEEK( 0 ) ||
123 FT_STREAM_READ( head, 4 ) )
126 /* head[0] && head[1] are the magic numbers; */
127 /* head[2] is the version, and head[3] the blocksize */
128 if ( head[0] != 0x42 ||
130 head[2] != 0x68 ) /* only support bzip2 (huffman) */
132 error = FT_THROW( Invalid_File_Format );
142 ft_bzip2_file_init( FT_BZip2File zip,
146 bz_stream* bzstream = &zip->bzstream;
147 FT_Error error = FT_Err_Ok;
150 zip->stream = stream;
151 zip->source = source;
152 zip->memory = stream->memory;
154 zip->limit = zip->buffer + FT_BZIP2_BUFFER_SIZE;
155 zip->cursor = zip->limit;
158 /* check .bz2 header */
162 error = ft_bzip2_check_header( stream );
166 if ( FT_STREAM_SEEK( 0 ) )
170 /* initialize bzlib */
171 bzstream->bzalloc = (alloc_func)ft_bzip2_alloc;
172 bzstream->bzfree = (free_func) ft_bzip2_free;
173 bzstream->opaque = zip->memory;
175 bzstream->avail_in = 0;
176 bzstream->next_in = (char*)zip->buffer;
178 if ( BZ2_bzDecompressInit( bzstream, 0, 0 ) != BZ_OK ||
180 error = FT_THROW( Invalid_File_Format );
188 ft_bzip2_file_done( FT_BZip2File zip )
190 bz_stream* bzstream = &zip->bzstream;
193 BZ2_bzDecompressEnd( bzstream );
196 bzstream->bzalloc = NULL;
197 bzstream->bzfree = NULL;
198 bzstream->opaque = NULL;
199 bzstream->next_in = NULL;
200 bzstream->next_out = NULL;
201 bzstream->avail_in = 0;
202 bzstream->avail_out = 0;
211 ft_bzip2_file_reset( FT_BZip2File zip )
213 FT_Stream stream = zip->source;
217 if ( !FT_STREAM_SEEK( 0 ) )
219 bz_stream* bzstream = &zip->bzstream;
222 BZ2_bzDecompressEnd( bzstream );
224 bzstream->avail_in = 0;
225 bzstream->next_in = (char*)zip->input;
226 bzstream->avail_out = 0;
227 bzstream->next_out = (char*)zip->buffer;
229 zip->limit = zip->buffer + FT_BZIP2_BUFFER_SIZE;
230 zip->cursor = zip->limit;
233 BZ2_bzDecompressInit( bzstream, 0, 0 );
241 ft_bzip2_file_fill_input( FT_BZip2File zip )
243 bz_stream* bzstream = &zip->bzstream;
244 FT_Stream stream = zip->source;
250 size = stream->read( stream, stream->pos, zip->input,
251 FT_BZIP2_BUFFER_SIZE );
254 zip->limit = zip->cursor;
255 return FT_THROW( Invalid_Stream_Operation );
260 size = stream->size - stream->pos;
261 if ( size > FT_BZIP2_BUFFER_SIZE )
262 size = FT_BZIP2_BUFFER_SIZE;
266 zip->limit = zip->cursor;
267 return FT_THROW( Invalid_Stream_Operation );
270 FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
274 bzstream->next_in = (char*)zip->input;
275 bzstream->avail_in = size;
282 ft_bzip2_file_fill_output( FT_BZip2File zip )
284 bz_stream* bzstream = &zip->bzstream;
285 FT_Error error = FT_Err_Ok;
288 zip->cursor = zip->buffer;
289 bzstream->next_out = (char*)zip->cursor;
290 bzstream->avail_out = FT_BZIP2_BUFFER_SIZE;
292 while ( bzstream->avail_out > 0 )
297 if ( bzstream->avail_in == 0 )
299 error = ft_bzip2_file_fill_input( zip );
304 err = BZ2_bzDecompress( bzstream );
306 if ( err == BZ_STREAM_END )
308 zip->limit = (FT_Byte*)bzstream->next_out;
309 if ( zip->limit == zip->cursor )
310 error = FT_THROW( Invalid_Stream_Operation );
313 else if ( err != BZ_OK )
315 zip->limit = zip->cursor;
316 error = FT_THROW( Invalid_Stream_Operation );
325 /* fill output buffer; `count' must be <= FT_BZIP2_BUFFER_SIZE */
327 ft_bzip2_file_skip_output( FT_BZip2File zip,
330 FT_Error error = FT_Err_Ok;
336 delta = (FT_ULong)( zip->limit - zip->cursor );
337 if ( delta >= count )
340 zip->cursor += delta;
347 error = ft_bzip2_file_fill_output( zip );
357 ft_bzip2_file_io( FT_BZip2File zip,
366 /* Reset inflate stream if we're seeking backwards. */
367 /* Yes, that is not too efficient, but it saves memory :-) */
368 if ( pos < zip->pos )
370 error = ft_bzip2_file_reset( zip );
375 /* skip unwanted bytes */
376 if ( pos > zip->pos )
378 error = ft_bzip2_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
386 /* now read the data */
392 delta = (FT_ULong)( zip->limit - zip->cursor );
393 if ( delta >= count )
396 FT_MEM_COPY( buffer, zip->cursor, delta );
399 zip->cursor += delta;
406 error = ft_bzip2_file_fill_output( zip );
416 /***************************************************************************/
417 /***************************************************************************/
419 /***** B Z E M B E D D I N G S T R E A M *****/
421 /***************************************************************************/
422 /***************************************************************************/
425 ft_bzip2_stream_close( FT_Stream stream )
427 FT_BZip2File zip = (FT_BZip2File)stream->descriptor.pointer;
428 FT_Memory memory = stream->memory;
433 /* finalize bzip file descriptor */
434 ft_bzip2_file_done( zip );
438 stream->descriptor.pointer = NULL;
444 ft_bzip2_stream_io( FT_Stream stream,
445 unsigned long offset,
446 unsigned char* buffer,
447 unsigned long count )
449 FT_BZip2File zip = (FT_BZip2File)stream->descriptor.pointer;
452 return ft_bzip2_file_io( zip, offset, buffer, count );
456 FT_EXPORT_DEF( FT_Error )
457 FT_Stream_OpenBzip2( FT_Stream stream,
462 FT_BZip2File zip = NULL;
465 if ( !stream || !source )
467 error = FT_THROW( Invalid_Stream_Handle );
471 memory = source->memory;
474 * check the header right now; this prevents allocating unnecessary
475 * objects when we don't need them
477 error = ft_bzip2_check_header( source );
482 stream->memory = memory;
484 if ( !FT_QNEW( zip ) )
486 error = ft_bzip2_file_init( zip, stream, source );
493 stream->descriptor.pointer = zip;
496 stream->size = 0x7FFFFFFFL; /* don't know the real size! */
499 stream->read = ft_bzip2_stream_io;
500 stream->close = ft_bzip2_stream_close;
506 #else /* !FT_CONFIG_OPTION_USE_BZIP2 */
508 FT_EXPORT_DEF( FT_Error )
509 FT_Stream_OpenBzip2( FT_Stream stream,
515 return FT_THROW( Unimplemented_Feature );
518 #endif /* !FT_CONFIG_OPTION_USE_BZIP2 */