2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
7 #define BT_COMP_LOG_SELF_COMP self_comp
8 #define BT_LOG_OUTPUT_LEVEL log_level
9 #define BT_LOG_TAG "PLUGIN/CTF/META/DECODER-DECODE-PACKET"
10 #include "logging/comp-logging.h"
12 #include "decoder-packetized-file-stream-to-buf.h"
19 #include "common/assert.h"
20 #include "common/uuid.h"
21 #include "compat/memstream.h"
22 #include <babeltrace2/babeltrace.h>
31 #define TSDL_MAGIC 0x75d11d57
33 struct ctf_metadata_decoder
{
34 struct ctf_visitor_generate_ir
*visitor
;
38 struct ctf_metadata_decoder_config config
;
41 struct packet_header
{
45 uint32_t content_size
;
47 uint8_t compression_scheme
;
48 uint8_t encryption_scheme
;
49 uint8_t checksum_scheme
;
52 } __attribute__((__packed__
));
55 int decode_packet(FILE *in_fp
, FILE *out_fp
,
56 int byte_order
, bool *is_uuid_set
, uint8_t *uuid
,
57 bt_logging_level log_level
, bt_self_component
*self_comp
)
59 struct packet_header header
;
60 size_t readlen
, writelen
, toread
;
61 uint8_t buf
[512 + 1]; /* + 1 for debug-mode \0 */
63 const long offset
= ftell(in_fp
);
66 BT_COMP_LOGE_ERRNO("Failed to get current metadata file position",
70 BT_COMP_LOGD("Decoding metadata packet: offset=%ld", offset
);
71 readlen
= fread(&header
, sizeof(header
), 1, in_fp
);
72 if (feof(in_fp
) != 0) {
73 BT_COMP_LOGI("Reached end of file: offset=%ld", ftell(in_fp
));
77 BT_COMP_LOGE("Cannot decode metadata packet: offset=%ld", offset
);
81 if (byte_order
!= BYTE_ORDER
) {
82 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
83 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
84 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
85 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
88 if (header
.compression_scheme
) {
89 BT_COMP_LOGE("Metadata packet compression is not supported as of this version: "
90 "compression-scheme=%u, offset=%ld",
91 (unsigned int) header
.compression_scheme
, offset
);
95 if (header
.encryption_scheme
) {
96 BT_COMP_LOGE("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_LOGE("Metadata packet checksum verification is not supported as of this version: "
104 "checksum-scheme=%u, checksum=%x, offset=%ld",
105 (unsigned int) header
.checksum_scheme
, header
.checksum
,
110 if (!ctf_metadata_decoder_is_packet_version_valid(header
.major
,
112 BT_COMP_LOGE("Invalid metadata packet version: "
113 "version=%u.%u, offset=%ld",
114 header
.major
, header
.minor
, offset
);
118 /* Set expected trace UUID if not set; otherwise validate it */
121 bt_uuid_copy(uuid
, header
.uuid
);
123 } else if (bt_uuid_compare(header
.uuid
, uuid
)) {
124 BT_COMP_LOGE("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
),
129 BT_UUID_FMT_VALUES(uuid
),
135 if ((header
.content_size
/ CHAR_BIT
) < sizeof(header
)) {
136 BT_COMP_LOGE("Bad metadata packet content size: content-size=%u, "
137 "offset=%ld", 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_LOGE("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_LOGE("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_LOGE("Cannot write decoded metadata text to buffer: "
164 "read-offset=%ld, write-size=%zu",
165 ftell(in_fp
), readlen
);
173 /* Read leftover padding */
174 toread
= (header
.packet_size
- header
.content_size
) /
176 fseek_ret
= fseek(in_fp
, toread
, SEEK_CUR
);
178 BT_COMP_LOGW_STR("Missing padding at the end of the metadata stream.");
194 int ctf_metadata_decoder_packetized_file_stream_to_buf(FILE *fp
,
195 char **buf
, int byte_order
, bool *is_uuid_set
,
196 uint8_t *uuid
, bt_logging_level log_level
,
197 bt_self_component
*self_comp
)
203 size_t packet_index
= 0;
205 out_fp
= bt_open_memstream(buf
, &size
);
207 BT_COMP_LOGE("Cannot open memory stream: %s.",
217 tret
= decode_packet(fp
, out_fp
, byte_order
, is_uuid_set
,
218 uuid
, log_level
, self_comp
);
220 BT_COMP_LOGE("Cannot decode packet: index=%zu",
228 /* Make sure the whole string ends with a null character */
229 tret
= fputc('\0', out_fp
);
232 "Cannot append '\\0' to the decoded metadata buffer.");
236 /* Close stream, which also flushes the buffer */
237 ret
= bt_close_memstream(buf
, &size
, out_fp
);
239 * See fclose(3). Further access to out_fp after both success
240 * and error, even through another bt_close_memstream(), results
241 * in undefined behavior. Nullify out_fp to ensure we don't
242 * fclose it twice on error.
246 BT_COMP_LOGE_ERRNO("Cannot close memory stream", ".");
256 if (bt_close_memstream(buf
, &size
, out_fp
)) {
257 BT_COMP_LOGE_ERRNO("Cannot close memory stream", ".");