+/**
+ * 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);
+ 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");
+ return SR_ERR;
+ }
+ ch = g_slist_nth_data(analog->meaning->channels, 0);
+ for (idx = 0; idx < outc->analog_ch_count; idx++) {
+ if (outc->analog_index_map[idx] == ch->index)
+ break;
+ }
+ if (idx == outc->analog_ch_count)
+ return SR_ERR_ARG;
+ nr = outc->first_analog_index + idx;
+ buff = &outc->analog_buff[idx];
+
+ /* Convert the analog data to an array of float values. */
+ values = g_try_malloc0(analog->num_samples * sizeof(values[0]));
+ if (!values)
+ return SR_ERR_MALLOC;
+ ret = sr_analog_to_float(analog, values);
+ if (ret != SR_OK) {
+ g_free(values);
+ return ret;
+ }
+
+ /*
+ * Queue most recently received samples to the local buffer.
+ * Flush to the ZIP archive when the buffer space is exhausted.
+ */
+ rdptr = values;
+ send_size = analog->num_samples;
+ while (send_size) {
+ remain = buff->alloc_size - buff->fill_size;
+ if (remain) {
+ wrptr = &buff->samples[buff->fill_size];
+ copy_size = MIN(send_size, remain);
+ send_size -= copy_size;
+ buff->fill_size += copy_size;
+ memcpy(wrptr, rdptr, copy_size * sizeof(values[0]));
+ rdptr += copy_size;
+ remain -= copy_size;
+ }
+ if (send_size && !remain) {
+ ret = zip_append_analog(o,
+ buff->samples, buff->fill_size, nr);
+ if (ret != SR_OK) {
+ g_free(values);
+ return ret;
+ }
+ buff->fill_size = 0;
+ remain = buff->alloc_size - buff->fill_size;
+ }
+ }
+ g_free(values);
+
+ /* Flush to the ZIP archive if the caller wants us to. */
+ if (flush && buff->fill_size) {
+ ret = zip_append_analog(o, buff->samples, buff->fill_size, nr);
+ if (ret != SR_OK)
+ return ret;
+ buff->fill_size = 0;
+ }
+
+ return SR_OK;
+}
+