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