OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
gzio.h File Reference
#include <zlib.h>
Include dependency graph for gzio.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

gzFile ZEXPORT c4_gzopen (const char *path, const char *mode)
 
gzFile ZEXPORT c4_gzdopen (int fd, const char *mode)
 
int ZEXPORT c4_gzsetparams (gzFile file, int level, int strategy)
 
int ZEXPORT c4_gzread (gzFile file, voidp buf, unsigned len)
 
int ZEXPORT c4_gzgetc (gzFile file)
 
int ZEXPORT c4_gzungetc (int c, gzFile file)
 
char *ZEXPORT c4_gzgets (gzFile file, char *buf, int len)
 
int ZEXPORT c4_gzwrite (gzFile file, voidpc buf, unsigned len)
 
int ZEXPORTVA c4_gzprintf (gzFile file, const char *format,...)
 
int ZEXPORT c4_gzputc (gzFile file, int c)
 
int ZEXPORT c4_gzputs (gzFile file, const char *s)
 
int ZEXPORT c4_gzflush (gzFile file, int flush)
 
z_off_t ZEXPORT c4_gzseek (gzFile file, z_off_t offset, int whence)
 
int ZEXPORT c4_gzrewind (gzFile file)
 
z_off_t ZEXPORT c4_gztell (gzFile file)
 
int ZEXPORT c4_gzeof (gzFile file)
 
int ZEXPORT c4_gzdirect (gzFile file)
 
int ZEXPORT c4_gzclose (gzFile file)
 
void ZEXPORT c4_gzclearerr (gzFile file)
 

Function Documentation

void ZEXPORT c4_gzclearerr ( gzFile  file)

Definition at line 1030 of file gzio.c.

References gz_stream::file, s, gz_stream::z_eof, and gz_stream::z_err.

1032 {
1033  gz_stream *s = (gz_stream*)file;
1034 
1035  if (s == NULL) return;
1036  if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
1037  s->z_eof = 0;
1038  clearerr(s->file);
1039 }
Definition: gzio.c:65
int z_eof
Definition: gzio.c:68
int z_err
Definition: gzio.c:67
FILE * file
Definition: gzio.c:69
#define s
int ZEXPORT c4_gzclose ( gzFile  file)

Definition at line 964 of file gzio.c.

References gz_stream::crc, destroy(), do_flush(), gz_stream::file, gz_stream::in, gz_stream::mode, putLong(), and s.

Referenced by CStdFile::Close(), CStdFile::Open(), and UncompressedFileSize().

966 {
967  gz_stream *s = (gz_stream*)file;
968 
969  if (s == NULL) return Z_STREAM_ERROR;
970 
971  if (s->mode == 'w') {
972 #ifdef NO_GZCOMPRESS
973  return Z_STREAM_ERROR;
974 #else
975  if (do_flush (file, Z_FINISH) != Z_OK)
976  return destroy((gz_stream*)file);
977 
978  putLong (s->file, s->crc);
979  putLong (s->file, (uLong)(s->in & 0xffffffff));
980 #endif
981  }
982  return destroy((gz_stream*)file);
983 }
z_off_t in
Definition: gzio.c:78
Definition: gzio.c:65
char mode
Definition: gzio.c:76
local int destroy(gz_stream *s)
Definition: gzio.c:366
uLong crc
Definition: gzio.c:72
local void putLong(FILE *file, uLong x)
Definition: gzio.c:931
FILE * file
Definition: gzio.c:69
#define s
local int do_flush(gzFile file, int flush)
Definition: gzio.c:716

Here is the call graph for this function:

Here is the caller graph for this function:

int ZEXPORT c4_gzdirect ( gzFile  file)

Definition at line 919 of file gzio.c.

References gz_stream::mode, s, and gz_stream::transparent.

Referenced by CStdFile::Open().

921 {
922  gz_stream *s = (gz_stream*)file;
923 
924  if (s == NULL || s->mode != 'r') return 0;
925  return s->transparent;
926 }
int transparent
Definition: gzio.c:75
Definition: gzio.c:65
char mode
Definition: gzio.c:76
#define s

Here is the caller graph for this function:

gzFile ZEXPORT c4_gzdopen ( int  fd,
const char *  mode 
)

Definition at line 228 of file gzio.c.

References gz_open(), and sprintf.

Referenced by CStdFile::Create(), CStdFile::Open(), and UncompressedFileSize().

231 {
232  char name[46]; /* allow for up to 128-bit integers */
233 
234  if (fd < 0) return (gzFile)Z_NULL;
235  sprintf(name, "<fd:%d>", fd); /* for debugging */
236 
237  return gz_open (name, mode, fd);
238 }
#define sprintf
Definition: Standard.h:164
local gzFile gz_open(char *path, const char *mode, int fd) const
Definition: gzio.c:102

Here is the call graph for this function:

Here is the caller graph for this function:

int ZEXPORT c4_gzeof ( gzFile  file)

Definition at line 902 of file gzio.c.

References gz_stream::mode, s, gz_stream::z_eof, and gz_stream::z_err.

904 {
905  gz_stream *s = (gz_stream*)file;
906 
907  /* With concatenated compressed files that can have embedded
908  * crc trailers, z_eof is no longer the only/best indicator of EOF
909  * on a gz_stream. Handle end-of-stream error explicitly here.
910  */
911  if (s == NULL || s->mode != 'r') return 0;
912  if (s->z_eof) return 1;
913  return s->z_err == Z_STREAM_END;
914 }
Definition: gzio.c:65
char mode
Definition: gzio.c:76
int z_eof
Definition: gzio.c:68
int z_err
Definition: gzio.c:67
#define s
int ZEXPORT c4_gzflush ( gzFile  file,
int  flush 
)

Definition at line 757 of file gzio.c.

References do_flush(), gz_stream::file, s, and gz_stream::z_err.

760 {
761  gz_stream *s = (gz_stream*)file;
762  int err = do_flush (file, flush);
763 
764  if (err) return err;
765  fflush(s->file);
766  return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
767 }
Definition: gzio.c:65
int z_err
Definition: gzio.c:67
FILE * file
Definition: gzio.c:69
#define s
local int do_flush(gzFile file, int flush)
Definition: gzio.c:716

Here is the call graph for this function:

int ZEXPORT c4_gzgetc ( gzFile  file)

Definition at line 514 of file gzio.c.

516 {
517  unsigned char c;
518 
519  return gzread(file, &c, 1) == 1 ? c : -1;
520 }
char* ZEXPORT c4_gzgets ( gzFile  file,
char *  buf,
int  len 
)

Definition at line 551 of file gzio.c.

References b.

555 {
556  char *b = buf;
557  if (buf == Z_NULL || len <= 0) return Z_NULL;
558 
559  while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
560  *buf = '\0';
561  return b == buf && len > 0 ? Z_NULL : b;
562 }
#define b
gzFile ZEXPORT c4_gzopen ( const char *  path,
const char *  mode 
)
int ZEXPORTVA c4_gzprintf ( gzFile  file,
const char *  format,
  ... 
)
int ZEXPORT c4_gzputc ( gzFile  file,
int  c 
)

Definition at line 689 of file gzio.c.

692 {
693  unsigned char cc = (unsigned char) c; /* required for big endian systems */
694 
695  return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
696 }
int ZEXPORT c4_gzputs ( gzFile  file,
const char *  s 
)

Definition at line 704 of file gzio.c.

707 {
708  return gzwrite(file, s, (unsigned)strlen(s));
709 }
#define s
int ZEXPORT c4_gzread ( gzFile  file,
voidp  buf,
unsigned  len 
)

Definition at line 405 of file gzio.c.

References gz_stream::back, check_header(), gz_stream::crc, gz_stream::file, getLong(), gz_stream::in, gz_stream::inbuf, gz_stream::last, gz_stream::mode, gz_stream::out, s, gz_stream::stream, gz_stream::transparent, Z_BUFSIZE, gz_stream::z_eof, and gz_stream::z_err.

Referenced by CStdFile::LoadBuffer(), and UncompressedFileSize().

409 {
410  gz_stream *s = (gz_stream*)file;
411  Bytef *start = (Bytef*)buf; /* starting point for crc computation */
412  Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
413 
414  if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
415 
416  if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
417  if (s->z_err == Z_STREAM_END) return 0; /* EOF */
418 
419  next_out = (Byte*)buf;
420  s->stream.next_out = (Bytef*)buf;
421  s->stream.avail_out = len;
422 
423  if (s->stream.avail_out && s->back != EOF) {
424  *next_out++ = s->back;
425  s->stream.next_out++;
426  s->stream.avail_out--;
427  s->back = EOF;
428  s->out++;
429  start++;
430  if (s->last) {
431  s->z_err = Z_STREAM_END;
432  return 1;
433  }
434  }
435 
436  while (s->stream.avail_out != 0) {
437 
438  if (s->transparent) {
439  /* Copy first the lookahead bytes: */
440  uInt n = s->stream.avail_in;
441  if (n > s->stream.avail_out) n = s->stream.avail_out;
442  if (n > 0) {
443  zmemcpy(s->stream.next_out, s->stream.next_in, n);
444  next_out += n;
445  s->stream.next_out = next_out;
446  s->stream.next_in += n;
447  s->stream.avail_out -= n;
448  s->stream.avail_in -= n;
449  }
450  if (s->stream.avail_out > 0) {
451  s->stream.avail_out -=
452  (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
453  }
454  len -= s->stream.avail_out;
455  s->in += len;
456  s->out += len;
457  if (len == 0) s->z_eof = 1;
458  return (int)len;
459  }
460  if (s->stream.avail_in == 0 && !s->z_eof) {
461 
462  errno = 0;
463  s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
464  if (s->stream.avail_in == 0) {
465  s->z_eof = 1;
466  if (ferror(s->file)) {
467  s->z_err = Z_ERRNO;
468  break;
469  }
470  }
471  s->stream.next_in = s->inbuf;
472  }
473  s->in += s->stream.avail_in;
474  s->out += s->stream.avail_out;
475  s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
476  s->in -= s->stream.avail_in;
477  s->out -= s->stream.avail_out;
478 
479  if (s->z_err == Z_STREAM_END) {
480  /* Check CRC and original size */
481  s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
482  start = s->stream.next_out;
483 
484  if (getLong(s) != s->crc) {
485  s->z_err = Z_DATA_ERROR;
486  } else {
487  (void)getLong(s);
488  /* The uncompressed length returned by above getlong() may be
489  * different from s->out in case of concatenated .gz files.
490  * Check for such files:
491  */
492  check_header(s);
493  if (s->z_err == Z_OK) {
494  inflateReset(&(s->stream));
495  s->crc = crc32(0L, Z_NULL, 0);
496  }
497  }
498  }
499  if (s->z_err != Z_OK || s->z_eof) break;
500  }
501  s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
502 
503  if (len == s->stream.avail_out &&
504  (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
505  return -1;
506  return (int)(len - s->stream.avail_out);
507 }
z_off_t in
Definition: gzio.c:78
int transparent
Definition: gzio.c:75
z_stream stream
Definition: gzio.c:66
local uLong getLong(gz_stream *s)
Definition: gzio.c:946
int last
Definition: gzio.c:81
Definition: gzio.c:65
z_off_t out
Definition: gzio.c:79
#define Z_BUFSIZE
Definition: gzio.c:33
char mode
Definition: gzio.c:76
local void check_header(gz_stream *s)
Definition: gzio.c:297
int z_eof
Definition: gzio.c:68
int z_err
Definition: gzio.c:67
uLong crc
Definition: gzio.c:72
Byte * inbuf
Definition: gzio.c:70
FILE * file
Definition: gzio.c:69
#define s
int back
Definition: gzio.c:80

Here is the call graph for this function:

Here is the caller graph for this function:

int ZEXPORT c4_gzrewind ( gzFile  file)

Definition at line 868 of file gzio.c.

References gz_stream::back, gz_stream::crc, gz_stream::file, gz_stream::in, gz_stream::inbuf, gz_stream::mode, gz_stream::out, s, gz_stream::start, gz_stream::stream, gz_stream::transparent, gz_stream::z_eof, and gz_stream::z_err.

Referenced by CStdFile::Rewind().

870 {
871  gz_stream *s = (gz_stream*)file;
872 
873  if (s == NULL || s->mode != 'r') return -1;
874 
875  s->z_err = Z_OK;
876  s->z_eof = 0;
877  s->back = EOF;
878  s->stream.avail_in = 0;
879  s->stream.next_in = s->inbuf;
880  s->crc = crc32(0L, Z_NULL, 0);
881  if (!s->transparent) (void)inflateReset(&s->stream);
882  s->in = 0;
883  s->out = 0;
884  return fseek(s->file, s->start, SEEK_SET);
885 }
z_off_t in
Definition: gzio.c:78
int transparent
Definition: gzio.c:75
z_stream stream
Definition: gzio.c:66
Definition: gzio.c:65
z_off_t out
Definition: gzio.c:79
char mode
Definition: gzio.c:76
int z_eof
Definition: gzio.c:68
int z_err
Definition: gzio.c:67
uLong crc
Definition: gzio.c:72
Byte * inbuf
Definition: gzio.c:70
z_off_t start
Definition: gzio.c:77
FILE * file
Definition: gzio.c:69
#define s
int back
Definition: gzio.c:80

Here is the caller graph for this function:

z_off_t ZEXPORT c4_gzseek ( gzFile  file,
z_off_t  offset,
int  whence 
)

Definition at line 778 of file gzio.c.

References ALLOC, gz_stream::back, gz_stream::file, gz_stream::in, gz_stream::inbuf, gz_stream::last, gz_stream::mode, gz_stream::out, gz_stream::outbuf, s, gz_stream::stream, gz_stream::transparent, Z_BUFSIZE, and gz_stream::z_err.

782 {
783  gz_stream *s = (gz_stream*)file;
784 
785  if (s == NULL || whence == SEEK_END ||
786  s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
787  return -1L;
788  }
789 
790  if (s->mode == 'w') {
791 #ifdef NO_GZCOMPRESS
792  return -1L;
793 #else
794  if (whence == SEEK_SET) {
795  offset -= s->in;
796  }
797  if (offset < 0) return -1L;
798 
799  /* At this point, offset is the number of zero bytes to write. */
800  if (s->inbuf == Z_NULL) {
801  s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
802  if (s->inbuf == Z_NULL) return -1L;
803  zmemzero(s->inbuf, Z_BUFSIZE);
804  }
805  while (offset > 0) {
806  uInt size = Z_BUFSIZE;
807  if (offset < Z_BUFSIZE) size = (uInt)offset;
808 
809  size = gzwrite(file, s->inbuf, size);
810  if (size == 0) return -1L;
811 
812  offset -= size;
813  }
814  return s->in;
815 #endif
816  }
817  /* Rest of function is for reading only */
818 
819  /* compute absolute position */
820  if (whence == SEEK_CUR) {
821  offset += s->out;
822  }
823  if (offset < 0) return -1L;
824 
825  if (s->transparent) {
826  /* map to fseek */
827  s->back = EOF;
828  s->stream.avail_in = 0;
829  s->stream.next_in = s->inbuf;
830  if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
831 
832  s->in = s->out = offset;
833  return offset;
834  }
835 
836  /* For a negative seek, rewind and use positive seek */
837  if (offset >= s->out) {
838  offset -= s->out;
839  } else if (gzrewind(file) < 0) {
840  return -1L;
841  }
842  /* offset is now the number of bytes to skip. */
843 
844  if (offset != 0 && s->outbuf == Z_NULL) {
845  s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
846  if (s->outbuf == Z_NULL) return -1L;
847  }
848  if (offset && s->back != EOF) {
849  s->back = EOF;
850  s->out++;
851  offset--;
852  if (s->last) s->z_err = Z_STREAM_END;
853  }
854  while (offset > 0) {
855  int size = Z_BUFSIZE;
856  if (offset < Z_BUFSIZE) size = (int)offset;
857 
858  size = gzread(file, s->outbuf, (uInt)size);
859  if (size <= 0) return -1L;
860  offset -= size;
861  }
862  return s->out;
863 }
z_off_t in
Definition: gzio.c:78
int transparent
Definition: gzio.c:75
z_stream stream
Definition: gzio.c:66
int last
Definition: gzio.c:81
Definition: gzio.c:65
z_off_t out
Definition: gzio.c:79
#define ALLOC(size)
Definition: gzio.c:50
#define Z_BUFSIZE
Definition: gzio.c:33
char mode
Definition: gzio.c:76
Byte * outbuf
Definition: gzio.c:71
int z_err
Definition: gzio.c:67
Byte * inbuf
Definition: gzio.c:70
FILE * file
Definition: gzio.c:69
#define s
int back
Definition: gzio.c:80
int ZEXPORT c4_gzsetparams ( gzFile  file,
int  level,
int  strategy 
)

Definition at line 243 of file gzio.c.

References gz_stream::file, gz_stream::mode, gz_stream::outbuf, s, gz_stream::stream, Z_BUFSIZE, and gz_stream::z_err.

247 {
248  gz_stream *s = (gz_stream*)file;
249 
250  if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
251 
252  /* Make room to allow flushing */
253  if (s->stream.avail_out == 0) {
254 
255  s->stream.next_out = s->outbuf;
256  if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
257  s->z_err = Z_ERRNO;
258  }
259  s->stream.avail_out = Z_BUFSIZE;
260  }
261 
262  return deflateParams (&(s->stream), level, strategy);
263 }
z_stream stream
Definition: gzio.c:66
Definition: gzio.c:65
#define Z_BUFSIZE
Definition: gzio.c:33
char mode
Definition: gzio.c:76
Byte * outbuf
Definition: gzio.c:71
int z_err
Definition: gzio.c:67
FILE * file
Definition: gzio.c:69
#define s
z_off_t ZEXPORT c4_gztell ( gzFile  file)

Definition at line 892 of file gzio.c.

894 {
895  return gzseek(file, 0L, SEEK_CUR);
896 }
int ZEXPORT c4_gzungetc ( int  c,
gzFile  file 
)

Definition at line 526 of file gzio.c.

References gz_stream::back, gz_stream::last, gz_stream::mode, gz_stream::out, s, gz_stream::z_eof, and gz_stream::z_err.

529 {
530  gz_stream *s = (gz_stream*)file;
531 
532  if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
533  s->back = c;
534  s->out--;
535  s->last = (s->z_err == Z_STREAM_END);
536  if (s->last) s->z_err = Z_OK;
537  s->z_eof = 0;
538  return c;
539 }
int last
Definition: gzio.c:81
Definition: gzio.c:65
z_off_t out
Definition: gzio.c:79
char mode
Definition: gzio.c:76
int z_eof
Definition: gzio.c:68
int z_err
Definition: gzio.c:67
#define s
int back
Definition: gzio.c:80
int ZEXPORT c4_gzwrite ( gzFile  file,
voidpc  buf,
unsigned  len 
)

Definition at line 570 of file gzio.c.

References gz_stream::crc, gz_stream::file, gz_stream::in, gz_stream::mode, gz_stream::out, gz_stream::outbuf, s, gz_stream::stream, Z_BUFSIZE, and gz_stream::z_err.

Referenced by CStdFile::SaveBuffer().

574 {
575  gz_stream *s = (gz_stream*)file;
576 
577  if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
578 
579  s->stream.next_in = *(Bytef**)(void*)&buf;
580  s->stream.avail_in = len;
581 
582  while (s->stream.avail_in != 0) {
583 
584  if (s->stream.avail_out == 0) {
585 
586  s->stream.next_out = s->outbuf;
587  if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
588  s->z_err = Z_ERRNO;
589  break;
590  }
591  s->stream.avail_out = Z_BUFSIZE;
592  }
593  s->in += s->stream.avail_in;
594  s->out += s->stream.avail_out;
595  s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
596  s->in -= s->stream.avail_in;
597  s->out -= s->stream.avail_out;
598  if (s->z_err != Z_OK) break;
599  }
600  s->crc = crc32(s->crc, (const Bytef *)buf, len);
601 
602  return (int)(len - s->stream.avail_in);
603 }
z_off_t in
Definition: gzio.c:78
z_stream stream
Definition: gzio.c:66
Definition: gzio.c:65
z_off_t out
Definition: gzio.c:79
#define Z_BUFSIZE
Definition: gzio.c:33
char mode
Definition: gzio.c:76
Byte * outbuf
Definition: gzio.c:71
int z_err
Definition: gzio.c:67
uLong crc
Definition: gzio.c:72
FILE * file
Definition: gzio.c:69
#define s

Here is the caller graph for this function: