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 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
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
48
/***************************************************************************/
49
/***************************************************************************/
51
/***** M E M O R Y M A N A G E M E N T *****/
53
/***************************************************************************/
54
/***************************************************************************/
56
/***************************************************************************/
57
/***************************************************************************/
59
/***** F I L E D E S C R I P T O R *****/
61
/***************************************************************************/
62
/***************************************************************************/
64
#define FT_LZW_BUFFER_SIZE 4096
66
typedef struct FT_LZWFileRec_
68
FT_Stream source; /* parent/source stream */
69
FT_Stream stream; /* embedding stream */
70
FT_Memory memory; /* memory allocator */
71
s_zstate_t zstream; /* lzw input stream */
73
FT_ULong start; /* starting position, after .Z header */
74
FT_Byte input[FT_LZW_BUFFER_SIZE]; /* input buffer */
76
FT_Byte buffer[FT_LZW_BUFFER_SIZE]; /* output buffer */
77
FT_ULong pos; /* position in output */
81
} FT_LZWFileRec, *FT_LZWFile;
84
/* check and skip .Z header */
86
ft_lzw_check_header( FT_Stream stream )
92
if ( FT_STREAM_SEEK( 0 ) ||
93
FT_STREAM_READ( head, 2 ) )
96
/* head[0] && head[1] are the magic numbers */
97
if ( head[0] != 0x1f ||
99
error = LZW_Err_Invalid_File_Format;
107
ft_lzw_file_init( FT_LZWFile zip,
111
s_zstate_t* zstream = &zip->zstream;
112
FT_Error error = LZW_Err_Ok;
115
zip->stream = stream;
116
zip->source = source;
117
zip->memory = stream->memory;
119
zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE;
120
zip->cursor = zip->limit;
123
/* check and skip .Z header */
127
error = ft_lzw_check_header( source );
131
zip->start = FT_STREAM_POS();
134
/* initialize internal lzw variable */
137
zstream->avail_in = 0;
138
zstream->next_in = zip->buffer;
139
zstream->zs_in_count = source->size - 2;
141
if ( zstream->next_in == NULL )
142
error = LZW_Err_Invalid_File_Format;
150
ft_lzw_file_done( FT_LZWFile zip )
152
s_zstate_t* zstream = &zip->zstream;
156
zstream->next_in = NULL;
157
zstream->next_out = NULL;
158
zstream->avail_in = 0;
159
zstream->avail_out = 0;
160
zstream->total_in = 0;
161
zstream->total_out = 0;
170
ft_lzw_file_reset( FT_LZWFile zip )
172
FT_Stream stream = zip->source;
176
if ( !FT_STREAM_SEEK( zip->start ) )
178
s_zstate_t* zstream = &zip->zstream;
183
zstream->avail_in = 0;
184
zstream->next_in = zip->input;
185
zstream->total_in = 0;
186
zstream->avail_out = 0;
187
zstream->next_out = zip->buffer;
188
zstream->total_out = 0;
189
zstream->zs_in_count = zip->source->size - 2;
191
zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE;
192
zip->cursor = zip->limit;
201
ft_lzw_file_fill_input( FT_LZWFile zip )
203
s_zstate_t* zstream = &zip->zstream;
204
FT_Stream stream = zip->source;
210
size = stream->read( stream, stream->pos, zip->input,
211
FT_LZW_BUFFER_SIZE );
213
return LZW_Err_Invalid_Stream_Operation;
217
size = stream->size - stream->pos;
218
if ( size > FT_LZW_BUFFER_SIZE )
219
size = FT_LZW_BUFFER_SIZE;
222
return LZW_Err_Invalid_Stream_Operation;
224
FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
228
zstream->next_in = zip->input;
229
zstream->avail_in = size;
237
ft_lzw_file_fill_output( FT_LZWFile zip )
239
s_zstate_t* zstream = &zip->zstream;
243
zip->cursor = zip->buffer;
244
zstream->next_out = zip->cursor;
245
zstream->avail_out = FT_LZW_BUFFER_SIZE;
247
while ( zstream->avail_out > 0 )
252
if ( zstream->avail_in == 0 )
254
error = ft_lzw_file_fill_input( zip );
259
num_read = zread( zstream );
261
if ( num_read == -1 && zstream->zs_in_count == 0 )
263
zip->limit = zstream->next_out;
264
if ( zip->limit == zip->cursor )
265
error = LZW_Err_Invalid_Stream_Operation;
268
else if ( num_read == -1 )
271
zstream->avail_out -= num_read;
278
/* fill output buffer; `count' must be <= FT_LZW_BUFFER_SIZE */
280
ft_lzw_file_skip_output( FT_LZWFile zip,
283
FT_Error error = LZW_Err_Ok;
289
delta = (FT_ULong)( zip->limit - zip->cursor );
290
if ( delta >= count )
293
zip->cursor += delta;
300
error = ft_lzw_file_fill_output( zip );
310
ft_lzw_file_io( FT_LZWFile zip,
319
/* Teset inflate stream if we're seeking backwards. */
320
/* Yes, that is not too efficient, but it saves memory :-) */
321
if ( pos < zip->pos )
323
error = ft_lzw_file_reset( zip );
328
/* skip unwanted bytes */
329
if ( pos > zip->pos )
331
error = ft_lzw_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
339
/* now read the data */
345
delta = (FT_ULong)( zip->limit - zip->cursor );
346
if ( delta >= count )
349
FT_MEM_COPY( buffer, zip->cursor, delta );
352
zip->cursor += delta;
359
error = ft_lzw_file_fill_output( zip );
369
/***************************************************************************/
370
/***************************************************************************/
372
/***** L Z W E M B E D D I N G S T R E A M *****/
374
/***************************************************************************/
375
/***************************************************************************/
378
ft_lzw_stream_close( FT_Stream stream )
380
FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
381
FT_Memory memory = stream->memory;
386
/* finalize lzw file descriptor */
387
ft_lzw_file_done( zip );
391
stream->descriptor.pointer = NULL;
397
ft_lzw_stream_io( FT_Stream stream,
402
FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
405
return ft_lzw_file_io( zip, pos, buffer, count );
409
FT_EXPORT_DEF( FT_Error )
410
FT_Stream_OpenLZW( FT_Stream stream,
414
FT_Memory memory = source->memory;
419
stream->memory = memory;
421
if ( !FT_NEW( zip ) )
423
error = ft_lzw_file_init( zip, stream, source );
430
stream->descriptor.pointer = zip;
433
stream->size = 0x7FFFFFFFL; /* don't know the real size! */
436
stream->read = ft_lzw_stream_io;
437
stream->close = ft_lzw_stream_close;
446
#else /* !FT_CONFIG_OPTION_USE_LZW */
449
FT_EXPORT_DEF( FT_Error )
450
FT_Stream_OpenLZW( FT_Stream stream,
456
return LZW_Err_Unimplemented_Feature;
460
#endif /* !FT_CONFIG_OPTION_USE_LZW */