[go: up one dir, main page]

File: gzmemio.cpp

package info (click to toggle)
coin3 3.1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 48,344 kB
  • ctags: 70,042
  • sloc: cpp: 314,328; ansic: 15,927; sh: 13,635; makefile: 8,780; perl: 2,149; lex: 1,302; lisp: 1,247; yacc: 184; xml: 175; sed: 68
file content (861 lines) | stat: -rw-r--r-- 25,461 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/*
   gzmemio.c, heavily based on gzio.c from zlib-1.1.4. Some cleanup
   and changes done by the Coin team to be able to read gzipped files
   from memory instead of from a FILE *. The zlib copyright notice is
   included below.

   Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler

   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
   arising from the use of this software.

   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.
   2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.

   Jean-loup Gailly        Mark Adler
   jloup@gzip.org          madler@alumni.caltech.edu
*/

#include "io/gzmemio.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "glue/zlib.h"

/* stuff copied from the zlib.h header file (we want to avoid
   including it here so that zlib is not required to compile Coin) */
struct internal_state;
typedef void * (*alloc_func)(void * opaque, unsigned int items, unsigned int size);
typedef void   (*free_func)(void * opaque, void * address);

#define Z_DEFAULT_COMPRESSION  (-1)
#define Z_DEFAULT_STRATEGY 0
#define Z_OK 0
#define Z_DATA_ERROR   (-3)
#define Z_STREAM_ERROR (-2)
#define Z_ERRNO        (-1)
#define Z_STREAM_END    1
#define Z_NO_FLUSH      0
#define Z_DEFLATED   8
#define MAX_WBITS   15 /* 32K LZ77 window */

/* This zlib struct should never change */
typedef struct {
  unsigned char * next_in;  /* next input byte */
  unsigned int avail_in;  /* number of bytes available at next_in */
  unsigned long total_in;  /* total nb of input bytes read so far */
  
  unsigned char * next_out; /* next output byte should be put there */
  unsigned int avail_out; /* remaining free space at next_out */
  unsigned long total_out; /* total nb of bytes output so far */
  
  char * msg;      /* last error message, NULL if no error */
  struct internal_state * state; /* not visible by applications */
  
  alloc_func zalloc;  /* used to allocate the internal state */
  free_func  zfree;   /* used to free the internal state */
  void * opaque;  /* private data object passed to zalloc and zfree */
  
  int data_type;  /* best guess about the data type: ascii or binary */
  unsigned long adler;      /* adler32 value of the uncompressed data */
  unsigned long reserved;   /* reserved for future use */
} z_stream;

/*
  Needed for inflateInit2().
*/
int 
cc_gzm_sizeof_z_stream(void)
{
  return sizeof(z_stream);
}

#define Z_BUFSIZE 16384
#define Z_NO_DEFLATE 1

#define Z_ALLOC(size) malloc(size)
#define Z_TRYFREE(p) {if (p) free(p);}

static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */

/* gzip flag byte */
#define Z_ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
#define Z_HEAD_CRC     0x02 /* bit 1 set: header CRC present */
#define Z_EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define Z_ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define Z_COMMENT      0x10 /* bit 4 set: file comment present */
#define Z_RESERVED     0xE0 /* bits 5..7: reserved */

typedef struct {
  unsigned char * buf;
  unsigned int buflen;
  unsigned int currpos;
} cc_gzm_file;

typedef struct {
  z_stream stream;
  int z_err;   /* error code for last stream operation */
  int z_eof;   /* set if end of input file */
  uint8_t * inbuf;  /* input buffer */
  uint8_t * outbuf; /* output buffer */
  uint32_t crc;     /* crc32 of uncompressed data */
  char * msg;    /* error message */
  char * path;   /* path name for debugging only */
  int transparent; /* 1 if input file is not a .gz file */
  char mode;    /* 'w' or 'r' */
  int32_t startpos; /* start of compressed data in file (header skipped) */

  /* memory file */
  cc_gzm_file * memfile;
} cc_gzm_stream;

static int get_byte(cc_gzm_stream * s);
static void check_header(cc_gzm_stream * s);
static int destroy(cc_gzm_stream * s);
static uint32_t getInt32(cc_gzm_stream * s);

static int cc_gzm_fseek(cc_gzm_file * file, long offset, int whence);
static int cc_gzm_ftell(cc_gzm_file * file);
static size_t cc_gzm_fread(void * ptr, size_t size, size_t nmemb, cc_gzm_file * file);
static int cc_gzm_ferror(cc_gzm_file * file);

/* ===========================================================================
     Opens a gzip (.gz) mem buffer for reading or writing.
*/

void * cc_gzm_open(const uint8_t * buffer, uint32_t len)
{
  int err;
  int level = Z_DEFAULT_COMPRESSION; /* compression level */
  int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
  cc_gzm_stream * s;


  s = (cc_gzm_stream *) Z_ALLOC(sizeof(cc_gzm_stream));
  if (!s) return NULL;

  s->stream.zalloc = (alloc_func)0;
  s->stream.zfree = (free_func)0;
  s->stream.opaque = (void *)0;
  s->stream.next_in = s->inbuf = NULL;
  s->stream.next_out = s->outbuf = NULL;
  s->stream.avail_in = s->stream.avail_out = 0;
  s->z_err = Z_OK;
  s->z_eof = 0;
  s->crc = cc_zlibglue_crc32(0L, NULL, 0);
  s->msg = NULL;
  s->transparent = 0;
  s->path = NULL;
  s->memfile = NULL;

  s->mode = 'r'; /* read */
  if (s->mode == 'w') { /* not supported yet */
#ifdef Z_NO_DEFLATE
    err = Z_STREAM_ERROR;
#else
    err = cc_zlibglue_deflateInit2(&(s->stream), level,
                                   Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
    /* windowBits is passed < 0 to suppress zlib header */

    s->stream.next_out = s->outbuf = (uint8_t*)Z_ALLOC(Z_BUFSIZE);
#endif
    if (err != Z_OK || s->outbuf == NULL) {
      destroy(s);
      return NULL;
    }
  }
  else {
    s->memfile = (cc_gzm_file*) Z_ALLOC(sizeof(cc_gzm_file));
    s->memfile->buf = (uint8_t*) buffer;
    s->memfile->buflen = len;
    s->memfile->currpos = 0;

    s->stream.next_in  = s->inbuf = (uint8_t*)Z_ALLOC(Z_BUFSIZE);
    
    err = cc_zlibglue_inflateInit2(&(s->stream), -MAX_WBITS);
    /* windowBits is passed < 0 to tell that there is no zlib header.
     * Note that in this case inflate *requires* an extra "dummy" byte
     * after the compressed stream in order to complete decompression and
     * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
     * present after the compressed stream.
     */
    if (err != Z_OK || s->inbuf == NULL) {
      destroy(s);
      return NULL;
    }
  }
  s->stream.avail_out = Z_BUFSIZE;

  if (s->mode == 'w') {
#ifndef Z_NO_DEFLATE
    /* Write a very simple .gz header:
     */
    fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
            Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
    s->startpos = 10L;
    /* We use 10L instead of ftell(s->file) to because ftell causes an
     * fflush on some systems. This version of the library doesn't use
     * startpos anyway in write mode, so this initialization is not
     * necessary.
     */
#endif /* Z_NO_DEFLATE */
  }
  else {
    check_header(s); /* skip the .gz header */
    s->startpos = (cc_gzm_ftell(s->memfile) - s->stream.avail_in);
  }
  return (void*) s;
}

/* ===========================================================================
     Read a byte from a cc_gzm_stream; update next_in and avail_in. Return EOF
   for end of file.
   IN assertion: the stream s has been sucessfully opened for reading.
*/
static int
get_byte(cc_gzm_stream * s)
{
  if (s->z_eof) return EOF;
  if (s->stream.avail_in == 0) {
    /* errno = 0; */
    s->stream.avail_in = cc_gzm_fread(s->inbuf, 1, Z_BUFSIZE, s->memfile);
    if (s->stream.avail_in == 0) {
      s->z_eof = 1;
      if (cc_gzm_ferror(s->memfile)) s->z_err = Z_ERRNO;
      return EOF;
    }
    s->stream.next_in = s->inbuf;
  }
  s->stream.avail_in--;
  return *(s->stream.next_in)++;
}

/* ===========================================================================
      Check the gzip header of a cc_gzm_stream opened for reading. Set the stream
    mode to transparent if the gzip magic header is not present; set s->err
    to Z_DATA_ERROR if the magic header is present but the rest of the header
    is incorrect.
    IN assertion: the stream s has already been created sucessfully;
       s->stream.avail_in is zero for the first time, but may be non-zero
       for concatenated .gz files.
*/
static void
check_header(cc_gzm_stream * s)
{
  int method; /* method byte */
  int flags;  /* flags byte */
  uint32_t len;
  int c;

  /* Check the gzip magic header */
  for (len = 0; len < 2; len++) {
    c = get_byte(s);
    if (c != gz_magic[len]) {
      if (len != 0) s->stream.avail_in++, s->stream.next_in--;
      if (c != EOF) {
        s->stream.avail_in++, s->stream.next_in--;
        s->transparent = 1;
      }
      s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
      return;
    }
  }
  method = get_byte(s);
  flags = get_byte(s);
  if (method != Z_DEFLATED || (flags & Z_RESERVED) != 0) {
    s->z_err = Z_DATA_ERROR;
    return;
  }

  /* Discard time, xflags and OS code: */
  for (len = 0; len < 6; len++) (void)get_byte(s);

  if ((flags & Z_EXTRA_FIELD) != 0) { /* skip the extra field */
    len  =  (uint32_t)get_byte(s);
    len += ((uint32_t)get_byte(s))<<8;
    /* len is garbage if EOF but the loop below will quit anyway */
    while (len-- != 0 && get_byte(s) != EOF) ;
  }
  if ((flags & Z_ORIG_NAME) != 0) { /* skip the original file name */
    while ((c = get_byte(s)) != 0 && c != EOF) ;
  }
  if ((flags & Z_COMMENT) != 0) {   /* skip the .gz file comment */
    while ((c = get_byte(s)) != 0 && c != EOF) ;
  }
  if ((flags & Z_HEAD_CRC) != 0) {  /* skip the header crc */
    for (len = 0; len < 2; len++) (void)get_byte(s);
  }
  s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
}

 /* ===========================================================================
 * Cleanup then free the given cc_gzm_stream. Return a zlib error code.
   Try freeing in the reverse order of allocations.
 */
static int destroy (cc_gzm_stream * s)
{
  int err = Z_OK;

  if (!s) return Z_STREAM_ERROR;

  Z_TRYFREE(s->msg);

  if (s->stream.state != NULL) {
    if (s->mode == 'w') {
#ifdef Z_NO_DEFLATE
      err = Z_STREAM_ERROR;
#else
      err = deflateEnd(&(s->stream));
#endif
    }
    else if (s->mode == 'r') {
      err = cc_zlibglue_inflateEnd(&(s->stream));
    }
  }

  if (s->z_err < 0) err = s->z_err;

  Z_TRYFREE(s->memfile);
  Z_TRYFREE(s->inbuf);
  Z_TRYFREE(s->outbuf);
  Z_TRYFREE(s->path);
  Z_TRYFREE(s);
  return err;
}

/* ===========================================================================
     Reads the given number of uncompressed bytes from the compressed file.
   gzread returns the number of bytes actually read (0 for end of file).
*/
int
cc_gzm_read (void * file, void * buf, uint32_t len)
{
  cc_gzm_stream *s = (cc_gzm_stream*)file;
  uint8_t *start = (uint8_t*)buf; /* starting point for crc computation */
  uint8_t * next_out; /* == stream.next_out but not forced far (for MSDOS) */

  if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;

  if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
  if (s->z_err == Z_STREAM_END) return 0;  /* EOF */

  next_out = (uint8_t*)buf;
  s->stream.next_out = (uint8_t*)buf;
  s->stream.avail_out = len;

  while (s->stream.avail_out != 0) {
    if (s->transparent) {
      /* Copy first the lookahead bytes: */
      uint32_t n = s->stream.avail_in;
      if (n > s->stream.avail_out) n = s->stream.avail_out;
      if (n > 0) {
        memcpy(s->stream.next_out, s->stream.next_in, n);
        next_out += n;
        s->stream.next_out = next_out;
        s->stream.next_in   += n;
        s->stream.avail_out -= n;
        s->stream.avail_in  -= n;
      }
      if (s->stream.avail_out > 0) {
        s->stream.avail_out -= cc_gzm_fread(next_out, 1, s->stream.avail_out,
                                         s->memfile);
      }
      len -= s->stream.avail_out;
      s->stream.total_in  += (uint32_t)len;
      s->stream.total_out += (uint32_t)len;
      if (len == 0) s->z_eof = 1;
      return (int)len;
    }
    if (s->stream.avail_in == 0 && !s->z_eof) {

      /* errno = 0; */
      s->stream.avail_in = cc_gzm_fread(s->inbuf, 1, Z_BUFSIZE, s->memfile);
      if (s->stream.avail_in == 0) {
        s->z_eof = 1;
        if (cc_gzm_ferror(s->memfile)) {
          s->z_err = Z_ERRNO;
          break;
        }
      }
      s->stream.next_in = s->inbuf;
    }
    s->z_err = cc_zlibglue_inflate(&(s->stream), Z_NO_FLUSH);

    if (s->z_err == Z_STREAM_END) {
      /* Check CRC and original size */
      s->crc = cc_zlibglue_crc32(s->crc, (const char*) start, (uint32_t)(s->stream.next_out - start));
      start = s->stream.next_out;

      if (getInt32(s) != s->crc) {
        s->z_err = Z_DATA_ERROR;
      } else {
        (void)getInt32(s);
        /* The uncompressed length returned by above getint32_t() may
         * be different from s->stream.total_out) in case of
         * concatenated .gz files. Check for such files:
         */
        check_header(s);
        if (s->z_err == Z_OK) {
          uint32_t total_in = s->stream.total_in;
          uint32_t total_out = s->stream.total_out;

          cc_zlibglue_inflateReset(&(s->stream));
          s->stream.total_in = total_in;
          s->stream.total_out = total_out;
          s->crc = cc_zlibglue_crc32(0L, NULL, 0);
        }
      }
    }
    if (s->z_err != Z_OK || s->z_eof) break;
  }
  s->crc = cc_zlibglue_crc32(s->crc, (const char*) start, (uint32_t)(s->stream.next_out - start));

  return (int)(len - s->stream.avail_out);
}

/* ===========================================================================
      Reads one byte from the compressed file. gzgetc returns this byte
   or -1 in case of end of file or error.
*/
int
cc_gzm_getc(void * file)
{
  unsigned char c;

  return cc_gzm_read(file, &c, 1) == 1 ? c : -1;
}

/* ===========================================================================
   Reads bytes from the compressed file until len-1 characters are
   read, or a newline character is read and transferred to buf, or an
   end-of-file condition is encountered.  The string is then terminated
   with a null character.
   gzgets returns buf, or NULL in case of error.

   The current implementation is not optimized at all.
*/
char *
cc_gzm_gets(void * file, char * buf, int len)
{
  char * b = buf;
  if (buf == NULL || len <= 0) return NULL;

  while (--len > 0 && cc_gzm_read(file, buf, 1) == 1 && *buf++ != '\n') ;
  *buf = '\0';
  return b == buf && len > 0 ? NULL : b;
}

#ifndef Z_NO_DEFLATE


/* ===========================================================================
 * Update the compression level and strategy
 */
int
cc_gzm_setparams(void * file, int level, int strategy)
{
  cc_gzm_stream *s = (cc_gzm_stream*)file;

  if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;

  /* Make room to allow flushing */
  if (s->stream.avail_out == 0) {
    s->stream.next_out = s->outbuf;
    if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
      s->z_err = Z_ERRNO;
    }
    s->stream.avail_out = Z_BUFSIZE;
  }
  return deflateParams (&(s->stream), level, strategy);
}

/* ===========================================================================
     Writes the given number of uncompressed bytes into the compressed file.
   gzwrite returns the number of bytes actually written (0 in case of error).
*/
int
cc_gzm_write(void * file, void * buf, unsigned int len)
{
  cc_gzm_stream *s = (cc_gzm_stream*)file;

  if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;

  s->stream.next_in = (uint8_t*)buf;
  s->stream.avail_in = len;

  while (s->stream.avail_in != 0) {

    if (s->stream.avail_out == 0) {

      s->stream.next_out = s->outbuf;
      if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
        s->z_err = Z_ERRNO;
        break;
      }
      s->stream.avail_out = Z_BUFSIZE;
    }
    s->z_err = cc_zlibglue_deflate(&(s->stream), Z_NO_FLUSH);
    if (s->z_err != Z_OK) break;
  }
  s->crc = cc_zlibglue_crc32(s->crc, (const uint8_t *)buf, len);

  return (int)(len - s->stream.avail_in);
}

/* ===========================================================================
      Writes c, converted to an unsigned char, into the compressed file.
   gzputc returns the value that was written, or -1 in case of error.
*/
int
cc_gzm_putc(void * file, int c)
{
  unsigned char cc = (unsigned char) c; /* required for big endian systems */
  return cc_gzm_write(file, &cc, 1) == 1 ? (int)cc : -1;
}

/* ===========================================================================
      Writes the given null-terminated string to the compressed file, excluding
   the terminating null character.
      gzputs returns the number of characters written, or -1 in case of error.
*/
int
cc_gzm_puts(void * file, const char * s)
{
  return gzwrite(file, (char*)s, (unsigned)strlen(s));
}


/* ===========================================================================
     Flushes all pending output into the compressed file. The parameter
     flush is as in the deflate() function.
*/
static int do_flush (void * file, int flush)
{
  uint32_t len;
  int done = 0;
  cc_gzm_stream *s = (cc_gzm_stream*)file;

  if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;

  s->stream.avail_in = 0; /* should be zero already anyway */

  for (;;) {
    len = Z_BUFSIZE - s->stream.avail_out;

    if (len != 0) {
      if ((uint32_t)fwrite(s->outbuf, 1, len, s->file) != len) {
        s->z_err = Z_ERRNO;
        return Z_ERRNO;
      }
      s->stream.next_out = s->outbuf;
      s->stream.avail_out = Z_BUFSIZE;
    }
    if (done) break;
    s->z_err = cc_zlibglue_deflate(&(s->stream), flush);

    /* Ignore the second of two consecutive flushes: */
    if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;

    /* deflate has finished flushing only when it hasn't used up
     * all the available space in the output buffer:
     */
    done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);

    if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
  }
  return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
}

int cc_gzm_flush (void * file, int flush)
{
  cc_gzm_stream *s = (cc_gzm_stream*)file;
  int err = do_flush (file, flush);

  if (err) return err;
  fflush(s->file);
  return  s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
}
#endif /* Z_NO_DEFLATE */

/* ===========================================================================
   Sets the starting position for the next gzread or gzwrite on the
   given compressed file. The offset represents a number of bytes in
   the gzseek returns the resulting offset location as measured in
   bytes from the beginning of the uncompressed stream, or -1 in case
   of error.  SEEK_END is not implemented, returns error.  In this
   version of the library, gzseek can be extremely slow.
*/
off_t
cc_gzm_seek(void * file, off_t offset, int whence)
{
  cc_gzm_stream *s = (cc_gzm_stream*)file;

  if (s == NULL || whence == SEEK_END ||
      s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
    return -1L;
  }

  if (s->mode == 'w') {
#ifdef Z_NO_DEFLATE
    return -1L;
#else
    if (whence == SEEK_SET) {
      offset -= s->stream.total_in;
    }
    if (offset < 0) return -1L;

    /* At this point, offset is the number of zero bytes to write. */
    if (s->inbuf == NULL) {
      s->inbuf = (uint8_t*)Z_ALLOC(Z_BUFSIZE); /* for seeking */
      zmemzero(s->inbuf, Z_BUFSIZE);
    }
    while (offset > 0)  {
      uint32_t size = Z_BUFSIZE;
      if (offset < Z_BUFSIZE) size = (uint32_t)offset;

      size = gzwrite(file, s->inbuf, size);
      if (size == 0) return -1L;

      offset -= size;
    }
    return (off_t)s->stream.total_in;
#endif
  }
  /* Rest of function is for reading only */

  /* compute absolute position */
  if (whence == SEEK_CUR) {
    offset += s->stream.total_out;
  }
  if (offset < 0) return -1L;

  if (s->transparent) {
    /* map to fseek */
    s->stream.avail_in = 0;
    s->stream.next_in = s->inbuf;
    if (cc_gzm_fseek(s->memfile, offset, SEEK_SET) < 0) return -1L;

    s->stream.total_in = s->stream.total_out = (uint32_t)offset;
    return offset;
  }

  /* For a negative seek, rewind and use positive seek */
  if ((uint32_t)offset >= s->stream.total_out) {
    offset -= s->stream.total_out;
  } else if (cc_zlibglue_gzrewind(file) < 0) {
    return -1L;
  }
  /* offset is now the number of bytes to skip. */

  if (offset != 0 && s->outbuf == NULL) {
    s->outbuf = (uint8_t*)Z_ALLOC(Z_BUFSIZE);
  }
  while (offset > 0)  {
    int size = Z_BUFSIZE;
    if (offset < Z_BUFSIZE) size = (int)offset;

    size = cc_zlibglue_gzread(file, s->outbuf, (uint32_t)size);
    if (size <= 0) return -1L;
    offset -= size;
  }
  return (off_t) s->stream.total_out;
}

/* ===========================================================================
     Rewinds input file.
*/
int cc_gzm_rewind(void * file)
{
  cc_gzm_stream *s = (cc_gzm_stream*)file;

  if (s == NULL || s->mode != 'r') return -1;

  s->z_err = Z_OK;
  s->z_eof = 0;
  s->stream.avail_in = 0;
  s->stream.next_in = s->inbuf;
  s->crc = cc_zlibglue_crc32(0L, NULL, 0);

  if (s->startpos == 0) { /* not a compressed file */
    s->memfile->currpos = 0;
    return 0;
  }

  (void) cc_zlibglue_inflateReset(&s->stream);
  return cc_gzm_fseek(s->memfile, s->startpos, SEEK_SET);
}

/* ===========================================================================
   Returns the starting position for the next gzread or gzwrite on the
   given compressed file. This position represents a number of bytes in the
   uncompressed data stream.
*/
off_t cc_gzm_tell(void * file)
{
  return cc_gzm_seek(file, 0L, SEEK_CUR);
}

/* ===========================================================================
   Returns 1 when EOF has previously been detected reading the given
   input stream, otherwise zero.
*/
int cc_gzm_eof(void * file)
{
  cc_gzm_stream *s = (cc_gzm_stream*)file;

  return (s == NULL || s->mode != 'r') ? 0 : s->z_eof;
}

#ifndef Z_NO_DEFLATE
/* ===========================================================================
   Outputs a int32_t in LSB order to the given file
*/
static void
putInt32(cc_gzm_file * file, uint32_t x)
{
  int n;
  for (n = 0; n < 4; n++) {
    cc_gzm_fputc((int)(x & 0xff), file);
    x >>= 8;
  }
}
#endif /* Z_NO_DEFLATE */

/* ===========================================================================
   Reads a int32_t in LSB order from the given cc_gzm_stream. Sets z_err in case
   of error.
*/
static uint32_t
getInt32(cc_gzm_stream * s)
{
  uint32_t x = (uint32_t)get_byte(s);
  int c;

  x += ((uint32_t)get_byte(s))<<8;
  x += ((uint32_t)get_byte(s))<<16;
  c = get_byte(s);
  if (c == EOF) s->z_err = Z_DATA_ERROR;
  x += ((uint32_t)c)<<24;
  return x;
}

/* ===========================================================================
   Flushes all pending output if necessary, closes the compressed file
   and deallocates all the (de)compression state.
*/
int cc_gzm_close(void * file)
{
  int err;
  cc_gzm_stream *s = (cc_gzm_stream*)file;

  if (s == NULL) return Z_STREAM_ERROR;

  if (s->mode == 'w') {
#ifdef Z_NO_DEFLATE
    err = Z_STREAM_ERROR;
    return err;
#else
    err = do_flush (file, Z_FINISH);
    if (err != Z_OK) return destroy((cc_gzm_stream*)file);

    putInt32(s->file, s->crc);
    putInt32(s->file, s->stream.total_in);
#endif
  }
  return destroy((cc_gzm_stream*)file);
}

/* stdio layer */

static int
cc_gzm_fseek(cc_gzm_file * file, long offset, int whence)
{
  switch (whence) {
  case SEEK_SET:
    if (offset > 0 && offset <= (long) file->buflen) {
      file->currpos = offset;
      return 0;
    }
    assert(0 && "illegal seek");
    return -1;
    break;
  case SEEK_CUR:
    if ((file->currpos + offset > 0) &&
        (file->currpos + offset <= file->buflen)) {
      file->currpos += offset;
      return 0;
    }
    assert(0 && "illegal seek");
    return -1;
    break;
  case SEEK_END:
    if ((file->buflen + offset > 0) &&
        (file->buflen + offset <= file->buflen)) {
      file->currpos = file->buflen + offset;
      return 0;
    }
    assert(0 && "illegal seek");
    return -1;
    break;
  default:
    assert(0 && "illegal whence");
    return -1;
  }
  return 0;
}

static int
cc_gzm_ftell(cc_gzm_file * file)
{
  return file->currpos;
}

static size_t
cc_gzm_fread(void * ptr, size_t size, size_t nmemb, cc_gzm_file * file)
{
  uint32_t remain;

  assert(size == 1); /* to simplify implementation */

  remain = file->buflen - file->currpos;
  if (remain > nmemb) remain = nmemb;
  if (remain == 0) return 0;

  (void) memcpy(ptr, file->buf + file->currpos, remain);
  file->currpos += remain;
  return remain;
}

static int
cc_gzm_ferror(cc_gzm_file * file)
{
  return 0;
}

#undef Z_BUFSIZE
#undef Z_NO_DEFLATE
#undef Z_ALLOC
#undef Z_TRYFREE
#undef Z_ASCII_FLAG
#undef Z_HEAD_CRC
#undef Z_EXTRA_FIELD
#undef Z_ORIG_NAME
#undef Z_COMMENT
#undef Z_RESERVED