2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
12 #include <babeltrace2/babeltrace.h>
14 #define BT_COMP_LOG_SELF_COMP self_comp
15 #define BT_COMP_LOG_SELF_COMP_CLASS self_comp_class
16 #define BT_LOG_OUTPUT_LEVEL log_level
17 #define BT_LOG_TAG "PLUGIN/CTF/META/DECODER-DECODE-PACKET"
18 #include "logging.hpp"
19 #include "logging/comp-logging.h"
21 #include "common/uuid.h"
22 #include "compat/memstream.h"
24 #include "decoder-packetized-file-stream-to-buf.hpp"
25 #include "decoder.hpp"
27 #define TSDL_MAGIC 0x75d11d57
29 struct ctf_metadata_decoder
31 struct ctf_visitor_generate_ir
*visitor
;
35 struct ctf_metadata_decoder_config config
;
43 uint32_t content_size
;
45 uint8_t compression_scheme
;
46 uint8_t encryption_scheme
;
47 uint8_t checksum_scheme
;
50 } __attribute__((__packed__
));
52 static int decode_packet(FILE *in_fp
, FILE *out_fp
, int byte_order
, bool *is_uuid_set
,
53 uint8_t *uuid
, bt_logging_level log_level
, bt_self_component
*self_comp
,
54 bt_self_component_class
*self_comp_class
)
56 struct packet_header header
;
57 size_t readlen
, writelen
, toread
;
58 uint8_t buf
[512 + 1]; /* + 1 for debug-mode \0 */
60 const long offset
= ftell(in_fp
);
63 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(BT_COMP_LOG_SELF_COMP
,
64 "Failed to get current metadata file position", ".");
67 BT_COMP_LOGD("Decoding metadata packet: offset=%ld", offset
);
68 readlen
= fread(&header
, sizeof(header
), 1, in_fp
);
69 if (feof(in_fp
) != 0) {
70 BT_COMP_LOGI("Reached end of file: offset=%ld", ftell(in_fp
));
74 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot decode metadata packet: offset=%ld",
79 if (byte_order
!= BYTE_ORDER
) {
80 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
81 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
82 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
83 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
86 if (header
.compression_scheme
) {
87 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
88 "Metadata packet compression is not supported as of this version: "
89 "compression-scheme=%u, offset=%ld",
90 (unsigned int) header
.compression_scheme
, offset
);
94 if (header
.encryption_scheme
) {
95 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
96 "Metadata packet encryption is not supported as of this version: "
97 "encryption-scheme=%u, offset=%ld",
98 (unsigned int) header
.encryption_scheme
, offset
);
102 if (header
.checksum
|| header
.checksum_scheme
) {
103 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
104 "Metadata packet checksum verification is not supported as of this version: "
105 "checksum-scheme=%u, checksum=%x, offset=%ld",
106 (unsigned int) header
.checksum_scheme
, header
.checksum
, offset
);
110 if (!ctf_metadata_decoder_is_packet_version_valid(header
.major
, header
.minor
)) {
111 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid metadata packet version: "
112 "version=%u.%u, offset=%ld",
113 header
.major
, header
.minor
, offset
);
117 /* Set expected trace UUID if not set; otherwise validate it */
120 bt_uuid_copy(uuid
, header
.uuid
);
122 } else if (bt_uuid_compare(header
.uuid
, uuid
)) {
123 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
124 "Metadata UUID mismatch between packets of the same stream: "
125 "packet-uuid=\"" BT_UUID_FMT
"\", "
126 "expected-uuid=\"" BT_UUID_FMT
"\", "
128 BT_UUID_FMT_VALUES(header
.uuid
), BT_UUID_FMT_VALUES(uuid
), offset
);
133 if ((header
.content_size
/ CHAR_BIT
) < sizeof(header
)) {
134 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
135 "Bad metadata packet content size: content-size=%u, "
137 header
.content_size
, offset
);
141 toread
= header
.content_size
/ CHAR_BIT
- sizeof(header
);
146 loop_read
= MIN(sizeof(buf
) - 1, toread
);
147 readlen
= fread(buf
, sizeof(uint8_t), loop_read
, in_fp
);
149 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot read metadata packet buffer: "
150 "offset=%ld, read-size=%zu",
151 ftell(in_fp
), loop_read
);
154 if (readlen
> loop_read
) {
155 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("fread returned more byte than expected: "
156 "read-size-asked=%zu, read-size-returned=%zu",
161 writelen
= fwrite(buf
, sizeof(uint8_t), readlen
, out_fp
);
162 if (writelen
< readlen
|| ferror(out_fp
)) {
163 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
164 "Cannot write decoded metadata text to buffer: "
165 "read-offset=%ld, write-size=%zu",
166 ftell(in_fp
), readlen
);
174 /* Read leftover padding */
175 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
176 fseek_ret
= fseek(in_fp
, toread
, SEEK_CUR
);
178 BT_COMP_LOGW_STR("Missing padding at the end of the metadata stream.");
193 int ctf_metadata_decoder_packetized_file_stream_to_buf(FILE *fp
, char **buf
, int byte_order
,
194 bool *is_uuid_set
, uint8_t *uuid
,
195 bt_logging_level log_level
,
196 bt_self_component
*self_comp
,
197 bt_self_component_class
*self_comp_class
)
203 size_t packet_index
= 0;
205 out_fp
= bt_open_memstream(buf
, &size
);
207 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot open memory stream: %s.", strerror(errno
));
216 tret
= decode_packet(fp
, out_fp
, byte_order
, is_uuid_set
, uuid
, log_level
, self_comp
,
219 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot decode packet: index=%zu",
227 /* Make sure the whole string ends with a null character */
228 tret
= fputc('\0', out_fp
);
230 _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(
231 "Cannot append '\\0' to the decoded metadata buffer.");
235 /* Close stream, which also flushes the buffer */
236 ret
= bt_close_memstream(buf
, &size
, out_fp
);
238 * See fclose(3). Further access to out_fp after both success
239 * and error, even through another bt_close_memstream(), results
240 * in undefined behavior. Nullify out_fp to ensure we don't
241 * fclose it twice on error.
245 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(BT_COMP_LOG_SELF_COMP
, "Cannot close memory stream", ".");
255 if (bt_close_memstream(buf
, &size
, out_fp
)) {
256 BT_COMP_LOGE_ERRNO("Cannot close memory stream", ".");