static void pdc_end_compress(pdc_output *out) { int status; pdc_core *pdc = out->pdc; /* this may happen during cleanup triggered by an exception handler */ if (!out->compressing) return; if (!pdc_get_compresslevel(out)) { out->compressing = pdc_false; return; } #ifdef HAVE_LIBZ /* Finish the stream */ do { pdc_check_stream(out, 128); out->z.next_out = (Bytef *) out->curpos; out->z.avail_out = (uInt) (out->maxpos - out->curpos); status = deflate(&(out->z), Z_FINISH); out->curpos = out->z.next_out; if (status != Z_STREAM_END && status != Z_OK) pdc_error(pdc, PDC_E_IO_COMPRESS, "Z_FINISH", 0, 0, 0); } while (status != Z_STREAM_END); out->compressing = pdc_false; #endif /* HAVE_LIBZ */ }
static void pdc_check_stream(pdc_output *out, size_t len) { size_t max; int cur; pdc_stream *stream = &out->stream; pdc_core *pdc = out->pdc; if (stream->curpos + len <= stream->maxpos) return; pdc_flush_stream(out); if (stream->curpos + len <= stream->maxpos) return; max = (size_t) (2*(stream->maxpos - stream->basepos)); cur = stream->curpos - stream->basepos; stream->basepos = (pdc_byte *) pdc_realloc(pdc, (void *) stream->basepos, max, "pdc_check_stream"); stream->maxpos = stream->basepos + max; stream->curpos = stream->basepos + cur; pdc_check_stream(out, len); }
void pdc_write(pdc_output *out, const void *data, size_t size) { pdc_stream *stream = &out->stream; int estimate = 0; pdc_core *pdc = out->pdc; #ifdef HAVE_LIBZ if (stream->compressing) { stream->z.avail_in = (uInt) size; stream->z.next_in = (Bytef *) data; stream->z.avail_out = 0; while (stream->z.avail_in > 0) { if (stream->z.avail_out == 0) { /* estimate output buffer size */ estimate = (int) (stream->z.avail_in/4 + 16); pdc_check_stream(out, (size_t) estimate); stream->z.next_out = (Bytef *) stream->curpos; stream->z.avail_out = (uInt) (stream->maxpos - stream->curpos); } if (deflate(&(stream->z), Z_NO_FLUSH) != Z_OK) pdc_error(pdc, PDC_E_IO_COMPRESS, "Z_NO_FLUSH", 0, 0, 0); stream->curpos = stream->z.next_out; } } else { #endif /* HAVE_LIBZ */ pdc_check_stream(out, size); memcpy(stream->curpos, data, size); stream->curpos += size; #ifdef HAVE_LIBZ } #endif /* HAVE_LIBZ */ }