Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
gzwrite.c File Reference
#include "gzguts.h"

Go to the source code of this file.

Functions

int gz_init OF ((gz_statep))
 
int gz_comp OF ((gz_statep, int))
 
int gz_zero OF ((gz_statep, z_off64_t))
 
z_size_t gz_write OF ((gz_statep, voidpc, z_size_t))
 
int gz_init (gz_statep state)
 
int gz_comp (gz_statep state, int flush)
 
int gz_zero (gz_statep state, z_off64_t len)
 
z_size_t gz_write (gz_statep state, voidpc buf, z_size_t len)
 
int ZEXPORT gzwrite (gzFile file, voidpc buf, unsigned len)
 
z_size_t ZEXPORT gzfwrite (voidpc buf, z_size_t size, z_size_t nitems, gzFile file)
 
int ZEXPORT gzputc (gzFile file, int c)
 
int ZEXPORT gzputs (gzFile file, const char *s)
 
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

◆ gz_comp()

int gz_comp ( gz_statep  state,
int  flush 
)

Definition at line 73 of file gzwrite.c.

76{
77 int ret, writ;
78 unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
79 z_streamp strm = &(state->strm);
80
81 /* allocate memory if this is the first time through */
82 if (state->size == 0 && gz_init(state) == -1)
83 return -1;
84
85 /* write directly if requested */
86 if (state->direct) {
87 while (strm->avail_in) {
88 put = strm->avail_in > max ? max : strm->avail_in;
89 writ = write(state->fd, strm->next_in, put);
90 if (writ < 0) {
91 gz_error(state, Z_ERRNO, zstrerror());
92 return -1;
93 }
94 strm->avail_in -= (unsigned)writ;
95 strm->next_in += writ;
96 }
97 return 0;
98 }
99
100 /* check for a pending reset */
101 if (state->reset) {
102 /* don't start a new gzip member unless there is data to write */
103 if (strm->avail_in == 0)
104 return 0;
105 deflateReset(strm);
106 state->reset = 0;
107 }
108
109 /* run deflate() on provided input until it produces no more output */
110 ret = Z_OK;
111 do {
112 /* write out current buffer contents if full, or if flushing, but if
113 doing Z_FINISH then don't write until we get to Z_STREAM_END */
114 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
115 (flush != Z_FINISH || ret == Z_STREAM_END))) {
116 while (strm->next_out > state->x.next) {
117 put = strm->next_out - state->x.next > (int)max ? max :
118 (unsigned)(strm->next_out - state->x.next);
119 writ = write(state->fd, state->x.next, put);
120 if (writ < 0) {
121 gz_error(state, Z_ERRNO, zstrerror());
122 return -1;
123 }
124 state->x.next += writ;
125 }
126 if (strm->avail_out == 0) {
127 strm->avail_out = state->size;
128 strm->next_out = state->out;
129 state->x.next = state->out;
130 }
131 }
132
133 /* compress */
134 have = strm->avail_out;
135 ret = deflate(strm, flush);
136 if (ret == Z_STREAM_ERROR) {
138 "internal error: deflate stream corrupt");
139 return -1;
140 }
141 have -= strm->avail_out;
142 } while (have);
143
144 /* if that completed a deflate stream, allow another to start */
145 if (flush == Z_FINISH)
146 state->reset = 1;
147
148 /* all done, no errors */
149 return 0;
150}
int ZEXPORT deflateReset(z_streamp strm)
Definition: deflate.c:548
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:807
#define zstrerror()
Definition: gzguts.h:133
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.c:581
int gz_init(gz_statep state)
Definition: gzwrite.c:17
T max(const T t1, const T t2)
brief Return the largest of the two arguments
#define Z_ERRNO
Definition: zlib.h:180
z_stream FAR * z_streamp
Definition: zlib.h:108
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_FINISH
Definition: zlib.h:172
#define Z_OK
Definition: zlib.h:177
#define Z_STREAM_ERROR
Definition: zlib.h:181
#define Z_NO_FLUSH
Definition: zlib.h:168

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

◆ gz_init()

int gz_init ( gz_statep  state)

Definition at line 17 of file gzwrite.c.

19{
20 int ret;
21 z_streamp strm = &(state->strm);
22
23 /* allocate input buffer (double size for gzprintf) */
24 state->in = (unsigned char *)malloc(state->want << 1);
25 if (state->in == NULL) {
26 gz_error(state, Z_MEM_ERROR, "out of memory");
27 return -1;
28 }
29
30 /* only need output buffer and deflate state if compressing */
31 if (!state->direct) {
32 /* allocate output buffer */
33 state->out = (unsigned char *)malloc(state->want);
34 if (state->out == NULL) {
35 free(state->in);
36 gz_error(state, Z_MEM_ERROR, "out of memory");
37 return -1;
38 }
39
40 /* allocate deflate memory, set up for gzip compression */
41 strm->zalloc = Z_NULL;
42 strm->zfree = Z_NULL;
43 strm->opaque = Z_NULL;
44 ret = deflateInit2(strm, state->level, Z_DEFLATED,
45 MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
46 if (ret != Z_OK) {
47 free(state->out);
48 free(state->in);
49 gz_error(state, Z_MEM_ERROR, "out of memory");
50 return -1;
51 }
52 strm->next_in = NULL;
53 }
54
55 /* mark state as initialized */
56 state->size = state->want;
57
58 /* initialize write buffer if compressing */
59 if (!state->direct) {
60 strm->avail_out = state->size;
61 strm->next_out = state->out;
62 state->x.next = strm->next_out;
63 }
64 return 0;
65}
#define DEF_MEM_LEVEL
Definition: gzguts.h:151
#define Z_DEFLATED
Definition: zlib.h:209
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1814
#define Z_NULL
Definition: zlib.h:212
#define Z_MEM_ERROR
Definition: zlib.h:183

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

◆ gz_write()

z_size_t gz_write ( gz_statep  state,
voidpc  buf,
z_size_t  len 
)

Definition at line 187 of file gzwrite.c.

191{
192 z_size_t put = len;
193
194 /* if len is zero, avoid unnecessary operations */
195 if (len == 0)
196 return 0;
197
198 /* allocate memory if this is the first time through */
199 if (state->size == 0 && gz_init(state) == -1)
200 return 0;
201
202 /* check for seek request */
203 if (state->seek) {
204 state->seek = 0;
205 if (gz_zero(state, state->skip) == -1)
206 return 0;
207 }
208
209 /* for small len, copy to input buffer, otherwise compress directly */
210 if (len < state->size) {
211 /* copy to input buffer, compress when full */
212 do {
213 unsigned have, copy;
214
215 if (state->strm.avail_in == 0)
216 state->strm.next_in = state->in;
217 have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
218 state->in);
219 copy = state->size - have;
220 if (copy > len)
221 copy = (unsigned)len;
222 memcpy(state->in + have, buf, copy);
223 state->strm.avail_in += copy;
224 state->x.pos += copy;
225 buf = (const char *)buf + copy;
226 len -= copy;
227 if (len && gz_comp(state, Z_NO_FLUSH) == -1)
228 return 0;
229 } while (len);
230 }
231 else {
232 /* consume whatever's left in the input buffer */
233 if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
234 return 0;
235
236 /* directly compress user buffer to file */
237 state->strm.next_in = (z_const Bytef *)buf;
238 do {
239 unsigned n = (unsigned)-1;
240 if (n > len)
241 n = (unsigned)len;
242 state->strm.avail_in = n;
243 state->x.pos += n;
244 if (gz_comp(state, Z_NO_FLUSH) == -1)
245 return 0;
246 len -= n;
247 } while (len);
248 }
249
250 /* input was all buffered or compressed */
251 return put;
252}
int gz_comp(gz_statep state, int flush)
Definition: gzwrite.c:73
int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.c:154
void copy(G4double dst[], const G4double src[], std::size_t size=G4FieldTrack::ncompSVEC)
Definition: G4FieldUtils.cc:98

Referenced by gzfwrite(), gzputc(), gzputs(), and gzwrite().

◆ gz_zero()

int gz_zero ( gz_statep  state,
z_off64_t  len 
)

Definition at line 154 of file gzwrite.c.

157{
158 int first;
159 unsigned n;
160 z_streamp strm = &(state->strm);
161
162 /* consume whatever's left in the input buffer */
163 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
164 return -1;
165
166 /* compress len zeros (len guaranteed > 0) */
167 first = 1;
168 while (len) {
169 n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
170 (unsigned)len : state->size;
171 if (first) {
172 memset(state->in, 0, n);
173 first = 0;
174 }
175 strm->avail_in = n;
176 strm->next_in = state->in;
177 state->x.pos += n;
178 if (gz_comp(state, Z_NO_FLUSH) == -1)
179 return -1;
180 len -= n;
181 }
182 return 0;
183}
#define GT_OFF(x)
Definition: gzguts.h:218

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

◆ gzclose_w()

int ZEXPORT gzclose_w ( gzFile  file)

Definition at line 639 of file gzwrite.c.

641{
642 int ret = Z_OK;
643 gz_statep state;
644
645 /* get internal structure */
646 if (file == NULL)
647 return Z_STREAM_ERROR;
648 state = (gz_statep)file;
649
650 /* check that we're writing */
651 if (state->mode != GZ_WRITE)
652 return Z_STREAM_ERROR;
653
654 /* check for seek request */
655 if (state->seek) {
656 state->seek = 0;
657 if (gz_zero(state, state->skip) == -1)
658 ret = state->err;
659 }
660
661 /* flush, free memory, and close file */
662 if (gz_comp(state, Z_FINISH) == -1)
663 ret = state->err;
664 if (state->size) {
665 if (!state->direct) {
666 (void)deflateEnd(&(state->strm));
667 free(state->out);
668 }
669 free(state->in);
670 }
671 gz_error(state, Z_OK, NULL);
672 free(state->path);
673 if (close(state->fd) == -1)
674 ret = Z_ERRNO;
675 free(state);
676 return ret;
677}
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1122
gz_state FAR * gz_statep
Definition: gzguts.h:203
#define GZ_WRITE
Definition: gzguts.h:161

Referenced by gzclose().

◆ gzflush()

int ZEXPORT gzflush ( gzFile  file,
int  flush 
)

Definition at line 565 of file gzwrite.c.

568{
569 gz_statep state;
570
571 /* get internal structure */
572 if (file == NULL)
573 return Z_STREAM_ERROR;
574 state = (gz_statep)file;
575
576 /* check that we're writing and that there's no error */
577 if (state->mode != GZ_WRITE || state->err != Z_OK)
578 return Z_STREAM_ERROR;
579
580 /* check flush parameter */
581 if (flush < 0 || flush > Z_FINISH)
582 return Z_STREAM_ERROR;
583
584 /* check for seek request */
585 if (state->seek) {
586 state->seek = 0;
587 if (gz_zero(state, state->skip) == -1)
588 return state->err;
589 }
590
591 /* compress remaining data with requested flush */
592 (void)gz_comp(state, flush);
593 return state->err;
594}

◆ gzfwrite()

z_size_t ZEXPORT gzfwrite ( voidpc  buf,
z_size_t  size,
z_size_t  nitems,
gzFile  file 
)

Definition at line 283 of file gzwrite.c.

288{
289 z_size_t len;
290 gz_statep state;
291
292 /* get internal structure */
293 if (file == NULL)
294 return 0;
295 state = (gz_statep)file;
296
297 /* check that we're writing and that there's no error */
298 if (state->mode != GZ_WRITE || state->err != Z_OK)
299 return 0;
300
301 /* compute bytes to read -- error on overflow */
302 len = nitems * size;
303 if (size && len / size != nitems) {
304 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
305 return 0;
306 }
307
308 /* write len bytes to buf, return the number of full items written */
309 return len ? gz_write(state, buf, len) / size : 0;
310}
z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len)
Definition: gzwrite.c:187

◆ gzprintf()

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 477 of file gzwrite.c.

483{
484 unsigned len, left;
485 char *next;
486 gz_statep state;
487 z_streamp strm;
488
489 /* get internal structure */
490 if (file == NULL)
491 return Z_STREAM_ERROR;
492 state = (gz_statep)file;
493 strm = &(state->strm);
494
495 /* check that can really pass pointer in ints */
496 if (sizeof(int) != sizeof(void *))
497 return Z_STREAM_ERROR;
498
499 /* check that we're writing and that there's no error */
500 if (state->mode != GZ_WRITE || state->err != Z_OK)
501 return Z_STREAM_ERROR;
502
503 /* make sure we have some buffer space */
504 if (state->size == 0 && gz_init(state) == -1)
505 return state->error;
506
507 /* check for seek request */
508 if (state->seek) {
509 state->seek = 0;
510 if (gz_zero(state, state->skip) == -1)
511 return state->error;
512 }
513
514 /* do the printf() into the input buffer, put length in len -- the input
515 buffer is double-sized just for this function, so there is guaranteed to
516 be state->size bytes available after the current contents */
517 if (strm->avail_in == 0)
518 strm->next_in = state->in;
519 next = (char *)(strm->next_in + strm->avail_in);
520 next[state->size - 1] = 0;
521#ifdef NO_snprintf
522# ifdef HAS_sprintf_void
523 sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
524 a13, a14, a15, a16, a17, a18, a19, a20);
525 for (len = 0; len < size; len++)
526 if (next[len] == 0)
527 break;
528# else
529 len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
530 a12, a13, a14, a15, a16, a17, a18, a19, a20);
531# endif
532#else
533# ifdef HAS_snprintf_void
534 snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
535 a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
536 len = strlen(next);
537# else
538 len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
539 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
540# endif
541#endif
542
543 /* check that printf() results fit in buffer */
544 if (len == 0 || len >= state->size || next[state->size - 1] != 0)
545 return 0;
546
547 /* update buffer and position, compress first half if past that */
548 strm->avail_in += len;
549 state->x.pos += len;
550 if (strm->avail_in >= state->size) {
551 left = strm->avail_in - state->size;
552 strm->avail_in = state->size;
553 if (gz_comp(state, Z_NO_FLUSH) == -1)
554 return state->err;
555 memmove(state->in, state->in + state->size, left);
556 strm->next_in = state->in;
557 strm->avail_in = left;
558 }
559 return (int)len;
560}

◆ gzputc()

int ZEXPORT gzputc ( gzFile  file,
int  c 
)

Definition at line 313 of file gzwrite.c.

316{
317 unsigned have;
318 unsigned char buf[1];
319 gz_statep state;
320 z_streamp strm;
321
322 /* get internal structure */
323 if (file == NULL)
324 return -1;
325 state = (gz_statep)file;
326 strm = &(state->strm);
327
328 /* check that we're writing and that there's no error */
329 if (state->mode != GZ_WRITE || state->err != Z_OK)
330 return -1;
331
332 /* check for seek request */
333 if (state->seek) {
334 state->seek = 0;
335 if (gz_zero(state, state->skip) == -1)
336 return -1;
337 }
338
339 /* try writing to input buffer for speed (state->size == 0 if buffer not
340 initialized) */
341 if (state->size) {
342 if (strm->avail_in == 0)
343 strm->next_in = state->in;
344 have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
345 if (have < state->size) {
346 state->in[have] = (unsigned char)c;
347 strm->avail_in++;
348 state->x.pos++;
349 return c & 0xff;
350 }
351 }
352
353 /* no room in buffer or not initialized, use gz_write() */
354 buf[0] = (unsigned char)c;
355 if (gz_write(state, buf, 1) != 1)
356 return -1;
357 return c & 0xff;
358}

◆ gzputs()

int ZEXPORT gzputs ( gzFile  file,
const char *  s 
)

Definition at line 361 of file gzwrite.c.

364{
365 z_size_t len, put;
366 gz_statep state;
367
368 /* get internal structure */
369 if (file == NULL)
370 return -1;
371 state = (gz_statep)file;
372
373 /* check that we're writing and that there's no error */
374 if (state->mode != GZ_WRITE || state->err != Z_OK)
375 return -1;
376
377 /* write string */
378 len = strlen(s);
379 if ((int)len < 0 || (unsigned)len != len) {
380 gz_error(state, Z_STREAM_ERROR, "string length does not fit in int");
381 return -1;
382 }
383 put = gz_write(state, s, len);
384 return put < len ? -1 : (int)len;
385}

◆ gzsetparams()

int ZEXPORT gzsetparams ( gzFile  file,
int  level,
int  strategy 
)

Definition at line 597 of file gzwrite.c.

601{
602 gz_statep state;
603 z_streamp strm;
604
605 /* get internal structure */
606 if (file == NULL)
607 return Z_STREAM_ERROR;
608 state = (gz_statep)file;
609 strm = &(state->strm);
610
611 /* check that we're writing and that there's no error */
612 if (state->mode != GZ_WRITE || state->err != Z_OK)
613 return Z_STREAM_ERROR;
614
615 /* if no change is requested, then do nothing */
616 if (level == state->level && strategy == state->strategy)
617 return Z_OK;
618
619 /* check for seek request */
620 if (state->seek) {
621 state->seek = 0;
622 if (gz_zero(state, state->skip) == -1)
623 return state->err;
624 }
625
626 /* change compression parameters for subsequent input */
627 if (state->size) {
628 /* flush previous input with previous parameters before changing */
629 if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
630 return state->err;
631 deflateParams(strm, level, strategy);
632 }
633 state->level = level;
634 state->strategy = strategy;
635 return Z_OK;
636}
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
Definition: deflate.c:612
#define Z_BLOCK
Definition: zlib.h:173

◆ gzwrite()

int ZEXPORT gzwrite ( gzFile  file,
voidpc  buf,
unsigned  len 
)

Definition at line 255 of file gzwrite.c.

259{
260 gz_statep state;
261
262 /* get internal structure */
263 if (file == NULL)
264 return 0;
265 state = (gz_statep)file;
266
267 /* check that we're writing and that there's no error */
268 if (state->mode != GZ_WRITE || state->err != Z_OK)
269 return 0;
270
271 /* since an int is returned, make sure len fits in one, otherwise return
272 with an error (this avoids a flaw in the interface) */
273 if ((int)len < 0) {
274 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
275 return 0;
276 }
277
278 /* write len bytes from buf (the return value will fit in an int) */
279 return (int)gz_write(state, buf, len);
280}
#define Z_DATA_ERROR
Definition: zlib.h:182

◆ OF() [1/4]

int gz_init OF ( (gz_statep )

◆ OF() [2/4]

int gz_comp OF ( (gz_statep, int)  )

◆ OF() [3/4]

z_size_t gz_write OF ( (gz_statep, voidpc, z_size_t)  )

◆ OF() [4/4]

int gz_zero OF ( (gz_statep, z_off64_t)  )