Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gzlib.cc File Reference
#include "gzguts.h"
Include dependency graph for gzlib.cc:

Go to the source code of this file.

Macros

#define LSEEK   lseek
 

Functions

local void gz_reset OF ((gz_statep))
 
local gzFile gz_open OF ((const void *, int, const char *))
 
local void gz_reset (gz_statep state)
 
local gzFile gz_open (const void *path, int fd, const char *mode)
 
gzFile ZEXPORT gzopen (const char *path, const char *mode)
 
gzFile ZEXPORT gzopen64 (const char *path, const char *mode)
 
gzFile ZEXPORT gzdopen (int fd, const char *mode)
 
int ZEXPORT gzbuffer (gzFile file, unsigned size)
 
int ZEXPORT gzrewind (gzFile file)
 
z_off64_t ZEXPORT gzseek64 (gzFile file, z_off64_t offset, int whence)
 
z_off_t ZEXPORT gzseek (gzFile file, z_off_t offset, int whence)
 
z_off64_t ZEXPORT gztell64 (gzFile file)
 
z_off_t ZEXPORT gztell (gzFile file)
 
z_off64_t ZEXPORT gzoffset64 (gzFile file)
 
z_off_t ZEXPORT gzoffset (gzFile file)
 
int ZEXPORT gzeof (gzFile file)
 
const char *ZEXPORT gzerror (gzFile file, int *errnum)
 
void ZEXPORT gzclearerr (gzFile file)
 
void ZLIB_INTERNAL gz_error (gz_statep state, int err, const char *msg)
 
unsigned ZLIB_INTERNAL gz_intmax ()
 

Macro Definition Documentation

#define LSEEK   lseek

Definition at line 14 of file gzlib.cc.

Function Documentation

void ZLIB_INTERNAL gz_error ( gz_statep  state,
int  err,
const char *  msg 
)

Definition at line 545 of file gzlib.cc.

546 {
547  /* free previously allocated message and clear */
548  if (state->msg != NULL) {
549  if (state->err != Z_MEM_ERROR)
550  free(state->msg);
551  state->msg = NULL;
552  }
553 
554  /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */
555  if (err != Z_OK && err != Z_BUF_ERROR)
556  state->x.have = 0;
557 
558  /* set error code, and if no message, then done */
559  state->err = err;
560  if (msg == NULL)
561  return;
562 
563  /* for an out of memory error, return literal string when requested */
564  if (err == Z_MEM_ERROR)
565  return;
566 
567  /* construct error message with path */
568  if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) ==
569  NULL) {
570  state->err = Z_MEM_ERROR;
571  return;
572  }
573 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
574  snprintf(state->msg, strlen(state->path) + strlen(msg) + 3,
575  "%s%s%s", state->path, ": ", msg);
576 #else
577  strcpy(state->msg, state->path);
578  strcat(state->msg, ": ");
579  strcat(state->msg, msg);
580 #endif
581  return;
582 }
#define Z_MEM_ERROR
Definition: zlib.h:179
#define Z_BUF_ERROR
Definition: zlib.h:180
#define Z_OK
Definition: zlib.h:173

Here is the caller graph for this function:

unsigned ZLIB_INTERNAL gz_intmax ( )

Definition at line 589 of file gzlib.cc.

590 {
591  unsigned p, q;
592 
593  p = 1;
594  do {
595  q = p;
596  p <<= 1;
597  p++;
598  } while (p > q);
599  return q >> 1;
600 }
const char * p
Definition: xmltok.h:285
local gzFile gz_open ( const void path,
int  fd,
const char *  mode 
)

Definition at line 89 of file gzlib.cc.

90 {
91  gz_statep state;
92  size_t len;
93  int oflag;
94 #ifdef O_CLOEXEC
95  int cloexec = 0;
96 #endif
97 #ifdef O_EXCL
98  int exclusive = 0;
99 #endif
100 
101  /* check input */
102  if (path == NULL)
103  return NULL;
104 
105  /* allocate gzFile structure to return */
106  state = (gz_statep)malloc(sizeof(gz_state));
107  if (state == NULL)
108  return NULL;
109  state->size = 0; /* no buffers allocated yet */
110  state->want = GZBUFSIZE; /* requested buffer size */
111  state->msg = NULL; /* no error message yet */
112 
113  /* interpret mode */
114  state->mode = GZ_NONE;
115  state->level = Z_DEFAULT_COMPRESSION;
116  state->strategy = Z_DEFAULT_STRATEGY;
117  state->direct = 0;
118  while (*mode) {
119  if (*mode >= '0' && *mode <= '9')
120  state->level = *mode - '0';
121  else
122  switch (*mode) {
123  case 'r':
124  state->mode = GZ_READ;
125  break;
126 #ifndef NO_GZCOMPRESS
127  case 'w':
128  state->mode = GZ_WRITE;
129  break;
130  case 'a':
131  state->mode = GZ_APPEND;
132  break;
133 #endif
134  case '+': /* can't read and write at the same time */
135  free(state);
136  return NULL;
137  case 'b': /* ignore -- will request binary anyway */
138  break;
139 #ifdef O_CLOEXEC
140  case 'e':
141  cloexec = 1;
142  break;
143 #endif
144 #ifdef O_EXCL
145  case 'x':
146  exclusive = 1;
147  break;
148 #endif
149  case 'f':
150  state->strategy = Z_FILTERED;
151  break;
152  case 'h':
153  state->strategy = Z_HUFFMAN_ONLY;
154  break;
155  case 'R':
156  state->strategy = Z_RLE;
157  break;
158  case 'F':
159  state->strategy = Z_FIXED;
160  break;
161  case 'T':
162  state->direct = 1;
163  break;
164  default: /* could consider as an error, but just ignore */
165  ;
166  }
167  mode++;
168  }
169 
170  /* must provide an "r", "w", or "a" */
171  if (state->mode == GZ_NONE) {
172  free(state);
173  return NULL;
174  }
175 
176  /* can't force transparent read */
177  if (state->mode == GZ_READ) {
178  if (state->direct) {
179  free(state);
180  return NULL;
181  }
182  state->direct = 1; /* for empty file */
183  }
184 
185  /* save the path name for error messages */
186 #ifdef _WIN32
187  if (fd == -2) {
188  len = wcstombs(NULL, (const wchar_t *)path, 0);
189  if (len == (size_t)-1)
190  len = 0;
191  }
192  else
193 #endif
194  len = strlen((const char *)path);
195  state->path = (char*)malloc(len + 1);
196  if (state->path == NULL) {
197  free(state);
198  return NULL;
199  }
200 #ifdef _WIN32
201  if (fd == -2)
202  if (len)
203  wcstombs(state->path, (const wchar_t *)path, len + 1);
204  else
205  *(state->path) = 0;
206  else
207 #endif
208 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
209  snprintf(state->path, len + 1, "%s", (const char *)path);
210 #else
211  strcpy(state->path, path);
212 #endif
213 
214  /* compute the flags for open() */
215  oflag =
216 #ifdef O_LARGEFILE
217  O_LARGEFILE |
218 #endif
219 #ifdef O_BINARY
220  O_BINARY |
221 #endif
222 #ifdef O_CLOEXEC
223  (cloexec ? O_CLOEXEC : 0) |
224 #endif
225  (state->mode == GZ_READ ?
226  O_RDONLY :
227  (O_WRONLY | O_CREAT |
228 #ifdef O_EXCL
229  (exclusive ? O_EXCL : 0) |
230 #endif
231  (state->mode == GZ_WRITE ?
232  O_TRUNC :
233  O_APPEND)));
234 
235  /* open the file with the appropriate flags (or just use fd) */
236  state->fd = fd > -1 ? fd : (
237 #ifdef _WIN32
238  fd == -2 ? _wopen((const wchar_t *)path, oflag, 0666) :
239 #endif
240  open((const char *)path, oflag, 0666));
241  if (state->fd == -1) {
242  free(state->path);
243  free(state);
244  return NULL;
245  }
246  if (state->mode == GZ_APPEND)
247  state->mode = GZ_WRITE; /* simplify later checks */
248 
249  /* save the current position for rewinding (only if reading) */
250  if (state->mode == GZ_READ) {
251  state->start = LSEEK(state->fd, 0, SEEK_CUR);
252  if (state->start == -1) state->start = 0;
253  }
254 
255  /* initialize stream */
256  gz_reset(state);
257 
258  /* return stream */
259  return (gzFile)state;
260 }
const XML_Char int len
Definition: expat.h:262
#define GZ_NONE
Definition: gzguts.h:150
#define GZ_APPEND
Definition: gzguts.h:153
#define GZ_READ
Definition: gzguts.h:151
#define Z_FILTERED
Definition: zlib.h:192
#define Z_FIXED
Definition: zlib.h:195
local void gz_reset(gz_statep state)
Definition: gzlib.cc:74
#define GZ_WRITE
Definition: gzguts.h:152
#define Z_HUFFMAN_ONLY
Definition: zlib.h:193
#define GZBUFSIZE
Definition: gzguts.h:147
#define LSEEK
Definition: gzlib.cc:14
gz_state FAR * gz_statep
Definition: gzguts.h:193
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:196
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:189
#define Z_RLE
Definition: zlib.h:194

Here is the call graph for this function:

Here is the caller graph for this function:

local void gz_reset ( gz_statep  state)

Definition at line 74 of file gzlib.cc.

75 {
76  state->x.have = 0; /* no output data available */
77  if (state->mode == GZ_READ) { /* for reading ... */
78  state->eof = 0; /* not at end of file */
79  state->past = 0; /* have not read past end yet */
80  state->how = LOOK; /* look for gzip header */
81  }
82  state->seek = 0; /* no seek request pending */
83  gz_error(state, Z_OK, NULL); /* clear error */
84  state->x.pos = 0; /* no uncompressed data yet */
85  state->strm.avail_in = 0; /* no input data yet */
86 }
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.cc:545
#define GZ_READ
Definition: gzguts.h:151
#define LOOK
Definition: gzguts.h:156
#define Z_OK
Definition: zlib.h:173

Here is the call graph for this function:

Here is the caller graph for this function:

int ZEXPORT gzbuffer ( gzFile  file,
unsigned  size 
)

Definition at line 301 of file gzlib.cc.

302 {
303  gz_statep state;
304 
305  /* get internal structure and check integrity */
306  if (file == NULL)
307  return -1;
308  state = (gz_statep)file;
309  if (state->mode != GZ_READ && state->mode != GZ_WRITE)
310  return -1;
311 
312  /* make sure we haven't already allocated memory */
313  if (state->size != 0)
314  return -1;
315 
316  /* check and set requested size */
317  if (size < 2)
318  size = 2; /* need two bytes to check magic header */
319  state->want = size;
320  return 0;
321 }
#define GZ_READ
Definition: gzguts.h:151
#define GZ_WRITE
Definition: gzguts.h:152
gz_state FAR * gz_statep
Definition: gzguts.h:193
void ZEXPORT gzclearerr ( gzFile  file)

Definition at line 520 of file gzlib.cc.

521 {
522  gz_statep state;
523 
524  /* get internal structure and check integrity */
525  if (file == NULL)
526  return;
527  state = (gz_statep)file;
528  if (state->mode != GZ_READ && state->mode != GZ_WRITE)
529  return;
530 
531  /* clear error and end-of-file */
532  if (state->mode == GZ_READ) {
533  state->eof = 0;
534  state->past = 0;
535  }
536  gz_error(state, Z_OK, NULL);
537 }
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.cc:545
#define GZ_READ
Definition: gzguts.h:151
#define GZ_WRITE
Definition: gzguts.h:152
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:193

Here is the call graph for this function:

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

Definition at line 275 of file gzlib.cc.

276 {
277  char *path; /* identifier for error messages */
278  gzFile gz;
279 
280  if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
281  return NULL;
282 #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
283  snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */
284 #else
285  sprintf(path, "<fd:%d>", fd); /* for debugging */
286 #endif
287  gz = gz_open(path, fd, mode);
288  free(path);
289  return gz;
290 }
local gzFile gz_open(const void *path, int fd, const char *mode)
Definition: gzlib.cc:89

Here is the call graph for this function:

int ZEXPORT gzeof ( gzFile  file)

Definition at line 485 of file gzlib.cc.

486 {
487  gz_statep state;
488 
489  /* get internal structure and check integrity */
490  if (file == NULL)
491  return 0;
492  state = (gz_statep)file;
493  if (state->mode != GZ_READ && state->mode != GZ_WRITE)
494  return 0;
495 
496  /* return end-of-file state */
497  return state->mode == GZ_READ ? state->past : 0;
498 }
#define GZ_READ
Definition: gzguts.h:151
#define GZ_WRITE
Definition: gzguts.h:152
gz_state FAR * gz_statep
Definition: gzguts.h:193
const char* ZEXPORT gzerror ( gzFile  file,
int errnum 
)

Definition at line 501 of file gzlib.cc.

502 {
503  gz_statep state;
504 
505  /* get internal structure and check integrity */
506  if (file == NULL)
507  return NULL;
508  state = (gz_statep)file;
509  if (state->mode != GZ_READ && state->mode != GZ_WRITE)
510  return NULL;
511 
512  /* return error information */
513  if (errnum != NULL)
514  *errnum = state->err;
515  return state->err == Z_MEM_ERROR ? "out of memory" :
516  (state->msg == NULL ? "" : state->msg);
517 }
#define GZ_READ
Definition: gzguts.h:151
#define GZ_WRITE
Definition: gzguts.h:152
#define Z_MEM_ERROR
Definition: zlib.h:179
gz_state FAR * gz_statep
Definition: gzguts.h:193
z_off_t ZEXPORT gzoffset ( gzFile  file)

Definition at line 476 of file gzlib.cc.

477 {
478  z_off64_t ret;
479 
480  ret = gzoffset64(file);
481  return ret == (z_off_t)ret ? (z_off_t)ret : -1;
482 }
z_off64_t ZEXPORT gzoffset64(gzFile file)
Definition: gzlib.cc:454

Here is the call graph for this function:

z_off64_t ZEXPORT gzoffset64 ( gzFile  file)

Definition at line 454 of file gzlib.cc.

455 {
456  z_off64_t offset;
457  gz_statep state;
458 
459  /* get internal structure and check integrity */
460  if (file == NULL)
461  return -1;
462  state = (gz_statep)file;
463  if (state->mode != GZ_READ && state->mode != GZ_WRITE)
464  return -1;
465 
466  /* compute and return effective offset in file */
467  offset = LSEEK(state->fd, 0, SEEK_CUR);
468  if (offset == -1)
469  return -1;
470  if (state->mode == GZ_READ) /* reading */
471  offset -= state->strm.avail_in; /* don't count buffered input */
472  return offset;
473 }
#define GZ_READ
Definition: gzguts.h:151
#define GZ_WRITE
Definition: gzguts.h:152
#define LSEEK
Definition: gzlib.cc:14
gz_state FAR * gz_statep
Definition: gzguts.h:193

Here is the caller graph for this function:

gzFile ZEXPORT gzopen ( const char *  path,
const char *  mode 
)

Definition at line 263 of file gzlib.cc.

264 {
265  return gz_open(path, -1, mode);
266 }
local gzFile gz_open(const void *path, int fd, const char *mode)
Definition: gzlib.cc:89

Here is the call graph for this function:

gzFile ZEXPORT gzopen64 ( const char *  path,
const char *  mode 
)

Definition at line 269 of file gzlib.cc.

270 {
271  return gz_open(path, -1, mode);
272 }
local gzFile gz_open(const void *path, int fd, const char *mode)
Definition: gzlib.cc:89

Here is the call graph for this function:

int ZEXPORT gzrewind ( gzFile  file)

Definition at line 324 of file gzlib.cc.

325 {
326  gz_statep state;
327 
328  /* get internal structure */
329  if (file == NULL)
330  return -1;
331  state = (gz_statep)file;
332 
333  /* check that we're reading and that there's no error */
334  if (state->mode != GZ_READ ||
335  (state->err != Z_OK && state->err != Z_BUF_ERROR))
336  return -1;
337 
338  /* back up and start over */
339  if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
340  return -1;
341  gz_reset(state);
342  return 0;
343 }
#define GZ_READ
Definition: gzguts.h:151
local void gz_reset(gz_statep state)
Definition: gzlib.cc:74
#define Z_BUF_ERROR
Definition: zlib.h:180
#define LSEEK
Definition: gzlib.cc:14
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:193

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 420 of file gzlib.cc.

421 {
422  z_off64_t ret;
423 
424  ret = gzseek64(file, (z_off64_t)offset, whence);
425  return ret == (z_off_t)ret ? (z_off_t)ret : -1;
426 }
z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence)
Definition: gzlib.cc:346

Here is the call graph for this function:

z_off64_t ZEXPORT gzseek64 ( gzFile  file,
z_off64_t  offset,
int  whence 
)

Definition at line 346 of file gzlib.cc.

347 {
348  unsigned n;
349  z_off64_t ret;
350  gz_statep state;
351 
352  /* get internal structure and check integrity */
353  if (file == NULL)
354  return -1;
355  state = (gz_statep)file;
356  if (state->mode != GZ_READ && state->mode != GZ_WRITE)
357  return -1;
358 
359  /* check that there's no error */
360  if (state->err != Z_OK && state->err != Z_BUF_ERROR)
361  return -1;
362 
363  /* can only seek from start or relative to current position */
364  if (whence != SEEK_SET && whence != SEEK_CUR)
365  return -1;
366 
367  /* normalize offset to a SEEK_CUR specification */
368  if (whence == SEEK_SET)
369  offset -= state->x.pos;
370  else if (state->seek)
371  offset += state->skip;
372  state->seek = 0;
373 
374  /* if within raw area while reading, just go there */
375  if (state->mode == GZ_READ && state->how == COPY &&
376  state->x.pos + offset >= 0) {
377  ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
378  if (ret == -1)
379  return -1;
380  state->x.have = 0;
381  state->eof = 0;
382  state->past = 0;
383  state->seek = 0;
384  gz_error(state, Z_OK, NULL);
385  state->strm.avail_in = 0;
386  state->x.pos += offset;
387  return state->x.pos;
388  }
389 
390  /* calculate skip amount, rewinding if needed for back seek when reading */
391  if (offset < 0) {
392  if (state->mode != GZ_READ) /* writing -- can't go backwards */
393  return -1;
394  offset += state->x.pos;
395  if (offset < 0) /* before start of file! */
396  return -1;
397  if (gzrewind(file) == -1) /* rewind, then skip to offset */
398  return -1;
399  }
400 
401  /* if reading, skip what's in output buffer (one less gzgetc() check) */
402  if (state->mode == GZ_READ) {
403  n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
404  (unsigned)offset : state->x.have;
405  state->x.have -= n;
406  state->x.next += n;
407  state->x.pos += n;
408  offset -= n;
409  }
410 
411  /* request skip (if not zero) */
412  if (offset) {
413  state->seek = 1;
414  state->skip = offset;
415  }
416  return state->x.pos + offset;
417 }
Definition: inflate.h:36
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.cc:545
#define GT_OFF(x)
Definition: gzguts.h:208
#define GZ_READ
Definition: gzguts.h:151
z_off64_t pos
Definition: zlib.h:1673
#define GZ_WRITE
Definition: gzguts.h:152
int ZEXPORT gzrewind(gzFile file)
Definition: gzlib.cc:324
#define Z_BUF_ERROR
Definition: zlib.h:180
#define LSEEK
Definition: gzlib.cc:14
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:193

Here is the call graph for this function:

Here is the caller graph for this function:

z_off_t ZEXPORT gztell ( gzFile  file)

Definition at line 445 of file gzlib.cc.

446 {
447  z_off64_t ret;
448 
449  ret = gztell64(file);
450  return ret == (z_off_t)ret ? (z_off_t)ret : -1;
451 }
z_off64_t ZEXPORT gztell64(gzFile file)
Definition: gzlib.cc:429

Here is the call graph for this function:

z_off64_t ZEXPORT gztell64 ( gzFile  file)

Definition at line 429 of file gzlib.cc.

430 {
431  gz_statep state;
432 
433  /* get internal structure and check integrity */
434  if (file == NULL)
435  return -1;
436  state = (gz_statep)file;
437  if (state->mode != GZ_READ && state->mode != GZ_WRITE)
438  return -1;
439 
440  /* return position */
441  return state->x.pos + (state->seek ? state->skip : 0);
442 }
#define GZ_READ
Definition: gzguts.h:151
z_off64_t pos
Definition: zlib.h:1673
#define GZ_WRITE
Definition: gzguts.h:152
gz_state FAR * gz_statep
Definition: gzguts.h:193

Here is the caller graph for this function:

local gzFile gz_open OF ( (const void *, int, const char *)  )