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 2005, 2006, 2007, 2008 by David Turner. */
13 /* This file is part of the FreeType project, and may only be used, */
14 /* modified, and distributed under the terms of the FreeType project */
15 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
16 /* this file you indicate that you have read the license and */
17 /* understand and accept it fully. */
19 /***************************************************************************/
21 #ifndef __FT_ZOPEN_H__
22 #define __FT_ZOPEN_H__
25 #include FT_FREETYPE_H
29 * This is a complete re-implementation of the LZW file reader,
30 * since the old one was incredibly badly written, using
31 * 400 KByte of heap memory before decompressing anything.
35 #define FT_LZW_IN_BUFF_SIZE 64
36 #define FT_LZW_DEFAULT_STACK_SIZE 64
38 #define LZW_INIT_BITS 9
39 #define LZW_MAX_BITS 16
44 #define LZW_BIT_MASK 0x1f
45 #define LZW_BLOCK_MASK 0x80
46 #define LZW_MASK( n ) ( ( 1U << (n) ) - 1U )
49 typedef enum FT_LzwPhase_
51 FT_LZW_PHASE_START
= 0,
60 * state of LZW decompressor
62 * small technical note
63 * --------------------
65 * We use a few tricks in this implementation that are explained here to
66 * ease debugging and maintenance.
68 * - First of all, the `prefix' and `suffix' arrays contain the suffix
69 * and prefix for codes over 256; this means that
71 * prefix_of(code) == state->prefix[code-256]
72 * suffix_of(code) == state->suffix[code-256]
74 * Each prefix is a 16-bit code, and each suffix an 8-bit byte.
76 * Both arrays are stored in a single memory block, pointed to by
77 * `state->prefix'. This means that the following equality is always
80 * state->suffix == (FT_Byte*)(state->prefix + state->prefix_size)
82 * Of course, state->prefix_size is the number of prefix/suffix slots
83 * in the arrays, corresponding to codes 256..255+prefix_size.
85 * - `free_ent' is the index of the next free entry in the `prefix'
86 * and `suffix' arrays. This means that the corresponding `next free
87 * code' is really `256+free_ent'.
89 * Moreover, `max_free' is the maximum value that `free_ent' can reach.
91 * `max_free' corresponds to `(1 << max_bits) - 256'. Note that this
92 * value is always <= 0xFF00, which means that both `free_ent' and
93 * `max_free' can be stored in an FT_UInt variable, even on 16-bit
96 * If `free_ent == max_free', you cannot add new codes to the
97 * prefix/suffix table.
99 * - `num_bits' is the current number of code bits, starting at 9 and
100 * growing each time `free_ent' reaches the value of `free_bits'. The
101 * latter is computed as follows
103 * if num_bits < max_bits:
104 * free_bits = (1 << num_bits)-256
106 * free_bits = max_free + 1
108 * Since the value of `max_free + 1' can never be reached by
109 * `free_ent', `num_bits' cannot grow larger than `max_bits'.
112 typedef struct FT_LzwStateRec_
123 FT_UInt max_bits
; /* max code bits, from file header */
124 FT_Int block_mode
; /* block mode flag, from file header */
125 FT_UInt max_free
; /* (1 << max_bits) - 256 */
127 FT_UInt num_bits
; /* current code bit number */
128 FT_UInt free_ent
; /* index of next free entry */
129 FT_UInt free_bits
; /* if reached by free_ent, increment num_bits */
134 FT_UShort
* prefix
; /* always dynamically allocated / reallocated */
135 FT_Byte
* suffix
; /* suffix = (FT_Byte*)(prefix + prefix_size) */
136 FT_UInt prefix_size
; /* number of slots in `prefix' or `suffix' */
138 FT_Byte
* stack
; /* character stack */
140 FT_Offset stack_size
;
141 FT_Byte stack_0
[FT_LZW_DEFAULT_STACK_SIZE
]; /* minimize heap alloc */
143 FT_Stream source
; /* source stream */
146 } FT_LzwStateRec
, *FT_LzwState
;
150 ft_lzwstate_init( FT_LzwState state
,
154 ft_lzwstate_done( FT_LzwState state
);
158 ft_lzwstate_reset( FT_LzwState state
);
162 ft_lzwstate_io( FT_LzwState state
,
168 #endif /* __FT_ZOPEN_H__ */