+ g_free(metabuf);
+
+ return SR_OK;
+}
+
+/**
+ * Queue a block of logic data for srzip archive writes.
+ *
+ * @param[in] o Output module instance.
+ * @param[in] buf Logic data samples as byte sequence.
+ * @param[in] unitsize Logic data unit size (bytes per sample).
+ * @param[in] length Number of bytes of sample data.
+ * @param[in] flush Force ZIP archive update (queue by default).
+ *
+ * @returns SR_OK et al error codes.
+ */
+static int zip_append_queue(const struct sr_output *o,
+ uint8_t *buf, size_t unitsize, size_t length, gboolean flush)
+{
+ struct out_context *outc;
+ struct logic_buff *buff;
+ size_t send_size, remain, copy_size;
+ uint8_t *wrptr, *rdptr;
+ int ret;
+
+ outc = o->priv;
+ buff = &outc->logic_buff;
+ if (length && unitsize != buff->unit_size) {
+ sr_warn("Unexpected unit size, discarding logic data.");
+ return SR_ERR_ARG;
+ }
+
+ /*
+ * Queue most recently received samples to the local buffer.
+ * Flush to the ZIP archive when the buffer space is exhausted.
+ */
+ rdptr = buf;
+ send_size = buff->unit_size ? length / buff->unit_size : 0;
+ while (send_size) {
+ remain = buff->alloc_size - buff->fill_size;
+ if (remain) {
+ wrptr = &buff->samples[buff->fill_size * buff->unit_size];
+ copy_size = MIN(send_size, remain);
+ send_size -= copy_size;
+ buff->fill_size += copy_size;
+ memcpy(wrptr, rdptr, copy_size * buff->unit_size);
+ rdptr += copy_size * buff->unit_size;
+ remain -= copy_size;
+ }
+ if (send_size && !remain) {
+ ret = zip_append(o, buff->samples, buff->unit_size,
+ buff->fill_size * buff->unit_size);
+ if (ret != SR_OK)
+ return ret;
+ buff->fill_size = 0;
+ remain = buff->alloc_size - buff->fill_size;
+ }
+ }
+
+ /* Flush to the ZIP archive if the caller wants us to. */
+ if (flush && buff->fill_size) {
+ ret = zip_append(o, buff->samples, buff->unit_size,
+ buff->fill_size * buff->unit_size);
+ if (ret != SR_OK)
+ return ret;
+ buff->fill_size = 0;
+ }
+
+ return SR_OK;
+}
+
+/**
+ * Append analog data of a channel to an srzip archive.
+ *
+ * @param[in] o Output module instance.
+ * @param[in] values Sample data as array of floating point values.
+ * @param[in] count Number of samples (float items, not bytes).
+ * @param[in] ch_nr 1-based channel number.
+ *
+ * @returns SR_OK et al error codes.
+ */
+static int zip_append_analog(const struct sr_output *o,
+ const float *values, size_t count, size_t ch_nr)
+{
+ struct out_context *outc;
+ struct zip *archive;
+ struct zip_source *analogsrc;
+ int64_t i, num_files;
+ size_t size;
+ struct zip_stat zs;
+ uint64_t chunk_num;
+ const char *entry_name;
+ char *basename;
+ gsize baselen;
+ char *chunkname;
+ unsigned int next_chunk_num;
+
+ outc = o->priv;
+
+ if (!(archive = zip_open(outc->filename, 0, NULL)))
+ return SR_ERR;
+
+ if (zip_stat(archive, "metadata", 0, &zs) < 0) {
+ sr_err("Failed to open metadata: %s", zip_strerror(archive));
+ zip_discard(archive);
+ return SR_ERR;
+ }
+
+ basename = g_strdup_printf("analog-1-%zu", ch_nr);
+ baselen = strlen(basename);
+ next_chunk_num = 1;
+ num_files = zip_get_num_entries(archive, 0);
+ for (i = 0; i < num_files; i++) {
+ entry_name = zip_get_name(archive, i, 0);
+ if (!entry_name || strncmp(entry_name, basename, baselen) != 0) {
+ continue;
+ } else if (entry_name[baselen] == '-') {
+ chunk_num = g_ascii_strtoull(entry_name + baselen + 1, NULL, 10);
+ if (chunk_num < G_MAXINT && chunk_num >= next_chunk_num)
+ next_chunk_num = chunk_num + 1;
+ }
+ }
+
+ size = sizeof(values[0]) * count;
+ analogsrc = zip_source_buffer(archive, values, size, FALSE);
+ chunkname = g_strdup_printf("%s-%u", basename, next_chunk_num);
+ i = zip_add(archive, chunkname, analogsrc);
+ if (i < 0) {
+ sr_err("Failed to add chunk '%s': %s", chunkname, zip_strerror(archive));
+ g_free(chunkname);
+ g_free(basename);
+ zip_source_free(analogsrc);
+ zip_discard(archive);
+ return SR_ERR;
+ }
+ g_free(chunkname);
+ if (zip_close(archive) < 0) {
+ sr_err("Error saving session file: %s", zip_strerror(archive));
+ g_free(basename);
+ zip_discard(archive);