-/* Compress the specified CTF data stream and write it to the specified file
- descriptor. */
-int
-ctf_compress_write (ctf_dict_t *fp, int fd)
-{
- unsigned char *buf;
- unsigned char *bp;
- ctf_header_t h;
- ctf_header_t *hp = &h;
- ssize_t header_len = sizeof (ctf_header_t);
- ssize_t compress_len;
- ssize_t len;
- int rc;
- int err = 0;
-
- if (ctf_serialize (fp) < 0)
- return -1; /* errno is set for us. */
-
- memcpy (hp, fp->ctf_header, header_len);
- hp->cth_flags |= CTF_F_COMPRESS;
- compress_len = compressBound (fp->ctf_size);
-
- if ((buf = malloc (compress_len)) == NULL)
- {
- ctf_err_warn (fp, 0, 0, _("ctf_compress_write: cannot allocate %li bytes"),
- (unsigned long) compress_len);
- return (ctf_set_errno (fp, ECTF_ZALLOC));
- }
-
- if ((rc = compress (buf, (uLongf *) &compress_len,
- fp->ctf_buf, fp->ctf_size)) != Z_OK)
- {
- err = ctf_set_errno (fp, ECTF_COMPRESS);
- ctf_err_warn (fp, 0, 0, _("zlib deflate err: %s"), zError (rc));
- goto ret;
- }
-
- while (header_len > 0)
- {
- if ((len = write (fd, hp, header_len)) < 0)
- {
- err = ctf_set_errno (fp, errno);
- ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing header"));
- goto ret;
- }
- header_len -= len;
- hp += len;
- }
-
- bp = buf;
- while (compress_len > 0)
- {
- if ((len = write (fd, bp, compress_len)) < 0)
- {
- err = ctf_set_errno (fp, errno);
- ctf_err_warn (fp, 0, 0, _("ctf_compress_write: error writing"));
- goto ret;
- }
- compress_len -= len;
- bp += len;
- }
-
-ret:
- free (buf);
- return err;
-}
-