+ 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,
+ const uint8_t *buf, size_t feed_unitsize, size_t length,
+ gboolean flush)
+{
+ static gboolean sizes_seen;
+
+ struct out_context *outc;
+ struct logic_buff *buff;
+ size_t sample_copy_size, sample_skip_size, sample_pad_size;
+ size_t send_count, remain, copy_count;
+ const uint8_t *rdptr;
+ uint8_t *wrptr;
+ int ret;
+
+ /*
+ * Check input parameters. Prepare to either grab data as is,
+ * or to adjust between differing input and output unit sizes.
+ * Diagnostics is rate limited for improved usability, assumes
+ * that session feeds are consistent across calls. Processing
+ * would cope with inconsistent calls though when required.
+ */
+ outc = o->priv;
+ buff = &outc->logic_buff;
+ if (length) {
+ if (!sizes_seen) {
+ sr_info("output unit size %zu, feed unit size %zu.",
+ buff->zip_unit_size, feed_unitsize);
+ }
+ if (feed_unitsize > buff->zip_unit_size) {
+ if (!sizes_seen)
+ sr_info("Large unit size, discarding excess logic data.");
+ sample_copy_size = buff->zip_unit_size;
+ sample_skip_size = feed_unitsize - buff->zip_unit_size;
+ sample_pad_size = 0;
+ } else if (feed_unitsize < buff->zip_unit_size) {
+ if (!sizes_seen)
+ sr_info("Small unit size, padding logic data.");
+ sample_copy_size = feed_unitsize;
+ sample_skip_size = 0;
+ sample_pad_size = buff->zip_unit_size - feed_unitsize;
+ } else {
+ if (!sizes_seen)
+ sr_dbg("Matching unit size, passing logic data as is.");
+ sample_copy_size = buff->zip_unit_size;
+ sample_skip_size = 0;
+ sample_pad_size = 0;
+ }
+ if (sample_copy_size + sample_skip_size != feed_unitsize) {
+ sr_err("Inconsistent input unit size. Implementation flaw?");
+ return SR_ERR_BUG;
+ }
+ if (sample_copy_size + sample_pad_size != buff->zip_unit_size) {
+ sr_err("Inconsistent output unit size. Implementation flaw?");
+ return SR_ERR_BUG;
+ }
+ sizes_seen = TRUE;
+ }
+
+ /*
+ * Queue most recently received samples to the local buffer.
+ * Flush to the ZIP archive when the buffer space is exhausted.
+ */
+ rdptr = buf;
+ send_count = feed_unitsize ? length / feed_unitsize : 0;
+ while (send_count) {
+ remain = buff->alloc_size - buff->fill_size;
+ wrptr = &buff->samples[buff->fill_size * buff->zip_unit_size];
+ if (remain) {
+ copy_count = MIN(send_count, remain);
+ if (sample_skip_size || sample_pad_size)
+ copy_count = 1;
+ send_count -= copy_count;
+ buff->fill_size += copy_count;
+ memcpy(wrptr, rdptr, copy_count * sample_copy_size);
+ if (sample_pad_size) {
+ wrptr += sample_copy_size;
+ memset(wrptr, 0, sample_pad_size);
+ }
+ rdptr += copy_count * sample_copy_size;
+ if (sample_skip_size)
+ rdptr += sample_skip_size;
+ remain -= copy_count;
+ }
+ if (send_count && !remain) {
+ ret = zip_append(o, buff->samples, buff->zip_unit_size,
+ buff->fill_size * buff->zip_unit_size);
+ if (ret != SR_OK)
+ return ret;
+ buff->fill_size = 0;
+ }
+ }
+
+ /* Flush to the ZIP archive if the caller wants us to. */
+ if (flush && buff->fill_size) {
+ ret = zip_append(o, buff->samples, buff->zip_unit_size,
+ buff->fill_size * buff->zip_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);
+ return SR_ERR;
+ }
+
+ g_free(basename);
+
+ return SR_OK;
+}
+
+/**
+ * Queue analog data of a channel for srzip archive writes.
+ *
+ * @param[in] o Output module instance.
+ * @param[in] analog Sample data (session feed packet format).
+ * @param[in] flush Force ZIP archive update (queue by default).
+ *
+ * @returns SR_OK et al error codes.
+ */
+static int zip_append_analog_queue(const struct sr_output *o,
+ const struct sr_datafeed_analog *analog, gboolean flush)
+{
+ struct out_context *outc;
+ const struct sr_channel *ch;
+ size_t idx, nr;
+ struct analog_buff *buff;
+ float *values, *wrptr, *rdptr;
+ size_t send_size, remain, copy_size;
+ int ret;
+
+ outc = o->priv;
+
+ /* Is this the DF_END flush call without samples submission? */
+ if (!analog && flush) {
+ for (idx = 0; idx < outc->analog_ch_count; idx++) {
+ nr = outc->first_analog_index + idx;
+ buff = &outc->analog_buff[idx];
+ if (!buff->fill_size)
+ continue;
+ ret = zip_append_analog(o,
+ buff->samples, buff->fill_size, nr);
+ if (ret != SR_OK)
+ return ret;
+ buff->fill_size = 0;
+ }
+ return SR_OK;
+ }
+
+ /* Lookup index and number of the analog channel. */
+ /* TODO: support packets covering multiple channels */
+ if (g_slist_length(analog->meaning->channels) != 1) {
+ sr_err("Analog packets covering multiple channels not supported yet");