abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
gzwrite.c
Go to the documentation of this file.
1 /* gzwrite.c -- zlib functions for writing gzip files
2  * Copyright (C) 2004, 2005, 2010 Mark Adler
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "misc/util/abc_global.h"
10 
11 #include "gzguts.h"
12 
14 
15 /* Local functions */
16 local int gz_init OF((gz_statep));
17 local int gz_comp OF((gz_statep, int));
19 
20 /* Initialize state for writing a gzip file. Mark initialization by setting
21  state->size to non-zero. Return -1 on failure or 0 on success. */
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 }
60 
61 /* Compress whatever is at avail_in and next_in and write to the output file.
62  Return -1 if there is an error writing to the output file, otherwise 0.
63  flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
64  then the deflate() state is reset to start a new gzip stream. */
65 local int gz_comp(gz_statep state, int flush)
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 }
113 
114 /* Compress len zeros to output. Return -1 on error, 0 on success. */
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 }
143 
144 /* -- see zlib.h -- */
145 int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
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 }
218 
219 /* -- see zlib.h -- */
220 int ZEXPORT gzputc(gzFile file, int 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 }
259 
260 /* -- see zlib.h -- */
261 int ZEXPORT gzputs(gzFile file, const char *str)
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 }
271 
272 #ifdef STDC
273 #include <stdarg.h>
274 
275 /* -- see zlib.h -- */
276 int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
277 {
278  int size, len;
279  gz_statep state;
280  z_streamp strm;
281  va_list va;
282 
283  /* get internal structure */
284  if (file == NULL)
285  return -1;
286  state = (gz_statep)file;
287  strm = &(state->strm);
288 
289  /* check that we're writing and that there's no error */
290  if (state->mode != GZ_WRITE || state->err != Z_OK)
291  return 0;
292 
293  /* make sure we have some buffer space */
294  if (state->size == 0 && gz_init(state) == -1)
295  return 0;
296 
297  /* check for seek request */
298  if (state->seek) {
299  state->seek = 0;
300  if (gz_zero(state, state->skip) == -1)
301  return 0;
302  }
303 
304  /* consume whatever's left in the input buffer */
305  if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
306  return 0;
307 
308  /* do the printf() into the input buffer, put length in len */
309  size = (int)(state->size);
310  state->in[size - 1] = 0;
311  va_start(va, format);
312 #ifdef NO_vsnprintf
313 # ifdef HAS_vsprintf_void
314  (void)vsprintf(state->in, format, va);
315  va_end(va);
316  for (len = 0; len < size; len++)
317  if (state->in[len] == 0) break;
318 # else
319  len = vsprintf(state->in, format, va);
320  va_end(va);
321 # endif
322 #else
323 # ifdef HAS_vsnprintf_void
324  (void)vsnprintf(state->in, size, format, va);
325  va_end(va);
326  len = strlen(state->in);
327 # else
328  len = vsnprintf((char *)(state->in), size, format, va);
329  va_end(va);
330 # endif
331 #endif
332 
333  /* check that printf() results fit in buffer */
334  if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
335  return 0;
336 
337  /* update buffer and position, defer compression until needed */
338  strm->avail_in = (unsigned)len;
339  strm->next_in = state->in;
340  state->pos += len;
341  return len;
342 }
343 
344 #else /* !STDC */
345 
346 /* -- see zlib.h -- */
347 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,
348  int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20)
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 }
413 
414 #endif
415 
416 /* -- see zlib.h -- */
417 int ZEXPORT gzflush(gzFile file, int flush)
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 }
445 
446 /* -- see zlib.h -- */
447 int ZEXPORT gzsetparams(gzFile file, int level, int strategy)
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 }
484 
485 /* -- see zlib.h -- */
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 }
517 
518 
520 
char * memset()
char * malloc()
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
Definition: deflate.c:398
VOID_HACK free()
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1558
#define Z_NO_FLUSH
Definition: zlib.h:172
#define Z_PARTIAL_FLUSH
Definition: zlib.h:173
#define GT_OFF(x)
Definition: gzguts.h:144
int ZEXPORT gzsetparams(gzFile file, int level, int strategy)
Definition: gzwrite.c:447
#define Z_ERRNO
Definition: zlib.h:184
#define z_off64_t
Definition: zconf.h:402
#define a1
Definition: extraBdd.h:80
char * memcpy()
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:866
#define Z_STREAM_ERROR
Definition: zlib.h:185
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
Definition: gzwrite.c:145
voidp gzFile
Definition: zlib.h:1173
local int gz_init(gz_statep state)
Definition: gzwrite.c:22
int ZEXPORT gzputc(gzFile file, int c)
Definition: gzwrite.c:220
#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_DEFLATED
Definition: zlib.h:213
#define GZ_WRITE
Definition: gzguts.h:90
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
local int gz_zero(gz_statep state, z_off64_t len)
Definition: gzwrite.c:115
#define Z_STREAM_END
Definition: zlib.h:182
#define local
Definition: adler32.c:17
static int size
Definition: cuddSign.c:86
char * sprintf()
int ZEXPORT gzclose_w(gzFile file)
Definition: gzwrite.c:486
int ZEXPORT gzputs(gzFile file, const char *str)
Definition: gzwrite.c:261
local int gz_comp(gz_statep state, int flush)
Definition: gzwrite.c:65
#define Z_MEM_ERROR
Definition: zlib.h:187
ABC_NAMESPACE_IMPL_START local int gz_init OF((gz_statep))
Byte * voidp
Definition: zconf.h:356
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:555
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: gzwrite.c:347
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
int ZEXPORT gzflush(gzFile file, int flush)
Definition: gzwrite.c:417
#define Z_BUF_ERROR
Definition: zlib.h:188
#define Z_OK
Definition: zlib.h:181
gz_state FAR * gz_statep
Definition: gzguts.h:129
int strlen()
#define Z_NULL
Definition: zlib.h:216
z_stream FAR * z_streamp
Definition: zlib.h:114
int ZEXPORT deflateReset(z_streamp strm)
Definition: deflate.c:345
Byte const * voidpc
Definition: zconf.h:354
#define ZEXPORT
Definition: zconf.h:322
#define ZEXPORTVA
Definition: zconf.h:325