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, 2009, 2010 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
37 #define FT_ERR_PREFIX LZW_Err_
38 #define FT_ERR_BASE FT_Mod_Err_LZW
43 #ifdef FT_CONFIG_OPTION_USE_LZW
45 #ifdef FT_CONFIG_OPTION_PIC
46 #error "lzw code does not support PIC yet"
52 /***************************************************************************/
53 /***************************************************************************/
55 /***** M E M O R Y M A N A G E M E N T *****/
57 /***************************************************************************/
58 /***************************************************************************/
60 /***************************************************************************/
61 /***************************************************************************/
63 /***** F I L E D E S C R I P T O R *****/
65 /***************************************************************************/
66 /***************************************************************************/
68 #define FT_LZW_BUFFER_SIZE 4096
70 typedef struct FT_LZWFileRec_
72 FT_Stream source
; /* parent/source stream */
73 FT_Stream stream
; /* embedding stream */
74 FT_Memory memory
; /* memory allocator */
75 FT_LzwStateRec lzw
; /* lzw decompressor state */
77 FT_Byte buffer
[FT_LZW_BUFFER_SIZE
]; /* output buffer */
78 FT_ULong pos
; /* position in output */
82 } FT_LZWFileRec
, *FT_LZWFile
;
85 /* check and skip .Z header */
87 ft_lzw_check_header( FT_Stream stream
)
93 if ( FT_STREAM_SEEK( 0 ) ||
94 FT_STREAM_READ( head
, 2 ) )
97 /* head[0] && head[1] are the magic numbers */
98 if ( head
[0] != 0x1f ||
100 error
= LZW_Err_Invalid_File_Format
;
108 ft_lzw_file_init( FT_LZWFile zip
,
112 FT_LzwState lzw
= &zip
->lzw
;
113 FT_Error error
= LZW_Err_Ok
;
116 zip
->stream
= stream
;
117 zip
->source
= source
;
118 zip
->memory
= stream
->memory
;
120 zip
->limit
= zip
->buffer
+ FT_LZW_BUFFER_SIZE
;
121 zip
->cursor
= zip
->limit
;
124 /* check and skip .Z header */
125 error
= ft_lzw_check_header( source
);
129 /* initialize internal lzw variable */
130 ft_lzwstate_init( lzw
, source
);
138 ft_lzw_file_done( FT_LZWFile zip
)
141 ft_lzwstate_done( &zip
->lzw
);
150 ft_lzw_file_reset( FT_LZWFile zip
)
152 FT_Stream stream
= zip
->source
;
156 if ( !FT_STREAM_SEEK( 0 ) )
158 ft_lzwstate_reset( &zip
->lzw
);
160 zip
->limit
= zip
->buffer
+ FT_LZW_BUFFER_SIZE
;
161 zip
->cursor
= zip
->limit
;
170 ft_lzw_file_fill_output( FT_LZWFile zip
)
172 FT_LzwState lzw
= &zip
->lzw
;
177 zip
->cursor
= zip
->buffer
;
179 count
= ft_lzwstate_io( lzw
, zip
->buffer
, FT_LZW_BUFFER_SIZE
);
181 zip
->limit
= zip
->cursor
+ count
;
184 error
= LZW_Err_Invalid_Stream_Operation
;
190 /* fill output buffer; `count' must be <= FT_LZW_BUFFER_SIZE */
192 ft_lzw_file_skip_output( FT_LZWFile zip
,
195 FT_Error error
= LZW_Err_Ok
;
198 /* first, we skip what we can from the output buffer */
200 FT_ULong delta
= (FT_ULong
)( zip
->limit
- zip
->cursor
);
203 if ( delta
>= count
)
206 zip
->cursor
+= delta
;
212 /* next, we skip as many bytes remaining as possible */
215 FT_ULong delta
= FT_LZW_BUFFER_SIZE
;
222 numread
= ft_lzwstate_io( &zip
->lzw
, NULL
, delta
);
223 if ( numread
< delta
)
225 /* not enough bytes */
226 error
= LZW_Err_Invalid_Stream_Operation
;
239 ft_lzw_file_io( FT_LZWFile zip
,
248 /* seeking backwards. */
249 if ( pos
< zip
->pos
)
251 /* If the new position is within the output buffer, simply */
252 /* decrement pointers, otherwise we reset the stream completely! */
253 if ( ( zip
->pos
- pos
) <= (FT_ULong
)( zip
->cursor
- zip
->buffer
) )
255 zip
->cursor
-= zip
->pos
- pos
;
260 error
= ft_lzw_file_reset( zip
);
266 /* skip unwanted bytes */
267 if ( pos
> zip
->pos
)
269 error
= ft_lzw_file_skip_output( zip
, (FT_ULong
)( pos
- zip
->pos
) );
277 /* now read the data */
283 delta
= (FT_ULong
)( zip
->limit
- zip
->cursor
);
284 if ( delta
>= count
)
287 FT_MEM_COPY( buffer
+ result
, zip
->cursor
, delta
);
289 zip
->cursor
+= delta
;
296 error
= ft_lzw_file_fill_output( zip
);
306 /***************************************************************************/
307 /***************************************************************************/
309 /***** L Z W E M B E D D I N G S T R E A M *****/
311 /***************************************************************************/
312 /***************************************************************************/
315 ft_lzw_stream_close( FT_Stream stream
)
317 FT_LZWFile zip
= (FT_LZWFile
)stream
->descriptor
.pointer
;
318 FT_Memory memory
= stream
->memory
;
323 /* finalize lzw file descriptor */
324 ft_lzw_file_done( zip
);
328 stream
->descriptor
.pointer
= NULL
;
334 ft_lzw_stream_io( FT_Stream stream
,
339 FT_LZWFile zip
= (FT_LZWFile
)stream
->descriptor
.pointer
;
342 return ft_lzw_file_io( zip
, pos
, buffer
, count
);
346 FT_EXPORT_DEF( FT_Error
)
347 FT_Stream_OpenLZW( FT_Stream stream
,
351 FT_Memory memory
= source
->memory
;
356 * Check the header right now; this prevents allocation of a huge
357 * LZWFile object (400 KByte of heap memory) if not necessary.
359 * Did I mention that you should never use .Z compressed font
362 error
= ft_lzw_check_header( source
);
367 stream
->memory
= memory
;
369 if ( !FT_NEW( zip
) )
371 error
= ft_lzw_file_init( zip
, stream
, source
);
378 stream
->descriptor
.pointer
= zip
;
381 stream
->size
= 0x7FFFFFFFL
; /* don't know the real size! */
384 stream
->read
= ft_lzw_stream_io
;
385 stream
->close
= ft_lzw_stream_close
;
395 #else /* !FT_CONFIG_OPTION_USE_LZW */
398 FT_EXPORT_DEF( FT_Error
)
399 FT_Stream_OpenLZW( FT_Stream stream
,
405 return LZW_Err_Unimplemented_Feature
;
409 #endif /* !FT_CONFIG_OPTION_USE_LZW */