+ w->zstream.next_in = CONST_CAST (uint8_t *, data);
+ w->zstream.avail_in = chunk;
+ do
+ {
+ uint8_t buf[4096];
+ int error;
+
+ w->zstream.next_out = buf;
+ w->zstream.avail_out = sizeof buf;
+ error = deflate (&w->zstream, Z_NO_FLUSH);
+ write_bytes (w, buf, w->zstream.next_out - buf);
+ if (error != Z_OK)
+ {
+ msg (ME, _("ZLIB stream compression failed (%s)."),
+ w->zstream.msg);
+ return;
+ }
+ }
+ while (w->zstream.avail_in > 0 || w->zstream.avail_out == 0);
+ data += chunk;
+ n -= chunk;
+ }
+}
+
+static void
+write_ztrailer (struct sfm_writer *w)
+{
+ long long int uncompressed_ofs;
+ long long int compressed_ofs;
+ const struct zblock *block;
+
+ write_int64 (w, -COMPRESSION_BIAS);
+ write_int64 (w, 0);
+ write_int (w, ZBLOCK_SIZE);
+ write_int (w, w->n_blocks);
+
+ uncompressed_ofs = w->zstart;
+ compressed_ofs = w->zstart + 24;
+ for (block = w->blocks; block < &w->blocks[w->n_blocks]; block++)
+ {
+ write_int64 (w, uncompressed_ofs);
+ write_int64 (w, compressed_ofs);
+ write_int (w, block->uncompressed_size);
+ write_int (w, block->compressed_size);
+
+ uncompressed_ofs += block->uncompressed_size;
+ compressed_ofs += block->compressed_size;
+ }
+
+ if (!fseeko (w->file, w->zstart + 8, SEEK_SET))
+ {
+ write_int64 (w, compressed_ofs);
+ write_int64 (w, 24 + (w->n_blocks * 24));
+ }
+ else
+ msg (ME, _("%s: Seek failed (%s)."),
+ fh_get_file_name (w->fh), strerror (errno));
+}
+
+/* Flushes buffered compressed opcodes and data to W. */
+static void
+flush_compressed (struct sfm_writer *w)
+{
+ if (w->n_opcodes)
+ {
+ unsigned int n = 8 * (1 + w->n_elements);
+ if (w->compression == ANY_COMP_SIMPLE)
+ write_bytes (w, w->cbuf, n);
+ else
+ write_zlib (w, w->cbuf, n);
+
+ w->n_opcodes = w->n_elements = 0;
+ memset (w->cbuf[0], 0, 8);
+ }