1 /***************************************************************************/
5 /* FreeType support for .Z compressed files. */
7 /* This optional component relies on NetBSD's zopen(). It should mainly */
8 /* be used to parse compressed PCF fonts, as found with many X11 server */
11 /* Copyright 2004-2006, 2009, 2010, 2012-2014 by */
12 /* Albert Chin-A-Young. */
14 /* Based on code in src/gzip/ftgzip.c, Copyright 2004 by */
15 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
17 /* This file is part of the FreeType project, and may only be used, */
18 /* modified, and distributed under the terms of the FreeType project */
19 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
20 /* this file you indicate that you have read the license and */
21 /* understand and accept it fully. */
23 /***************************************************************************/
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 LZW_Err_
39 #define FT_ERR_BASE FT_Mod_Err_LZW
44 #ifdef FT_CONFIG_OPTION_USE_LZW
46 #ifdef FT_CONFIG_OPTION_PIC
47 #error "lzw code does not support PIC yet"
53 /***************************************************************************/
54 /***************************************************************************/
56 /***** M E M O R Y M A N A G E M E N T *****/
58 /***************************************************************************/
59 /***************************************************************************/
61 /***************************************************************************/
62 /***************************************************************************/
64 /***** F I L E D E S C R I P T O R *****/
66 /***************************************************************************/
67 /***************************************************************************/
69 #define FT_LZW_BUFFER_SIZE 4096
71 typedef struct FT_LZWFileRec_
73 FT_Stream source; /* parent/source stream */
74 FT_Stream stream; /* embedding stream */
75 FT_Memory memory; /* memory allocator */
76 FT_LzwStateRec lzw; /* lzw decompressor state */
78 FT_Byte buffer[FT_LZW_BUFFER_SIZE]; /* output buffer */
79 FT_ULong pos; /* position in output */
83 } FT_LZWFileRec, *FT_LZWFile;
86 /* check and skip .Z header */
88 ft_lzw_check_header( FT_Stream stream )
94 if ( FT_STREAM_SEEK( 0 ) ||
95 FT_STREAM_READ( head, 2 ) )
98 /* head[0] && head[1] are the magic numbers */
99 if ( head[0] != 0x1F ||
101 error = FT_THROW( Invalid_File_Format );
109 ft_lzw_file_init( FT_LZWFile zip,
113 FT_LzwState lzw = &zip->lzw;
117 zip->stream = stream;
118 zip->source = source;
119 zip->memory = stream->memory;
121 zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE;
122 zip->cursor = zip->limit;
125 /* check and skip .Z header */
126 error = ft_lzw_check_header( source );
130 /* initialize internal lzw variable */
131 ft_lzwstate_init( lzw, source );
139 ft_lzw_file_done( FT_LZWFile zip )
142 ft_lzwstate_done( &zip->lzw );
151 ft_lzw_file_reset( FT_LZWFile zip )
153 FT_Stream stream = zip->source;
157 if ( !FT_STREAM_SEEK( 0 ) )
159 ft_lzwstate_reset( &zip->lzw );
161 zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE;
162 zip->cursor = zip->limit;
171 ft_lzw_file_fill_output( FT_LZWFile zip )
173 FT_LzwState lzw = &zip->lzw;
175 FT_Error error = FT_Err_Ok;
178 zip->cursor = zip->buffer;
180 count = ft_lzwstate_io( lzw, zip->buffer, FT_LZW_BUFFER_SIZE );
182 zip->limit = zip->cursor + count;
185 error = FT_THROW( Invalid_Stream_Operation );
191 /* fill output buffer; `count' must be <= FT_LZW_BUFFER_SIZE */
193 ft_lzw_file_skip_output( FT_LZWFile zip,
196 FT_Error error = FT_Err_Ok;
199 /* first, we skip what we can from the output buffer */
201 FT_ULong delta = (FT_ULong)( zip->limit - zip->cursor );
204 if ( delta >= count )
207 zip->cursor += delta;
213 /* next, we skip as many bytes remaining as possible */
216 FT_ULong delta = FT_LZW_BUFFER_SIZE;
223 numread = ft_lzwstate_io( &zip->lzw, NULL, delta );
224 if ( numread < delta )
226 /* not enough bytes */
227 error = FT_THROW( Invalid_Stream_Operation );
240 ft_lzw_file_io( FT_LZWFile zip,
249 /* seeking backwards. */
250 if ( pos < zip->pos )
252 /* If the new position is within the output buffer, simply */
253 /* decrement pointers, otherwise we reset the stream completely! */
254 if ( ( zip->pos - pos ) <= (FT_ULong)( zip->cursor - zip->buffer ) )
256 zip->cursor -= zip->pos - pos;
261 error = ft_lzw_file_reset( zip );
267 /* skip unwanted bytes */
268 if ( pos > zip->pos )
270 error = ft_lzw_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
278 /* now read the data */
284 delta = (FT_ULong)( zip->limit - zip->cursor );
285 if ( delta >= count )
288 FT_MEM_COPY( buffer + result, zip->cursor, delta );
290 zip->cursor += delta;
297 error = ft_lzw_file_fill_output( zip );
307 /***************************************************************************/
308 /***************************************************************************/
310 /***** L Z W E M B E D D I N G S T R E A M *****/
312 /***************************************************************************/
313 /***************************************************************************/
316 ft_lzw_stream_close( FT_Stream stream )
318 FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
319 FT_Memory memory = stream->memory;
324 /* finalize lzw file descriptor */
325 ft_lzw_file_done( zip );
329 stream->descriptor.pointer = NULL;
335 ft_lzw_stream_io( FT_Stream stream,
340 FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
343 return ft_lzw_file_io( zip, pos, buffer, count );
347 FT_EXPORT_DEF( FT_Error )
348 FT_Stream_OpenLZW( FT_Stream stream,
353 FT_LZWFile zip = NULL;
356 if ( !stream || !source )
358 error = FT_THROW( Invalid_Stream_Handle );
362 memory = source->memory;
365 * Check the header right now; this prevents allocation of a huge
366 * LZWFile object (400 KByte of heap memory) if not necessary.
368 * Did I mention that you should never use .Z compressed font
371 error = ft_lzw_check_header( source );
376 stream->memory = memory;
378 if ( !FT_NEW( zip ) )
380 error = ft_lzw_file_init( zip, stream, source );
387 stream->descriptor.pointer = zip;
390 stream->size = 0x7FFFFFFFL; /* don't know the real size! */
393 stream->read = ft_lzw_stream_io;
394 stream->close = ft_lzw_stream_close;
404 #else /* !FT_CONFIG_OPTION_USE_LZW */
407 FT_EXPORT_DEF( FT_Error )
408 FT_Stream_OpenLZW( FT_Stream stream,
414 return FT_THROW( Unimplemented_Feature );
418 #endif /* !FT_CONFIG_OPTION_USE_LZW */