misc/libfreetype/src/gzip/ftgzip.c
author koda
Wed, 06 Mar 2013 12:12:02 +0100
changeset 8666 1652c1d9adc8
parent 5172 88f2e05288ba
permissions -rw-r--r--
rework ffmpeg/libav/videorec linking and their cmake discovery
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
/*  ftgzip.c                                                               */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
/*    FreeType support for .gz compressed files.                           */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
/*  This optional component relies on zlib.  It should mainly be used to   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
/*  parse compressed PCF fonts, as found with many X11 server              */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
/*  distributions.                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
/*  Copyright 2002-2006, 2009-2011 by                                      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
/*  This file is part of the FreeType project, and may only be used,       */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
/*  modified, and distributed under the terms of the FreeType project      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
/*  this file you indicate that you have read the license and              */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
/*  understand and accept it fully.                                        */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
/*                                                                         */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
#include <ft2build.h>
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
#include FT_INTERNAL_MEMORY_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
#include FT_INTERNAL_STREAM_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
#include FT_INTERNAL_DEBUG_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
#include FT_GZIP_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
#include FT_CONFIG_STANDARD_LIBRARY_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
#include FT_MODULE_ERRORS_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
#undef __FTERRORS_H__
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
#define FT_ERR_PREFIX  Gzip_Err_
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
#define FT_ERR_BASE    FT_Mod_Err_Gzip
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
#include FT_ERRORS_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
#ifdef FT_CONFIG_OPTION_USE_ZLIB
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
#ifdef FT_CONFIG_OPTION_PIC
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
#error "gzip code does not support PIC yet"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
#endif 
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
#ifdef FT_CONFIG_OPTION_SYSTEM_ZLIB
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
#include <zlib.h>
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
#else /* !FT_CONFIG_OPTION_SYSTEM_ZLIB */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
 /* In this case, we include our own modified sources of the ZLib    */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
 /* within the "ftgzip" component.  The modifications were necessary */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
 /* to #include all files without conflicts, as well as preventing   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
 /* the definition of "extern" functions that may cause linking      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
 /* conflicts when a program is linked with both FreeType and the    */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
 /* original ZLib.                                                   */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
#define NO_DUMMY_DECL
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
#ifndef USE_ZLIB_ZCALLOC
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
#define MY_ZCALLOC /* prevent all zcalloc() & zfree() in zutils.c */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
#include "zlib.h"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
#undef  SLOW
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
#define SLOW  1  /* we can't use asm-optimized sources here! */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
  /* Urgh.  `inflate_mask' must not be declared twice -- C++ doesn't like
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
     this.  We temporarily disable it and load all necessary header files. */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
#define NO_INFLATE_MASK
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
#include "zutil.h"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
#include "inftrees.h"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
#include "infblock.h"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
#include "infcodes.h"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
#include "infutil.h"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
#undef  NO_INFLATE_MASK
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    80
  /* infutil.c must be included before infcodes.c */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    81
#include "zutil.c"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    82
#include "inftrees.c"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    83
#include "infutil.c"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    84
#include "infcodes.c"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    85
#include "infblock.c"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    86
#include "inflate.c"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
#include "adler32.c"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
#endif /* !FT_CONFIG_OPTION_SYSTEM_ZLIB */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
/*****                                                                 *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
/*****            Z L I B   M E M O R Y   M A N A G E M E N T          *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
/*****                                                                 *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    99
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   100
  /* it is better to use FreeType memory routines instead of raw
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
     'malloc/free' */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
  static voidpf
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   104
  ft_gzip_alloc( FT_Memory  memory,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   105
                 uInt       items,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
                 uInt       size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
    FT_ULong    sz = (FT_ULong)size * items;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
    FT_Error    error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
    FT_Pointer  p  = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
    (void)FT_ALLOC( p, sz );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
    return p;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
  ft_gzip_free( FT_Memory  memory,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
                voidpf     address )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
    FT_MEM_FREE( address );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   124
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   125
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   126
#if !defined( FT_CONFIG_OPTION_SYSTEM_ZLIB ) && !defined( USE_ZLIB_ZCALLOC )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   127
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
  local voidpf
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
  zcalloc ( voidpf    opaque,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
            unsigned  items,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
            unsigned  size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   132
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   133
    return ft_gzip_alloc( (FT_Memory)opaque, items, size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   134
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   135
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   136
  local void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   137
  zcfree( voidpf  opaque,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   138
          voidpf  ptr )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   139
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   140
    ft_gzip_free( (FT_Memory)opaque, ptr );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   141
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   142
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   143
#endif /* !SYSTEM_ZLIB && !USE_ZLIB_ZCALLOC */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   144
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   145
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   146
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   147
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   148
/*****                                                                 *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   149
/*****               Z L I B   F I L E   D E S C R I P T O R           *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   150
/*****                                                                 *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   151
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   152
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   153
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   154
#define FT_GZIP_BUFFER_SIZE  4096
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   155
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   156
  typedef struct  FT_GZipFileRec_
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   157
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   158
    FT_Stream  source;         /* parent/source stream        */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   159
    FT_Stream  stream;         /* embedding stream            */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   160
    FT_Memory  memory;         /* memory allocator            */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   161
    z_stream   zstream;        /* zlib input stream           */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   162
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   163
    FT_ULong   start;          /* starting position, after .gz header */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   164
    FT_Byte    input[FT_GZIP_BUFFER_SIZE];   /* input read buffer  */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   165
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   166
    FT_Byte    buffer[FT_GZIP_BUFFER_SIZE];  /* output buffer      */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   167
    FT_ULong   pos;                          /* position in output */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   168
    FT_Byte*   cursor;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   169
    FT_Byte*   limit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   170
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   171
  } FT_GZipFileRec, *FT_GZipFile;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   173
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   174
  /* gzip flag byte */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   175
#define FT_GZIP_ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   176
#define FT_GZIP_HEAD_CRC     0x02 /* bit 1 set: header CRC present */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   177
#define FT_GZIP_EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   178
#define FT_GZIP_ORIG_NAME    0x08 /* bit 3 set: original file name present */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   179
#define FT_GZIP_COMMENT      0x10 /* bit 4 set: file comment present */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   180
#define FT_GZIP_RESERVED     0xE0 /* bits 5..7: reserved */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   181
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   182
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   183
  /* check and skip .gz header - we don't support `transparent' compression */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   184
  static FT_Error
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   185
  ft_gzip_check_header( FT_Stream  stream )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   186
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   187
    FT_Error  error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   188
    FT_Byte   head[4];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   189
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   190
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   191
    if ( FT_STREAM_SEEK( 0 )       ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   192
         FT_STREAM_READ( head, 4 ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   193
      goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   194
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   195
    /* head[0] && head[1] are the magic numbers;    */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   196
    /* head[2] is the method, and head[3] the flags */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   197
    if ( head[0] != 0x1f              ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   198
         head[1] != 0x8b              ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   199
         head[2] != Z_DEFLATED        ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   200
        (head[3] & FT_GZIP_RESERVED)  )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   201
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   202
      error = Gzip_Err_Invalid_File_Format;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   203
      goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   204
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   205
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   206
    /* skip time, xflags and os code */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   207
    (void)FT_STREAM_SKIP( 6 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   208
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   209
    /* skip the extra field */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   210
    if ( head[3] & FT_GZIP_EXTRA_FIELD )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   211
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   212
      FT_UInt  len;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   213
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   214
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   215
      if ( FT_READ_USHORT_LE( len ) ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   216
           FT_STREAM_SKIP( len )    )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   217
        goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   218
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   219
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   220
    /* skip original file name */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   221
    if ( head[3] & FT_GZIP_ORIG_NAME )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   222
      for (;;)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   223
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   224
        FT_UInt  c;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   225
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   226
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   227
        if ( FT_READ_BYTE( c ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   228
          goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   229
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   230
        if ( c == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   231
          break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   232
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   233
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   234
    /* skip .gz comment */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   235
    if ( head[3] & FT_GZIP_COMMENT )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   236
      for (;;)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   237
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   238
        FT_UInt  c;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   239
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   240
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   241
        if ( FT_READ_BYTE( c ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   242
          goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   243
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   244
        if ( c == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   245
          break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   246
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   247
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   248
    /* skip CRC */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   249
    if ( head[3] & FT_GZIP_HEAD_CRC )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   250
      if ( FT_STREAM_SKIP( 2 ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   251
        goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   252
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   253
  Exit:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   254
    return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   255
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   256
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   257
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   258
  static FT_Error
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   259
  ft_gzip_file_init( FT_GZipFile  zip,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   260
                     FT_Stream    stream,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   261
                     FT_Stream    source )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   262
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   263
    z_stream*  zstream = &zip->zstream;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   264
    FT_Error   error   = Gzip_Err_Ok;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   265
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   266
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   267
    zip->stream = stream;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   268
    zip->source = source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   269
    zip->memory = stream->memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   270
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   271
    zip->limit  = zip->buffer + FT_GZIP_BUFFER_SIZE;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   272
    zip->cursor = zip->limit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   273
    zip->pos    = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   274
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   275
    /* check and skip .gz header */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   276
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   277
      stream = source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   278
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   279
      error = ft_gzip_check_header( stream );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   280
      if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   281
        goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   282
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   283
      zip->start = FT_STREAM_POS();
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   284
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   285
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   286
    /* initialize zlib -- there is no zlib header in the compressed stream */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   287
    zstream->zalloc = (alloc_func)ft_gzip_alloc;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   288
    zstream->zfree  = (free_func) ft_gzip_free;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   289
    zstream->opaque = stream->memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   290
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   291
    zstream->avail_in = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   292
    zstream->next_in  = zip->buffer;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   293
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   294
    if ( inflateInit2( zstream, -MAX_WBITS ) != Z_OK ||
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   295
         zstream->next_in == NULL                     )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   296
      error = Gzip_Err_Invalid_File_Format;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   297
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   298
  Exit:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   299
    return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   300
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   301
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   302
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   303
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   304
  ft_gzip_file_done( FT_GZipFile  zip )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   305
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   306
    z_stream*  zstream = &zip->zstream;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   307
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   308
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   309
    inflateEnd( zstream );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   310
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   311
    /* clear the rest */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   312
    zstream->zalloc    = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   313
    zstream->zfree     = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   314
    zstream->opaque    = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   315
    zstream->next_in   = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   316
    zstream->next_out  = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   317
    zstream->avail_in  = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   318
    zstream->avail_out = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   319
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   320
    zip->memory = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   321
    zip->source = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   322
    zip->stream = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   323
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   324
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   325
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   326
  static FT_Error
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   327
  ft_gzip_file_reset( FT_GZipFile  zip )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   328
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   329
    FT_Stream  stream = zip->source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   330
    FT_Error   error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   331
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   332
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   333
    if ( !FT_STREAM_SEEK( zip->start ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   334
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   335
      z_stream*  zstream = &zip->zstream;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   336
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   337
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   338
      inflateReset( zstream );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   339
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   340
      zstream->avail_in  = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   341
      zstream->next_in   = zip->input;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   342
      zstream->avail_out = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   343
      zstream->next_out  = zip->buffer;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   344
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   345
      zip->limit  = zip->buffer + FT_GZIP_BUFFER_SIZE;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   346
      zip->cursor = zip->limit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   347
      zip->pos    = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   348
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   349
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   350
    return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   351
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   352
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   353
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   354
  static FT_Error
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   355
  ft_gzip_file_fill_input( FT_GZipFile  zip )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   356
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   357
    z_stream*  zstream = &zip->zstream;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   358
    FT_Stream  stream  = zip->source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   359
    FT_ULong   size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   360
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   361
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   362
    if ( stream->read )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   363
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   364
      size = stream->read( stream, stream->pos, zip->input,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   365
                           FT_GZIP_BUFFER_SIZE );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   366
      if ( size == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   367
        return Gzip_Err_Invalid_Stream_Operation;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   368
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   369
    else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   370
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   371
      size = stream->size - stream->pos;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   372
      if ( size > FT_GZIP_BUFFER_SIZE )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   373
        size = FT_GZIP_BUFFER_SIZE;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   374
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   375
      if ( size == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   376
        return Gzip_Err_Invalid_Stream_Operation;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   377
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   378
      FT_MEM_COPY( zip->input, stream->base + stream->pos, size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   379
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   380
    stream->pos += size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   381
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   382
    zstream->next_in  = zip->input;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   383
    zstream->avail_in = size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   384
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   385
    return Gzip_Err_Ok;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   386
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   387
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   388
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   389
  static FT_Error
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   390
  ft_gzip_file_fill_output( FT_GZipFile  zip )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   391
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   392
    z_stream*  zstream = &zip->zstream;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   393
    FT_Error   error   = Gzip_Err_Ok;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   394
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   395
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   396
    zip->cursor        = zip->buffer;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   397
    zstream->next_out  = zip->cursor;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   398
    zstream->avail_out = FT_GZIP_BUFFER_SIZE;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   399
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   400
    while ( zstream->avail_out > 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   401
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   402
      int  err;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   403
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   404
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   405
      if ( zstream->avail_in == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   406
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   407
        error = ft_gzip_file_fill_input( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   408
        if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   409
          break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   410
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   411
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   412
      err = inflate( zstream, Z_NO_FLUSH );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   413
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   414
      if ( err == Z_STREAM_END )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   415
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   416
        zip->limit = zstream->next_out;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   417
        if ( zip->limit == zip->cursor )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   418
          error = Gzip_Err_Invalid_Stream_Operation;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   419
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   420
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   421
      else if ( err != Z_OK )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   422
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   423
        error = Gzip_Err_Invalid_Stream_Operation;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   424
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   425
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   426
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   427
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   428
    return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   429
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   430
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   431
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   432
  /* fill output buffer; `count' must be <= FT_GZIP_BUFFER_SIZE */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   433
  static FT_Error
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   434
  ft_gzip_file_skip_output( FT_GZipFile  zip,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   435
                            FT_ULong     count )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   436
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   437
    FT_Error  error = Gzip_Err_Ok;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   438
    FT_ULong  delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   439
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   440
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   441
    for (;;)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   442
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   443
      delta = (FT_ULong)( zip->limit - zip->cursor );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   444
      if ( delta >= count )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   445
        delta = count;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   446
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   447
      zip->cursor += delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   448
      zip->pos    += delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   449
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   450
      count -= delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   451
      if ( count == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   452
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   453
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   454
      error = ft_gzip_file_fill_output( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   455
      if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   456
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   457
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   458
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   459
    return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   460
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   461
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   462
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   463
  static FT_ULong
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   464
  ft_gzip_file_io( FT_GZipFile  zip,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   465
                   FT_ULong     pos,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   466
                   FT_Byte*     buffer,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   467
                   FT_ULong     count )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   468
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   469
    FT_ULong  result = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   470
    FT_Error  error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   471
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   472
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   473
    /* Reset inflate stream if we're seeking backwards.        */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   474
    /* Yes, that is not too efficient, but it saves memory :-) */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   475
    if ( pos < zip->pos )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   476
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   477
      error = ft_gzip_file_reset( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   478
      if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   479
        goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   480
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   481
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   482
    /* skip unwanted bytes */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   483
    if ( pos > zip->pos )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   484
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   485
      error = ft_gzip_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   486
      if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   487
        goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   488
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   489
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   490
    if ( count == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   491
      goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   492
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   493
    /* now read the data */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   494
    for (;;)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   495
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   496
      FT_ULong  delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   497
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   498
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   499
      delta = (FT_ULong)( zip->limit - zip->cursor );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   500
      if ( delta >= count )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   501
        delta = count;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   502
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   503
      FT_MEM_COPY( buffer, zip->cursor, delta );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   504
      buffer      += delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   505
      result      += delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   506
      zip->cursor += delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   507
      zip->pos    += delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   508
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   509
      count -= delta;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   510
      if ( count == 0 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   511
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   512
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   513
      error = ft_gzip_file_fill_output( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   514
      if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   515
        break;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   516
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   517
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   518
  Exit:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   519
    return result;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   520
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   521
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   522
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   523
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   524
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   525
/*****                                                                 *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   526
/*****               G Z   E M B E D D I N G   S T R E A M             *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   527
/*****                                                                 *****/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   528
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   529
/***************************************************************************/
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   530
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   531
  static void
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   532
  ft_gzip_stream_close( FT_Stream  stream )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   533
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   534
    FT_GZipFile  zip    = (FT_GZipFile)stream->descriptor.pointer;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   535
    FT_Memory    memory = stream->memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   536
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   537
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   538
    if ( zip )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   539
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   540
      /* finalize gzip file descriptor */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   541
      ft_gzip_file_done( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   542
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   543
      FT_FREE( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   544
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   545
      stream->descriptor.pointer = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   546
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   547
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   548
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   549
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   550
  static FT_ULong
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   551
  ft_gzip_stream_io( FT_Stream  stream,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   552
                     FT_ULong   pos,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   553
                     FT_Byte*   buffer,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   554
                     FT_ULong   count )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   555
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   556
    FT_GZipFile  zip = (FT_GZipFile)stream->descriptor.pointer;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   557
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   558
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   559
    return ft_gzip_file_io( zip, pos, buffer, count );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   560
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   561
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   562
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   563
  static FT_ULong
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   564
  ft_gzip_get_uncompressed_size( FT_Stream  stream )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   565
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   566
    FT_Error  error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   567
    FT_ULong  old_pos;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   568
    FT_ULong  result = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   569
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   570
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   571
    old_pos = stream->pos;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   572
    if ( !FT_Stream_Seek( stream, stream->size - 4 ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   573
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   574
      result = FT_Stream_ReadULong( stream, &error );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   575
      if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   576
        result = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   577
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   578
      (void)FT_Stream_Seek( stream, old_pos );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   579
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   580
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   581
    return result;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   582
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   583
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   584
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   585
  FT_EXPORT_DEF( FT_Error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   586
  FT_Stream_OpenGzip( FT_Stream  stream,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   587
                      FT_Stream  source )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   588
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   589
    FT_Error     error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   590
    FT_Memory    memory = source->memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   591
    FT_GZipFile  zip;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   592
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   593
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   594
    /*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   595
     *  check the header right now; this prevents allocating un-necessary
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   596
     *  objects when we don't need them
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   597
     */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   598
    error = ft_gzip_check_header( source );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   599
    if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   600
      goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   601
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   602
    FT_ZERO( stream );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   603
    stream->memory = memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   604
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   605
    if ( !FT_QNEW( zip ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   606
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   607
      error = ft_gzip_file_init( zip, stream, source );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   608
      if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   609
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   610
        FT_FREE( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   611
        goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   612
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   613
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   614
      stream->descriptor.pointer = zip;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   615
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   616
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   617
    /*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   618
     *  We use the following trick to try to dramatically improve the
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   619
     *  performance while dealing with small files.  If the original stream
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   620
     *  size is less than a certain threshold, we try to load the whole font
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   621
     *  file into memory.  This saves us from using the 32KB buffer needed
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   622
     *  to inflate the file, plus the two 4KB intermediate input/output
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   623
     *  buffers used in the `FT_GZipFile' structure.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   624
     */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   625
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   626
      FT_ULong  zip_size = ft_gzip_get_uncompressed_size( source );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   627
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   628
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   629
      if ( zip_size != 0 && zip_size < 40 * 1024 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   630
      {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   631
        FT_Byte*  zip_buff;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   632
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   633
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   634
        if ( !FT_ALLOC( zip_buff, zip_size ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   635
        {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   636
          FT_ULong  count;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   637
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   638
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   639
          count = ft_gzip_file_io( zip, 0, zip_buff, zip_size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   640
          if ( count == zip_size )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   641
          {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   642
            ft_gzip_file_done( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   643
            FT_FREE( zip );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   644
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   645
            stream->descriptor.pointer = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   646
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   647
            stream->size  = zip_size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   648
            stream->pos   = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   649
            stream->base  = zip_buff;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   650
            stream->read  = NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   651
            stream->close = ft_gzip_stream_close;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   652
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   653
            goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   654
          }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   655
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   656
          ft_gzip_file_io( zip, 0, NULL, 0 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   657
          FT_FREE( zip_buff );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   658
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   659
        error = Gzip_Err_Ok;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   660
      }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   661
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   662
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   663
    stream->size  = 0x7FFFFFFFL;  /* don't know the real size! */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   664
    stream->pos   = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   665
    stream->base  = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   666
    stream->read  = ft_gzip_stream_io;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   667
    stream->close = ft_gzip_stream_close;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   668
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   669
  Exit:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   670
    return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   671
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   672
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   673
#else  /* !FT_CONFIG_OPTION_USE_ZLIB */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   674
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   675
  FT_EXPORT_DEF( FT_Error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   676
  FT_Stream_OpenGzip( FT_Stream  stream,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   677
                      FT_Stream  source )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   678
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   679
    FT_UNUSED( stream );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   680
    FT_UNUSED( source );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   681
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   682
    return Gzip_Err_Unimplemented_Feature;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   683
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   684
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   685
#endif /* !FT_CONFIG_OPTION_USE_ZLIB */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   686
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   687
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   688
/* END */