| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | #include "gzguts.h" |
| 7 | |
| 8 | |
| 9 | localstatic int gz_init OF((gz_statep))(gz_statep); |
| 10 | localstatic int gz_comp OF((gz_statep, int))(gz_statep, int); |
| 11 | localstatic int gz_zero OF((gz_statep, z_off64_t))(gz_statep, off_t); |
| 12 | |
| 13 | |
| 14 | |
| 15 | localstatic int gz_init(state) |
| 16 | gz_statep state; |
| 17 | { |
| 18 | int ret; |
| 19 | z_streamp strm = &(state->strm); |
| 20 | |
| 21 | |
| 22 | state->in = malloc(state->want); |
| 23 | state->out = malloc(state->want); |
| 24 | if (state->in == NULL((void*)0) || state->out == NULL((void*)0)) { |
| |
| 25 | if (state->out != NULL((void*)0)) |
| 26 | free(state->out); |
| 27 | if (state->in != NULL((void*)0)) |
| 28 | free(state->in); |
| 29 | gz_errorz_gz_error(state, Z_MEM_ERROR(-4), "out of memory"); |
| 30 | return -1; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | strm->zalloc = Z_NULL0; |
| 35 | strm->zfree = Z_NULL0; |
| 36 | strm->opaque = Z_NULL0; |
| 37 | ret = deflateInit2(strm, state->level, Z_DEFLATED,z_deflateInit2_((strm),(state->level),(8),(15 + 16),(8), ( state->strategy), "1.2.4", sizeof(z_stream)) |
| 38 | 15 + 16, 8, state->strategy)z_deflateInit2_((strm),(state->level),(8),(15 + 16),(8), ( state->strategy), "1.2.4", sizeof(z_stream)); |
| 39 | if (ret != Z_OK0) { |
| |
| 40 | free(state->in); |
| 41 | gz_errorz_gz_error(state, Z_MEM_ERROR(-4), "out of memory"); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | state->size = state->want; |
| 47 | |
| 48 | |
| 49 | strm->avail_out = state->size; |
| 50 | strm->next_out = state->out; |
| 51 | state->next = strm->next_out; |
| 52 | return 0; |
| 3 | Allocated memory never released. Potential memory leak |
|
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | localstatic int gz_comp(state, flush) |
| 60 | gz_statep state; |
| 61 | int flush; |
| 62 | { |
| 63 | int ret, got; |
| 64 | unsigned have; |
| 65 | z_streamp strm = &(state->strm); |
| 66 | |
| 67 | |
| 68 | if (state->size == 0 && gz_init(state) == -1) |
| 69 | return -1; |
| 70 | |
| 71 | |
| 72 | ret = Z_OK0; |
| 73 | do { |
| 74 | |
| 75 | |
| 76 | if (strm->avail_out == 0 || (flush != Z_NO_FLUSH0 && |
| 77 | (flush != Z_FINISH4 || ret == Z_STREAM_END1))) { |
| 78 | have = (unsigned)(strm->next_out - state->next); |
| 79 | if (have && ((got = write(state->fd, state->next, have)) < 0 || |
| 80 | (unsigned)got != have)) { |
| 81 | gz_errorz_gz_error(state, Z_ERRNO(-1), zstrerror()strerror((*__errno_location ()))); |
| 82 | return -1; |
| 83 | } |
| 84 | if (strm->avail_out == 0) { |
| 85 | strm->avail_out = state->size; |
| 86 | strm->next_out = state->out; |
| 87 | } |
| 88 | state->next = strm->next_out; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | have = strm->avail_out; |
| 93 | ret = deflatez_deflate(strm, flush); |
| 94 | if (ret == Z_STREAM_ERROR(-2)) { |
| 95 | gz_errorz_gz_error(state, Z_STREAM_ERROR(-2), |
| 96 | "internal error: deflate stream corrupt"); |
| 97 | return -1; |
| 98 | } |
| 99 | have -= strm->avail_out; |
| 100 | } while (have); |
| 101 | |
| 102 | |
| 103 | if (flush == Z_FINISH4) |
| 104 | deflateResetz_deflateReset(strm); |
| 105 | |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | localstatic int gz_zero(state, len) |
| 112 | gz_statep state; |
| 113 | z_off64_toff_t len; |
| 114 | { |
| 115 | int first; |
| 116 | unsigned n; |
| 117 | z_streamp strm = &(state->strm); |
| 118 | |
| 119 | |
| 120 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH0) == -1) |
| 121 | return -1; |
| 122 | |
| 123 | |
| 124 | first = 1; |
| 125 | while (len) { |
| 126 | n = GT_OFF(state->size)(sizeof(int) == sizeof(off_t) && (state->size) > 2147483647) || (z_off64_toff_t)state->size > len ? |
| 127 | (unsigned)len : state->size; |
| 128 | if (first) { |
| 129 | memset(state->in, 0, n); |
| 130 | first = 0; |
| 131 | } |
| 132 | strm->avail_in = n; |
| 133 | strm->next_in = state->in; |
| 134 | state->pos += n; |
| 135 | if (gz_comp(state, Z_NO_FLUSH0) == -1) |
| 136 | return -1; |
| 137 | len -= n; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | int ZEXPORT gzwritez_gzwrite(file, buf, len) |
| 144 | gzFilez_gzFile file; |
| 145 | voidpcz_voidpc buf; |
| 146 | unsigned len; |
| 147 | { |
| 148 | unsigned put = len; |
| 149 | unsigned n; |
| 150 | gz_statep state; |
| 151 | z_streamp strm; |
| 152 | |
| 153 | |
| 154 | if (file == NULL((void*)0)) |
| 155 | return 0; |
| 156 | state = (gz_statep)file; |
| 157 | strm = &(state->strm); |
| 158 | |
| 159 | |
| 160 | if (state->mode != GZ_WRITE31153 || state->err != Z_OK0) |
| 161 | return 0; |
| 162 | |
| 163 | |
| 164 | |
| 165 | if ((int)len < 0) { |
| 166 | gz_errorz_gz_error(state, Z_BUF_ERROR(-5), "requested length does not fit in int"); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | if (len == 0) |
| 172 | return 0; |
| 173 | |
| 174 | |
| 175 | if (state->size == 0 && gz_init(state) == -1) |
| 176 | return 0; |
| 177 | |
| 178 | |
| 179 | if (state->seek) { |
| 180 | state->seek = 0; |
| 181 | if (gz_zero(state, state->skip) == -1) |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | if (len < state->size) { |
| 187 | |
| 188 | do { |
| 189 | if (strm->avail_in == 0) |
| 190 | strm->next_in = state->in; |
| 191 | n = state->size - strm->avail_in; |
| 192 | if (n > len) |
| 193 | n = len; |
| 194 | memcpy(strm->next_in + strm->avail_in, buf, n); |
| 195 | strm->avail_in += n; |
| 196 | state->pos += n; |
| 197 | buf = (char *)buf + n; |
| 198 | len -= n; |
| 199 | if (len && gz_comp(state, Z_NO_FLUSH0) == -1) |
| 200 | return 0; |
| 201 | } while (len); |
| 202 | } |
| 203 | else { |
| 204 | |
| 205 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH0) == -1) |
| 206 | return 0; |
| 207 | |
| 208 | |
| 209 | strm->avail_in = len; |
| 210 | strm->next_in = (voidpz_voidp)buf; |
| 211 | state->pos += len; |
| 212 | if (gz_comp(state, Z_NO_FLUSH0) == -1) |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | return (int)put; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | int ZEXPORT gzputcz_gzputc(file, c) |
| 222 | gzFilez_gzFile file; |
| 223 | int c; |
| 224 | { |
| 225 | unsigned char buf[1]; |
| 226 | gz_statep state; |
| 227 | z_streamp strm; |
| 228 | |
| 229 | |
| 230 | if (file == NULL((void*)0)) |
| 231 | return -1; |
| 232 | state = (gz_statep)file; |
| 233 | strm = &(state->strm); |
| 234 | |
| 235 | |
| 236 | if (state->mode != GZ_WRITE31153 || state->err != Z_OK0) |
| 237 | return -1; |
| 238 | |
| 239 | |
| 240 | if (state->seek) { |
| 241 | state->seek = 0; |
| 242 | if (gz_zero(state, state->skip) == -1) |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | if (strm->avail_in < state->size) { |
| 249 | if (strm->avail_in == 0) |
| 250 | strm->next_in = state->in; |
| 251 | strm->next_in[strm->avail_in++] = c; |
| 252 | state->pos++; |
| 253 | return c; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | buf[0] = c; |
| 258 | if (gzwritez_gzwrite(file, buf, 1) != 1) |
| 259 | return -1; |
| 260 | return c; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | int ZEXPORT gzputsz_gzputs(file, str) |
| 265 | gzFilez_gzFile file; |
| 266 | const char *str; |
| 267 | { |
| 268 | int ret; |
| 269 | unsigned len; |
| 270 | |
| 271 | |
| 272 | len = (unsigned)strlen(str); |
| 273 | ret = gzwritez_gzwrite(file, str, len); |
| 274 | return ret == 0 && len != 0 ? -1 : ret; |
| 275 | } |
| 276 | |
| 277 | #ifdef STDC |
| 278 | #include <stdarg.h> |
| 279 | |
| 280 | |
| 281 | int ZEXPORTVA gzprintfz_gzprintf (gzFilez_gzFile file, const char *format, ...) |
| 282 | { |
| 283 | int size, len; |
| 284 | gz_statep state; |
| 285 | z_streamp strm; |
| 286 | va_list va; |
| 287 | |
| 288 | |
| 289 | if (file == NULL((void*)0)) |
| 290 | return -1; |
| 291 | state = (gz_statep)file; |
| 292 | strm = &(state->strm); |
| 293 | |
| 294 | |
| 295 | if (state->mode != GZ_WRITE31153 || state->err != Z_OK0) |
| 296 | return 0; |
| 297 | |
| 298 | |
| 299 | if (state->size == 0 && gz_init(state) == -1) |
| 300 | return 0; |
| 301 | |
| 302 | |
| 303 | if (state->seek) { |
| 304 | state->seek = 0; |
| 305 | if (gz_zero(state, state->skip) == -1) |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH0) == -1) |
| 311 | return 0; |
| 312 | |
| 313 | |
| 314 | size = (int)(state->size); |
| 315 | state->in[size - 1] = 0; |
| 316 | va_start(va, format)__builtin_va_start(va, format); |
| 317 | #ifdef NO_vsnprintf |
| 318 | # ifdef HAS_vsprintf_void |
| 319 | (void)vsprintf(state->in, format, va); |
| 320 | va_end(va)__builtin_va_end(va); |
| 321 | for (len = 0; len < size; len++) |
| 322 | if (state->in[len] == 0) break; |
| 323 | # else |
| 324 | len = vsprintf(state->in, format, va); |
| 325 | va_end(va)__builtin_va_end(va); |
| 326 | # endif |
| 327 | #else |
| 328 | # ifdef HAS_vsnprintf_void |
| 329 | (void)vsnprintf(state->in, size, format, va); |
| 330 | va_end(va)__builtin_va_end(va); |
| 331 | len = strlen(state->in); |
| 332 | # else |
| 333 | len = vsnprintf((char *)(state->in), size, format, va); |
| 334 | va_end(va)__builtin_va_end(va); |
| 335 | # endif |
| 336 | #endif |
| 337 | |
| 338 | |
| 339 | if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) |
| 340 | return 0; |
| 341 | |
| 342 | |
| 343 | strm->avail_in = (unsigned)len; |
| 344 | strm->next_in = state->in; |
| 345 | state->pos += len; |
| 346 | return len; |
| 347 | } |
| 348 | |
| 349 | #else /* !STDC */ |
| 350 | |
| 351 | |
| 352 | int ZEXPORTVA gzprintfz_gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
| 353 | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) |
| 354 | gzFilez_gzFile file; |
| 355 | const char *format; |
| 356 | int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
| 357 | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; |
| 358 | { |
| 359 | int size, len; |
| 360 | gz_statep state; |
| 361 | z_streamp strm; |
| 362 | |
| 363 | |
| 364 | if (file == NULL((void*)0)) |
| 365 | return -1; |
| 366 | state = (gz_statep)file; |
| 367 | strm = &(state->strm); |
| 368 | |
| 369 | |
| 370 | if (state->mode != GZ_WRITE31153 || state->err != Z_OK0) |
| 371 | return 0; |
| 372 | |
| 373 | |
| 374 | if (state->size == 0 && gz_init(state) == -1) |
| 375 | return 0; |
| 376 | |
| 377 | |
| 378 | if (state->seek) { |
| 379 | state->seek = 0; |
| 380 | if (gz_zero(state, state->skip) == -1) |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH0) == -1) |
| 386 | return 0; |
| 387 | |
| 388 | |
| 389 | size = (int)(state->size); |
| 390 | state->in[size - 1] = 0; |
| 391 | #ifdef NO_snprintf |
| 392 | # ifdef HAS_sprintf_void |
| 393 | sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8, |
| 394 | a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
| 395 | for (len = 0; len < size; len++) |
| 396 | if (state->in[len] == 0) break; |
| 397 | # else |
| 398 | len = sprintf(state->in, 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 | #else |
| 402 | # ifdef HAS_snprintf_void |
| 403 | snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8, |
| 404 | a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
| 405 | len = strlen(state->in); |
| 406 | # else |
| 407 | len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8, |
| 408 | a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); |
| 409 | # endif |
| 410 | #endif |
| 411 | |
| 412 | |
| 413 | if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) |
| 414 | return 0; |
| 415 | |
| 416 | |
| 417 | strm->avail_in = (unsigned)len; |
| 418 | strm->next_in = state->in; |
| 419 | state->pos += len; |
| 420 | return len; |
| 421 | } |
| 422 | |
| 423 | #endif |
| 424 | |
| 425 | |
| 426 | int ZEXPORT gzflushz_gzflush(file, flush) |
| 427 | gzFilez_gzFile file; |
| 428 | int flush; |
| 429 | { |
| 430 | gz_statep state; |
| 431 | |
| 432 | |
| 433 | if (file == NULL((void*)0)) |
| 434 | return -1; |
| 435 | state = (gz_statep)file; |
| 436 | |
| 437 | |
| 438 | if (state->mode != GZ_WRITE31153 || state->err != Z_OK0) |
| 439 | return Z_STREAM_ERROR(-2); |
| 440 | |
| 441 | |
| 442 | if (flush < 0 || flush > Z_FINISH4) |
| 443 | return Z_STREAM_ERROR(-2); |
| 444 | |
| 445 | |
| 446 | if (state->seek) { |
| 447 | state->seek = 0; |
| 448 | if (gz_zero(state, state->skip) == -1) |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | gz_comp(state, flush); |
| 454 | return state->err; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | int ZEXPORT gzsetparamsz_gzsetparams(file, level, strategy) |
| 459 | gzFilez_gzFile file; |
| 460 | int level; |
| 461 | int strategy; |
| 462 | { |
| 463 | gz_statep state; |
| 464 | z_streamp strm; |
| 465 | |
| 466 | |
| 467 | if (file == NULL((void*)0)) |
| 468 | return Z_STREAM_ERROR(-2); |
| 469 | state = (gz_statep)file; |
| 470 | strm = &(state->strm); |
| 471 | |
| 472 | |
| 473 | if (state->mode != GZ_WRITE31153 || state->err != Z_OK0) |
| 474 | return Z_STREAM_ERROR(-2); |
| 475 | |
| 476 | |
| 477 | if (level == state->level && strategy == state->strategy) |
| 478 | return Z_OK0; |
| 479 | |
| 480 | |
| 481 | if (state->seek) { |
| 482 | state->seek = 0; |
| 483 | if (gz_zero(state, state->skip) == -1) |
| 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | if (state->size) { |
| 489 | |
| 490 | if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH1) == -1) |
| 491 | return state->err; |
| 492 | deflateParamsz_deflateParams(strm, level, strategy); |
| 493 | } |
| 494 | state->level = level; |
| 495 | state->strategy = strategy; |
| 496 | return Z_OK0; |
| 497 | } |
| 498 | |
| 499 | |
| 500 | int ZEXPORT gzclose_wz_gzclose_w(file) |
| 501 | gzFilez_gzFile file; |
| 502 | { |
| 503 | int ret = 0; |
| 504 | gz_statep state; |
| 505 | |
| 506 | |
| 507 | if (file == NULL((void*)0)) |
| 508 | return Z_STREAM_ERROR(-2); |
| 509 | state = (gz_statep)file; |
| 510 | |
| 511 | |
| 512 | if (state->mode != GZ_WRITE31153) |
| 513 | return Z_STREAM_ERROR(-2); |
| 514 | |
| 515 | |
| 516 | if (state->seek) { |
| 517 | state->seek = 0; |
| 518 | ret += gz_zero(state, state->skip); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | ret += gz_comp(state, Z_FINISH4); |
| 523 | (void)deflateEndz_deflateEnd(&(state->strm)); |
| 524 | free(state->out); |
| 525 | free(state->in); |
| 526 | gz_errorz_gz_error(state, Z_OK0, NULL((void*)0)); |
| 527 | free(state->path); |
| 528 | ret += close(state->fd); |
| 529 | free(state); |
| 530 | return ret ? Z_ERRNO(-1) : Z_OK0; |
| 531 | } |