abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
gzwrite.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc/util/abc_global.h"
#include "gzguts.h"

Go to the source code of this file.

Functions

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

66 {
67  int ret, got;
68  unsigned have;
69  z_streamp strm = &(state->strm);
70 
71  /* allocate memory if this is the first time through */
72  if (state->size == 0 && gz_init(state) == -1)
73  return -1;
74 
75  /* run deflate() on provided input until it produces no more output */
76  ret = Z_OK;
77  do {
78  /* write out current buffer contents if full, or if flushing, but if
79  doing Z_FINISH then don't write until we get to Z_STREAM_END */
80  if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
81  (flush != Z_FINISH || ret == Z_STREAM_END))) {
82  have = (unsigned)(strm->next_out - state->next);
83  if (have && ((got = write(state->fd, state->next, have)) < 0 ||
84  (unsigned)got != have)) {
85  gz_error(state, Z_ERRNO, zstrerror());
86  return -1;
87  }
88  if (strm->avail_out == 0) {
89  strm->avail_out = state->size;
90  strm->next_out = state->out;
91  }
92  state->next = strm->next_out;
93  }
94 
95  /* compress */
96  have = strm->avail_out;
97  ret = deflate(strm, flush);
98  if (ret == Z_STREAM_ERROR) {
99  gz_error(state, Z_STREAM_ERROR,
100  "internal error: deflate stream corrupt");
101  return -1;
102  }
103  have -= strm->avail_out;
104  } while (have);
105 
106  /* if that completed a deflate stream, allow another to start */
107  if (flush == Z_FINISH)
108  deflateReset(strm);
109 
110  /* all done, no errors */
111  return 0;
112 }
#define Z_NO_FLUSH
Definition: zlib.h:172
#define Z_ERRNO
Definition: zlib.h:184
#define Z_STREAM_ERROR
Definition: zlib.h:185
local int gz_init(gz_statep state)
Definition: gzwrite.c:22
#define Z_FINISH
Definition: zlib.h:176
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.c:464
#define zstrerror()
Definition: gzguts.h:59
#define Z_STREAM_END
Definition: zlib.h:182
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:555
#define Z_OK
Definition: zlib.h:181
z_stream FAR * z_streamp
Definition: zlib.h:114
int ZEXPORT deflateReset(z_streamp strm)
Definition: deflate.c:345
local int gz_init ( gz_statep  state)

Definition at line 22 of file gzwrite.c.

23 {
24  int ret;
25  z_streamp strm = &(state->strm);
26 
27  /* allocate input and output buffers */
28  state->in = (unsigned char *)malloc(state->want);
29  state->out = (unsigned char *)malloc(state->want);
30  if (state->in == NULL || state->out == NULL) {
31  if (state->out != NULL)
32  free(state->out);
33  if (state->in != NULL)
34  free(state->in);
35  gz_error(state, Z_MEM_ERROR, "out of memory");
36  return -1;
37  }
38 
39  /* allocate deflate memory, set up for gzip compression */
40  strm->zalloc = Z_NULL;
41  strm->zfree = Z_NULL;
42  strm->opaque = Z_NULL;
43  ret = deflateInit2(strm, state->level, Z_DEFLATED,
44  15 + 16, 8, state->strategy);
45  if (ret != Z_OK) {
46  free(state->in);
47  gz_error(state, Z_MEM_ERROR, "out of memory");
48  return -1;
49  }
50 
51  /* mark state as initialized */
52  state->size = state->want;
53 
54  /* initialize write buffer */
55  strm->avail_out = state->size;
56  strm->next_out = state->out;
57  state->next = strm->next_out;
58  return 0;
59 }
char * malloc()
VOID_HACK free()
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1558
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.c:464
#define Z_DEFLATED
Definition: zlib.h:213
#define Z_MEM_ERROR
Definition: zlib.h:187
#define Z_OK
Definition: zlib.h:181
#define Z_NULL
Definition: zlib.h:216
z_stream FAR * z_streamp
Definition: zlib.h:114
local int gz_zero ( gz_statep  state,
z_off64_t  len 
)

Definition at line 115 of file gzwrite.c.

116 {
117  int first;
118  unsigned n;
119  z_streamp strm = &(state->strm);
120 
121  /* consume whatever's left in the input buffer */
122  if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
123  return -1;
124 
125  /* compress len zeros (len guaranteed > 0) */
126  first = 1;
127  while (len) {
128  n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
129  (unsigned)len : state->size;
130  if (first) {
131  memset(state->in, 0, n);
132  first = 0;
133  }
134  strm->avail_in = n;
135  strm->next_in = state->in;
136  state->pos += n;
137  if (gz_comp(state, Z_NO_FLUSH) == -1)
138  return -1;
139  len -= n;
140  }
141  return 0;
142 }
char * memset()
#define Z_NO_FLUSH
Definition: zlib.h:172
#define GT_OFF(x)
Definition: gzguts.h:144
#define z_off64_t
Definition: zconf.h:402
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.c:65
z_stream FAR * z_streamp
Definition: zlib.h:114
int ZEXPORT gzclose_w ( gzFile  file)

Definition at line 486 of file gzwrite.c.

487 {
488  int ret = 0;
489  gz_statep state;
490 
491  /* get internal structure */
492  if (file == NULL)
493  return Z_STREAM_ERROR;
494  state = (gz_statep)file;
495 
496  /* check that we're writing */
497  if (state->mode != GZ_WRITE)
498  return Z_STREAM_ERROR;
499 
500  /* check for seek request */
501  if (state->seek) {
502  state->seek = 0;
503  ret += gz_zero(state, state->skip);
504  }
505 
506  /* flush, free memory, and close file */
507  ret += gz_comp(state, Z_FINISH);
508  (void)deflateEnd(&(state->strm));
509  free(state->out);
510  free(state->in);
511  gz_error(state, Z_OK, NULL);
512  free(state->path);
513  ret += close(state->fd);
514  free(state);
515  return ret ? Z_ERRNO : Z_OK;
516 }
VOID_HACK free()
#define Z_ERRNO
Definition: zlib.h:184
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:866
#define Z_STREAM_ERROR
Definition: zlib.h:185
#define Z_FINISH
Definition: zlib.h:176
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.c:464
#define GZ_WRITE
Definition: gzguts.h:90
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.c:115
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.c:65
#define Z_OK
Definition: zlib.h:181
gz_state FAR * gz_statep
Definition: gzguts.h:129
int ZEXPORT gzflush ( gzFile  file,
int  flush 
)

Definition at line 417 of file gzwrite.c.

418 {
419  gz_statep state;
420 
421  /* get internal structure */
422  if (file == NULL)
423  return -1;
424  state = (gz_statep)file;
425 
426  /* check that we're writing and that there's no error */
427  if (state->mode != GZ_WRITE || state->err != Z_OK)
428  return Z_STREAM_ERROR;
429 
430  /* check flush parameter */
431  if (flush < 0 || flush > Z_FINISH)
432  return Z_STREAM_ERROR;
433 
434  /* check for seek request */
435  if (state->seek) {
436  state->seek = 0;
437  if (gz_zero(state, state->skip) == -1)
438  return -1;
439  }
440 
441  /* compress remaining data with requested flush */
442  gz_comp(state, flush);
443  return state->err;
444 }
#define Z_STREAM_ERROR
Definition: zlib.h:185
#define Z_FINISH
Definition: zlib.h:176
#define GZ_WRITE
Definition: gzguts.h:90
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.c:115
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.c:65
#define Z_OK
Definition: zlib.h:181
gz_state FAR * gz_statep
Definition: gzguts.h:129
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 347 of file gzwrite.c.

349 {
350  int size, len;
351  gz_statep state;
352  z_streamp strm;
353 
354  /* get internal structure */
355  if (file == NULL)
356  return -1;
357  state = (gz_statep)file;
358  strm = &(state->strm);
359 
360  /* check that we're writing and that there's no error */
361  if (state->mode != GZ_WRITE || state->err != Z_OK)
362  return 0;
363 
364  /* make sure we have some buffer space */
365  if (state->size == 0 && gz_init(state) == -1)
366  return 0;
367 
368  /* check for seek request */
369  if (state->seek) {
370  state->seek = 0;
371  if (gz_zero(state, state->skip) == -1)
372  return 0;
373  }
374 
375  /* consume whatever's left in the input buffer */
376  if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
377  return 0;
378 
379  /* do the printf() into the input buffer, put length in len */
380  size = (int)(state->size);
381  state->in[size - 1] = 0;
382 #ifdef NO_snprintf
383 # ifdef HAS_sprintf_void
384  sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
385  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
386  for (len = 0; len < size; len++)
387  if (state->in[len] == 0) break;
388 # else
389  len = sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
390  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
391 # endif
392 #else
393 # ifdef HAS_snprintf_void
394  snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
395  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
396  len = strlen(state->in);
397 # else
398  len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
399  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
400 # endif
401 #endif
402 
403  /* check that printf() results fit in buffer */
404  if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
405  return 0;
406 
407  /* update buffer and position, defer compression until needed */
408  strm->avail_in = (unsigned)len;
409  strm->next_in = state->in;
410  state->pos += len;
411  return len;
412 }
#define Z_NO_FLUSH
Definition: zlib.h:172
#define a1
Definition: extraBdd.h:80
local int gz_init(gz_statep state)
Definition: gzwrite.c:22
#define GZ_WRITE
Definition: gzguts.h:90
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.c:115
static int size
Definition: cuddSign.c:86
char * sprintf()
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.c:65
#define Z_OK
Definition: zlib.h:181
gz_state FAR * gz_statep
Definition: gzguts.h:129
int strlen()
z_stream FAR * z_streamp
Definition: zlib.h:114
int ZEXPORT gzputc ( gzFile  file,
int  c 
)

Definition at line 220 of file gzwrite.c.

221 {
222  unsigned char buf[1];
223  gz_statep state;
224  z_streamp strm;
225 
226  /* get internal structure */
227  if (file == NULL)
228  return -1;
229  state = (gz_statep)file;
230  strm = &(state->strm);
231 
232  /* check that we're writing and that there's no error */
233  if (state->mode != GZ_WRITE || state->err != Z_OK)
234  return -1;
235 
236  /* check for seek request */
237  if (state->seek) {
238  state->seek = 0;
239  if (gz_zero(state, state->skip) == -1)
240  return -1;
241  }
242 
243  /* try writing to input buffer for speed (state->size == 0 if buffer not
244  initialized) */
245  if (strm->avail_in < state->size) {
246  if (strm->avail_in == 0)
247  strm->next_in = state->in;
248  strm->next_in[strm->avail_in++] = c;
249  state->pos++;
250  return c;
251  }
252 
253  /* no room in buffer or not initialized, use gz_write() */
254  buf[0] = c;
255  if (gzwrite(file, buf, 1) != 1)
256  return -1;
257  return c;
258 }
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
Definition: gzwrite.c:145
#define GZ_WRITE
Definition: gzguts.h:90
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.c:115
#define Z_OK
Definition: zlib.h:181
gz_state FAR * gz_statep
Definition: gzguts.h:129
z_stream FAR * z_streamp
Definition: zlib.h:114
int ZEXPORT gzputs ( gzFile  file,
const char *  str 
)

Definition at line 261 of file gzwrite.c.

262 {
263  int ret;
264  unsigned len;
265 
266  /* write string */
267  len = (unsigned)strlen(str);
268  ret = gzwrite(file, str, len);
269  return ret == 0 && len != 0 ? -1 : ret;
270 }
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
Definition: gzwrite.c:145
int strlen()
int ZEXPORT gzsetparams ( gzFile  file,
int  level,
int  strategy 
)

Definition at line 447 of file gzwrite.c.

448 {
449  gz_statep state;
450  z_streamp strm;
451 
452  /* get internal structure */
453  if (file == NULL)
454  return Z_STREAM_ERROR;
455  state = (gz_statep)file;
456  strm = &(state->strm);
457 
458  /* check that we're writing and that there's no error */
459  if (state->mode != GZ_WRITE || state->err != Z_OK)
460  return Z_STREAM_ERROR;
461 
462  /* if no change is requested, then do nothing */
463  if (level == state->level && strategy == state->strategy)
464  return Z_OK;
465 
466  /* check for seek request */
467  if (state->seek) {
468  state->seek = 0;
469  if (gz_zero(state, state->skip) == -1)
470  return -1;
471  }
472 
473  /* change compression parameters for subsequent input */
474  if (state->size) {
475  /* flush previous input with previous parameters before changing */
476  if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
477  return state->err;
478  deflateParams(strm, level, strategy);
479  }
480  state->level = level;
481  state->strategy = strategy;
482  return Z_OK;
483 }
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
Definition: deflate.c:398
#define Z_PARTIAL_FLUSH
Definition: zlib.h:173
#define Z_STREAM_ERROR
Definition: zlib.h:185
#define GZ_WRITE
Definition: gzguts.h:90
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.c:115
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.c:65
#define Z_OK
Definition: zlib.h:181
gz_state FAR * gz_statep
Definition: gzguts.h:129
z_stream FAR * z_streamp
Definition: zlib.h:114
int ZEXPORT gzwrite ( gzFile  file,
voidpc  buf,
unsigned  len 
)

Definition at line 145 of file gzwrite.c.

146 {
147  unsigned put = len;
148  unsigned n;
149  gz_statep state;
150  z_streamp strm;
151 
152  /* get internal structure */
153  if (file == NULL)
154  return 0;
155  state = (gz_statep)file;
156  strm = &(state->strm);
157 
158  /* check that we're writing and that there's no error */
159  if (state->mode != GZ_WRITE || state->err != Z_OK)
160  return 0;
161 
162  /* since an int is returned, make sure len fits in one, otherwise return
163  with an error (this avoids the flaw in the interface) */
164  if ((int)len < 0) {
165  gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
166  return 0;
167  }
168 
169  /* if len is zero, avoid unnecessary operations */
170  if (len == 0)
171  return 0;
172 
173  /* allocate memory if this is the first time through */
174  if (state->size == 0 && gz_init(state) == -1)
175  return 0;
176 
177  /* check for seek request */
178  if (state->seek) {
179  state->seek = 0;
180  if (gz_zero(state, state->skip) == -1)
181  return 0;
182  }
183 
184  /* for small len, copy to input buffer, otherwise compress directly */
185  if (len < state->size) {
186  /* copy to input buffer, compress when full */
187  do {
188  if (strm->avail_in == 0)
189  strm->next_in = state->in;
190  n = state->size - strm->avail_in;
191  if (n > len)
192  n = len;
193  memcpy(strm->next_in + strm->avail_in, buf, n);
194  strm->avail_in += n;
195  state->pos += n;
196  buf = (char *)buf + n;
197  len -= n;
198  if (len && gz_comp(state, Z_NO_FLUSH) == -1)
199  return 0;
200  } while (len);
201  }
202  else {
203  /* consume whatever's left in the input buffer */
204  if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
205  return 0;
206 
207  /* directly compress user buffer to file */
208  strm->avail_in = len;
209  strm->next_in = (unsigned char *)(voidp)buf;
210  state->pos += len;
211  if (gz_comp(state, Z_NO_FLUSH) == -1)
212  return 0;
213  }
214 
215  /* input was all buffered or compressed (put will fit in int) */
216  return (int)put;
217 }
#define Z_NO_FLUSH
Definition: zlib.h:172
char * memcpy()
local int gz_init(gz_statep state)
Definition: gzwrite.c:22
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
Definition: gzlib.c:464
#define GZ_WRITE
Definition: gzguts.h:90
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.c:115
static int size
Definition: cuddSign.c:86
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.c:65
Byte * voidp
Definition: zconf.h:356
#define Z_BUF_ERROR
Definition: zlib.h:188
#define Z_OK
Definition: zlib.h:181
gz_state FAR * gz_statep
Definition: gzguts.h:129
z_stream FAR * z_streamp
Definition: zlib.h:114
local int gz_comp OF ( (gz_statep, int)  )
local int gz_zero OF ( (gz_statep, z_off64_t )