update for HEAD-2003050101
[reactos.git] / lib / freetype / src / gzip / ftgzip.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftgzip.c                                                               */
4 /*                                                                         */
5 /*    FreeType support for .gz compressed fileds                           */
6 /*                                                                         */
7 /*  this optional component relies on zlib. It should mainly be used to    */
8 /*  parse compressed PCF fonts, as found with many X11 server              */
9 /*  distributions.                                                         */
10 /*                                                                         */
11 /*  Copyright 2002 by                                                      */
12 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
13 /*                                                                         */
14 /*  This file is part of the FreeType project, and may only be used,       */
15 /*  modified, and distributed under the terms of the FreeType project      */
16 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
17 /*  this file you indicate that you have read the license and              */
18 /*  understand and accept it fully.                                        */
19 /*                                                                         */
20 /***************************************************************************/
21
22 #include <ft2build.h>
23 #include FT_INTERNAL_MEMORY_H
24 #include FT_INTERNAL_STREAM_H
25 #include FT_INTERNAL_DEBUG_H
26 #include <string.h>
27
28 #ifdef FT_CONFIG_OPTION_USE_ZLIB
29
30 #ifdef FT_CONFIG_OPTION_SYSTEM_ZLIB
31
32 #  include <zlib.h>
33
34 #else /* !SYSTEM_ZLIB */
35
36  /* in this case, we include our own modified sources of the ZLib   */
37  /* within the "ftgzip" component. The modifications were necessary */
38  /* to #include all files without conflicts, as well as preventing  */
39  /* the definition of "extern" functions that may cause linking     */
40  /* conflicts when a program is linked with both FreeType and the   */
41  /* original ZLib                                                   */
42
43 #  define  NO_DUMMY_DECL
44 #  define  BUILDFIXED    /* save code size */
45 #  define  MY_ZCALLOC
46
47 #  include "zlib.h"
48
49 #    undef   SLOW
50 #    define  SLOW  1  /* we can't use asm-optimized sources here !! */
51
52 #    include "zutil.c"
53 #    include "inftrees.c"
54 #    include "infcodes.c"
55 #    include "infutil.c"
56 #    include "infblock.c"
57 #    include "inflate.c"
58 #    include "adler32.c"
59
60 #endif /* !SYSTEM_ZLIB */
61
62
63 /***************************************************************************/
64 /***************************************************************************/
65 /*****                                                                 *****/
66 /*****            Z L I B   M E M O R Y   M A N A G E M E N T          *****/
67 /*****                                                                 *****/
68 /***************************************************************************/
69 /***************************************************************************/
70
71  /* it's better to use FreeType memory routines instead of raw 'malloc/free' */
72
73
74  static voidpf
75  ft_gzip_alloc( FT_Memory  memory,
76                 uInt       items,
77                 uInt       size )
78  {
79    FT_ULong    sz = (FT_ULong)size * items;
80    FT_Pointer  p;
81
82    FT_MEM_ALLOC( p, sz );
83
84    return (voidpf) p;
85  }
86
87
88  static void
89  ft_gzip_free( FT_Memory  memory,
90                voidpf     address )
91  {
92    FT_MEM_FREE( address );
93  }
94
95
96 #ifndef FT_CONFIG_OPTION_SYSTEM_ZLIB
97
98  local voidpf
99  zcalloc ( /* opaque, items, size) */
100     voidpf opaque,
101     unsigned items,
102     unsigned size )
103  {
104    return ft_gzip_alloc( opaque, items, size );
105  }
106
107  local void
108  zcfree( voidpf  opaque,
109          voidpf  ptr )
110  {
111    ft_gzip_free( opaque, ptr );
112  }
113
114 #endif /* !SYSTEM_ZLIB */
115
116
117 /***************************************************************************/
118 /***************************************************************************/
119 /*****                                                                 *****/
120 /*****               Z L I B   F I L E   D E S C R I P T O R           *****/
121 /*****                                                                 *****/
122 /***************************************************************************/
123 /***************************************************************************/
124
125 #define  FT_GZIP_BUFFER_SIZE          4096
126
127   typedef struct FT_GZipFileRec_
128   {
129     FT_Stream    source;         /* parent/source stream        */
130     FT_Stream    stream;         /* embedding stream            */
131     FT_Memory    memory;         /* memory allocator            */
132     z_stream     zstream;        /* zlib input stream           */
133
134     FT_ULong     start;          /* starting position, after .gz header */
135     FT_Byte      input[ FT_GZIP_BUFFER_SIZE ];  /* input read buffer */
136
137     FT_Byte      buffer[ FT_GZIP_BUFFER_SIZE ];  /* output buffer      */
138     FT_ULong     pos;                            /* position in output */
139     FT_Byte*     cursor;
140     FT_Byte*     limit;
141
142   } FT_GZipFileRec, *FT_GZipFile;
143
144
145 /* gzip flag byte */
146 #define FT_GZIP_ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
147 #define FT_GZIP_HEAD_CRC     0x02 /* bit 1 set: header CRC present */
148 #define FT_GZIP_EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
149 #define FT_GZIP_ORIG_NAME    0x08 /* bit 3 set: original file name present */
150 #define FT_GZIP_COMMENT      0x10 /* bit 4 set: file comment present */
151 #define FT_GZIP_RESERVED     0xE0 /* bits 5..7: reserved */
152
153
154  /* check and skip .gz header - we don't support "transparent" compression */
155   static FT_Error
156   ft_gzip_check_header( FT_Stream  stream )
157   {
158     FT_Error  error;
159     FT_Byte   head[4];
160
161     if ( FT_STREAM_SEEK( 0 )       ||
162          FT_STREAM_READ( head, 4 ) )
163       goto Exit;
164
165     /* head[0] && head[1] are the magic numbers     */
166     /* head[2] is the method, and head[3] the flags */
167     if ( head[0] != 0x1f              ||
168          head[1] != 0x8b              ||
169          head[2] != Z_DEFLATED        ||
170         (head[3] & FT_GZIP_RESERVED)  )
171     {
172       error = FT_Err_Invalid_File_Format;
173       goto Exit;
174     }
175
176     /* skip time, xflags and os code */
177     (void)FT_STREAM_SKIP( 6 );
178
179     /* skip the extra field */
180     if ( head[3] & FT_GZIP_EXTRA_FIELD )
181     {
182       FT_UInt  len;
183
184       if ( FT_READ_USHORT_LE( len ) ||
185            FT_STREAM_SKIP( len )    )
186         goto Exit;
187     }
188
189     /* skip original file name */
190     if ( head[3] & FT_GZIP_ORIG_NAME )
191       for (;;)
192       {
193         FT_UInt  c;
194
195         if ( FT_READ_BYTE( c) )
196           goto Exit;
197
198         if ( c == 0 )
199           break;
200       }
201
202     /* skip .gz comment */
203     if ( head[3] & FT_GZIP_COMMENT )
204       for (;;)
205       {
206         FT_UInt  c;
207
208         if ( FT_READ_BYTE( c) )
209           goto Exit;
210
211         if ( c == 0 )
212           break;
213       }
214
215     /* skip CRC */
216     if ( head[3] & FT_GZIP_HEAD_CRC )
217       if ( FT_STREAM_SKIP( 2 ) )
218         goto Exit;
219
220   Exit:
221     return error;
222   }
223
224
225
226   static FT_Error
227   ft_gzip_file_init( FT_GZipFile   zip,
228                      FT_Stream     stream,
229                      FT_Stream     source )
230   {
231     z_stream*  zstream = &zip->zstream;
232     FT_Error   error   = 0;
233
234     zip->stream = stream;
235     zip->source = source;
236     zip->memory = stream->memory;
237
238     zip->limit  = zip->buffer + FT_GZIP_BUFFER_SIZE;
239     zip->cursor = zip->limit;
240     zip->pos    = 0;
241
242     /* check and skip .gz header */
243     {
244       stream = source;
245
246       error = ft_gzip_check_header( stream );
247       if (error)
248         goto Exit;
249
250       zip->start = FT_STREAM_POS();
251     }
252
253     /* initialize zlib - there is no zlib header in the compressed stream */
254     zstream->zalloc = (alloc_func) ft_gzip_alloc;
255     zstream->zfree  = (free_func)  ft_gzip_free;
256     zstream->opaque = stream->memory;
257
258     zstream->avail_in = 0;
259     zstream->next_in  = zip->buffer;
260
261     if ( inflateInit2( zstream, -MAX_WBITS ) != Z_OK ||
262          zstream->next_in == NULL                     )
263     {
264       error = FT_Err_Invalid_File_Format;
265       goto Exit;
266     }
267
268   Exit:
269     return error;
270   }
271
272
273
274   static void
275   ft_gzip_file_done( FT_GZipFile  zip )
276   {
277     z_stream*  zstream = &zip->zstream;
278
279     inflateEnd( zstream );
280
281     /* clear the rest */
282     zstream->zalloc    = NULL;
283     zstream->zfree     = NULL;
284     zstream->opaque    = NULL;
285     zstream->next_in   = NULL;
286     zstream->next_out  = NULL;
287     zstream->avail_in  = 0;
288     zstream->avail_out = 0;
289
290     zip->memory = NULL;
291     zip->source = NULL;
292     zip->stream = NULL;
293   }
294
295
296   static FT_Error
297   ft_gzip_file_reset( FT_GZipFile  zip )
298   {
299     FT_Stream  stream = zip->source;
300     FT_Error   error;
301
302     if ( !FT_STREAM_SEEK( zip->start ) )
303     {
304       z_stream*  zstream = &zip->zstream;
305
306       inflateReset( zstream );
307
308       zstream->avail_in  = 0;
309       zstream->next_in   = zip->input;
310       zstream->avail_out = 0;
311       zstream->next_out  = zip->buffer;
312
313       zip->limit  = zip->buffer + FT_GZIP_BUFFER_SIZE;
314       zip->cursor = zip->limit;
315       zip->pos    = 0;
316     }
317     return  error;
318   }
319
320
321   static FT_Error
322   ft_gzip_file_fill_input( FT_GZipFile  zip )
323   {
324     z_stream*  zstream = &zip->zstream;
325     FT_Stream  stream  = zip->source;
326     FT_ULong   size;
327
328     if ( stream->read )
329     {
330       size = stream->read( stream, stream->pos, zip->input, FT_GZIP_BUFFER_SIZE );
331       if ( size == 0 )
332         return FT_Err_Invalid_Stream_Operation;
333     }
334     else
335     {
336       size = stream->size - stream->pos;
337       if ( size > FT_GZIP_BUFFER_SIZE )
338         size = FT_GZIP_BUFFER_SIZE;
339
340       if ( size == 0 )
341        return FT_Err_Invalid_Stream_Operation;
342
343       FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
344     }
345     stream->pos += size;
346
347     zstream->next_in  = zip->input;
348     zstream->avail_in = size;
349
350     return 0;
351   }
352
353
354
355   static FT_Error
356   ft_gzip_file_fill_output( FT_GZipFile  zip )
357   {
358     z_stream*  zstream = &zip->zstream;
359     FT_Error   error   = 0;
360
361     zip->cursor        = zip->buffer;
362     zstream->next_out  = zip->cursor;
363     zstream->avail_out = FT_GZIP_BUFFER_SIZE;
364
365     while ( zstream->avail_out > 0 )
366     {
367       int  err;
368
369       if ( zstream->avail_in == 0 )
370       {
371         error = ft_gzip_file_fill_input( zip );
372         if ( error )
373           break;
374       }
375
376       err = inflate( zstream, Z_NO_FLUSH );
377
378       if ( err == Z_STREAM_END )
379       {
380         zip->limit = zstream->next_out;
381         error      = FT_Err_Invalid_Stream_Operation;
382         break;
383       }
384       else if ( err != Z_OK )
385       {
386         error = FT_Err_Invalid_Stream_Operation;
387         break;
388       }
389     }
390     return error;
391   }
392
393
394  /* fill output buffer, 'count' must be <= FT_GZIP_BUFFER_SIZE */
395   static FT_Error
396   ft_gzip_file_skip_output( FT_GZipFile  zip,
397                             FT_ULong     count )
398   {
399     FT_Error   error   = 0;
400     FT_ULong   delta;
401
402     for (;;)
403     {
404       delta = (FT_ULong)( zip->limit - zip->cursor );
405       if ( delta >= count )
406         delta = count;
407
408       zip->cursor += delta;
409       zip->pos    += delta;
410
411       count -= delta;
412       if ( count == 0 )
413         break;
414
415       error = ft_gzip_file_fill_output( zip );
416       if ( error )
417         break;
418     }
419
420     return error;
421   }
422
423
424   static FT_ULong
425   ft_gzip_file_io( FT_GZipFile   zip,
426                    FT_ULong      pos,
427                    FT_Byte*      buffer,
428                    FT_ULong      count )
429   {
430     FT_ULong   result = 0;
431     FT_Error   error;
432
433     /* reset inflate stream if we're seeking backwards        */
434     /* yes, that's not too efficient, but it saves memory :-) */
435     if ( pos < zip->pos )
436     {
437       error = ft_gzip_file_reset( zip );
438       if ( error ) goto Exit;
439     }
440
441     /* skip unwanted bytes */
442     if ( pos > zip->pos )
443     {
444       error = ft_gzip_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
445       if (error)
446         goto Exit;
447     }
448
449     if ( count == 0 )
450       goto Exit;
451
452     /* now read the data */
453     for (;;)
454     {
455       FT_ULong   delta;
456
457       delta = (FT_ULong)( zip->limit - zip->cursor );
458       if ( delta >= count )
459         delta = count;
460
461       FT_MEM_COPY( buffer, zip->cursor, delta );
462       buffer      += delta;
463       result      += delta;
464       zip->cursor += delta;
465       zip->pos    += delta;
466
467       count -= delta;
468       if ( count == 0 )
469         break;
470
471       error = ft_gzip_file_fill_output( zip );
472       if (error)
473         break;
474     }
475
476   Exit:
477     return result;
478   }
479
480
481 /***************************************************************************/
482 /***************************************************************************/
483 /*****                                                                 *****/
484 /*****               G Z   E M B E D D I N G   S T R E A M             *****/
485 /*****                                                                 *****/
486 /***************************************************************************/
487 /***************************************************************************/
488
489   static void
490   ft_gzip_stream_close( FT_Stream  stream )
491   {
492     FT_GZipFile  zip    = stream->descriptor.pointer;
493     FT_Memory    memory = stream->memory;
494
495     if ( zip )
496     {
497       /* finalize gzip file descriptor */
498       ft_gzip_file_done( zip );
499
500       FT_FREE( zip );
501
502       stream->descriptor.pointer = NULL;
503     }
504   }
505
506
507   static FT_ULong
508   ft_gzip_stream_io( FT_Stream   stream,
509                      FT_ULong    pos,
510                      FT_Byte*    buffer,
511                      FT_ULong    count )
512   {
513     FT_GZipFile  zip = stream->descriptor.pointer;
514
515     return ft_gzip_file_io( zip, pos, buffer, count );
516   }
517
518
519   FT_EXPORT_DEF( FT_Error )
520   FT_Stream_OpenGzip( FT_Stream    stream,
521                       FT_Stream    source )
522   {
523     FT_Error     error;
524     FT_Memory    memory = source->memory;
525     FT_GZipFile  zip;
526
527     FT_ZERO( stream );
528     stream->memory = memory;
529
530     if ( !FT_NEW( zip ) )
531     {
532       error = ft_gzip_file_init( zip, stream, source );
533       if ( error )
534       {
535         FT_FREE( zip );
536         goto Exit;
537       }
538
539       stream->descriptor.pointer = zip;
540     }
541
542      stream->size = 0x7FFFFFFF;  /* don't know the real size !! */
543     stream->pos   = 0;
544     stream->base  = 0;
545     stream->read  = ft_gzip_stream_io;
546     stream->close = ft_gzip_stream_close;
547
548   Exit:
549     return error;
550   }
551
552 #else  /* !FT_CONFIG_OPTION_USE_ZLIB */
553
554   FT_EXPORT_DEF( FT_Error )
555   FT_Stream_OpenGzip( FT_Stream    stream,
556                       FT_Stream    source )
557   {
558     FT_UNUSED( stream );
559     FT_UNUSED( source );
560
561     return FT_Err_Unimplemented_Feature;
562   }
563
564 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */