Merge 25584, 25588.
[reactos.git] / reactos / dll / 3rdparty / freetype / src / lzw / ftzopen.c
1 /***************************************************************************/
2 /* */
3 /* ftzopen.c */
4 /* */
5 /* FreeType support for .Z compressed files. */
6 /* */
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 */
9 /* distributions. */
10 /* */
11 /* Copyright 2005, 2006 by David Turner. */
12 /* */
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. */
18 /* */
19 /***************************************************************************/
20
21 #include "ftzopen.h"
22 #include FT_INTERNAL_MEMORY_H
23 #include FT_INTERNAL_STREAM_H
24 #include FT_INTERNAL_DEBUG_H
25
26 /* refill input buffer, return 0 on success, or -1 if eof */
27 static int
28 ft_lzwstate_refill( FT_LzwState state )
29 {
30 int result = -1;
31
32
33 if ( !state->in_eof )
34 {
35 FT_ULong count = FT_Stream_TryRead( state->source,
36 state->in_buff,
37 sizeof ( state->in_buff ) );
38
39 state->in_cursor = state->in_buff;
40 state->in_limit = state->in_buff + count;
41 state->in_eof = FT_BOOL( count < sizeof ( state->in_buff ) );
42
43 if ( count > 0 )
44 result = 0;
45 }
46 return result;
47 }
48
49
50 /* return new code of 'num_bits', or -1 if eof */
51 static FT_Int32
52 ft_lzwstate_get_code( FT_LzwState state,
53 FT_UInt num_bits )
54 {
55 FT_Int32 result = -1;
56 FT_UInt32 pad = state->pad;
57 FT_UInt pad_bits = state->pad_bits;
58
59
60 while ( num_bits > pad_bits )
61 {
62 if ( state->in_cursor >= state->in_limit &&
63 ft_lzwstate_refill( state ) < 0 )
64 goto Exit;
65
66 pad |= (FT_UInt32)(*state->in_cursor++) << pad_bits;
67 pad_bits += 8;
68 }
69
70 result = (FT_Int32)( pad & LZW_MASK( num_bits ) );
71 state->pad_bits = pad_bits - num_bits;
72 state->pad = pad >> num_bits;
73
74 Exit:
75 return result;
76 }
77
78
79 /* grow the character stack */
80 static int
81 ft_lzwstate_stack_grow( FT_LzwState state )
82 {
83 if ( state->stack_top >= state->stack_size )
84 {
85 FT_Memory memory = state->memory;
86 FT_Error error;
87 FT_UInt old_size = state->stack_size;
88 FT_UInt new_size = old_size;
89
90 new_size = new_size + ( new_size >> 1 ) + 4;
91
92 if ( state->stack == state->stack_0 )
93 {
94 state->stack = NULL;
95 old_size = 0;
96 }
97
98 if ( FT_RENEW_ARRAY( state->stack, old_size, new_size ) )
99 return -1;
100
101 state->stack_size = new_size;
102 }
103 return 0;
104 }
105
106
107 /* grow the prefix/suffix arrays */
108 static int
109 ft_lzwstate_prefix_grow( FT_LzwState state )
110 {
111 FT_UInt old_size = state->prefix_size;
112 FT_UInt new_size = old_size;
113 FT_Memory memory = state->memory;
114 FT_Error error;
115
116
117 if ( new_size == 0 ) /* first allocation -> 9 bits */
118 new_size = 512;
119 else
120 new_size += new_size >> 2; /* don't grow too fast */
121
122 /*
123 * Note that the `suffix' array is located in the same memory block
124 * pointed to by `prefix'.
125 *
126 * I know that sizeof(FT_Byte) == 1 by definition, but it is clearer
127 * to write it literally.
128 *
129 */
130 if ( FT_REALLOC_MULT( state->prefix, old_size, new_size,
131 sizeof ( FT_UShort ) + sizeof ( FT_Byte ) ) )
132 return -1;
133
134 /* now adjust `suffix' and move the data accordingly */
135 state->suffix = (FT_Byte*)( state->prefix + new_size );
136
137 FT_MEM_MOVE( state->suffix,
138 state->prefix + old_size,
139 old_size * sizeof ( FT_Byte ) );
140
141 state->prefix_size = new_size;
142 return 0;
143 }
144
145
146 FT_LOCAL_DEF( void )
147 ft_lzwstate_reset( FT_LzwState state )
148 {
149 state->in_cursor = state->in_buff;
150 state->in_limit = state->in_buff;
151 state->in_eof = 0;
152 state->pad_bits = 0;
153 state->pad = 0;
154
155 state->stack_top = 0;
156 state->num_bits = LZW_INIT_BITS;
157 state->phase = FT_LZW_PHASE_START;
158 }
159
160
161 FT_LOCAL_DEF( void )
162 ft_lzwstate_init( FT_LzwState state,
163 FT_Stream source )
164 {
165 FT_ZERO( state );
166
167 state->source = source;
168 state->memory = source->memory;
169
170 state->prefix = NULL;
171 state->suffix = NULL;
172 state->prefix_size = 0;
173
174 state->stack = state->stack_0;
175 state->stack_size = sizeof ( state->stack_0 );
176
177 ft_lzwstate_reset( state );
178 }
179
180
181 FT_LOCAL_DEF( void )
182 ft_lzwstate_done( FT_LzwState state )
183 {
184 FT_Memory memory = state->memory;
185
186
187 ft_lzwstate_reset( state );
188
189 if ( state->stack != state->stack_0 )
190 FT_FREE( state->stack );
191
192 FT_FREE( state->prefix );
193 state->suffix = NULL;
194
195 FT_ZERO( state );
196 }
197
198
199 #define FTLZW_STACK_PUSH( c ) \
200 FT_BEGIN_STMNT \
201 if ( state->stack_top >= state->stack_size && \
202 ft_lzwstate_stack_grow( state ) < 0 ) \
203 goto Eof; \
204 \
205 state->stack[ state->stack_top++ ] = (FT_Byte)(c); \
206 FT_END_STMNT
207
208
209 FT_LOCAL_DEF( FT_ULong )
210 ft_lzwstate_io( FT_LzwState state,
211 FT_Byte* buffer,
212 FT_ULong out_size )
213 {
214 FT_ULong result = 0;
215
216 FT_UInt num_bits = state->num_bits;
217 FT_UInt free_ent = state->free_ent;
218 FT_UInt old_char = state->old_char;
219 FT_UInt old_code = state->old_code;
220 FT_UInt in_code = state->in_code;
221
222
223 if ( out_size == 0 )
224 goto Exit;
225
226 switch ( state->phase )
227 {
228 case FT_LZW_PHASE_START:
229 {
230 FT_Byte max_bits;
231 FT_Int32 c;
232
233
234 /* skip magic bytes, and read max_bits + block_flag */
235 if ( FT_Stream_Seek( state->source, 2 ) != 0 ||
236 FT_Stream_TryRead( state->source, &max_bits, 1 ) != 1 )
237 goto Eof;
238
239 state->max_bits = max_bits & LZW_BIT_MASK;
240 state->block_mode = max_bits & LZW_BLOCK_MASK;
241 state->max_free = (FT_UInt)( ( 1UL << state->max_bits ) - 256 );
242
243 if ( state->max_bits > LZW_MAX_BITS )
244 goto Eof;
245
246 num_bits = LZW_INIT_BITS;
247 free_ent = ( state->block_mode ? LZW_FIRST : LZW_CLEAR ) - 256;
248 in_code = 0;
249
250 state->free_bits = num_bits < state->max_bits
251 ? (FT_UInt)( ( 1UL << num_bits ) - 256 )
252 : state->max_free + 1;
253
254 c = ft_lzwstate_get_code( state, num_bits );
255 if ( c < 0 )
256 goto Eof;
257
258 old_code = old_char = (FT_UInt)c;
259
260 if ( buffer )
261 buffer[result] = (FT_Byte)old_char;
262
263 if ( ++result >= out_size )
264 goto Exit;
265
266 state->phase = FT_LZW_PHASE_CODE;
267 }
268 /* fall-through */
269
270 case FT_LZW_PHASE_CODE:
271 {
272 FT_Int32 c;
273 FT_UInt code;
274
275
276 NextCode:
277 c = ft_lzwstate_get_code( state, num_bits );
278 if ( c < 0 )
279 goto Eof;
280
281 code = (FT_UInt)c;
282
283 if ( code == LZW_CLEAR && state->block_mode )
284 {
285 free_ent = ( LZW_FIRST - 1 ) - 256; /* why not LZW_FIRST-256 ? */
286 num_bits = LZW_INIT_BITS;
287
288 state->free_bits = num_bits < state->max_bits
289 ? (FT_UInt)( ( 1UL << num_bits ) - 256 )
290 : state->max_free + 1;
291
292 c = ft_lzwstate_get_code( state, num_bits );
293 if ( c < 0 )
294 goto Eof;
295
296 code = (FT_UInt)c;
297 }
298
299 in_code = code; /* save code for later */
300
301 if ( code >= 256U )
302 {
303 /* special case for KwKwKwK */
304 if ( code - 256U >= free_ent )
305 {
306 FTLZW_STACK_PUSH( old_char );
307 code = old_code;
308 }
309
310 while ( code >= 256U )
311 {
312 FTLZW_STACK_PUSH( state->suffix[code - 256] );
313 code = state->prefix[code - 256];
314 }
315 }
316
317 old_char = code;
318 FTLZW_STACK_PUSH( old_char );
319
320 state->phase = FT_LZW_PHASE_STACK;
321 }
322 /* fall-through */
323
324 case FT_LZW_PHASE_STACK:
325 {
326 while ( state->stack_top > 0 )
327 {
328 --state->stack_top;
329
330 if ( buffer )
331 buffer[result] = state->stack[state->stack_top];
332
333 if ( ++result == out_size )
334 goto Exit;
335 }
336
337 /* now create new entry */
338 if ( free_ent < state->max_free )
339 {
340 if ( free_ent >= state->prefix_size &&
341 ft_lzwstate_prefix_grow( state ) < 0 )
342 goto Eof;
343
344 FT_ASSERT( free_ent < state->prefix_size );
345
346 state->prefix[free_ent] = (FT_UShort)old_code;
347 state->suffix[free_ent] = (FT_Byte) old_char;
348
349 if ( ++free_ent == state->free_bits )
350 {
351 num_bits++;
352
353 state->free_bits = num_bits < state->max_bits
354 ? (FT_UInt)( ( 1UL << num_bits ) - 256 )
355 : state->max_free + 1;
356 }
357 }
358
359 old_code = in_code;
360
361 state->phase = FT_LZW_PHASE_CODE;
362 goto NextCode;
363 }
364
365 default: /* state == EOF */
366 ;
367 }
368
369 Exit:
370 state->num_bits = num_bits;
371 state->free_ent = free_ent;
372 state->old_code = old_code;
373 state->old_char = old_char;
374 state->in_code = in_code;
375
376 return result;
377
378 Eof:
379 state->phase = FT_LZW_PHASE_EOF;
380 goto Exit;
381 }
382
383
384 /* END */