annotate src/zlib-1.2.7/gzwrite.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 8a15ff55d9af
children
rev   line source
cannam@89 1 /* gzwrite.c -- zlib functions for writing gzip files
cannam@89 2 * Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
cannam@89 3 * For conditions of distribution and use, see copyright notice in zlib.h
cannam@89 4 */
cannam@89 5
cannam@89 6 #include "gzguts.h"
cannam@89 7
cannam@89 8 /* Local functions */
cannam@89 9 local int gz_init OF((gz_statep));
cannam@89 10 local int gz_comp OF((gz_statep, int));
cannam@89 11 local int gz_zero OF((gz_statep, z_off64_t));
cannam@89 12
cannam@89 13 /* Initialize state for writing a gzip file. Mark initialization by setting
cannam@89 14 state->size to non-zero. Return -1 on failure or 0 on success. */
cannam@89 15 local int gz_init(state)
cannam@89 16 gz_statep state;
cannam@89 17 {
cannam@89 18 int ret;
cannam@89 19 z_streamp strm = &(state->strm);
cannam@89 20
cannam@89 21 /* allocate input buffer */
cannam@89 22 state->in = malloc(state->want);
cannam@89 23 if (state->in == NULL) {
cannam@89 24 gz_error(state, Z_MEM_ERROR, "out of memory");
cannam@89 25 return -1;
cannam@89 26 }
cannam@89 27
cannam@89 28 /* only need output buffer and deflate state if compressing */
cannam@89 29 if (!state->direct) {
cannam@89 30 /* allocate output buffer */
cannam@89 31 state->out = malloc(state->want);
cannam@89 32 if (state->out == NULL) {
cannam@89 33 free(state->in);
cannam@89 34 gz_error(state, Z_MEM_ERROR, "out of memory");
cannam@89 35 return -1;
cannam@89 36 }
cannam@89 37
cannam@89 38 /* allocate deflate memory, set up for gzip compression */
cannam@89 39 strm->zalloc = Z_NULL;
cannam@89 40 strm->zfree = Z_NULL;
cannam@89 41 strm->opaque = Z_NULL;
cannam@89 42 ret = deflateInit2(strm, state->level, Z_DEFLATED,
cannam@89 43 MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
cannam@89 44 if (ret != Z_OK) {
cannam@89 45 free(state->out);
cannam@89 46 free(state->in);
cannam@89 47 gz_error(state, Z_MEM_ERROR, "out of memory");
cannam@89 48 return -1;
cannam@89 49 }
cannam@89 50 }
cannam@89 51
cannam@89 52 /* mark state as initialized */
cannam@89 53 state->size = state->want;
cannam@89 54
cannam@89 55 /* initialize write buffer if compressing */
cannam@89 56 if (!state->direct) {
cannam@89 57 strm->avail_out = state->size;
cannam@89 58 strm->next_out = state->out;
cannam@89 59 state->x.next = strm->next_out;
cannam@89 60 }
cannam@89 61 return 0;
cannam@89 62 }
cannam@89 63
cannam@89 64 /* Compress whatever is at avail_in and next_in and write to the output file.
cannam@89 65 Return -1 if there is an error writing to the output file, otherwise 0.
cannam@89 66 flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
cannam@89 67 then the deflate() state is reset to start a new gzip stream. If gz->direct
cannam@89 68 is true, then simply write to the output file without compressing, and
cannam@89 69 ignore flush. */
cannam@89 70 local int gz_comp(state, flush)
cannam@89 71 gz_statep state;
cannam@89 72 int flush;
cannam@89 73 {
cannam@89 74 int ret, got;
cannam@89 75 unsigned have;
cannam@89 76 z_streamp strm = &(state->strm);
cannam@89 77
cannam@89 78 /* allocate memory if this is the first time through */
cannam@89 79 if (state->size == 0 && gz_init(state) == -1)
cannam@89 80 return -1;
cannam@89 81
cannam@89 82 /* write directly if requested */
cannam@89 83 if (state->direct) {
cannam@89 84 got = write(state->fd, strm->next_in, strm->avail_in);
cannam@89 85 if (got < 0 || (unsigned)got != strm->avail_in) {
cannam@89 86 gz_error(state, Z_ERRNO, zstrerror());
cannam@89 87 return -1;
cannam@89 88 }
cannam@89 89 strm->avail_in = 0;
cannam@89 90 return 0;
cannam@89 91 }
cannam@89 92
cannam@89 93 /* run deflate() on provided input until it produces no more output */
cannam@89 94 ret = Z_OK;
cannam@89 95 do {
cannam@89 96 /* write out current buffer contents if full, or if flushing, but if
cannam@89 97 doing Z_FINISH then don't write until we get to Z_STREAM_END */
cannam@89 98 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
cannam@89 99 (flush != Z_FINISH || ret == Z_STREAM_END))) {
cannam@89 100 have = (unsigned)(strm->next_out - state->x.next);
cannam@89 101 if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
cannam@89 102 (unsigned)got != have)) {
cannam@89 103 gz_error(state, Z_ERRNO, zstrerror());
cannam@89 104 return -1;
cannam@89 105 }
cannam@89 106 if (strm->avail_out == 0) {
cannam@89 107 strm->avail_out = state->size;
cannam@89 108 strm->next_out = state->out;
cannam@89 109 }
cannam@89 110 state->x.next = strm->next_out;
cannam@89 111 }
cannam@89 112
cannam@89 113 /* compress */
cannam@89 114 have = strm->avail_out;
cannam@89 115 ret = deflate(strm, flush);
cannam@89 116 if (ret == Z_STREAM_ERROR) {
cannam@89 117 gz_error(state, Z_STREAM_ERROR,
cannam@89 118 "internal error: deflate stream corrupt");
cannam@89 119 return -1;
cannam@89 120 }
cannam@89 121 have -= strm->avail_out;
cannam@89 122 } while (have);
cannam@89 123
cannam@89 124 /* if that completed a deflate stream, allow another to start */
cannam@89 125 if (flush == Z_FINISH)
cannam@89 126 deflateReset(strm);
cannam@89 127
cannam@89 128 /* all done, no errors */
cannam@89 129 return 0;
cannam@89 130 }
cannam@89 131
cannam@89 132 /* Compress len zeros to output. Return -1 on error, 0 on success. */
cannam@89 133 local int gz_zero(state, len)
cannam@89 134 gz_statep state;
cannam@89 135 z_off64_t len;
cannam@89 136 {
cannam@89 137 int first;
cannam@89 138 unsigned n;
cannam@89 139 z_streamp strm = &(state->strm);
cannam@89 140
cannam@89 141 /* consume whatever's left in the input buffer */
cannam@89 142 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
cannam@89 143 return -1;
cannam@89 144
cannam@89 145 /* compress len zeros (len guaranteed > 0) */
cannam@89 146 first = 1;
cannam@89 147 while (len) {
cannam@89 148 n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
cannam@89 149 (unsigned)len : state->size;
cannam@89 150 if (first) {
cannam@89 151 memset(state->in, 0, n);
cannam@89 152 first = 0;
cannam@89 153 }
cannam@89 154 strm->avail_in = n;
cannam@89 155 strm->next_in = state->in;
cannam@89 156 state->x.pos += n;
cannam@89 157 if (gz_comp(state, Z_NO_FLUSH) == -1)
cannam@89 158 return -1;
cannam@89 159 len -= n;
cannam@89 160 }
cannam@89 161 return 0;
cannam@89 162 }
cannam@89 163
cannam@89 164 /* -- see zlib.h -- */
cannam@89 165 int ZEXPORT gzwrite(file, buf, len)
cannam@89 166 gzFile file;
cannam@89 167 voidpc buf;
cannam@89 168 unsigned len;
cannam@89 169 {
cannam@89 170 unsigned put = len;
cannam@89 171 unsigned n;
cannam@89 172 gz_statep state;
cannam@89 173 z_streamp strm;
cannam@89 174
cannam@89 175 /* get internal structure */
cannam@89 176 if (file == NULL)
cannam@89 177 return 0;
cannam@89 178 state = (gz_statep)file;
cannam@89 179 strm = &(state->strm);
cannam@89 180
cannam@89 181 /* check that we're writing and that there's no error */
cannam@89 182 if (state->mode != GZ_WRITE || state->err != Z_OK)
cannam@89 183 return 0;
cannam@89 184
cannam@89 185 /* since an int is returned, make sure len fits in one, otherwise return
cannam@89 186 with an error (this avoids the flaw in the interface) */
cannam@89 187 if ((int)len < 0) {
cannam@89 188 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
cannam@89 189 return 0;
cannam@89 190 }
cannam@89 191
cannam@89 192 /* if len is zero, avoid unnecessary operations */
cannam@89 193 if (len == 0)
cannam@89 194 return 0;
cannam@89 195
cannam@89 196 /* allocate memory if this is the first time through */
cannam@89 197 if (state->size == 0 && gz_init(state) == -1)
cannam@89 198 return 0;
cannam@89 199
cannam@89 200 /* check for seek request */
cannam@89 201 if (state->seek) {
cannam@89 202 state->seek = 0;
cannam@89 203 if (gz_zero(state, state->skip) == -1)
cannam@89 204 return 0;
cannam@89 205 }
cannam@89 206
cannam@89 207 /* for small len, copy to input buffer, otherwise compress directly */
cannam@89 208 if (len < state->size) {
cannam@89 209 /* copy to input buffer, compress when full */
cannam@89 210 do {
cannam@89 211 if (strm->avail_in == 0)
cannam@89 212 strm->next_in = state->in;
cannam@89 213 n = state->size - strm->avail_in;
cannam@89 214 if (n > len)
cannam@89 215 n = len;
cannam@89 216 memcpy(strm->next_in + strm->avail_in, buf, n);
cannam@89 217 strm->avail_in += n;
cannam@89 218 state->x.pos += n;
cannam@89 219 buf = (char *)buf + n;
cannam@89 220 len -= n;
cannam@89 221 if (len && gz_comp(state, Z_NO_FLUSH) == -1)
cannam@89 222 return 0;
cannam@89 223 } while (len);
cannam@89 224 }
cannam@89 225 else {
cannam@89 226 /* consume whatever's left in the input buffer */
cannam@89 227 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
cannam@89 228 return 0;
cannam@89 229
cannam@89 230 /* directly compress user buffer to file */
cannam@89 231 strm->avail_in = len;
cannam@89 232 strm->next_in = (voidp)buf;
cannam@89 233 state->x.pos += len;
cannam@89 234 if (gz_comp(state, Z_NO_FLUSH) == -1)
cannam@89 235 return 0;
cannam@89 236 }
cannam@89 237
cannam@89 238 /* input was all buffered or compressed (put will fit in int) */
cannam@89 239 return (int)put;
cannam@89 240 }
cannam@89 241
cannam@89 242 /* -- see zlib.h -- */
cannam@89 243 int ZEXPORT gzputc(file, c)
cannam@89 244 gzFile file;
cannam@89 245 int c;
cannam@89 246 {
cannam@89 247 unsigned char buf[1];
cannam@89 248 gz_statep state;
cannam@89 249 z_streamp strm;
cannam@89 250
cannam@89 251 /* get internal structure */
cannam@89 252 if (file == NULL)
cannam@89 253 return -1;
cannam@89 254 state = (gz_statep)file;
cannam@89 255 strm = &(state->strm);
cannam@89 256
cannam@89 257 /* check that we're writing and that there's no error */
cannam@89 258 if (state->mode != GZ_WRITE || state->err != Z_OK)
cannam@89 259 return -1;
cannam@89 260
cannam@89 261 /* check for seek request */
cannam@89 262 if (state->seek) {
cannam@89 263 state->seek = 0;
cannam@89 264 if (gz_zero(state, state->skip) == -1)
cannam@89 265 return -1;
cannam@89 266 }
cannam@89 267
cannam@89 268 /* try writing to input buffer for speed (state->size == 0 if buffer not
cannam@89 269 initialized) */
cannam@89 270 if (strm->avail_in < state->size) {
cannam@89 271 if (strm->avail_in == 0)
cannam@89 272 strm->next_in = state->in;
cannam@89 273 strm->next_in[strm->avail_in++] = c;
cannam@89 274 state->x.pos++;
cannam@89 275 return c & 0xff;
cannam@89 276 }
cannam@89 277
cannam@89 278 /* no room in buffer or not initialized, use gz_write() */
cannam@89 279 buf[0] = c;
cannam@89 280 if (gzwrite(file, buf, 1) != 1)
cannam@89 281 return -1;
cannam@89 282 return c & 0xff;
cannam@89 283 }
cannam@89 284
cannam@89 285 /* -- see zlib.h -- */
cannam@89 286 int ZEXPORT gzputs(file, str)
cannam@89 287 gzFile file;
cannam@89 288 const char *str;
cannam@89 289 {
cannam@89 290 int ret;
cannam@89 291 unsigned len;
cannam@89 292
cannam@89 293 /* write string */
cannam@89 294 len = (unsigned)strlen(str);
cannam@89 295 ret = gzwrite(file, str, len);
cannam@89 296 return ret == 0 && len != 0 ? -1 : ret;
cannam@89 297 }
cannam@89 298
cannam@89 299 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
cannam@89 300 #include <stdarg.h>
cannam@89 301
cannam@89 302 /* -- see zlib.h -- */
cannam@89 303 int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
cannam@89 304 {
cannam@89 305 int size, len;
cannam@89 306 gz_statep state;
cannam@89 307 z_streamp strm;
cannam@89 308 va_list va;
cannam@89 309
cannam@89 310 /* get internal structure */
cannam@89 311 if (file == NULL)
cannam@89 312 return -1;
cannam@89 313 state = (gz_statep)file;
cannam@89 314 strm = &(state->strm);
cannam@89 315
cannam@89 316 /* check that we're writing and that there's no error */
cannam@89 317 if (state->mode != GZ_WRITE || state->err != Z_OK)
cannam@89 318 return 0;
cannam@89 319
cannam@89 320 /* make sure we have some buffer space */
cannam@89 321 if (state->size == 0 && gz_init(state) == -1)
cannam@89 322 return 0;
cannam@89 323
cannam@89 324 /* check for seek request */
cannam@89 325 if (state->seek) {
cannam@89 326 state->seek = 0;
cannam@89 327 if (gz_zero(state, state->skip) == -1)
cannam@89 328 return 0;
cannam@89 329 }
cannam@89 330
cannam@89 331 /* consume whatever's left in the input buffer */
cannam@89 332 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
cannam@89 333 return 0;
cannam@89 334
cannam@89 335 /* do the printf() into the input buffer, put length in len */
cannam@89 336 size = (int)(state->size);
cannam@89 337 state->in[size - 1] = 0;
cannam@89 338 va_start(va, format);
cannam@89 339 #ifdef NO_vsnprintf
cannam@89 340 # ifdef HAS_vsprintf_void
cannam@89 341 (void)vsprintf((char *)(state->in), format, va);
cannam@89 342 va_end(va);
cannam@89 343 for (len = 0; len < size; len++)
cannam@89 344 if (state->in[len] == 0) break;
cannam@89 345 # else
cannam@89 346 len = vsprintf((char *)(state->in), format, va);
cannam@89 347 va_end(va);
cannam@89 348 # endif
cannam@89 349 #else
cannam@89 350 # ifdef HAS_vsnprintf_void
cannam@89 351 (void)vsnprintf((char *)(state->in), size, format, va);
cannam@89 352 va_end(va);
cannam@89 353 len = strlen((char *)(state->in));
cannam@89 354 # else
cannam@89 355 len = vsnprintf((char *)(state->in), size, format, va);
cannam@89 356 va_end(va);
cannam@89 357 # endif
cannam@89 358 #endif
cannam@89 359
cannam@89 360 /* check that printf() results fit in buffer */
cannam@89 361 if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
cannam@89 362 return 0;
cannam@89 363
cannam@89 364 /* update buffer and position, defer compression until needed */
cannam@89 365 strm->avail_in = (unsigned)len;
cannam@89 366 strm->next_in = state->in;
cannam@89 367 state->x.pos += len;
cannam@89 368 return len;
cannam@89 369 }
cannam@89 370
cannam@89 371 #else /* !STDC && !Z_HAVE_STDARG_H */
cannam@89 372
cannam@89 373 /* -- see zlib.h -- */
cannam@89 374 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
cannam@89 375 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
cannam@89 376 gzFile file;
cannam@89 377 const char *format;
cannam@89 378 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
cannam@89 379 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
cannam@89 380 {
cannam@89 381 int size, len;
cannam@89 382 gz_statep state;
cannam@89 383 z_streamp strm;
cannam@89 384
cannam@89 385 /* get internal structure */
cannam@89 386 if (file == NULL)
cannam@89 387 return -1;
cannam@89 388 state = (gz_statep)file;
cannam@89 389 strm = &(state->strm);
cannam@89 390
cannam@89 391 /* check that can really pass pointer in ints */
cannam@89 392 if (sizeof(int) != sizeof(void *))
cannam@89 393 return 0;
cannam@89 394
cannam@89 395 /* check that we're writing and that there's no error */
cannam@89 396 if (state->mode != GZ_WRITE || state->err != Z_OK)
cannam@89 397 return 0;
cannam@89 398
cannam@89 399 /* make sure we have some buffer space */
cannam@89 400 if (state->size == 0 && gz_init(state) == -1)
cannam@89 401 return 0;
cannam@89 402
cannam@89 403 /* check for seek request */
cannam@89 404 if (state->seek) {
cannam@89 405 state->seek = 0;
cannam@89 406 if (gz_zero(state, state->skip) == -1)
cannam@89 407 return 0;
cannam@89 408 }
cannam@89 409
cannam@89 410 /* consume whatever's left in the input buffer */
cannam@89 411 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
cannam@89 412 return 0;
cannam@89 413
cannam@89 414 /* do the printf() into the input buffer, put length in len */
cannam@89 415 size = (int)(state->size);
cannam@89 416 state->in[size - 1] = 0;
cannam@89 417 #ifdef NO_snprintf
cannam@89 418 # ifdef HAS_sprintf_void
cannam@89 419 sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
cannam@89 420 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
cannam@89 421 for (len = 0; len < size; len++)
cannam@89 422 if (state->in[len] == 0) break;
cannam@89 423 # else
cannam@89 424 len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
cannam@89 425 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
cannam@89 426 # endif
cannam@89 427 #else
cannam@89 428 # ifdef HAS_snprintf_void
cannam@89 429 snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
cannam@89 430 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
cannam@89 431 len = strlen((char *)(state->in));
cannam@89 432 # else
cannam@89 433 len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
cannam@89 434 a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
cannam@89 435 a19, a20);
cannam@89 436 # endif
cannam@89 437 #endif
cannam@89 438
cannam@89 439 /* check that printf() results fit in buffer */
cannam@89 440 if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
cannam@89 441 return 0;
cannam@89 442
cannam@89 443 /* update buffer and position, defer compression until needed */
cannam@89 444 strm->avail_in = (unsigned)len;
cannam@89 445 strm->next_in = state->in;
cannam@89 446 state->x.pos += len;
cannam@89 447 return len;
cannam@89 448 }
cannam@89 449
cannam@89 450 #endif
cannam@89 451
cannam@89 452 /* -- see zlib.h -- */
cannam@89 453 int ZEXPORT gzflush(file, flush)
cannam@89 454 gzFile file;
cannam@89 455 int flush;
cannam@89 456 {
cannam@89 457 gz_statep state;
cannam@89 458
cannam@89 459 /* get internal structure */
cannam@89 460 if (file == NULL)
cannam@89 461 return -1;
cannam@89 462 state = (gz_statep)file;
cannam@89 463
cannam@89 464 /* check that we're writing and that there's no error */
cannam@89 465 if (state->mode != GZ_WRITE || state->err != Z_OK)
cannam@89 466 return Z_STREAM_ERROR;
cannam@89 467
cannam@89 468 /* check flush parameter */
cannam@89 469 if (flush < 0 || flush > Z_FINISH)
cannam@89 470 return Z_STREAM_ERROR;
cannam@89 471
cannam@89 472 /* check for seek request */
cannam@89 473 if (state->seek) {
cannam@89 474 state->seek = 0;
cannam@89 475 if (gz_zero(state, state->skip) == -1)
cannam@89 476 return -1;
cannam@89 477 }
cannam@89 478
cannam@89 479 /* compress remaining data with requested flush */
cannam@89 480 gz_comp(state, flush);
cannam@89 481 return state->err;
cannam@89 482 }
cannam@89 483
cannam@89 484 /* -- see zlib.h -- */
cannam@89 485 int ZEXPORT gzsetparams(file, level, strategy)
cannam@89 486 gzFile file;
cannam@89 487 int level;
cannam@89 488 int strategy;
cannam@89 489 {
cannam@89 490 gz_statep state;
cannam@89 491 z_streamp strm;
cannam@89 492
cannam@89 493 /* get internal structure */
cannam@89 494 if (file == NULL)
cannam@89 495 return Z_STREAM_ERROR;
cannam@89 496 state = (gz_statep)file;
cannam@89 497 strm = &(state->strm);
cannam@89 498
cannam@89 499 /* check that we're writing and that there's no error */
cannam@89 500 if (state->mode != GZ_WRITE || state->err != Z_OK)
cannam@89 501 return Z_STREAM_ERROR;
cannam@89 502
cannam@89 503 /* if no change is requested, then do nothing */
cannam@89 504 if (level == state->level && strategy == state->strategy)
cannam@89 505 return Z_OK;
cannam@89 506
cannam@89 507 /* check for seek request */
cannam@89 508 if (state->seek) {
cannam@89 509 state->seek = 0;
cannam@89 510 if (gz_zero(state, state->skip) == -1)
cannam@89 511 return -1;
cannam@89 512 }
cannam@89 513
cannam@89 514 /* change compression parameters for subsequent input */
cannam@89 515 if (state->size) {
cannam@89 516 /* flush previous input with previous parameters before changing */
cannam@89 517 if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
cannam@89 518 return state->err;
cannam@89 519 deflateParams(strm, level, strategy);
cannam@89 520 }
cannam@89 521 state->level = level;
cannam@89 522 state->strategy = strategy;
cannam@89 523 return Z_OK;
cannam@89 524 }
cannam@89 525
cannam@89 526 /* -- see zlib.h -- */
cannam@89 527 int ZEXPORT gzclose_w(file)
cannam@89 528 gzFile file;
cannam@89 529 {
cannam@89 530 int ret = Z_OK;
cannam@89 531 gz_statep state;
cannam@89 532
cannam@89 533 /* get internal structure */
cannam@89 534 if (file == NULL)
cannam@89 535 return Z_STREAM_ERROR;
cannam@89 536 state = (gz_statep)file;
cannam@89 537
cannam@89 538 /* check that we're writing */
cannam@89 539 if (state->mode != GZ_WRITE)
cannam@89 540 return Z_STREAM_ERROR;
cannam@89 541
cannam@89 542 /* check for seek request */
cannam@89 543 if (state->seek) {
cannam@89 544 state->seek = 0;
cannam@89 545 if (gz_zero(state, state->skip) == -1)
cannam@89 546 ret = state->err;
cannam@89 547 }
cannam@89 548
cannam@89 549 /* flush, free memory, and close file */
cannam@89 550 if (state->size) {
cannam@89 551 if (gz_comp(state, Z_FINISH) == -1)
cannam@89 552 ret = state->err;
cannam@89 553 if (!state->direct) {
cannam@89 554 (void)deflateEnd(&(state->strm));
cannam@89 555 free(state->out);
cannam@89 556 }
cannam@89 557 free(state->in);
cannam@89 558 }
cannam@89 559 gz_error(state, Z_OK, NULL);
cannam@89 560 free(state->path);
cannam@89 561 if (close(state->fd) == -1)
cannam@89 562 ret = Z_ERRNO;
cannam@89 563 free(state);
cannam@89 564 return ret;
cannam@89 565 }