jdk/src/share/native/java/util/zip/zlib-1.2.3/gzio.c
author sherman
Tue, 25 Aug 2009 15:14:53 -0700
changeset 3704 bd98409d8c32
child 5506 202f599c92aa
permissions -rw-r--r--
4963968: zlib should be upgraded to current version of zlib Summary: upgrade zlib to the latest ver 1.2.3 Reviewed-by: martin, alanb, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3704
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     1
/*
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     3
 *
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
     9
 *
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    14
 * accompanied this code).
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    15
 *
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    19
 *
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    22
 * have any questions.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    23
 */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    24
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    25
/* gzio.c -- IO on .gz files
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    26
 * Copyright (C) 1995-2005 Jean-loup Gailly.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    27
 * For conditions of distribution and use, see copyright notice in zlib.h
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    28
 *
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    29
 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    30
 */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    31
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    32
/* @(#) $Id$ */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    33
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    34
#include <stdio.h>
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    35
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    36
#include "zutil.h"
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    37
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    38
#ifdef NO_DEFLATE       /* for compatibility with old definition */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    39
#  define NO_GZCOMPRESS
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    40
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    41
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    42
#ifndef NO_DUMMY_DECL
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    43
struct internal_state {int dummy;}; /* for buggy compilers */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    44
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    45
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    46
#ifndef Z_BUFSIZE
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    47
#  ifdef MAXSEG_64K
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    48
#    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    49
#  else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    50
#    define Z_BUFSIZE 16384
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    51
#  endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    52
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    53
#ifndef Z_PRINTF_BUFSIZE
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    54
#  define Z_PRINTF_BUFSIZE 4096
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    55
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    56
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    57
#ifdef __MVS__
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    58
#  pragma map (fdopen , "\174\174FDOPEN")
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    59
   FILE *fdopen(int, const char *);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    60
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    61
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    62
#ifndef STDC
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    63
extern voidp  malloc OF((uInt size));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    64
extern void   free   OF((voidpf ptr));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    65
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    66
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    67
#define ALLOC(size) malloc(size)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    68
#define TRYFREE(p) {if (p) free(p);}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    69
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    70
static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    71
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    72
/* gzip flag byte */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    73
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    74
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    75
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    76
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    77
#define COMMENT      0x10 /* bit 4 set: file comment present */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    78
#define RESERVED     0xE0 /* bits 5..7: reserved */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    79
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    80
typedef struct gz_stream {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    81
    z_stream stream;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    82
    int      z_err;   /* error code for last stream operation */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    83
    int      z_eof;   /* set if end of input file */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    84
    FILE     *file;   /* .gz file */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    85
    Byte     *inbuf;  /* input buffer */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    86
    Byte     *outbuf; /* output buffer */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    87
    uLong    crc;     /* crc32 of uncompressed data */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    88
    char     *msg;    /* error message */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    89
    char     *path;   /* path name for debugging only */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    90
    int      transparent; /* 1 if input file is not a .gz file */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    91
    char     mode;    /* 'w' or 'r' */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    92
    z_off_t  start;   /* start of compressed data in file (header skipped) */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    93
    z_off_t  in;      /* bytes into deflate or inflate */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    94
    z_off_t  out;     /* bytes out of deflate or inflate */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    95
    int      back;    /* one character push-back */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    96
    int      last;    /* true if push-back is last character */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    97
} gz_stream;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    98
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
    99
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   100
local gzFile gz_open      OF((const char *path, const char *mode, int  fd));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   101
local int do_flush        OF((gzFile file, int flush));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   102
local int    get_byte     OF((gz_stream *s));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   103
local void   check_header OF((gz_stream *s));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   104
local int    destroy      OF((gz_stream *s));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   105
local void   putLong      OF((FILE *file, uLong x));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   106
local uLong  getLong      OF((gz_stream *s));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   107
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   108
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   109
     Opens a gzip (.gz) file for reading or writing. The mode parameter
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   110
   is as in fopen ("rb" or "wb"). The file is given either by file descriptor
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   111
   or path name (if fd == -1).
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   112
     gz_open returns NULL if the file could not be opened or if there was
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   113
   insufficient memory to allocate the (de)compression state; errno
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   114
   can be checked to distinguish the two cases (if errno is zero, the
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   115
   zlib error is Z_MEM_ERROR).
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   116
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   117
local gzFile gz_open (path, mode, fd)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   118
    const char *path;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   119
    const char *mode;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   120
    int  fd;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   121
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   122
    int err;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   123
    int level = Z_DEFAULT_COMPRESSION; /* compression level */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   124
    int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   125
    char *p = (char*)mode;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   126
    gz_stream *s;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   127
    char fmode[80]; /* copy of mode, without the compression level */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   128
    char *m = fmode;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   129
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   130
    if (!path || !mode) return Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   131
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   132
    s = (gz_stream *)ALLOC(sizeof(gz_stream));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   133
    if (!s) return Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   134
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   135
    s->stream.zalloc = (alloc_func)0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   136
    s->stream.zfree = (free_func)0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   137
    s->stream.opaque = (voidpf)0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   138
    s->stream.next_in = s->inbuf = Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   139
    s->stream.next_out = s->outbuf = Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   140
    s->stream.avail_in = s->stream.avail_out = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   141
    s->file = NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   142
    s->z_err = Z_OK;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   143
    s->z_eof = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   144
    s->in = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   145
    s->out = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   146
    s->back = EOF;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   147
    s->crc = crc32(0L, Z_NULL, 0);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   148
    s->msg = NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   149
    s->transparent = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   150
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   151
    s->path = (char*)ALLOC(strlen(path)+1);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   152
    if (s->path == NULL) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   153
        return destroy(s), (gzFile)Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   154
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   155
    strcpy(s->path, path); /* do this early for debugging */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   156
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   157
    s->mode = '\0';
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   158
    do {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   159
        if (*p == 'r') s->mode = 'r';
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   160
        if (*p == 'w' || *p == 'a') s->mode = 'w';
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   161
        if (*p >= '0' && *p <= '9') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   162
            level = *p - '0';
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   163
        } else if (*p == 'f') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   164
          strategy = Z_FILTERED;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   165
        } else if (*p == 'h') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   166
          strategy = Z_HUFFMAN_ONLY;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   167
        } else if (*p == 'R') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   168
          strategy = Z_RLE;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   169
        } else {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   170
            *m++ = *p; /* copy the mode */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   171
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   172
    } while (*p++ && m != fmode + sizeof(fmode));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   173
    if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   174
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   175
    if (s->mode == 'w') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   176
#ifdef NO_GZCOMPRESS
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   177
        err = Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   178
#else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   179
        err = deflateInit2(&(s->stream), level,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   180
                           Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   181
        /* windowBits is passed < 0 to suppress zlib header */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   182
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   183
        s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   184
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   185
        if (err != Z_OK || s->outbuf == Z_NULL) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   186
            return destroy(s), (gzFile)Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   187
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   188
    } else {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   189
        s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   190
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   191
        err = inflateInit2(&(s->stream), -MAX_WBITS);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   192
        /* windowBits is passed < 0 to tell that there is no zlib header.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   193
         * Note that in this case inflate *requires* an extra "dummy" byte
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   194
         * after the compressed stream in order to complete decompression and
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   195
         * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   196
         * present after the compressed stream.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   197
         */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   198
        if (err != Z_OK || s->inbuf == Z_NULL) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   199
            return destroy(s), (gzFile)Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   200
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   201
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   202
    s->stream.avail_out = Z_BUFSIZE;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   203
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   204
    errno = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   205
    s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   206
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   207
    if (s->file == NULL) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   208
        return destroy(s), (gzFile)Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   209
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   210
    if (s->mode == 'w') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   211
        /* Write a very simple .gz header:
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   212
         */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   213
        fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   214
             Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   215
        s->start = 10L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   216
        /* We use 10L instead of ftell(s->file) to because ftell causes an
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   217
         * fflush on some systems. This version of the library doesn't use
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   218
         * start anyway in write mode, so this initialization is not
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   219
         * necessary.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   220
         */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   221
    } else {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   222
        check_header(s); /* skip the .gz header */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   223
        s->start = ftell(s->file) - s->stream.avail_in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   224
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   225
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   226
    return (gzFile)s;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   227
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   228
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   229
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   230
     Opens a gzip (.gz) file for reading or writing.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   231
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   232
gzFile ZEXPORT gzopen (path, mode)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   233
    const char *path;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   234
    const char *mode;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   235
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   236
    return gz_open (path, mode, -1);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   237
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   238
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   239
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   240
     Associate a gzFile with the file descriptor fd. fd is not dup'ed here
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   241
   to mimic the behavio(u)r of fdopen.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   242
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   243
gzFile ZEXPORT gzdopen (fd, mode)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   244
    int fd;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   245
    const char *mode;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   246
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   247
    char name[46];      /* allow for up to 128-bit integers */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   248
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   249
    if (fd < 0) return (gzFile)Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   250
    sprintf(name, "<fd:%d>", fd); /* for debugging */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   251
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   252
    return gz_open (name, mode, fd);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   253
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   254
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   255
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   256
 * Update the compression level and strategy
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   257
 */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   258
int ZEXPORT gzsetparams (file, level, strategy)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   259
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   260
    int level;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   261
    int strategy;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   262
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   263
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   264
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   265
    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   266
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   267
    /* Make room to allow flushing */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   268
    if (s->stream.avail_out == 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   269
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   270
        s->stream.next_out = s->outbuf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   271
        if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   272
            s->z_err = Z_ERRNO;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   273
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   274
        s->stream.avail_out = Z_BUFSIZE;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   275
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   276
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   277
    return deflateParams (&(s->stream), level, strategy);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   278
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   279
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   280
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   281
     Read a byte from a gz_stream; update next_in and avail_in. Return EOF
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   282
   for end of file.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   283
   IN assertion: the stream s has been sucessfully opened for reading.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   284
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   285
local int get_byte(s)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   286
    gz_stream *s;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   287
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   288
    if (s->z_eof) return EOF;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   289
    if (s->stream.avail_in == 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   290
        errno = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   291
        s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   292
        if (s->stream.avail_in == 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   293
            s->z_eof = 1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   294
            if (ferror(s->file)) s->z_err = Z_ERRNO;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   295
            return EOF;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   296
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   297
        s->stream.next_in = s->inbuf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   298
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   299
    s->stream.avail_in--;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   300
    return *(s->stream.next_in)++;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   301
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   302
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   303
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   304
      Check the gzip header of a gz_stream opened for reading. Set the stream
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   305
    mode to transparent if the gzip magic header is not present; set s->err
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   306
    to Z_DATA_ERROR if the magic header is present but the rest of the header
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   307
    is incorrect.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   308
    IN assertion: the stream s has already been created sucessfully;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   309
       s->stream.avail_in is zero for the first time, but may be non-zero
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   310
       for concatenated .gz files.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   311
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   312
local void check_header(s)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   313
    gz_stream *s;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   314
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   315
    int method; /* method byte */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   316
    int flags;  /* flags byte */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   317
    uInt len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   318
    int c;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   319
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   320
    /* Assure two bytes in the buffer so we can peek ahead -- handle case
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   321
       where first byte of header is at the end of the buffer after the last
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   322
       gzip segment */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   323
    len = s->stream.avail_in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   324
    if (len < 2) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   325
        if (len) s->inbuf[0] = s->stream.next_in[0];
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   326
        errno = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   327
        len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   328
        if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   329
        s->stream.avail_in += len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   330
        s->stream.next_in = s->inbuf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   331
        if (s->stream.avail_in < 2) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   332
            s->transparent = s->stream.avail_in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   333
            return;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   334
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   335
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   336
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   337
    /* Peek ahead to check the gzip magic header */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   338
    if (s->stream.next_in[0] != gz_magic[0] ||
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   339
        s->stream.next_in[1] != gz_magic[1]) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   340
        s->transparent = 1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   341
        return;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   342
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   343
    s->stream.avail_in -= 2;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   344
    s->stream.next_in += 2;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   345
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   346
    /* Check the rest of the gzip header */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   347
    method = get_byte(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   348
    flags = get_byte(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   349
    if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   350
        s->z_err = Z_DATA_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   351
        return;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   352
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   353
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   354
    /* Discard time, xflags and OS code: */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   355
    for (len = 0; len < 6; len++) (void)get_byte(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   356
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   357
    if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   358
        len  =  (uInt)get_byte(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   359
        len += ((uInt)get_byte(s))<<8;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   360
        /* len is garbage if EOF but the loop below will quit anyway */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   361
        while (len-- != 0 && get_byte(s) != EOF) ;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   362
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   363
    if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   364
        while ((c = get_byte(s)) != 0 && c != EOF) ;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   365
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   366
    if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   367
        while ((c = get_byte(s)) != 0 && c != EOF) ;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   368
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   369
    if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   370
        for (len = 0; len < 2; len++) (void)get_byte(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   371
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   372
    s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   373
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   374
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   375
 /* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   376
 * Cleanup then free the given gz_stream. Return a zlib error code.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   377
   Try freeing in the reverse order of allocations.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   378
 */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   379
local int destroy (s)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   380
    gz_stream *s;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   381
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   382
    int err = Z_OK;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   383
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   384
    if (!s) return Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   385
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   386
    TRYFREE(s->msg);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   387
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   388
    if (s->stream.state != NULL) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   389
        if (s->mode == 'w') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   390
#ifdef NO_GZCOMPRESS
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   391
            err = Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   392
#else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   393
            err = deflateEnd(&(s->stream));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   394
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   395
        } else if (s->mode == 'r') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   396
            err = inflateEnd(&(s->stream));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   397
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   398
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   399
    if (s->file != NULL && fclose(s->file)) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   400
#ifdef ESPIPE
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   401
        if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   402
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   403
            err = Z_ERRNO;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   404
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   405
    if (s->z_err < 0) err = s->z_err;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   406
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   407
    TRYFREE(s->inbuf);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   408
    TRYFREE(s->outbuf);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   409
    TRYFREE(s->path);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   410
    TRYFREE(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   411
    return err;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   412
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   413
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   414
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   415
     Reads the given number of uncompressed bytes from the compressed file.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   416
   gzread returns the number of bytes actually read (0 for end of file).
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   417
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   418
int ZEXPORT gzread (file, buf, len)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   419
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   420
    voidp buf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   421
    unsigned len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   422
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   423
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   424
    Bytef *start = (Bytef*)buf; /* starting point for crc computation */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   425
    Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   426
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   427
    if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   428
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   429
    if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   430
    if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   431
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   432
    next_out = (Byte*)buf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   433
    s->stream.next_out = (Bytef*)buf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   434
    s->stream.avail_out = len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   435
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   436
    if (s->stream.avail_out && s->back != EOF) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   437
        *next_out++ = s->back;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   438
        s->stream.next_out++;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   439
        s->stream.avail_out--;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   440
        s->back = EOF;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   441
        s->out++;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   442
        start++;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   443
        if (s->last) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   444
            s->z_err = Z_STREAM_END;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   445
            return 1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   446
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   447
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   448
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   449
    while (s->stream.avail_out != 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   450
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   451
        if (s->transparent) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   452
            /* Copy first the lookahead bytes: */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   453
            uInt n = s->stream.avail_in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   454
            if (n > s->stream.avail_out) n = s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   455
            if (n > 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   456
                zmemcpy(s->stream.next_out, s->stream.next_in, n);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   457
                next_out += n;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   458
                s->stream.next_out = next_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   459
                s->stream.next_in   += n;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   460
                s->stream.avail_out -= n;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   461
                s->stream.avail_in  -= n;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   462
            }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   463
            if (s->stream.avail_out > 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   464
                s->stream.avail_out -=
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   465
                    (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   466
            }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   467
            len -= s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   468
            s->in  += len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   469
            s->out += len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   470
            if (len == 0) s->z_eof = 1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   471
            return (int)len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   472
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   473
        if (s->stream.avail_in == 0 && !s->z_eof) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   474
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   475
            errno = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   476
            s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   477
            if (s->stream.avail_in == 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   478
                s->z_eof = 1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   479
                if (ferror(s->file)) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   480
                    s->z_err = Z_ERRNO;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   481
                    break;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   482
                }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   483
            }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   484
            s->stream.next_in = s->inbuf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   485
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   486
        s->in += s->stream.avail_in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   487
        s->out += s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   488
        s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   489
        s->in -= s->stream.avail_in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   490
        s->out -= s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   491
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   492
        if (s->z_err == Z_STREAM_END) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   493
            /* Check CRC and original size */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   494
            s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   495
            start = s->stream.next_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   496
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   497
            if (getLong(s) != s->crc) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   498
                s->z_err = Z_DATA_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   499
            } else {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   500
                (void)getLong(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   501
                /* The uncompressed length returned by above getlong() may be
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   502
                 * different from s->out in case of concatenated .gz files.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   503
                 * Check for such files:
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   504
                 */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   505
                check_header(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   506
                if (s->z_err == Z_OK) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   507
                    inflateReset(&(s->stream));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   508
                    s->crc = crc32(0L, Z_NULL, 0);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   509
                }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   510
            }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   511
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   512
        if (s->z_err != Z_OK || s->z_eof) break;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   513
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   514
    s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   515
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   516
    if (len == s->stream.avail_out &&
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   517
        (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   518
        return -1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   519
    return (int)(len - s->stream.avail_out);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   520
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   521
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   522
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   523
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   524
      Reads one byte from the compressed file. gzgetc returns this byte
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   525
   or -1 in case of end of file or error.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   526
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   527
int ZEXPORT gzgetc(file)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   528
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   529
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   530
    unsigned char c;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   531
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   532
    return gzread(file, &c, 1) == 1 ? c : -1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   533
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   534
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   535
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   536
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   537
      Push one byte back onto the stream.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   538
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   539
int ZEXPORT gzungetc(c, file)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   540
    int c;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   541
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   542
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   543
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   544
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   545
    if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   546
    s->back = c;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   547
    s->out--;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   548
    s->last = (s->z_err == Z_STREAM_END);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   549
    if (s->last) s->z_err = Z_OK;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   550
    s->z_eof = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   551
    return c;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   552
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   553
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   554
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   555
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   556
      Reads bytes from the compressed file until len-1 characters are
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   557
   read, or a newline character is read and transferred to buf, or an
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   558
   end-of-file condition is encountered.  The string is then terminated
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   559
   with a null character.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   560
      gzgets returns buf, or Z_NULL in case of error.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   561
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   562
      The current implementation is not optimized at all.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   563
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   564
char * ZEXPORT gzgets(file, buf, len)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   565
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   566
    char *buf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   567
    int len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   568
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   569
    char *b = buf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   570
    if (buf == Z_NULL || len <= 0) return Z_NULL;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   571
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   572
    while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   573
    *buf = '\0';
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   574
    return b == buf && len > 0 ? Z_NULL : b;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   575
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   576
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   577
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   578
#ifndef NO_GZCOMPRESS
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   579
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   580
     Writes the given number of uncompressed bytes into the compressed file.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   581
   gzwrite returns the number of bytes actually written (0 in case of error).
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   582
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   583
int ZEXPORT gzwrite (file, buf, len)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   584
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   585
    voidpc buf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   586
    unsigned len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   587
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   588
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   589
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   590
    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   591
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   592
    s->stream.next_in = (Bytef*)buf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   593
    s->stream.avail_in = len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   594
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   595
    while (s->stream.avail_in != 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   596
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   597
        if (s->stream.avail_out == 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   598
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   599
            s->stream.next_out = s->outbuf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   600
            if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   601
                s->z_err = Z_ERRNO;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   602
                break;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   603
            }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   604
            s->stream.avail_out = Z_BUFSIZE;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   605
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   606
        s->in += s->stream.avail_in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   607
        s->out += s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   608
        s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   609
        s->in -= s->stream.avail_in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   610
        s->out -= s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   611
        if (s->z_err != Z_OK) break;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   612
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   613
    s->crc = crc32(s->crc, (const Bytef *)buf, len);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   614
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   615
    return (int)(len - s->stream.avail_in);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   616
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   617
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   618
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   619
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   620
     Converts, formats, and writes the args to the compressed file under
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   621
   control of the format string, as in fprintf. gzprintf returns the number of
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   622
   uncompressed bytes actually written (0 in case of error).
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   623
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   624
#ifdef STDC
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   625
#include <stdarg.h>
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   626
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   627
int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   628
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   629
    char buf[Z_PRINTF_BUFSIZE];
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   630
    va_list va;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   631
    int len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   632
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   633
    buf[sizeof(buf) - 1] = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   634
    va_start(va, format);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   635
#ifdef NO_vsnprintf
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   636
#  ifdef HAS_vsprintf_void
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   637
    (void)vsprintf(buf, format, va);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   638
    va_end(va);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   639
    for (len = 0; len < sizeof(buf); len++)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   640
        if (buf[len] == 0) break;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   641
#  else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   642
    len = vsprintf(buf, format, va);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   643
    va_end(va);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   644
#  endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   645
#else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   646
#  ifdef HAS_vsnprintf_void
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   647
    (void)vsnprintf(buf, sizeof(buf), format, va);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   648
    va_end(va);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   649
    len = strlen(buf);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   650
#  else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   651
    len = vsnprintf(buf, sizeof(buf), format, va);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   652
    va_end(va);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   653
#  endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   654
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   655
    if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   656
        return 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   657
    return gzwrite(file, buf, (unsigned)len);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   658
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   659
#else /* not ANSI C */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   660
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   661
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   662
                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   663
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   664
    const char *format;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   665
    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   666
        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   667
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   668
    char buf[Z_PRINTF_BUFSIZE];
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   669
    int len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   670
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   671
    buf[sizeof(buf) - 1] = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   672
#ifdef NO_snprintf
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   673
#  ifdef HAS_sprintf_void
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   674
    sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   675
            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   676
    for (len = 0; len < sizeof(buf); len++)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   677
        if (buf[len] == 0) break;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   678
#  else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   679
    len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   680
                a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   681
#  endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   682
#else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   683
#  ifdef HAS_snprintf_void
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   684
    snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   685
             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   686
    len = strlen(buf);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   687
#  else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   688
    len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   689
                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   690
#  endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   691
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   692
    if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   693
        return 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   694
    return gzwrite(file, buf, len);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   695
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   696
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   697
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   698
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   699
      Writes c, converted to an unsigned char, into the compressed file.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   700
   gzputc returns the value that was written, or -1 in case of error.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   701
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   702
int ZEXPORT gzputc(file, c)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   703
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   704
    int c;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   705
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   706
    unsigned char cc = (unsigned char) c; /* required for big endian systems */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   707
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   708
    return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   709
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   710
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   711
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   712
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   713
      Writes the given null-terminated string to the compressed file, excluding
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   714
   the terminating null character.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   715
      gzputs returns the number of characters written, or -1 in case of error.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   716
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   717
int ZEXPORT gzputs(file, s)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   718
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   719
    const char *s;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   720
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   721
    return gzwrite(file, (char*)s, (unsigned)strlen(s));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   722
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   723
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   724
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   725
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   726
     Flushes all pending output into the compressed file. The parameter
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   727
   flush is as in the deflate() function.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   728
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   729
local int do_flush (file, flush)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   730
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   731
    int flush;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   732
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   733
    uInt len;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   734
    int done = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   735
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   736
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   737
    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   738
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   739
    s->stream.avail_in = 0; /* should be zero already anyway */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   740
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   741
    for (;;) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   742
        len = Z_BUFSIZE - s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   743
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   744
        if (len != 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   745
            if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   746
                s->z_err = Z_ERRNO;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   747
                return Z_ERRNO;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   748
            }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   749
            s->stream.next_out = s->outbuf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   750
            s->stream.avail_out = Z_BUFSIZE;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   751
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   752
        if (done) break;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   753
        s->out += s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   754
        s->z_err = deflate(&(s->stream), flush);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   755
        s->out -= s->stream.avail_out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   756
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   757
        /* Ignore the second of two consecutive flushes: */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   758
        if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   759
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   760
        /* deflate has finished flushing only when it hasn't used up
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   761
         * all the available space in the output buffer:
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   762
         */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   763
        done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   764
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   765
        if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   766
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   767
    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   768
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   769
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   770
int ZEXPORT gzflush (file, flush)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   771
     gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   772
     int flush;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   773
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   774
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   775
    int err = do_flush (file, flush);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   776
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   777
    if (err) return err;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   778
    fflush(s->file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   779
    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   780
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   781
#endif /* NO_GZCOMPRESS */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   782
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   783
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   784
      Sets the starting position for the next gzread or gzwrite on the given
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   785
   compressed file. The offset represents a number of bytes in the
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   786
      gzseek returns the resulting offset location as measured in bytes from
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   787
   the beginning of the uncompressed stream, or -1 in case of error.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   788
      SEEK_END is not implemented, returns error.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   789
      In this version of the library, gzseek can be extremely slow.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   790
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   791
z_off_t ZEXPORT gzseek (file, offset, whence)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   792
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   793
    z_off_t offset;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   794
    int whence;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   795
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   796
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   797
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   798
    if (s == NULL || whence == SEEK_END ||
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   799
        s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   800
        return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   801
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   802
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   803
    if (s->mode == 'w') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   804
#ifdef NO_GZCOMPRESS
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   805
        return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   806
#else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   807
        if (whence == SEEK_SET) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   808
            offset -= s->in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   809
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   810
        if (offset < 0) return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   811
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   812
        /* At this point, offset is the number of zero bytes to write. */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   813
        if (s->inbuf == Z_NULL) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   814
            s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   815
            if (s->inbuf == Z_NULL) return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   816
            zmemzero(s->inbuf, Z_BUFSIZE);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   817
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   818
        while (offset > 0)  {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   819
            uInt size = Z_BUFSIZE;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   820
            if (offset < Z_BUFSIZE) size = (uInt)offset;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   821
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   822
            size = gzwrite(file, s->inbuf, size);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   823
            if (size == 0) return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   824
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   825
            offset -= size;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   826
        }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   827
        return s->in;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   828
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   829
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   830
    /* Rest of function is for reading only */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   831
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   832
    /* compute absolute position */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   833
    if (whence == SEEK_CUR) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   834
        offset += s->out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   835
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   836
    if (offset < 0) return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   837
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   838
    if (s->transparent) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   839
        /* map to fseek */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   840
        s->back = EOF;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   841
        s->stream.avail_in = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   842
        s->stream.next_in = s->inbuf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   843
        if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   844
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   845
        s->in = s->out = offset;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   846
        return offset;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   847
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   848
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   849
    /* For a negative seek, rewind and use positive seek */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   850
    if (offset >= s->out) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   851
        offset -= s->out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   852
    } else if (gzrewind(file) < 0) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   853
        return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   854
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   855
    /* offset is now the number of bytes to skip. */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   856
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   857
    if (offset != 0 && s->outbuf == Z_NULL) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   858
        s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   859
        if (s->outbuf == Z_NULL) return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   860
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   861
    if (offset && s->back != EOF) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   862
        s->back = EOF;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   863
        s->out++;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   864
        offset--;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   865
        if (s->last) s->z_err = Z_STREAM_END;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   866
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   867
    while (offset > 0)  {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   868
        int size = Z_BUFSIZE;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   869
        if (offset < Z_BUFSIZE) size = (int)offset;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   870
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   871
        size = gzread(file, s->outbuf, (uInt)size);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   872
        if (size <= 0) return -1L;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   873
        offset -= size;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   874
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   875
    return s->out;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   876
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   877
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   878
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   879
     Rewinds input file.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   880
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   881
int ZEXPORT gzrewind (file)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   882
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   883
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   884
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   885
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   886
    if (s == NULL || s->mode != 'r') return -1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   887
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   888
    s->z_err = Z_OK;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   889
    s->z_eof = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   890
    s->back = EOF;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   891
    s->stream.avail_in = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   892
    s->stream.next_in = s->inbuf;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   893
    s->crc = crc32(0L, Z_NULL, 0);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   894
    if (!s->transparent) (void)inflateReset(&s->stream);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   895
    s->in = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   896
    s->out = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   897
    return fseek(s->file, s->start, SEEK_SET);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   898
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   899
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   900
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   901
     Returns the starting position for the next gzread or gzwrite on the
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   902
   given compressed file. This position represents a number of bytes in the
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   903
   uncompressed data stream.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   904
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   905
z_off_t ZEXPORT gztell (file)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   906
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   907
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   908
    return gzseek(file, 0L, SEEK_CUR);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   909
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   910
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   911
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   912
     Returns 1 when EOF has previously been detected reading the given
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   913
   input stream, otherwise zero.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   914
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   915
int ZEXPORT gzeof (file)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   916
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   917
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   918
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   919
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   920
    /* With concatenated compressed files that can have embedded
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   921
     * crc trailers, z_eof is no longer the only/best indicator of EOF
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   922
     * on a gz_stream. Handle end-of-stream error explicitly here.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   923
     */
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   924
    if (s == NULL || s->mode != 'r') return 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   925
    if (s->z_eof) return 1;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   926
    return s->z_err == Z_STREAM_END;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   927
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   928
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   929
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   930
     Returns 1 if reading and doing so transparently, otherwise zero.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   931
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   932
int ZEXPORT gzdirect (file)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   933
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   934
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   935
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   936
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   937
    if (s == NULL || s->mode != 'r') return 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   938
    return s->transparent;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   939
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   940
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   941
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   942
   Outputs a long in LSB order to the given file
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   943
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   944
local void putLong (file, x)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   945
    FILE *file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   946
    uLong x;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   947
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   948
    int n;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   949
    for (n = 0; n < 4; n++) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   950
        fputc((int)(x & 0xff), file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   951
        x >>= 8;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   952
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   953
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   954
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   955
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   956
   Reads a long in LSB order from the given gz_stream. Sets z_err in case
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   957
   of error.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   958
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   959
local uLong getLong (s)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   960
    gz_stream *s;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   961
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   962
    uLong x = (uLong)get_byte(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   963
    int c;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   964
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   965
    x += ((uLong)get_byte(s))<<8;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   966
    x += ((uLong)get_byte(s))<<16;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   967
    c = get_byte(s);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   968
    if (c == EOF) s->z_err = Z_DATA_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   969
    x += ((uLong)c)<<24;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   970
    return x;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   971
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   972
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   973
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   974
     Flushes all pending output if necessary, closes the compressed file
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   975
   and deallocates all the (de)compression state.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   976
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   977
int ZEXPORT gzclose (file)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   978
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   979
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   980
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   981
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   982
    if (s == NULL) return Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   983
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   984
    if (s->mode == 'w') {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   985
#ifdef NO_GZCOMPRESS
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   986
        return Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   987
#else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   988
        if (do_flush (file, Z_FINISH) != Z_OK)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   989
            return destroy((gz_stream*)file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   990
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   991
        putLong (s->file, s->crc);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   992
        putLong (s->file, (uLong)(s->in & 0xffffffff));
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   993
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   994
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   995
    return destroy((gz_stream*)file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   996
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   997
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   998
#ifdef STDC
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
   999
#  define zstrerror(errnum) strerror(errnum)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1000
#else
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1001
#  define zstrerror(errnum) ""
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1002
#endif
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1003
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1004
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1005
     Returns the error message for the last error which occurred on the
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1006
   given compressed file. errnum is set to zlib error number. If an
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1007
   error occurred in the file system and not in the compression library,
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1008
   errnum is set to Z_ERRNO and the application may consult errno
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1009
   to get the exact error code.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1010
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1011
const char * ZEXPORT gzerror (file, errnum)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1012
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1013
    int *errnum;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1014
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1015
    char *m;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1016
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1017
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1018
    if (s == NULL) {
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1019
        *errnum = Z_STREAM_ERROR;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1020
        return (const char*)ERR_MSG(Z_STREAM_ERROR);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1021
    }
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1022
    *errnum = s->z_err;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1023
    if (*errnum == Z_OK) return (const char*)"";
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1024
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1025
    m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1026
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1027
    if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1028
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1029
    TRYFREE(s->msg);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1030
    s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1031
    if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1032
    strcpy(s->msg, s->path);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1033
    strcat(s->msg, ": ");
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1034
    strcat(s->msg, m);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1035
    return (const char*)s->msg;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1036
}
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1037
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1038
/* ===========================================================================
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1039
     Clear the error and end-of-file flags, and do the same for the real file.
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1040
*/
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1041
void ZEXPORT gzclearerr (file)
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1042
    gzFile file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1043
{
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1044
    gz_stream *s = (gz_stream*)file;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1045
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1046
    if (s == NULL) return;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1047
    if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1048
    s->z_eof = 0;
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1049
    clearerr(s->file);
bd98409d8c32 4963968: zlib should be upgraded to current version of zlib
sherman
parents:
diff changeset
  1050
}