24 |
#include <ft2build.h> |
#include <ft2build.h> |
25 |
#include FT_FREETYPE_H |
#include FT_FREETYPE_H |
26 |
|
|
27 |
/* this is a complete re-implementation of the LZW file reader, |
|
28 |
* since the old one was incredibly badly written, and used |
/* |
29 |
* 400 Kb of heap memory before decompressing anything. |
* 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. |
32 |
#define FT_LZW_IN_BUFF_SIZE 64 |
* |
33 |
#define FT_LZW_DEFAULT_STACK_SIZE 64 |
*/ |
34 |
|
|
35 |
#define LZW_INIT_BITS 9 |
#define FT_LZW_IN_BUFF_SIZE 64 |
36 |
#define LZW_MAX_BITS 16 |
#define FT_LZW_DEFAULT_STACK_SIZE 64 |
37 |
|
|
38 |
#define LZW_CLEAR 256 |
#define LZW_INIT_BITS 9 |
39 |
#define LZW_FIRST 257 |
#define LZW_MAX_BITS 16 |
40 |
|
|
41 |
#define LZW_BIT_MASK 0x1f |
#define LZW_CLEAR 256 |
42 |
#define LZW_BLOCK_MASK 0x80 |
#define LZW_FIRST 257 |
43 |
#define LZW_MASK(n) ((1U << (n)) - 1U) |
|
44 |
|
#define LZW_BIT_MASK 0x1f |
45 |
typedef enum |
#define LZW_BLOCK_MASK 0x80 |
46 |
{ |
#define LZW_MASK( n ) ( ( 1U << (n) ) - 1U ) |
47 |
FT_LZW_PHASE_START = 0, |
|
48 |
FT_LZW_PHASE_CODE, |
|
49 |
FT_LZW_PHASE_STACK, |
typedef enum |
50 |
FT_LZW_PHASE_EOF |
{ |
51 |
|
FT_LZW_PHASE_START = 0, |
52 |
} FT_LzwPhase; |
FT_LZW_PHASE_CODE, |
53 |
|
FT_LZW_PHASE_STACK, |
54 |
|
FT_LZW_PHASE_EOF |
55 |
/* state of LZW decompressor |
|
56 |
* |
} FT_LzwPhase; |
57 |
* small technical note: |
|
58 |
* |
|
59 |
* we use a few tricks in this implementation that are explained here to |
/* |
60 |
* ease debugging and maintenance. |
* state of LZW decompressor |
61 |
* |
* |
62 |
* - first of all, the "prefix" and "suffix" arrays contain the |
* small technical note |
63 |
* suffix and prefix for codes over 256, this means that: |
* -------------------- |
64 |
* |
* |
65 |
* prefix_of(code) == state->prefix[ code-256 ] |
* We use a few tricks in this implementation that are explained here to |
66 |
* suffix_of(code) == state->suffix[ code-256 ] |
* ease debugging and maintenance. |
67 |
* |
* |
68 |
* each prefix is a 16-bit code, and each suffix an 8-bit byte |
* - First of all, the `prefix' and `suffix' arrays contain the suffix |
69 |
* |
* and prefix for codes over 256; this means that |
70 |
* both arrays are stored in a single memory block, pointed to by |
* |
71 |
* 'state->prefix', this means that the following equality is always |
* prefix_of(code) == state->prefix[code-256] |
72 |
* true: |
* suffix_of(code) == state->suffix[code-256] |
73 |
* |
* |
74 |
* state->suffix == (FT_Byte*)(state->prefix + state->prefix_size) |
* Each prefix is a 16-bit code, and each suffix an 8-bit byte. |
75 |
* |
* |
76 |
* of course, state->prefix_size is the number of prefix/suffix slots |
* Both arrays are stored in a single memory block, pointed to by |
77 |
* in the arrays, corresponding to codes 256..255+prefix_size |
* `state->prefix'. This means that the following equality is always |
78 |
* |
* true: |
79 |
* - 'free_ent' is the index of the next free entry in the "prefix" |
* |
80 |
* and "suffix" arrays. This means that the corresponding "next free |
* state->suffix == (FT_Byte*)(state->prefix + state->prefix_size) |
81 |
* code" is really '256+free_ent' |
* |
82 |
* |
* Of course, state->prefix_size is the number of prefix/suffix slots |
83 |
* moreover, 'max_free' is the maximum value that 'free_ent' can reach. |
* in the arrays, corresponding to codes 256..255+prefix_size. |
84 |
* |
* |
85 |
* 'max_free' corresponds to "(1 << max_bits) - 256". Note that this value |
* - `free_ent' is the index of the next free entry in the `prefix' |
86 |
* is always <= 0xFF00, which means that both 'free_ent' and 'max_free' can |
* and `suffix' arrays. This means that the corresponding `next free |
87 |
* be stored in FT_UInt variable, even on 16-bit machines. |
* code' is really `256+free_ent'. |
88 |
* |
* |
89 |
* if 'free_ent == max_free', you cannot add new codes to the prefix/suffix |
* Moreover, 'max_free' is the maximum value that 'free_ent' can reach. |
90 |
* table. |
* |
91 |
* |
* `max_free' corresponds to `(1 << max_bits) - 256'. Note that this |
92 |
* - 'num_bits' is the current number of code bits, starting at 9 and |
* value is always <= 0xFF00, which means that both `free_ent' and |
93 |
* growing each time 'free_ent' reaches the value of 'free_bits'. the |
* `max_free' can be stored in an FT_UInt variable, even on 16-bit |
94 |
* latter is computed as follows: |
* machines. |
95 |
* |
* |
96 |
* if num_bits < max_bits: |
* If `free_ent == max_free', you cannot add new codes to the |
97 |
* free_bits = (1 << num_bits)-256 |
* prefix/suffix table. |
98 |
* else: |
* |
99 |
* free_bits = max_free + 1 |
* - `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 |
* since the value of 'max_free + 1' can never be reached by 'free_ent', |
* latter is computed as follows |
102 |
* 'num_bits' cannot grow larger than 'max_bits' |
* |
103 |
*/ |
* if num_bits < max_bits: |
104 |
typedef struct |
* free_bits = (1 << num_bits)-256 |
105 |
{ |
* else: |
106 |
FT_LzwPhase phase; |
* free_bits = max_free + 1 |
107 |
|
* |
108 |
FT_Int in_eof; |
* Since the value of `max_free + 1' can never be reached by |
109 |
FT_Byte* in_cursor; /* current buffer pos */ |
* `free_ent', `num_bits' cannot grow larger than `max_bits'. |
110 |
FT_Byte* in_limit; /* current buffer limit */ |
*/ |
111 |
|
|
112 |
FT_UInt32 pad; /* a pad value where incoming bits were read */ |
typedef struct _FT_LzwStateRec |
113 |
FT_Int pad_bits; /* number of meaningful bits in pad value */ |
{ |
114 |
|
FT_LzwPhase phase; |
115 |
FT_UInt max_bits; /* max code bits, from file header */ |
|
116 |
FT_Int block_mode; /* block mode flag, from file header */ |
FT_Int in_eof; |
117 |
FT_UInt max_free; /* (1 << max_bits) - 256 */ |
FT_Byte* in_cursor; /* current buffer pos */ |
118 |
|
FT_Byte* in_limit; /* current buffer limit */ |
119 |
FT_UInt num_bits; /* current code bit number */ |
|
120 |
FT_UInt free_ent; /* index of next free entry */ |
FT_UInt32 pad; /* a pad value where incoming bits were read */ |
121 |
FT_UInt free_bits; /* if free_ent reaches this, increment num_bits */ |
FT_Int pad_bits; /* number of meaningful bits in pad value */ |
122 |
FT_UInt old_code; |
|
123 |
FT_UInt old_char; |
FT_UInt max_bits; /* max code bits, from file header */ |
124 |
FT_UInt in_code; |
FT_Int block_mode; /* block mode flag, from file header */ |
125 |
|
FT_UInt max_free; /* (1 << max_bits) - 256 */ |
126 |
FT_UShort* prefix; /* always dynamically allocated / reallocated */ |
|
127 |
FT_Byte* suffix; /* suffix = (FT_Byte*)(prefix + prefix_size) */ |
FT_UInt num_bits; /* current code bit number */ |
128 |
FT_UInt prefix_size; /* number of slots in 'prefix' or 'suffix' */ |
FT_UInt free_ent; /* index of next free entry */ |
129 |
|
FT_UInt free_bits; /* if reached by free_ent, increment num_bits */ |
130 |
FT_Byte* stack; /* character stack */ |
FT_UInt old_code; |
131 |
FT_UInt stack_top; |
FT_UInt old_char; |
132 |
FT_UInt stack_size; |
FT_UInt in_code; |
133 |
|
|
134 |
FT_Byte in_buff[ FT_LZW_IN_BUFF_SIZE ]; /* small buffer to read data */ |
FT_UShort* prefix; /* always dynamically allocated / reallocated */ |
135 |
FT_Byte stack_0[ FT_LZW_DEFAULT_STACK_SIZE ]; /* minimize heap alloc */ |
FT_Byte* suffix; /* suffix = (FT_Byte*)(prefix + prefix_size) */ |
136 |
|
FT_UInt prefix_size; /* number of slots in `prefix' or `suffix' */ |
137 |
FT_Stream source; /* source stream */ |
|
138 |
FT_Memory memory; |
FT_Byte* stack; /* character stack */ |
139 |
|
FT_UInt stack_top; |
140 |
} FT_LzwStateRec, *FT_LzwState; |
FT_UInt stack_size; |
141 |
|
|
142 |
|
FT_Byte in_buff[FT_LZW_IN_BUFF_SIZE]; /* small read-buffer */ |
143 |
FT_LOCAL( void ) |
FT_Byte stack_0[FT_LZW_DEFAULT_STACK_SIZE]; /* minimize heap alloc */ |
144 |
ft_lzwstate_init( FT_LzwState state, |
|
145 |
FT_Stream source ); |
FT_Stream source; /* source stream */ |
146 |
|
FT_Memory memory; |
147 |
FT_LOCAL( void ) |
|
148 |
ft_lzwstate_done( FT_LzwState state ); |
} FT_LzwStateRec, *FT_LzwState; |
149 |
|
|
150 |
|
|
151 |
FT_LOCAL( void ) |
FT_LOCAL( void ) |
152 |
ft_lzwstate_reset( FT_LzwState state ); |
ft_lzwstate_init( FT_LzwState state, |
153 |
|
FT_Stream source ); |
154 |
|
|
155 |
FT_LOCAL( FT_ULong ) |
FT_LOCAL( void ) |
156 |
ft_lzwstate_io( FT_LzwState state, |
ft_lzwstate_done( FT_LzwState state ); |
157 |
FT_Byte* buffer, |
|
158 |
FT_ULong out_size ); |
|
159 |
|
FT_LOCAL( void ) |
160 |
|
ft_lzwstate_reset( FT_LzwState state ); |
161 |
|
|
162 |
|
|
163 |
|
FT_LOCAL( FT_ULong ) |
164 |
|
ft_lzwstate_io( FT_LzwState state, |
165 |
|
FT_Byte* buffer, |
166 |
|
FT_ULong out_size ); |
167 |
|
|
168 |
/* */ |
/* */ |
169 |
|
|
170 |
#endif /* __FT_ZOPEN_H__ */ |
#endif /* __FT_ZOPEN_H__ */ |
171 |
|
|
172 |
|
|
173 |
|
/* END */ |