Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
gzwrite.cc File Reference
#include "gzguts.h"

Go to the source code of this file.

Functions

local int gz_init OF ((gz_statep))
 
local int gz_comp OF ((gz_statep, int))
 
local int gz_zero OF ((gz_statep, z_off64_t))
 
local int gz_init (gz_statep state)
 
local int gz_comp (gz_statep state, int flush)
 
local int gz_zero (gz_statep state, z_off64_t len)
 
int ZEXPORT gzwrite (gzFile file, voidpc buf, unsigned len)
 
int ZEXPORT gzputc (gzFile file, int c)
 
int ZEXPORT gzputs (gzFile file, const char *str)
 
int ZEXPORTVA gzprintf (gzFile file, const char *format, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20)
 
int ZEXPORT gzflush (gzFile file, int flush)
 
int ZEXPORT gzsetparams (gzFile file, int level, int strategy)
 
int ZEXPORT gzclose_w (gzFile file)
 

Function Documentation

local int gz_comp ( gz_statep  state,
int  flush 
)

Definition at line 69 of file gzwrite.cc.

References deflate(), deflateReset(), gz_error(), gz_init(), Z_ERRNO, Z_FINISH, Z_NO_FLUSH, Z_OK, Z_STREAM_END, Z_STREAM_ERROR, and zstrerror.

Referenced by gz_zero(), gzclose_w(), gzflush(), gzprintf(), gzsetparams(), and gzwrite().

70 {
71  int ret, got;
72  unsigned have;
73  z_streamp strm = &(state->strm);
74 
75  /* allocate memory if this is the first time through */
76  if (state->size == 0 && gz_init(state) == -1)
77  return -1;
78 
79  /* write directly if requested */
80  if (state->direct) {
81  got = write(state->fd, strm->next_in, strm->avail_in);
82  if (got < 0 || (unsigned)got != strm->avail_in) {
83  gz_error(state, Z_ERRNO, zstrerror());
84  return -1;
85  }
86  strm->avail_in = 0;
87  return 0;
88  }
89 
90  /* run deflate() on provided input until it produces no more output */
91  ret = Z_OK;
92  do {
93  /* write out current buffer contents if full, or if flushing, but if
94  doing Z_FINISH then don't write until we get to Z_STREAM_END */
95  if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
96  (flush != Z_FINISH || ret == Z_STREAM_END))) {
97  have = (unsigned)(strm->next_out - state->x.next);
98  if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
99  (unsigned)got != have)) {
100  gz_error(state, Z_ERRNO, zstrerror());
101  return -1;
102  }
103  if (strm->avail_out == 0) {
104  strm->avail_out = state->size;
105  strm->next_out = state->out;
106  }
107  state->x.next = strm->next_out;
108  }
109 
110  /* compress */
111  have = strm->avail_out;
112  ret = deflate(strm, flush);
113  if (ret == Z_STREAM_ERROR) {
114  gz_error(state, Z_STREAM_ERROR,
115  "internal error: deflate stream corrupt");
116  return -1;
117  }
118  have -= strm->avail_out;
119  } while (have);
120 
121  /* if that completed a deflate stream, allow another to start */
122  if (flush == Z_FINISH)
123  deflateReset(strm);
124 
125  /* all done, no errors */
126  return 0;
127 }
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.cc:534
#define Z_NO_FLUSH
Definition: zlib.h:164
#define Z_ERRNO
Definition: zlib.h:176
#define Z_STREAM_ERROR
Definition: zlib.h:177
#define Z_FINISH
Definition: zlib.h:168
#define zstrerror()
Definition: gzguts.h:109
#define Z_STREAM_END
Definition: zlib.h:174
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.cc:625
#define Z_OK
Definition: zlib.h:173
z_stream FAR * z_streamp
Definition: zlib.h:106
local int gz_init(gz_statep state)
Definition: gzwrite.cc:15
int ZEXPORT deflateReset(z_streamp strm)
Definition: deflate.cc:411
local int gz_init ( gz_statep  state)

Definition at line 15 of file gzwrite.cc.

References DEF_MEM_LEVEL, deflateInit2, free(), gz_error(), malloc(), Z_DEFLATED, Z_MEM_ERROR, Z_NULL, and Z_OK.

Referenced by gz_comp(), gzprintf(), and gzwrite().

16 {
17  int ret;
18  z_streamp strm = &(state->strm);
19 
20  /* allocate input buffer */
21  state->in = (unsigned char *)malloc(state->want);
22  if (state->in == NULL) {
23  gz_error(state, Z_MEM_ERROR, "out of memory");
24  return -1;
25  }
26 
27  /* only need output buffer and deflate state if compressing */
28  if (!state->direct) {
29  /* allocate output buffer */
30  state->out = (unsigned char *)malloc(state->want);
31  if (state->out == NULL) {
32  free(state->in);
33  gz_error(state, Z_MEM_ERROR, "out of memory");
34  return -1;
35  }
36 
37  /* allocate deflate memory, set up for gzip compression */
38  strm->zalloc = Z_NULL;
39  strm->zfree = Z_NULL;
40  strm->opaque = Z_NULL;
41  ret = deflateInit2(strm, state->level, Z_DEFLATED,
42  MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
43  if (ret != Z_OK) {
44  free(state->out);
45  free(state->in);
46  gz_error(state, Z_MEM_ERROR, "out of memory");
47  return -1;
48  }
49  }
50 
51  /* mark state as initialized */
52  state->size = state->want;
53 
54  /* initialize write buffer if compressing */
55  if (!state->direct) {
56  strm->avail_out = state->size;
57  strm->next_out = state->out;
58  state->x.next = strm->next_out;
59  }
60  return 0;
61 }
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.cc:534
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1634
void free(void *__ptr)
Definition: hjmalloc.cc:140
#define Z_DEFLATED
Definition: zlib.h:205
#define Z_MEM_ERROR
Definition: zlib.h:179
#define DEF_MEM_LEVEL
Definition: gzguts.h:127
#define Z_OK
Definition: zlib.h:173
#define Z_NULL
Definition: zlib.h:208
z_stream FAR * z_streamp
Definition: zlib.h:106
void * malloc(size_t __size)
Definition: hjmalloc.cc:30
local int gz_zero ( gz_statep  state,
z_off64_t  len 
)

Definition at line 130 of file gzwrite.cc.

References GT_OFF, gz_comp(), n, and Z_NO_FLUSH.

Referenced by gzclose_w(), gzflush(), gzprintf(), gzputc(), gzsetparams(), and gzwrite().

131 {
132  int first;
133  unsigned n;
134  z_streamp strm = &(state->strm);
135 
136  /* consume whatever's left in the input buffer */
137  if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
138  return -1;
139 
140  /* compress len zeros (len guaranteed > 0) */
141  first = 1;
142  while (len) {
143  n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
144  (unsigned)len : state->size;
145  if (first) {
146  memset(state->in, 0, n);
147  first = 0;
148  }
149  strm->avail_in = n;
150  strm->next_in = state->in;
151  state->x.pos += n;
152  if (gz_comp(state, Z_NO_FLUSH) == -1)
153  return -1;
154  len -= n;
155  }
156  return 0;
157 }
#define Z_NO_FLUSH
Definition: zlib.h:164
#define GT_OFF(x)
Definition: gzguts.h:192
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.cc:69
const G4int n
const XML_Char int len
z_stream FAR * z_streamp
Definition: zlib.h:106
int ZEXPORT gzclose_w ( gzFile  file)

Definition at line 507 of file gzwrite.cc.

References deflateEnd(), free(), gz_comp(), gz_error(), GZ_WRITE, gz_zero(), void(), Z_ERRNO, Z_FINISH, Z_OK, and Z_STREAM_ERROR.

Referenced by gzclose().

508 {
509  int ret = Z_OK;
510  gz_statep state;
511 
512  /* get internal structure */
513  if (file == NULL)
514  return Z_STREAM_ERROR;
515  state = (gz_statep)file;
516 
517  /* check that we're writing */
518  if (state->mode != GZ_WRITE)
519  return Z_STREAM_ERROR;
520 
521  /* check for seek request */
522  if (state->seek) {
523  state->seek = 0;
524  if (gz_zero(state, state->skip) == -1)
525  ret = state->err;
526  }
527 
528  /* flush, free memory, and close file */
529  if (state->size) {
530  if (gz_comp(state, Z_FINISH) == -1)
531  ret = state->err;
532  if (!state->direct) {
533  (void)deflateEnd(&(state->strm));
534  free(state->out);
535  }
536  free(state->in);
537  }
538  gz_error(state, Z_OK, NULL);
539  free(state->path);
540  if (close(state->fd) == -1)
541  ret = Z_ERRNO;
542  free(state);
543  return ret;
544 }
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.cc:937
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.cc:534
void free(void *__ptr)
Definition: hjmalloc.cc:140
#define Z_ERRNO
Definition: zlib.h:176
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData
#define Z_STREAM_ERROR
Definition: zlib.h:177
#define Z_FINISH
Definition: zlib.h:168
#define GZ_WRITE
Definition: gzguts.h:136
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.cc:69
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:177
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.cc:130
int ZEXPORT gzflush ( gzFile  file,
int  flush 
)

Definition at line 438 of file gzwrite.cc.

References gz_comp(), GZ_WRITE, gz_zero(), Z_FINISH, Z_OK, and Z_STREAM_ERROR.

439 {
440  gz_statep state;
441 
442  /* get internal structure */
443  if (file == NULL)
444  return -1;
445  state = (gz_statep)file;
446 
447  /* check that we're writing and that there's no error */
448  if (state->mode != GZ_WRITE || state->err != Z_OK)
449  return Z_STREAM_ERROR;
450 
451  /* check flush parameter */
452  if (flush < 0 || flush > Z_FINISH)
453  return Z_STREAM_ERROR;
454 
455  /* check for seek request */
456  if (state->seek) {
457  state->seek = 0;
458  if (gz_zero(state, state->skip) == -1)
459  return -1;
460  }
461 
462  /* compress remaining data with requested flush */
463  gz_comp(state, flush);
464  return state->err;
465 }
#define Z_STREAM_ERROR
Definition: zlib.h:177
#define Z_FINISH
Definition: zlib.h:168
#define GZ_WRITE
Definition: gzguts.h:136
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.cc:69
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:177
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.cc:130
int ZEXPORTVA gzprintf ( gzFile  file,
const char *  format,
int  a1,
int  a2,
int  a3,
int  a4,
int  a5,
int  a6,
int  a7,
int  a8,
int  a9,
int  a10,
int  a11,
int  a12,
int  a13,
int  a14,
int  a15,
int  a16,
int  a17,
int  a18,
int  a19,
int  a20 
)

Definition at line 362 of file gzwrite.cc.

References gz_comp(), gz_init(), GZ_WRITE, gz_zero(), int(), Z_NO_FLUSH, and Z_OK.

365 {
366  int size, len;
367  gz_statep state;
368  z_streamp strm;
369 
370  /* get internal structure */
371  if (file == NULL)
372  return -1;
373  state = (gz_statep)file;
374  strm = &(state->strm);
375 
376  /* check that can really pass pointer in ints */
377  if (sizeof(int) != sizeof(void *))
378  return 0;
379 
380  /* check that we're writing and that there's no error */
381  if (state->mode != GZ_WRITE || state->err != Z_OK)
382  return 0;
383 
384  /* make sure we have some buffer space */
385  if (state->size == 0 && gz_init(state) == -1)
386  return 0;
387 
388  /* check for seek request */
389  if (state->seek) {
390  state->seek = 0;
391  if (gz_zero(state, state->skip) == -1)
392  return 0;
393  }
394 
395  /* consume whatever's left in the input buffer */
396  if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
397  return 0;
398 
399  /* do the printf() into the input buffer, put length in len */
400  size = (int)(state->size);
401  state->in[size - 1] = 0;
402 #ifdef NO_snprintf
403 # ifdef HAS_sprintf_void
404  sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
405  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
406  for (len = 0; len < size; len++)
407  if (state->in[len] == 0) break;
408 # else
409  len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
410  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
411 # endif
412 #else
413 # ifdef HAS_snprintf_void
414  snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
415  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
416  len = strlen((char *)(state->in));
417 # else
418  len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
419  a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
420  a19, a20);
421 # endif
422 #endif
423 
424  /* check that printf() results fit in buffer */
425  if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
426  return 0;
427 
428  /* update buffer and position, defer compression until needed */
429  strm->avail_in = (unsigned)len;
430  strm->next_in = state->in;
431  state->x.pos += len;
432  return len;
433 }
#define Z_NO_FLUSH
Definition: zlib.h:164
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
#define GZ_WRITE
Definition: gzguts.h:136
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.cc:69
const XML_Char int len
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:177
z_stream FAR * z_streamp
Definition: zlib.h:106
local int gz_init(gz_statep state)
Definition: gzwrite.cc:15
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.cc:130
int ZEXPORT gzputc ( gzFile  file,
int  c 
)

Definition at line 235 of file gzwrite.cc.

References test::c, GZ_WRITE, gz_zero(), gzwrite(), and Z_OK.

236 {
237  unsigned char buf[1];
238  gz_statep state;
239  z_streamp strm;
240 
241  /* get internal structure */
242  if (file == NULL)
243  return -1;
244  state = (gz_statep)file;
245  strm = &(state->strm);
246 
247  /* check that we're writing and that there's no error */
248  if (state->mode != GZ_WRITE || state->err != Z_OK)
249  return -1;
250 
251  /* check for seek request */
252  if (state->seek) {
253  state->seek = 0;
254  if (gz_zero(state, state->skip) == -1)
255  return -1;
256  }
257 
258  /* try writing to input buffer for speed (state->size == 0 if buffer not
259  initialized) */
260  if (strm->avail_in < state->size) {
261  if (strm->avail_in == 0)
262  strm->next_in = state->in;
263  strm->next_in[strm->avail_in++] = c;
264  state->x.pos++;
265  return c & 0xff;
266  }
267 
268  /* no room in buffer or not initialized, use gz_write() */
269  buf[0] = c;
270  if (gzwrite(file, buf, 1) != 1)
271  return -1;
272  return c & 0xff;
273 }
#define GZ_WRITE
Definition: gzguts.h:136
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
Definition: gzwrite.cc:160
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:177
z_stream FAR * z_streamp
Definition: zlib.h:106
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.cc:130
int ZEXPORT gzputs ( gzFile  file,
const char *  str 
)

Definition at line 276 of file gzwrite.cc.

References gzwrite().

277 {
278  int ret;
279  unsigned len;
280 
281  /* write string */
282  len = (unsigned)strlen(str);
283  ret = gzwrite(file, str, len);
284  return ret == 0 && len != 0 ? -1 : ret;
285 }
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
Definition: gzwrite.cc:160
const XML_Char int len
int ZEXPORT gzsetparams ( gzFile  file,
int  level,
int  strategy 
)

Definition at line 468 of file gzwrite.cc.

References deflateParams(), gz_comp(), GZ_WRITE, gz_zero(), Z_OK, Z_PARTIAL_FLUSH, and Z_STREAM_ERROR.

469 {
470  gz_statep state;
471  z_streamp strm;
472 
473  /* get internal structure */
474  if (file == NULL)
475  return Z_STREAM_ERROR;
476  state = (gz_statep)file;
477  strm = &(state->strm);
478 
479  /* check that we're writing and that there's no error */
480  if (state->mode != GZ_WRITE || state->err != Z_OK)
481  return Z_STREAM_ERROR;
482 
483  /* if no change is requested, then do nothing */
484  if (level == state->level && strategy == state->strategy)
485  return Z_OK;
486 
487  /* check for seek request */
488  if (state->seek) {
489  state->seek = 0;
490  if (gz_zero(state, state->skip) == -1)
491  return -1;
492  }
493 
494  /* change compression parameters for subsequent input */
495  if (state->size) {
496  /* flush previous input with previous parameters before changing */
497  if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
498  return state->err;
499  deflateParams(strm, level, strategy);
500  }
501  state->level = level;
502  state->strategy = strategy;
503  return Z_OK;
504 }
#define Z_PARTIAL_FLUSH
Definition: zlib.h:165
#define Z_STREAM_ERROR
Definition: zlib.h:177
#define GZ_WRITE
Definition: gzguts.h:136
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
Definition: deflate.cc:465
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.cc:69
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:177
z_stream FAR * z_streamp
Definition: zlib.h:106
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.cc:130
int ZEXPORT gzwrite ( gzFile  file,
voidpc  buf,
unsigned  len 
)

Definition at line 160 of file gzwrite.cc.

References gz_comp(), gz_error(), gz_init(), GZ_WRITE, gz_zero(), n, Z_DATA_ERROR, Z_NO_FLUSH, and Z_OK.

Referenced by gzputc(), and gzputs().

161 {
162  unsigned put = len;
163  unsigned n;
164  gz_statep state;
165  z_streamp strm;
166 
167  /* get internal structure */
168  if (file == NULL)
169  return 0;
170  state = (gz_statep)file;
171  strm = &(state->strm);
172 
173  /* check that we're writing and that there's no error */
174  if (state->mode != GZ_WRITE || state->err != Z_OK)
175  return 0;
176 
177  /* since an int is returned, make sure len fits in one, otherwise return
178  with an error (this avoids the flaw in the interface) */
179  if ((int)len < 0) {
180  gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
181  return 0;
182  }
183 
184  /* if len is zero, avoid unnecessary operations */
185  if (len == 0)
186  return 0;
187 
188  /* allocate memory if this is the first time through */
189  if (state->size == 0 && gz_init(state) == -1)
190  return 0;
191 
192  /* check for seek request */
193  if (state->seek) {
194  state->seek = 0;
195  if (gz_zero(state, state->skip) == -1)
196  return 0;
197  }
198 
199  /* for small len, copy to input buffer, otherwise compress directly */
200  if (len < state->size) {
201  /* copy to input buffer, compress when full */
202  do {
203  if (strm->avail_in == 0)
204  strm->next_in = state->in;
205  n = state->size - strm->avail_in;
206  if (n > len)
207  n = len;
208  memcpy(strm->next_in + strm->avail_in, buf, n);
209  strm->avail_in += n;
210  state->x.pos += n;
211  buf = (char *)buf + n;
212  len -= n;
213  if (len && gz_comp(state, Z_NO_FLUSH) == -1)
214  return 0;
215  } while (len);
216  }
217  else {
218  /* consume whatever's left in the input buffer */
219  if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
220  return 0;
221 
222  /* directly compress user buffer to file */
223  strm->avail_in = len;
224  strm->next_in = (z_const Bytef *)buf;
225  state->x.pos += len;
226  if (gz_comp(state, Z_NO_FLUSH) == -1)
227  return 0;
228  }
229 
230  /* input was all buffered or compressed (put will fit in int) */
231  return (int)put;
232 }
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.cc:534
#define Z_NO_FLUSH
Definition: zlib.h:164
#define Z_DATA_ERROR
Definition: zlib.h:178
#define GZ_WRITE
Definition: gzguts.h:136
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.cc:69
const G4int n
const XML_Char int len
#define Z_OK
Definition: zlib.h:173
gz_state FAR * gz_statep
Definition: gzguts.h:177
z_stream FAR * z_streamp
Definition: zlib.h:106
local int gz_init(gz_statep state)
Definition: gzwrite.cc:15
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.cc:130
local int gz_init OF ( (gz_statep )
local int gz_comp OF ( (gz_statep, int )
local int gz_zero OF ( (gz_statep, z_off64_t)  )