os/ossrv/compressionlibs/ziplib/test/oldezlib/Zlib/gzio.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* gzio.c -- IO on .gz files
sl@0
     2
 * Copyright (C) 1995-1998 Jean-loup Gailly.
sl@0
     3
 * For conditions of distribution and use, see copyright notice in zlib.h
sl@0
     4
 *
sl@0
     5
 * Compile this file with -DNO_DEFLATE to avoid the compression code.
sl@0
     6
 */
sl@0
     7
sl@0
     8
/* @(#) $Id$ */
sl@0
     9
sl@0
    10
#include <stdio.h>
sl@0
    11
sl@0
    12
#include "zutil.h"
sl@0
    13
sl@0
    14
struct internal_state {int dummy;}; /* for buggy compilers */
sl@0
    15
sl@0
    16
#ifndef Z_BUFSIZE
sl@0
    17
#  ifdef MAXSEG_64K
sl@0
    18
#    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
sl@0
    19
#  else
sl@0
    20
#    define Z_BUFSIZE 16384
sl@0
    21
#  endif
sl@0
    22
#endif
sl@0
    23
#ifndef Z_PRINTF_BUFSIZE
sl@0
    24
#  define Z_PRINTF_BUFSIZE 4096
sl@0
    25
#endif
sl@0
    26
sl@0
    27
#define ALLOC(size) malloc(size)
sl@0
    28
#define TRYFREE(p) {if (p) free(p);}
sl@0
    29
sl@0
    30
static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
sl@0
    31
sl@0
    32
/* gzip flag byte */
sl@0
    33
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
sl@0
    34
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
sl@0
    35
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
sl@0
    36
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
sl@0
    37
#define COMMENT      0x10 /* bit 4 set: file comment present */
sl@0
    38
#define RESERVED     0xE0 /* bits 5..7: reserved */
sl@0
    39
sl@0
    40
typedef struct gz_stream {
sl@0
    41
    z_stream stream;
sl@0
    42
    int      z_err;   /* error code for last stream operation */
sl@0
    43
    int      z_eof;   /* set if end of input file */
sl@0
    44
    FILE     *file;   /* .gz file */
sl@0
    45
    Byte     *inbuf;  /* input buffer */
sl@0
    46
    Byte     *outbuf; /* output buffer */
sl@0
    47
    uLong    crc;     /* crc32 of uncompressed data */
sl@0
    48
    char     *msg;    /* error message */
sl@0
    49
    char     *path;   /* path name for debugging only */
sl@0
    50
    int      transparent; /* 1 if input file is not a .gz file */
sl@0
    51
    char     mode;    /* 'w' or 'r' */
sl@0
    52
    long     startpos; /* start of compressed data in file (header skipped) */
sl@0
    53
} gz_stream;
sl@0
    54
sl@0
    55
sl@0
    56
local gzFile gz_open      OF((const char *path, const char *mode, int  fd));
sl@0
    57
local int do_flush        OF((gzFile file, int flush));
sl@0
    58
local int    get_byte     OF((gz_stream *s));
sl@0
    59
local void   check_header OF((gz_stream *s));
sl@0
    60
local int    destroy      OF((gz_stream *s));
sl@0
    61
local void   putLong      OF((FILE *file, uLong x));
sl@0
    62
local uLong  getLong      OF((gz_stream *s));
sl@0
    63
sl@0
    64
/* ===========================================================================
sl@0
    65
     Opens a gzip (.gz) file for reading or writing. The mode parameter
sl@0
    66
   is as in fopen ("rb" or "wb"). The file is given either by file descriptor
sl@0
    67
   or path name (if fd == -1).
sl@0
    68
     gz_open return NULL if the file could not be opened or if there was
sl@0
    69
   insufficient memory to allocate the (de)compression state; errno
sl@0
    70
   can be checked to distinguish the two cases (if errno is zero, the
sl@0
    71
   zlib error is Z_MEM_ERROR).
sl@0
    72
*/
sl@0
    73
local gzFile gz_open (const char *path, const char *mode, int fd)
sl@0
    74
{
sl@0
    75
    int err;
sl@0
    76
    int level = Z_DEFAULT_COMPRESSION; /* compression level */
sl@0
    77
    int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
sl@0
    78
    char *p = (char*)mode;
sl@0
    79
    gz_stream *s;
sl@0
    80
    char fmode[80]; /* copy of mode, without the compression level */
sl@0
    81
    char *m = fmode;
sl@0
    82
sl@0
    83
    if (!path || !mode) return Z_NULL;
sl@0
    84
sl@0
    85
    s = (gz_stream *)ALLOC(sizeof(gz_stream));
sl@0
    86
    if (!s) return Z_NULL;
sl@0
    87
sl@0
    88
    s->stream.zalloc = (alloc_func)0;
sl@0
    89
    s->stream.zfree = (free_func)0;
sl@0
    90
    s->stream.opaque = (voidpf)0;
sl@0
    91
    s->stream.next_in = s->inbuf = Z_NULL;
sl@0
    92
    s->stream.next_out = s->outbuf = Z_NULL;
sl@0
    93
    s->stream.avail_in = s->stream.avail_out = 0;
sl@0
    94
    s->file = NULL;
sl@0
    95
    s->z_err = Z_OK;
sl@0
    96
    s->z_eof = 0;
sl@0
    97
    s->crc = crc32(0L, Z_NULL, 0);
sl@0
    98
    s->msg = NULL;
sl@0
    99
    s->transparent = 0;
sl@0
   100
sl@0
   101
    s->path = (char*)ALLOC(strlen(path)+1);
sl@0
   102
    if (s->path == NULL) {
sl@0
   103
        return destroy(s), (gzFile)Z_NULL;
sl@0
   104
    }
sl@0
   105
    strcpy(s->path, path); /* do this early for debugging */
sl@0
   106
sl@0
   107
    s->mode = '\0';
sl@0
   108
    do {
sl@0
   109
        if (*p == 'r') s->mode = 'r';
sl@0
   110
        if (*p == 'w' || *p == 'a') s->mode = 'w';
sl@0
   111
        if (*p >= '0' && *p <= '9') {
sl@0
   112
	    level = *p - '0';
sl@0
   113
	} else if (*p == 'f') {
sl@0
   114
	  strategy = Z_FILTERED;
sl@0
   115
	} else if (*p == 'h') {
sl@0
   116
	  strategy = Z_HUFFMAN_ONLY;
sl@0
   117
	} else {
sl@0
   118
	    *m++ = *p; /* copy the mode */
sl@0
   119
	}
sl@0
   120
    } while (*p++ && m != fmode + sizeof(fmode));
sl@0
   121
    if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
sl@0
   122
    
sl@0
   123
    if (s->mode == 'w') {
sl@0
   124
#ifdef NO_DEFLATE
sl@0
   125
        err = Z_STREAM_ERROR;
sl@0
   126
#else
sl@0
   127
        err = deflateInit2(&(s->stream), level,
sl@0
   128
                           Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
sl@0
   129
        /* windowBits is passed < 0 to suppress zlib header */
sl@0
   130
sl@0
   131
        s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
sl@0
   132
#endif
sl@0
   133
        if (err != Z_OK || s->outbuf == Z_NULL) {
sl@0
   134
            return destroy(s), (gzFile)Z_NULL;
sl@0
   135
        }
sl@0
   136
    } else {
sl@0
   137
        s->stream.next_in  = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
sl@0
   138
sl@0
   139
        err = inflateInit2(&(s->stream), -MAX_WBITS);
sl@0
   140
        /* windowBits is passed < 0 to tell that there is no zlib header.
sl@0
   141
         * Note that in this case inflate *requires* an extra "dummy" byte
sl@0
   142
         * after the compressed stream in order to complete decompression and
sl@0
   143
         * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
sl@0
   144
         * present after the compressed stream.
sl@0
   145
         */
sl@0
   146
        if (err != Z_OK || s->inbuf == Z_NULL) {
sl@0
   147
            return destroy(s), (gzFile)Z_NULL;
sl@0
   148
        }
sl@0
   149
    }
sl@0
   150
    s->stream.avail_out = Z_BUFSIZE;
sl@0
   151
sl@0
   152
    errno = 0;
sl@0
   153
    s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
sl@0
   154
sl@0
   155
    if (s->file == NULL) {
sl@0
   156
        return destroy(s), (gzFile)Z_NULL;
sl@0
   157
    }
sl@0
   158
    if (s->mode == 'w') {
sl@0
   159
        /* Write a very simple .gz header:
sl@0
   160
         */
sl@0
   161
        fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
sl@0
   162
             Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
sl@0
   163
	s->startpos = 10L;
sl@0
   164
	/* We use 10L instead of ftell(s->file) to because ftell causes an
sl@0
   165
         * fflush on some systems. This version of the library doesn't use
sl@0
   166
         * startpos anyway in write mode, so this initialization is not
sl@0
   167
         * necessary.
sl@0
   168
         */
sl@0
   169
    } else {
sl@0
   170
	check_header(s); /* skip the .gz header */
sl@0
   171
	s->startpos = (ftell(s->file) - s->stream.avail_in);
sl@0
   172
    }
sl@0
   173
    
sl@0
   174
    return (gzFile)s;
sl@0
   175
}
sl@0
   176
sl@0
   177
/* ===========================================================================
sl@0
   178
     Opens a gzip (.gz) file for reading or writing.
sl@0
   179
*/
sl@0
   180
gzFile ZEXPORT gzopen (const char *path, const char *mode)
sl@0
   181
{
sl@0
   182
    return gz_open (path, mode, -1);
sl@0
   183
}
sl@0
   184
sl@0
   185
/* ===========================================================================
sl@0
   186
     Associate a gzFile with the file descriptor fd. fd is not dup'ed here
sl@0
   187
   to mimic the behavio(u)r of fdopen.
sl@0
   188
*/
sl@0
   189
gzFile ZEXPORT gzdopen (int fd, const char *mode)
sl@0
   190
{
sl@0
   191
    char name[20];
sl@0
   192
sl@0
   193
    if (fd < 0) return (gzFile)Z_NULL;
sl@0
   194
    sprintf(name, "<fd:%d>", fd); /* for debugging */
sl@0
   195
sl@0
   196
    return gz_open (name, mode, fd);
sl@0
   197
}
sl@0
   198
sl@0
   199
/* ===========================================================================
sl@0
   200
 * Update the compression level and strategy
sl@0
   201
 */
sl@0
   202
int ZEXPORT gzsetparams (gzFile file, int level, int strategy)
sl@0
   203
{
sl@0
   204
    gz_stream *s = (gz_stream*)file;
sl@0
   205
sl@0
   206
    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
sl@0
   207
sl@0
   208
    /* Make room to allow flushing */
sl@0
   209
    if (s->stream.avail_out == 0) {
sl@0
   210
sl@0
   211
	s->stream.next_out = s->outbuf;
sl@0
   212
	if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
sl@0
   213
	    s->z_err = Z_ERRNO;
sl@0
   214
	}
sl@0
   215
	s->stream.avail_out = Z_BUFSIZE;
sl@0
   216
    }
sl@0
   217
sl@0
   218
    return deflateParams (&(s->stream), level, strategy);
sl@0
   219
}
sl@0
   220
sl@0
   221
/* ===========================================================================
sl@0
   222
     Read a byte from a gz_stream; update next_in and avail_in. Return EOF
sl@0
   223
   for end of file.
sl@0
   224
   IN assertion: the stream s has been sucessfully opened for reading.
sl@0
   225
*/
sl@0
   226
local int get_byte(gz_stream *s)
sl@0
   227
{
sl@0
   228
    if (s->z_eof) return EOF;
sl@0
   229
    if (s->stream.avail_in == 0) {
sl@0
   230
	errno = 0;
sl@0
   231
	s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
sl@0
   232
	if (s->stream.avail_in == 0) {
sl@0
   233
	    s->z_eof = 1;
sl@0
   234
	    if (ferror(s->file)) s->z_err = Z_ERRNO;
sl@0
   235
	    return EOF;
sl@0
   236
	}
sl@0
   237
	s->stream.next_in = s->inbuf;
sl@0
   238
    }
sl@0
   239
    s->stream.avail_in--;
sl@0
   240
    return *(s->stream.next_in)++;
sl@0
   241
}
sl@0
   242
sl@0
   243
/* ===========================================================================
sl@0
   244
      Check the gzip header of a gz_stream opened for reading. Set the stream
sl@0
   245
    mode to transparent if the gzip magic header is not present; set s->err
sl@0
   246
    to Z_DATA_ERROR if the magic header is present but the rest of the header
sl@0
   247
    is incorrect.
sl@0
   248
    IN assertion: the stream s has already been created sucessfully;
sl@0
   249
       s->stream.avail_in is zero for the first time, but may be non-zero
sl@0
   250
       for concatenated .gz files.
sl@0
   251
*/
sl@0
   252
local void check_header(gz_stream *s)
sl@0
   253
{
sl@0
   254
    int method; /* method byte */
sl@0
   255
    int flags;  /* flags byte */
sl@0
   256
    uInt len;
sl@0
   257
    int c;
sl@0
   258
sl@0
   259
    /* Check the gzip magic header */
sl@0
   260
    for (len = 0; len < 2; len++) {
sl@0
   261
	c = get_byte(s);
sl@0
   262
	if (c != gz_magic[len]) {
sl@0
   263
	    if (len != 0) s->stream.avail_in++, s->stream.next_in--;
sl@0
   264
	    if (c != EOF) {
sl@0
   265
		s->stream.avail_in++, s->stream.next_in--;
sl@0
   266
		s->transparent = 1;
sl@0
   267
	    }
sl@0
   268
	    s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
sl@0
   269
	    return;
sl@0
   270
	}
sl@0
   271
    }
sl@0
   272
    method = get_byte(s);
sl@0
   273
    flags = get_byte(s);
sl@0
   274
    if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
sl@0
   275
	s->z_err = Z_DATA_ERROR;
sl@0
   276
	return;
sl@0
   277
    }
sl@0
   278
sl@0
   279
    /* Discard time, xflags and OS code: */
sl@0
   280
    for (len = 0; len < 6; len++) (void)get_byte(s);
sl@0
   281
sl@0
   282
    if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
sl@0
   283
	len  =  (uInt)get_byte(s);
sl@0
   284
	len += ((uInt)get_byte(s))<<8;
sl@0
   285
	/* len is garbage if EOF but the loop below will quit anyway */
sl@0
   286
	while (len-- != 0 && get_byte(s) != EOF) ;
sl@0
   287
    }
sl@0
   288
    if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
sl@0
   289
	while ((c = get_byte(s)) != 0 && c != EOF) ;
sl@0
   290
    }
sl@0
   291
    if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
sl@0
   292
	while ((c = get_byte(s)) != 0 && c != EOF) ;
sl@0
   293
    }
sl@0
   294
    if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
sl@0
   295
	for (len = 0; len < 2; len++) (void)get_byte(s);
sl@0
   296
    }
sl@0
   297
    s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
sl@0
   298
}
sl@0
   299
sl@0
   300
 /* ===========================================================================
sl@0
   301
 * Cleanup then free the given gz_stream. Return a zlib error code.
sl@0
   302
   Try freeing in the reverse order of allocations.
sl@0
   303
 */
sl@0
   304
local int destroy (gz_stream *s)
sl@0
   305
{
sl@0
   306
    int err = Z_OK;
sl@0
   307
sl@0
   308
    if (!s) return Z_STREAM_ERROR;
sl@0
   309
sl@0
   310
    TRYFREE(s->msg);
sl@0
   311
sl@0
   312
    if (s->stream.state != NULL) {
sl@0
   313
	if (s->mode == 'w') {
sl@0
   314
#ifdef NO_DEFLATE
sl@0
   315
	    err = Z_STREAM_ERROR;
sl@0
   316
#else
sl@0
   317
	    err = deflateEnd(&(s->stream));
sl@0
   318
#endif
sl@0
   319
	} else if (s->mode == 'r') {
sl@0
   320
	    err = inflateEnd(&(s->stream));
sl@0
   321
	}
sl@0
   322
    }
sl@0
   323
    if (s->file != NULL && fclose(s->file)) {
sl@0
   324
#ifdef ESPIPE
sl@0
   325
	if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
sl@0
   326
#endif
sl@0
   327
	    err = Z_ERRNO;
sl@0
   328
    }
sl@0
   329
    if (s->z_err < 0) err = s->z_err;
sl@0
   330
sl@0
   331
    TRYFREE(s->inbuf);
sl@0
   332
    TRYFREE(s->outbuf);
sl@0
   333
    TRYFREE(s->path);
sl@0
   334
    TRYFREE(s);
sl@0
   335
    return err;
sl@0
   336
}
sl@0
   337
sl@0
   338
/* ===========================================================================
sl@0
   339
     Reads the given number of uncompressed bytes from the compressed file.
sl@0
   340
   gzread returns the number of bytes actually read (0 for end of file).
sl@0
   341
*/
sl@0
   342
int ZEXPORT gzread (gzFile file, voidp buf, unsigned len)
sl@0
   343
{
sl@0
   344
    gz_stream *s = (gz_stream*)file;
sl@0
   345
    Bytef *start = (Bytef*)buf; /* starting point for crc computation */
sl@0
   346
    Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
sl@0
   347
sl@0
   348
    if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
sl@0
   349
sl@0
   350
    if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
sl@0
   351
    if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
sl@0
   352
sl@0
   353
    next_out = (Byte*)buf;
sl@0
   354
    s->stream.next_out = (Bytef*)buf;
sl@0
   355
    s->stream.avail_out = len;
sl@0
   356
sl@0
   357
    while (s->stream.avail_out != 0) {
sl@0
   358
sl@0
   359
	if (s->transparent) {
sl@0
   360
	    /* Copy first the lookahead bytes: */
sl@0
   361
	    uInt n = s->stream.avail_in;
sl@0
   362
	    if (n > s->stream.avail_out) n = s->stream.avail_out;
sl@0
   363
	    if (n > 0) {
sl@0
   364
		zmemcpy(s->stream.next_out, s->stream.next_in, n);
sl@0
   365
		next_out += n;
sl@0
   366
		s->stream.next_out = next_out;
sl@0
   367
		s->stream.next_in   += n;
sl@0
   368
		s->stream.avail_out -= n;
sl@0
   369
		s->stream.avail_in  -= n;
sl@0
   370
	    }
sl@0
   371
	    if (s->stream.avail_out > 0) {
sl@0
   372
		s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
sl@0
   373
					     s->file);
sl@0
   374
	    }
sl@0
   375
	    len -= s->stream.avail_out;
sl@0
   376
	    s->stream.total_in  += (uLong)len;
sl@0
   377
	    s->stream.total_out += (uLong)len;
sl@0
   378
            if (len == 0) s->z_eof = 1;
sl@0
   379
	    return (int)len;
sl@0
   380
	}
sl@0
   381
        if (s->stream.avail_in == 0 && !s->z_eof) {
sl@0
   382
sl@0
   383
            errno = 0;
sl@0
   384
            s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
sl@0
   385
            if (s->stream.avail_in == 0) {
sl@0
   386
                s->z_eof = 1;
sl@0
   387
		if (ferror(s->file)) {
sl@0
   388
		    s->z_err = Z_ERRNO;
sl@0
   389
		    break;
sl@0
   390
		}
sl@0
   391
            }
sl@0
   392
            s->stream.next_in = s->inbuf;
sl@0
   393
        }
sl@0
   394
        s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
sl@0
   395
sl@0
   396
	if (s->z_err == Z_STREAM_END) {
sl@0
   397
	    /* Check CRC and original size */
sl@0
   398
	    s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
sl@0
   399
	    start = s->stream.next_out;
sl@0
   400
sl@0
   401
	    if (getLong(s) != s->crc) {
sl@0
   402
		s->z_err = Z_DATA_ERROR;
sl@0
   403
	    } else {
sl@0
   404
	        (void)getLong(s);
sl@0
   405
                /* The uncompressed length returned by above getlong() may
sl@0
   406
                 * be different from s->stream.total_out) in case of
sl@0
   407
		 * concatenated .gz files. Check for such files:
sl@0
   408
		 */
sl@0
   409
		check_header(s);
sl@0
   410
		if (s->z_err == Z_OK) {
sl@0
   411
		    uLong total_in = s->stream.total_in;
sl@0
   412
		    uLong total_out = s->stream.total_out;
sl@0
   413
sl@0
   414
		    inflateReset(&(s->stream));
sl@0
   415
		    s->stream.total_in = total_in;
sl@0
   416
		    s->stream.total_out = total_out;
sl@0
   417
		    s->crc = crc32(0L, Z_NULL, 0);
sl@0
   418
		}
sl@0
   419
	    }
sl@0
   420
	}
sl@0
   421
	if (s->z_err != Z_OK || s->z_eof) break;
sl@0
   422
    }
sl@0
   423
    s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
sl@0
   424
sl@0
   425
    return (int)(len - s->stream.avail_out);
sl@0
   426
}
sl@0
   427
sl@0
   428
sl@0
   429
/* ===========================================================================
sl@0
   430
      Reads one byte from the compressed file. gzgetc returns this byte
sl@0
   431
   or -1 in case of end of file or error.
sl@0
   432
*/
sl@0
   433
int ZEXPORT gzgetc(gzFile file)
sl@0
   434
{
sl@0
   435
    unsigned char c;
sl@0
   436
sl@0
   437
    return gzread(file, &c, 1) == 1 ? c : -1;
sl@0
   438
}
sl@0
   439
sl@0
   440
sl@0
   441
/* ===========================================================================
sl@0
   442
      Reads bytes from the compressed file until len-1 characters are
sl@0
   443
   read, or a newline character is read and transferred to buf, or an
sl@0
   444
   end-of-file condition is encountered.  The string is then terminated
sl@0
   445
   with a null character.
sl@0
   446
      gzgets returns buf, or Z_NULL in case of error.
sl@0
   447
sl@0
   448
      The current implementation is not optimized at all.
sl@0
   449
*/
sl@0
   450
char * ZEXPORT gzgets(gzFile file, char *buf, int len)
sl@0
   451
{
sl@0
   452
    char *b = buf;
sl@0
   453
    if (buf == Z_NULL || len <= 0) return Z_NULL;
sl@0
   454
sl@0
   455
    while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
sl@0
   456
    *buf = '\0';
sl@0
   457
    return b == buf && len > 0 ? Z_NULL : b;
sl@0
   458
}
sl@0
   459
sl@0
   460
sl@0
   461
#ifndef NO_DEFLATE
sl@0
   462
/* ===========================================================================
sl@0
   463
     Writes the given number of uncompressed bytes into the compressed file.
sl@0
   464
   gzwrite returns the number of bytes actually written (0 in case of error).
sl@0
   465
*/
sl@0
   466
int ZEXPORT gzwrite (gzFile file, const voidp buf, unsigned len)
sl@0
   467
{
sl@0
   468
    gz_stream *s = (gz_stream*)file;
sl@0
   469
sl@0
   470
    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
sl@0
   471
sl@0
   472
    s->stream.next_in = (Bytef*)buf;
sl@0
   473
    s->stream.avail_in = len;
sl@0
   474
sl@0
   475
    while (s->stream.avail_in != 0) {
sl@0
   476
sl@0
   477
        if (s->stream.avail_out == 0) {
sl@0
   478
sl@0
   479
            s->stream.next_out = s->outbuf;
sl@0
   480
            if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
sl@0
   481
                s->z_err = Z_ERRNO;
sl@0
   482
                break;
sl@0
   483
            }
sl@0
   484
            s->stream.avail_out = Z_BUFSIZE;
sl@0
   485
        }
sl@0
   486
        s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
sl@0
   487
        if (s->z_err != Z_OK) break;
sl@0
   488
    }
sl@0
   489
    s->crc = crc32(s->crc, (const Bytef *)buf, len);
sl@0
   490
sl@0
   491
    return (int)(len - s->stream.avail_in);
sl@0
   492
}
sl@0
   493
sl@0
   494
/* ===========================================================================
sl@0
   495
     Converts, formats, and writes the args to the compressed file under
sl@0
   496
   control of the format string, as in fprintf. gzprintf returns the number of
sl@0
   497
   uncompressed bytes actually written (0 in case of error).
sl@0
   498
*/
sl@0
   499
#ifdef STDC
sl@0
   500
#include <stdarg.h>
sl@0
   501
sl@0
   502
int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
sl@0
   503
{
sl@0
   504
    char buf[Z_PRINTF_BUFSIZE];
sl@0
   505
    va_list va;
sl@0
   506
    int len;
sl@0
   507
sl@0
   508
    va_start(va, format);
sl@0
   509
#ifdef HAS_vsnprintf
sl@0
   510
    (void)vsnprintf(buf, sizeof(buf), format, va);
sl@0
   511
#else
sl@0
   512
    (void)vsprintf(buf, format, va);
sl@0
   513
#endif
sl@0
   514
    va_end(va);
sl@0
   515
    len = strlen(buf); /* some *sprintf don't return the nb of bytes written */
sl@0
   516
    if (len <= 0) return 0;
sl@0
   517
sl@0
   518
    return gzwrite(file, buf, (unsigned)len);
sl@0
   519
}
sl@0
   520
#else /* not ANSI C */
sl@0
   521
sl@0
   522
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
sl@0
   523
	               a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
sl@0
   524
    gzFile file;
sl@0
   525
    const char *format;
sl@0
   526
    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
sl@0
   527
	a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
sl@0
   528
{
sl@0
   529
    char buf[Z_PRINTF_BUFSIZE];
sl@0
   530
    int len;
sl@0
   531
sl@0
   532
#ifdef HAS_snprintf
sl@0
   533
    snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
sl@0
   534
	     a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
sl@0
   535
#else
sl@0
   536
    sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
sl@0
   537
	    a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
sl@0
   538
#endif
sl@0
   539
    len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */
sl@0
   540
    if (len <= 0) return 0;
sl@0
   541
sl@0
   542
    return gzwrite(file, buf, len);
sl@0
   543
}
sl@0
   544
#endif
sl@0
   545
sl@0
   546
/* ===========================================================================
sl@0
   547
      Writes c, converted to an unsigned char, into the compressed file.
sl@0
   548
   gzputc returns the value that was written, or -1 in case of error.
sl@0
   549
*/
sl@0
   550
int ZEXPORT gzputc(gzFile file, int c)
sl@0
   551
{
sl@0
   552
    unsigned char cc = (unsigned char) c; /* required for big endian systems */
sl@0
   553
sl@0
   554
    return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
sl@0
   555
}
sl@0
   556
sl@0
   557
sl@0
   558
/* ===========================================================================
sl@0
   559
      Writes the given null-terminated string to the compressed file, excluding
sl@0
   560
   the terminating null character.
sl@0
   561
      gzputs returns the number of characters written, or -1 in case of error.
sl@0
   562
*/
sl@0
   563
int ZEXPORT gzputs(gzFile file, const char *s)
sl@0
   564
{
sl@0
   565
    return gzwrite(file, (char*)s, (unsigned)strlen(s));
sl@0
   566
}
sl@0
   567
sl@0
   568
sl@0
   569
/* ===========================================================================
sl@0
   570
     Flushes all pending output into the compressed file. The parameter
sl@0
   571
   flush is as in the deflate() function.
sl@0
   572
*/
sl@0
   573
local int do_flush (gzFile file, int flush)
sl@0
   574
{
sl@0
   575
    uInt len;
sl@0
   576
    int done = 0;
sl@0
   577
    gz_stream *s = (gz_stream*)file;
sl@0
   578
sl@0
   579
    if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
sl@0
   580
sl@0
   581
    s->stream.avail_in = 0; /* should be zero already anyway */
sl@0
   582
sl@0
   583
    for (;;) {
sl@0
   584
        len = Z_BUFSIZE - s->stream.avail_out;
sl@0
   585
sl@0
   586
        if (len != 0) {
sl@0
   587
            if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
sl@0
   588
                s->z_err = Z_ERRNO;
sl@0
   589
                return Z_ERRNO;
sl@0
   590
            }
sl@0
   591
            s->stream.next_out = s->outbuf;
sl@0
   592
            s->stream.avail_out = Z_BUFSIZE;
sl@0
   593
        }
sl@0
   594
        if (done) break;
sl@0
   595
        s->z_err = deflate(&(s->stream), flush);
sl@0
   596
sl@0
   597
	/* Ignore the second of two consecutive flushes: */
sl@0
   598
	if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
sl@0
   599
sl@0
   600
        /* deflate has finished flushing only when it hasn't used up
sl@0
   601
         * all the available space in the output buffer: 
sl@0
   602
         */
sl@0
   603
        done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
sl@0
   604
 
sl@0
   605
        if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
sl@0
   606
    }
sl@0
   607
    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
sl@0
   608
}
sl@0
   609
sl@0
   610
int ZEXPORT gzflush (gzFile file, int flush)
sl@0
   611
{
sl@0
   612
    gz_stream *s = (gz_stream*)file;
sl@0
   613
    int err = do_flush (file, flush);
sl@0
   614
sl@0
   615
    if (err) return err;
sl@0
   616
    fflush(s->file);
sl@0
   617
    return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
sl@0
   618
}
sl@0
   619
#endif /* NO_DEFLATE */
sl@0
   620
sl@0
   621
/* ===========================================================================
sl@0
   622
      Sets the starting position for the next gzread or gzwrite on the given
sl@0
   623
   compressed file. The offset represents a number of bytes in the
sl@0
   624
      gzseek returns the resulting offset location as measured in bytes from
sl@0
   625
   the beginning of the uncompressed stream, or -1 in case of error.
sl@0
   626
      SEEK_END is not implemented, returns error.
sl@0
   627
      In this version of the library, gzseek can be extremely slow.
sl@0
   628
*/
sl@0
   629
z_off_t ZEXPORT gzseek (gzFile file, z_off_t offset, int whence)
sl@0
   630
{
sl@0
   631
    gz_stream *s = (gz_stream*)file;
sl@0
   632
sl@0
   633
    if (s == NULL || whence == SEEK_END ||
sl@0
   634
	s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
sl@0
   635
	return -1L;
sl@0
   636
    }
sl@0
   637
    
sl@0
   638
    if (s->mode == 'w') {
sl@0
   639
#ifdef NO_DEFLATE
sl@0
   640
	return -1L;
sl@0
   641
#else
sl@0
   642
	if (whence == SEEK_SET) {
sl@0
   643
	    offset -= s->stream.total_in;
sl@0
   644
	}
sl@0
   645
	if (offset < 0) return -1L;
sl@0
   646
sl@0
   647
	/* At this point, offset is the number of zero bytes to write. */
sl@0
   648
	if (s->inbuf == Z_NULL) {
sl@0
   649
	    s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
sl@0
   650
	    zmemzero(s->inbuf, Z_BUFSIZE);
sl@0
   651
	}
sl@0
   652
	while (offset > 0)  {
sl@0
   653
	    uInt size = Z_BUFSIZE;
sl@0
   654
	    if (offset < Z_BUFSIZE) size = (uInt)offset;
sl@0
   655
sl@0
   656
	    size = gzwrite(file, s->inbuf, size);
sl@0
   657
	    if (size == 0) return -1L;
sl@0
   658
sl@0
   659
	    offset -= size;
sl@0
   660
	}
sl@0
   661
	return (z_off_t)s->stream.total_in;
sl@0
   662
#endif
sl@0
   663
    }
sl@0
   664
    /* Rest of function is for reading only */
sl@0
   665
sl@0
   666
    /* compute absolute position */
sl@0
   667
    if (whence == SEEK_CUR) {
sl@0
   668
	offset += s->stream.total_out;
sl@0
   669
    }
sl@0
   670
    if (offset < 0) return -1L;
sl@0
   671
sl@0
   672
    if (s->transparent) {
sl@0
   673
	/* map to fseek */
sl@0
   674
	s->stream.avail_in = 0;
sl@0
   675
	s->stream.next_in = s->inbuf;
sl@0
   676
        if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
sl@0
   677
sl@0
   678
	s->stream.total_in = s->stream.total_out = (uLong)offset;
sl@0
   679
	return offset;
sl@0
   680
    }
sl@0
   681
sl@0
   682
    /* For a negative seek, rewind and use positive seek */
sl@0
   683
    if ((uLong)offset >= s->stream.total_out) {
sl@0
   684
	offset -= s->stream.total_out;
sl@0
   685
    } else if (gzrewind(file) < 0) {
sl@0
   686
	return -1L;
sl@0
   687
    }
sl@0
   688
    /* offset is now the number of bytes to skip. */
sl@0
   689
sl@0
   690
    if (offset != 0 && s->outbuf == Z_NULL) {
sl@0
   691
	s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
sl@0
   692
    }
sl@0
   693
    while (offset > 0)  {
sl@0
   694
	int size = Z_BUFSIZE;
sl@0
   695
	if (offset < Z_BUFSIZE) size = (int)offset;
sl@0
   696
sl@0
   697
	size = gzread(file, s->outbuf, (uInt)size);
sl@0
   698
	if (size <= 0) return -1L;
sl@0
   699
	offset -= size;
sl@0
   700
    }
sl@0
   701
    return (z_off_t)s->stream.total_out;
sl@0
   702
}
sl@0
   703
sl@0
   704
/* ===========================================================================
sl@0
   705
     Rewinds input file. 
sl@0
   706
*/
sl@0
   707
int ZEXPORT gzrewind (gzFile file)
sl@0
   708
{
sl@0
   709
    gz_stream *s = (gz_stream*)file;
sl@0
   710
    
sl@0
   711
    if (s == NULL || s->mode != 'r') return -1;
sl@0
   712
sl@0
   713
    s->z_err = Z_OK;
sl@0
   714
    s->z_eof = 0;
sl@0
   715
    s->stream.avail_in = 0;
sl@0
   716
    s->stream.next_in = s->inbuf;
sl@0
   717
    s->crc = crc32(0L, Z_NULL, 0);
sl@0
   718
	
sl@0
   719
    if (s->startpos == 0) { /* not a compressed file */
sl@0
   720
	rewind(s->file);
sl@0
   721
	return 0;
sl@0
   722
    }
sl@0
   723
sl@0
   724
    (void) inflateReset(&s->stream);
sl@0
   725
    return fseek(s->file, s->startpos, SEEK_SET);
sl@0
   726
}
sl@0
   727
sl@0
   728
/* ===========================================================================
sl@0
   729
     Returns the starting position for the next gzread or gzwrite on the
sl@0
   730
   given compressed file. This position represents a number of bytes in the
sl@0
   731
   uncompressed data stream.
sl@0
   732
*/
sl@0
   733
z_off_t ZEXPORT gztell (gzFile file)
sl@0
   734
{
sl@0
   735
    return gzseek(file, 0L, SEEK_CUR);
sl@0
   736
}
sl@0
   737
sl@0
   738
/* ===========================================================================
sl@0
   739
     Returns 1 when EOF has previously been detected reading the given
sl@0
   740
   input stream, otherwise zero.
sl@0
   741
*/
sl@0
   742
int ZEXPORT gzeof (gzFile file)
sl@0
   743
{
sl@0
   744
    gz_stream *s = (gz_stream*)file;
sl@0
   745
    
sl@0
   746
    return (s == NULL || s->mode != 'r') ? 0 : s->z_eof;
sl@0
   747
}
sl@0
   748
sl@0
   749
/* ===========================================================================
sl@0
   750
   Outputs a long in LSB order to the given file
sl@0
   751
*/
sl@0
   752
local void putLong (FILE *file, uLong x)
sl@0
   753
{
sl@0
   754
    int n;
sl@0
   755
    for (n = 0; n < 4; n++) {
sl@0
   756
        fputc((int)(x & 0xff), file);
sl@0
   757
        x >>= 8;
sl@0
   758
    }
sl@0
   759
}
sl@0
   760
sl@0
   761
/* ===========================================================================
sl@0
   762
   Reads a long in LSB order from the given gz_stream. Sets z_err in case
sl@0
   763
   of error.
sl@0
   764
*/
sl@0
   765
local uLong getLong (gz_stream *s)
sl@0
   766
{
sl@0
   767
    uLong x = (uLong)get_byte(s);
sl@0
   768
    int c;
sl@0
   769
sl@0
   770
    x += ((uLong)get_byte(s))<<8;
sl@0
   771
    x += ((uLong)get_byte(s))<<16;
sl@0
   772
    c = get_byte(s);
sl@0
   773
    if (c == EOF) s->z_err = Z_DATA_ERROR;
sl@0
   774
    x += ((uLong)c)<<24;
sl@0
   775
    return x;
sl@0
   776
}
sl@0
   777
sl@0
   778
/* ===========================================================================
sl@0
   779
     Flushes all pending output if necessary, closes the compressed file
sl@0
   780
   and deallocates all the (de)compression state.
sl@0
   781
*/
sl@0
   782
int ZEXPORT gzclose (gzFile file)
sl@0
   783
{
sl@0
   784
    int err;
sl@0
   785
    gz_stream *s = (gz_stream*)file;
sl@0
   786
sl@0
   787
    if (s == NULL) return Z_STREAM_ERROR;
sl@0
   788
sl@0
   789
    if (s->mode == 'w') {
sl@0
   790
#ifdef NO_DEFLATE
sl@0
   791
	return Z_STREAM_ERROR;
sl@0
   792
#else
sl@0
   793
        err = do_flush (file, Z_FINISH);
sl@0
   794
        if (err != Z_OK) return destroy((gz_stream*)file);
sl@0
   795
sl@0
   796
        putLong (s->file, s->crc);
sl@0
   797
        putLong (s->file, s->stream.total_in);
sl@0
   798
#endif
sl@0
   799
    }
sl@0
   800
    return destroy((gz_stream*)file);
sl@0
   801
}
sl@0
   802
sl@0
   803
/* ===========================================================================
sl@0
   804
     Returns the error message for the last error which occured on the
sl@0
   805
   given compressed file. errnum is set to zlib error number. If an
sl@0
   806
   error occured in the file system and not in the compression library,
sl@0
   807
   errnum is set to Z_ERRNO and the application may consult errno
sl@0
   808
   to get the exact error code.
sl@0
   809
*/
sl@0
   810
const char*  ZEXPORT gzerror (gzFile file, int *errnum)
sl@0
   811
{
sl@0
   812
    char *m;
sl@0
   813
    gz_stream *s = (gz_stream*)file;
sl@0
   814
sl@0
   815
    if (s == NULL) {
sl@0
   816
        *errnum = Z_STREAM_ERROR;
sl@0
   817
        return (const char*)ERR_MSG(Z_STREAM_ERROR);
sl@0
   818
    }
sl@0
   819
    *errnum = s->z_err;
sl@0
   820
    if (*errnum == Z_OK) return (const char*)"";
sl@0
   821
sl@0
   822
    m =  (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
sl@0
   823
sl@0
   824
    if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
sl@0
   825
sl@0
   826
    TRYFREE(s->msg);
sl@0
   827
    s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
sl@0
   828
    strcpy(s->msg, s->path);
sl@0
   829
    strcat(s->msg, ": ");
sl@0
   830
    strcat(s->msg, m);
sl@0
   831
    return (const char*)s->msg;
sl@0
   832
}