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