Bug Summary

File:lzw/ftlzw.c
Location:line 126, column 7
Description:Value stored to 'stream' is never read

Annotated Source Code

1/***************************************************************************/
2/* */
3/* ftlzw.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 2004, 2005, 2006, 2009 by */
12/* Albert Chin-A-Young. */
13/* */
14/* Based on code in src/gzip/ftgzip.c, Copyright 2004 by */
15/* David Turner, Robert Wilhelm, and Werner Lemberg. */
16/* */
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. */
22/* */
23/***************************************************************************/
24
25#include <ft2build.h>
26#include FT_INTERNAL_MEMORY_H<freetype/internal/ftmemory.h>
27#include FT_INTERNAL_STREAM_H<freetype/internal/ftstream.h>
28#include FT_INTERNAL_DEBUG_H<freetype/internal/ftdebug.h>
29#include FT_LZW_H<freetype/ftlzw.h>
30#include FT_CONFIG_STANDARD_LIBRARY_H<freetype/config/ftstdlib.h>
31
32
33#include FT_MODULE_ERRORS_H<freetype/ftmoderr.h>
34
35#undef __FTERRORS_H__
36
37#define FT_ERR_PREFIX LZW_Err_
38#define FT_ERR_BASE FT_Mod_Err_LZW
39
40#include FT_ERRORS_H<freetype/fterrors.h>
41
42
43#ifdef FT_CONFIG_OPTION_USE_LZW
44
45#ifdef FT_CONFIG_OPTION_PIC
46#error "lzw code does not support PIC yet"
47#endif
48
49#include "ftzopen.h"
50
51
52/***************************************************************************/
53/***************************************************************************/
54/***** *****/
55/***** M E M O R Y M A N A G E M E N T *****/
56/***** *****/
57/***************************************************************************/
58/***************************************************************************/
59
60/***************************************************************************/
61/***************************************************************************/
62/***** *****/
63/***** F I L E D E S C R I P T O R *****/
64/***** *****/
65/***************************************************************************/
66/***************************************************************************/
67
68#define FT_LZW_BUFFER_SIZE4096 4096
69
70 typedef struct FT_LZWFileRec_
71 {
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 */
76
77 FT_Byte buffer[FT_LZW_BUFFER_SIZE4096]; /* output buffer */
78 FT_ULong pos; /* position in output */
79 FT_Byte* cursor;
80 FT_Byte* limit;
81
82 } FT_LZWFileRec, *FT_LZWFile;
83
84
85 /* check and skip .Z header */
86 static FT_Error
87 ft_lzw_check_header( FT_Stream stream )
88 {
89 FT_Error error;
90 FT_Byte head[2];
91
92
93 if ( FT_STREAM_SEEK( 0 )( ( error = (FT_Stream_Seek( stream, 0 )) ) != 0 ) ||
94 FT_STREAM_READ( head, 2 )( ( error = (FT_Stream_Read( stream, (FT_Byte*)head, 2 )) ) !=
0 )
)
95 goto Exit;
96
97 /* head[0] && head[1] are the magic numbers */
98 if ( head[0] != 0x1f ||
99 head[1] != 0x9d )
100 error = LZW_Err_Invalid_File_Format;
101
102 Exit:
103 return error;
104 }
105
106
107 static FT_Error
108 ft_lzw_file_init( FT_LZWFile zip,
109 FT_Stream stream,
110 FT_Stream source )
111 {
112 FT_LzwState lzw = &zip->lzw;
113 FT_Error error = LZW_Err_Ok;
114
115
116 zip->stream = stream;
117 zip->source = source;
118 zip->memory = stream->memory;
119
120 zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE4096;
121 zip->cursor = zip->limit;
122 zip->pos = 0;
123
124 /* check and skip .Z header */
125 {
126 stream = source;
Value stored to 'stream' is never read
127
128 error = ft_lzw_check_header( source );
129 if ( error )
130 goto Exit;
131 }
132
133 /* initialize internal lzw variable */
134 ft_lzwstate_init( lzw, source );
135
136 Exit:
137 return error;
138 }
139
140
141 static void
142 ft_lzw_file_done( FT_LZWFile zip )
143 {
144 /* clear the rest */
145 ft_lzwstate_done( &zip->lzw );
146
147 zip->memory = NULL((void*)0);
148 zip->source = NULL((void*)0);
149 zip->stream = NULL((void*)0);
150 }
151
152
153 static FT_Error
154 ft_lzw_file_reset( FT_LZWFile zip )
155 {
156 FT_Stream stream = zip->source;
157 FT_Error error;
158
159
160 if ( !FT_STREAM_SEEK( 0 )( ( error = (FT_Stream_Seek( stream, 0 )) ) != 0 ) )
161 {
162 ft_lzwstate_reset( &zip->lzw );
163
164 zip->limit = zip->buffer + FT_LZW_BUFFER_SIZE4096;
165 zip->cursor = zip->limit;
166 zip->pos = 0;
167 }
168
169 return error;
170 }
171
172
173 static FT_Error
174 ft_lzw_file_fill_output( FT_LZWFile zip )
175 {
176 FT_LzwState lzw = &zip->lzw;
177 FT_ULong count;
178 FT_Error error = 0;
179
180
181 zip->cursor = zip->buffer;
182
183 count = ft_lzwstate_io( lzw, zip->buffer, FT_LZW_BUFFER_SIZE4096 );
184
185 zip->limit = zip->cursor + count;
186
187 if ( count == 0 )
188 error = LZW_Err_Invalid_Stream_Operation;
189
190 return error;
191 }
192
193
194 /* fill output buffer; `count' must be <= FT_LZW_BUFFER_SIZE */
195 static FT_Error
196 ft_lzw_file_skip_output( FT_LZWFile zip,
197 FT_ULong count )
198 {
199 FT_Error error = LZW_Err_Ok;
200
201
202 /* first, we skip what we can from the output buffer */
203 {
204 FT_ULong delta = (FT_ULong)( zip->limit - zip->cursor );
205
206
207 if ( delta >= count )
208 delta = count;
209
210 zip->cursor += delta;
211 zip->pos += delta;
212
213 count -= delta;
214 }
215
216 /* next, we skip as many bytes remaining as possible */
217 while ( count > 0 )
218 {
219 FT_ULong delta = FT_LZW_BUFFER_SIZE4096;
220 FT_ULong numread;
221
222
223 if ( delta > count )
224 delta = count;
225
226 numread = ft_lzwstate_io( &zip->lzw, NULL((void*)0), delta );
227 if ( numread < delta )
228 {
229 /* not enough bytes */
230 error = LZW_Err_Invalid_Stream_Operation;
231 break;
232 }
233
234 zip->pos += delta;
235 count -= delta;
236 }
237
238 return error;
239 }
240
241
242 static FT_ULong
243 ft_lzw_file_io( FT_LZWFile zip,
244 FT_ULong pos,
245 FT_Byte* buffer,
246 FT_ULong count )
247 {
248 FT_ULong result = 0;
249 FT_Error error;
250
251
252 /* seeking backwards. */
253 if ( pos < zip->pos )
254 {
255 /* If the new position is within the output buffer, simply */
256 /* decrement pointers, otherwise we reset the stream completely! */
257 if ( ( zip->pos - pos ) <= (FT_ULong)( zip->cursor - zip->buffer ) )
258 {
259 zip->cursor -= zip->pos - pos;
260 zip->pos = pos;
261 }
262 else
263 {
264 error = ft_lzw_file_reset( zip );
265 if ( error )
266 goto Exit;
267 }
268 }
269
270 /* skip unwanted bytes */
271 if ( pos > zip->pos )
272 {
273 error = ft_lzw_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
274 if ( error )
275 goto Exit;
276 }
277
278 if ( count == 0 )
279 goto Exit;
280
281 /* now read the data */
282 for (;;)
283 {
284 FT_ULong delta;
285
286
287 delta = (FT_ULong)( zip->limit - zip->cursor );
288 if ( delta >= count )
289 delta = count;
290
291 FT_MEM_COPY( buffer + result, zip->cursor, delta )((__builtin_object_size (buffer + result, 0) != (size_t) -1) ?
__builtin___memcpy_chk (buffer + result, zip->cursor, delta
, __builtin_object_size (buffer + result, 0)) : __inline_memcpy_chk
(buffer + result, zip->cursor, delta))
;
292 result += delta;
293 zip->cursor += delta;
294 zip->pos += delta;
295
296 count -= delta;
297 if ( count == 0 )
298 break;
299
300 error = ft_lzw_file_fill_output( zip );
301 if ( error )
302 break;
303 }
304
305 Exit:
306 return result;
307 }
308
309
310/***************************************************************************/
311/***************************************************************************/
312/***** *****/
313/***** L Z W E M B E D D I N G S T R E A M *****/
314/***** *****/
315/***************************************************************************/
316/***************************************************************************/
317
318 static void
319 ft_lzw_stream_close( FT_Stream stream )
320 {
321 FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
322 FT_Memory memory = stream->memory;
323
324
325 if ( zip )
326 {
327 /* finalize lzw file descriptor */
328 ft_lzw_file_done( zip );
329
330 FT_FREE( zip )do { ft_mem_free( memory, (zip) ); (zip) = ((void*)0); } while
( 0 )
;
331
332 stream->descriptor.pointer = NULL((void*)0);
333 }
334 }
335
336
337 static FT_ULong
338 ft_lzw_stream_io( FT_Stream stream,
339 FT_ULong pos,
340 FT_Byte* buffer,
341 FT_ULong count )
342 {
343 FT_LZWFile zip = (FT_LZWFile)stream->descriptor.pointer;
344
345
346 return ft_lzw_file_io( zip, pos, buffer, count );
347 }
348
349
350 FT_EXPORT_DEF( FT_Error )extern FT_Error
351 FT_Stream_OpenLZW( FT_Stream stream,
352 FT_Stream source )
353 {
354 FT_Error error;
355 FT_Memory memory = source->memory;
356 FT_LZWFile zip;
357
358
359 /*
360 * Check the header right now; this prevents allocation of a huge
361 * LZWFile object (400 KByte of heap memory) if not necessary.
362 *
363 * Did I mention that you should never use .Z compressed font
364 * files?
365 */
366 error = ft_lzw_check_header( source );
367 if ( error )
368 goto Exit;
369
370 FT_ZERO( stream )((__builtin_object_size (stream, 0) != (size_t) -1) ? __builtin___memset_chk
(stream, 0, sizeof ( *(stream) ), __builtin_object_size (stream
, 0)) : __inline_memset_chk (stream, 0, sizeof ( *(stream) ))
)
;
371 stream->memory = memory;
372
373 if ( !FT_NEW( zip )( ((zip) = (ft_mem_alloc( memory, (sizeof ( *(zip) )), &error
))), error != 0 )
)
374 {
375 error = ft_lzw_file_init( zip, stream, source );
376 if ( error )
377 {
378 FT_FREE( zip )do { ft_mem_free( memory, (zip) ); (zip) = ((void*)0); } while
( 0 )
;
379 goto Exit;
380 }
381
382 stream->descriptor.pointer = zip;
383 }
384
385 stream->size = 0x7FFFFFFFL; /* don't know the real size! */
386 stream->pos = 0;
387 stream->base = 0;
388 stream->read = ft_lzw_stream_io;
389 stream->close = ft_lzw_stream_close;
390
391 Exit:
392 return error;
393 }
394
395
396#include "ftzopen.c"
397
398
399#else /* !FT_CONFIG_OPTION_USE_LZW */
400
401
402 FT_EXPORT_DEF( FT_Error )extern FT_Error
403 FT_Stream_OpenLZW( FT_Stream stream,
404 FT_Stream source )
405 {
406 FT_UNUSED( stream )( (stream) = (stream) );
407 FT_UNUSED( source )( (source) = (source) );
408
409 return LZW_Err_Unimplemented_Feature;
410 }
411
412
413#endif /* !FT_CONFIG_OPTION_USE_LZW */
414
415
416/* END */