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 (C) 2004-2020 by
12 * Albert Chin-A-Young.
14 * based on code in `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.
24 #include <freetype/internal/ftmemory.h>
25 #include <freetype/internal/ftstream.h>
26 #include <freetype/internal/ftdebug.h>
27 #include <freetype/ftlzw.h>
28 #include FT_CONFIG_STANDARD_LIBRARY_H
31 #include <freetype/ftmoderr.h>
36 #define FT_ERR_PREFIX LZW_Err_
37 #define FT_ERR_BASE FT_Mod_Err_LZW
39 #include <freetype/fterrors.h>
42 #ifdef FT_CONFIG_OPTION_USE_LZW
47 /***************************************************************************/
48 /***************************************************************************/
50 /***** M E M O R Y M A N A G E M E N T *****/
52 /***************************************************************************/
53 /***************************************************************************/
55 /***************************************************************************/
56 /***************************************************************************/
58 /***** F I L E D E S C R I P T O R *****/
60 /***************************************************************************/
61 /***************************************************************************/
63 #define FT_LZW_BUFFER_SIZE 4096
65 typedef struct FT_LZWFileRec_
67 FT_Stream source; /* parent/source stream */
68 FT_Stream stream; /* embedding stream */
69 FT_Memory memory; /* memory allocator */
70 FT_LzwStateRec lzw; /* lzw decompressor state */
72 FT_Byte buffer[FT_LZW_BUFFER_SIZE]; /* output buffer */
73 FT_ULong pos; /* position in output */
77 } FT_LZWFileRec, *FT_LZWFile;
80 /* check and skip .Z header */
82 ft_lzw_check_header( FT_Stream stream )
88 if ( FT_STREAM_SEEK( 0 ) ||
89 FT_STREAM_READ( head, 2 ) )
92 /* head[0] && head[1] are the magic numbers */
93 if ( head[0] != 0x1F ||
95 error = FT_THROW( Invalid_File_Format );
103 ft_lzw_file_init( FT_LZWFile zip,
107 FT_LzwState lzw = &zip->lzw;
111 zip->stream = stream;
112 zip->source = source;
113 zip->memory = stream->memory;
115 zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE;
116 zip->cursor = zip->limit;
119 /* check and skip .Z header */
120 error = ft_lzw_check_header( source );
124 /* initialize internal lzw variable */
125 ft_lzwstate_init( lzw, source );
133 ft_lzw_file_done( FT_LZWFile zip )
136 ft_lzwstate_done( &zip->lzw );
145 ft_lzw_file_reset( FT_LZWFile zip )
147 FT_Stream stream = zip->source;
151 if ( !FT_STREAM_SEEK( 0 ) )
153 ft_lzwstate_reset( &zip->lzw );
155 zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE;
156 zip->cursor = zip->limit;
165 ft_lzw_file_fill_output( FT_LZWFile zip )
167 FT_LzwState lzw = &zip->lzw;
169 FT_Error error = FT_Err_Ok;
172 zip->cursor = zip->buffer;
174 count = ft_lzwstate_io( lzw, zip->buffer, FT_LZW_BUFFER_SIZE );
176 zip->limit = zip->cursor + count;
179 error = FT_THROW( Invalid_Stream_Operation );
185 /* fill output buffer; `count' must be <= FT_LZW_BUFFER_SIZE */
187 ft_lzw_file_skip_output( FT_LZWFile zip,
190 FT_Error error = FT_Err_Ok;
193 /* first, we skip what we can from the output buffer */
195 FT_ULong delta = (FT_ULong)( zip->limit - zip->cursor );
198 if ( delta >= count )
201 zip->cursor += delta;
207 /* next, we skip as many bytes remaining as possible */
210 FT_ULong delta = FT_LZW_BUFFER_SIZE;
217 numread = ft_lzwstate_io( &zip->lzw, NULL, delta );
218 if ( numread < delta )
220 /* not enough bytes */
221 error = FT_THROW( Invalid_Stream_Operation );
234 ft_lzw_file_io( FT_LZWFile zip,
243 /* seeking backwards. */
244 if ( pos < zip->pos )
246 /* If the new position is within the output buffer, simply */
247 /* decrement pointers, otherwise we reset the stream completely! */
248 if ( ( zip->pos - pos ) <= (FT_ULong)( zip->cursor - zip->buffer ) )
250 zip->cursor -= zip->pos - pos;
255 error = ft_lzw_file_reset( zip );
261 /* skip unwanted bytes */
262 if ( pos > zip->pos )
264 error = ft_lzw_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
272 /* now read the data */
278 delta = (FT_ULong)( zip->limit - zip->cursor );
279 if ( delta >= count )
282 FT_MEM_COPY( buffer + result, zip->cursor, delta );
284 zip->cursor += delta;
291 error = ft_lzw_file_fill_output( zip );
301 /***************************************************************************/
302 /***************************************************************************/
304 /***** L Z W E M B E D D I N G S T R E A M *****/
306 /***************************************************************************/
307 /***************************************************************************/
310 ft_lzw_stream_close( FT_Stream stream )
312 FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
313 FT_Memory memory = stream->memory;
318 /* finalize lzw file descriptor */
319 ft_lzw_file_done( zip );
323 stream->descriptor.pointer = NULL;
329 ft_lzw_stream_io( FT_Stream stream,
330 unsigned long offset,
331 unsigned char* buffer,
332 unsigned long count )
334 FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
337 return ft_lzw_file_io( zip, offset, buffer, count );
341 FT_EXPORT_DEF( FT_Error )
342 FT_Stream_OpenLZW( FT_Stream stream,
347 FT_LZWFile zip = NULL;
350 if ( !stream || !source )
352 error = FT_THROW( Invalid_Stream_Handle );
356 memory = source->memory;
359 * Check the header right now; this prevents allocation of a huge
360 * LZWFile object (400 KByte of heap memory) if not necessary.
362 * Did I mention that you should never use .Z compressed font
365 error = ft_lzw_check_header( source );
370 stream->memory = memory;
372 if ( !FT_NEW( zip ) )
374 error = ft_lzw_file_init( zip, stream, source );
381 stream->descriptor.pointer = zip;
384 stream->size = 0x7FFFFFFFL; /* don't know the real size! */
387 stream->read = ft_lzw_stream_io;
388 stream->close = ft_lzw_stream_close;
398 #else /* !FT_CONFIG_OPTION_USE_LZW */
401 FT_EXPORT_DEF( FT_Error )
402 FT_Stream_OpenLZW( FT_Stream stream,
408 return FT_THROW( Unimplemented_Feature );
412 #endif /* !FT_CONFIG_OPTION_USE_LZW */