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, 2005, 2006 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
34 #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
49 /***************************************************************************/
50 /***************************************************************************/
52 /***** M E M O R Y M A N A G E M E N T *****/
54 /***************************************************************************/
55 /***************************************************************************/
57 /***************************************************************************/
58 /***************************************************************************/
60 /***** F I L E D E S C R I P T O R *****/
62 /***************************************************************************/
63 /***************************************************************************/
65 #define FT_LZW_BUFFER_SIZE 4096
67 typedef struct FT_LZWFileRec_
69 FT_Stream source
; /* parent/source stream */
70 FT_Stream stream
; /* embedding stream */
71 FT_Memory memory
; /* memory allocator */
72 FT_LzwStateRec lzw
; /* lzw decompressor state */
74 FT_Byte buffer
[FT_LZW_BUFFER_SIZE
]; /* output buffer */
75 FT_ULong pos
; /* position in output */
79 } FT_LZWFileRec
, *FT_LZWFile
;
82 /* check and skip .Z header */
84 ft_lzw_check_header( FT_Stream stream
)
90 if ( FT_STREAM_SEEK( 0 ) ||
91 FT_STREAM_READ( head
, 2 ) )
94 /* head[0] && head[1] are the magic numbers */
95 if ( head
[0] != 0x1f ||
97 error
= LZW_Err_Invalid_File_Format
;
105 ft_lzw_file_init( FT_LZWFile zip
,
109 FT_LzwState lzw
= &zip
->lzw
;
110 FT_Error error
= LZW_Err_Ok
;
113 zip
->stream
= stream
;
114 zip
->source
= source
;
115 zip
->memory
= stream
->memory
;
117 zip
->limit
= zip
->buffer
+ FT_LZW_BUFFER_SIZE
;
118 zip
->cursor
= zip
->limit
;
121 /* check and skip .Z header */
125 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
;
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
= LZW_Err_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
= LZW_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
= LZW_Err_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
,
352 FT_Memory memory
= source
->memory
;
357 * Check the header right now; this prevents allocation of a huge
358 * LZWFile object (400 KByte of heap memory) if not necessary.
360 * Did I mention that you should never use .Z compressed font
363 error
= ft_lzw_check_header( source
);
368 stream
->memory
= memory
;
370 if ( !FT_NEW( zip
) )
372 error
= ft_lzw_file_init( zip
, stream
, source
);
379 stream
->descriptor
.pointer
= zip
;
382 stream
->size
= 0x7FFFFFFFL
; /* don't know the real size! */
385 stream
->read
= ft_lzw_stream_io
;
386 stream
->close
= ft_lzw_stream_close
;
396 #else /* !FT_CONFIG_OPTION_USE_LZW */
399 FT_EXPORT_DEF( FT_Error
)
400 FT_Stream_OpenLZW( FT_Stream stream
,
406 return LZW_Err_Unimplemented_Feature
;
410 #endif /* !FT_CONFIG_OPTION_USE_LZW */