+int check_version(unsigned int major, unsigned int minor)
+{
+ switch (major) {
+ case 1:
+ switch (minor) {
+ case 8:
+ return 0;
+ default:
+ goto warning;
+ }
+ default:
+ goto warning;
+
+ }
+
+ /* eventually return an error instead of warning */
+warning:
+ fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
+ major, minor);
+ return 0;
+}
+
+static
+int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
+ FILE *out)
+{
+ struct metadata_packet_header header;
+ size_t readlen, writelen, toread;
+ char buf[4096 + 1]; /* + 1 for debug-mode \0 */
+ int ret = 0;
+
+ readlen = fread(&header, header_sizeof(header), 1, in);
+ if (readlen < 1)
+ return -EINVAL;
+
+ if (td->byte_order != BYTE_ORDER) {
+ header.magic = GUINT32_SWAP_LE_BE(header.magic);
+ header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
+ header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
+ header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
+ }
+ if (header.checksum)
+ fprintf(stderr, "[warning] checksum verification not supported yet.\n");
+ if (header.compression_scheme) {
+ fprintf(stderr, "[error] compression (%u) not supported yet.\n",
+ header.compression_scheme);
+ return -EINVAL;
+ }
+ if (header.encryption_scheme) {
+ fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
+ header.encryption_scheme);
+ return -EINVAL;
+ }
+ if (header.checksum_scheme) {
+ fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
+ header.checksum_scheme);
+ return -EINVAL;
+ }
+ if (check_version(header.major, header.minor) < 0)
+ return -EINVAL;
+ if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
+ memcpy(td->uuid, header.uuid, sizeof(header.uuid));
+ CTF_TRACE_SET_FIELD(td, uuid);
+ } else {
+ if (uuid_compare(header.uuid, td->uuid))
+ return -EINVAL;
+ }
+
+ toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
+
+ for (;;) {
+ readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
+ if (ferror(in)) {
+ ret = -EINVAL;
+ break;
+ }
+ if (babeltrace_debug) {
+ buf[readlen] = '\0';
+ fprintf(stderr, "[debug] metadata packet read: %s\n",
+ buf);
+ }
+
+ writelen = fwrite(buf, sizeof(char), readlen, out);
+ if (writelen < readlen) {
+ ret = -EIO;
+ break;
+ }
+ if (ferror(out)) {
+ ret = -EINVAL;
+ break;
+ }
+ toread -= readlen;
+ if (!toread) {
+ ret = 0; /* continue reading next packet */
+ goto read_padding;
+ }
+ }
+ return ret;
+
+read_padding:
+ toread = (header.packet_size - header.content_size) / CHAR_BIT;
+ ret = fseek(in, toread, SEEK_CUR);
+ if (ret < 0) {
+ fprintf(stderr, "[warning] Missing padding at end of file\n");
+ ret = 0;
+ }
+ return ret;
+}
+
+static
+int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
+ char **buf)
+{
+ FILE *in, *out;
+ size_t size;
+ int ret;
+
+ in = *fp;
+ /*
+ * Using strlen on *buf instead of size of open_memstream
+ * because its size includes garbage at the end (after final
+ * \0). This is the allocated size, not the actual string size.
+ */
+ out = open_memstream(buf, &size);
+ if (out == NULL) {
+ perror("Metadata open_memstream");
+ return -errno;
+ }
+ for (;;) {
+ ret = ctf_open_trace_metadata_packet_read(td, in, out);
+ if (ret) {
+ break;
+ }
+ if (feof(in)) {
+ ret = 0;
+ break;
+ }
+ }
+ fclose(out); /* flush the buffer */
+ fclose(in);
+ /* open for reading */
+ *fp = fmemopen(*buf, strlen(*buf), "rb");
+ if (!*fp) {
+ perror("Metadata fmemopen");
+ return -errno;
+ }
+ return 0;
+}
+
+static
+int ctf_open_trace_metadata_read(struct ctf_trace *td,
+ void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
+ int whence), FILE *metadata_fp)