2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
15 #define BT_COMP_LOG_SELF_COMP self_comp
16 #define BT_LOG_OUTPUT_LEVEL log_level
17 #define BT_LOG_TAG "PLUGIN/CTF/META/DECODER-DECODE-PACKET"
18 #include "plugins/comp-logging.h"
25 #include "common/assert.h"
26 #include "compat/uuid.h"
27 #include "compat/memstream.h"
28 #include <babeltrace2/babeltrace.h>
37 #define TSDL_MAGIC 0x75d11d57
42 struct ctf_metadata_decoder
{
43 struct ctf_visitor_generate_ir
*visitor
;
47 struct ctf_metadata_decoder_config config
;
50 struct packet_header
{
54 uint32_t content_size
;
56 uint8_t compression_scheme
;
57 uint8_t encryption_scheme
;
58 uint8_t checksum_scheme
;
61 } __attribute__((__packed__
));
64 int decode_packet(FILE *in_fp
, FILE *out_fp
,
65 int byte_order
, bool *is_uuid_set
, uint8_t *uuid
,
66 bt_logging_level log_level
, bt_self_component
*self_comp
)
68 struct packet_header header
;
69 size_t readlen
, writelen
, toread
;
70 uint8_t buf
[512 + 1]; /* + 1 for debug-mode \0 */
72 const long offset
= ftell(in_fp
);
75 BT_COMP_LOGE_ERRNO("Failed to get current metadata file position",
79 BT_COMP_LOGD("Decoding metadata packet: offset=%ld", offset
);
80 readlen
= fread(&header
, sizeof(header
), 1, in_fp
);
81 if (feof(in_fp
) != 0) {
82 BT_COMP_LOGI("Reached end of file: offset=%ld", ftell(in_fp
));
86 BT_COMP_LOGE("Cannot decode metadata packet: offset=%ld", offset
);
90 if (byte_order
!= BYTE_ORDER
) {
91 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
92 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
93 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
94 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
97 if (header
.compression_scheme
) {
98 BT_COMP_LOGE("Metadata packet compression is not supported as of this version: "
99 "compression-scheme=%u, offset=%ld",
100 (unsigned int) header
.compression_scheme
, offset
);
104 if (header
.encryption_scheme
) {
105 BT_COMP_LOGE("Metadata packet encryption is not supported as of this version: "
106 "encryption-scheme=%u, offset=%ld",
107 (unsigned int) header
.encryption_scheme
, offset
);
111 if (header
.checksum
|| header
.checksum_scheme
) {
112 BT_COMP_LOGE("Metadata packet checksum verification is not supported as of this version: "
113 "checksum-scheme=%u, checksum=%x, offset=%ld",
114 (unsigned int) header
.checksum_scheme
, header
.checksum
,
119 if (!ctf_metadata_decoder_is_packet_version_valid(header
.major
,
121 BT_COMP_LOGE("Invalid metadata packet version: "
122 "version=%u.%u, offset=%ld",
123 header
.major
, header
.minor
, offset
);
127 /* Set expected trace UUID if not set; otherwise validate it */
130 memcpy(uuid
, header
.uuid
, sizeof(header
.uuid
));
132 } else if (bt_uuid_compare(header
.uuid
, uuid
)) {
133 BT_COMP_LOGE("Metadata UUID mismatch between packets of the same stream: "
134 "packet-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
135 "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
137 (unsigned int) header
.uuid
[0],
138 (unsigned int) header
.uuid
[1],
139 (unsigned int) header
.uuid
[2],
140 (unsigned int) header
.uuid
[3],
141 (unsigned int) header
.uuid
[4],
142 (unsigned int) header
.uuid
[5],
143 (unsigned int) header
.uuid
[6],
144 (unsigned int) header
.uuid
[7],
145 (unsigned int) header
.uuid
[8],
146 (unsigned int) header
.uuid
[9],
147 (unsigned int) header
.uuid
[10],
148 (unsigned int) header
.uuid
[11],
149 (unsigned int) header
.uuid
[12],
150 (unsigned int) header
.uuid
[13],
151 (unsigned int) header
.uuid
[14],
152 (unsigned int) header
.uuid
[15],
153 (unsigned int) uuid
[0],
154 (unsigned int) uuid
[1],
155 (unsigned int) uuid
[2],
156 (unsigned int) uuid
[3],
157 (unsigned int) uuid
[4],
158 (unsigned int) uuid
[5],
159 (unsigned int) uuid
[6],
160 (unsigned int) uuid
[7],
161 (unsigned int) uuid
[8],
162 (unsigned int) uuid
[9],
163 (unsigned int) uuid
[10],
164 (unsigned int) uuid
[11],
165 (unsigned int) uuid
[12],
166 (unsigned int) uuid
[13],
167 (unsigned int) uuid
[14],
168 (unsigned int) uuid
[15],
174 if ((header
.content_size
/ CHAR_BIT
) < sizeof(header
)) {
175 BT_COMP_LOGE("Bad metadata packet content size: content-size=%u, "
176 "offset=%ld", header
.content_size
, offset
);
180 toread
= header
.content_size
/ CHAR_BIT
- sizeof(header
);
185 loop_read
= MIN(sizeof(buf
) - 1, toread
);
186 readlen
= fread(buf
, sizeof(uint8_t), loop_read
, in_fp
);
188 BT_COMP_LOGE("Cannot read metadata packet buffer: "
189 "offset=%ld, read-size=%zu",
190 ftell(in_fp
), loop_read
);
193 if (readlen
> loop_read
) {
194 BT_COMP_LOGE("fread returned more byte than expected: "
195 "read-size-asked=%zu, read-size-returned=%zu",
200 writelen
= fwrite(buf
, sizeof(uint8_t), readlen
, out_fp
);
201 if (writelen
< readlen
|| ferror(out_fp
)) {
202 BT_COMP_LOGE("Cannot write decoded metadata text to buffer: "
203 "read-offset=%ld, write-size=%zu",
204 ftell(in_fp
), readlen
);
212 /* Read leftover padding */
213 toread
= (header
.packet_size
- header
.content_size
) /
215 fseek_ret
= fseek(in_fp
, toread
, SEEK_CUR
);
217 BT_COMP_LOGW_STR("Missing padding at the end of the metadata stream.");
233 int ctf_metadata_decoder_packetized_file_stream_to_buf(FILE *fp
,
234 char **buf
, int byte_order
, bool *is_uuid_set
,
235 uint8_t *uuid
, bt_logging_level log_level
,
236 bt_self_component
*self_comp
)
242 size_t packet_index
= 0;
244 out_fp
= bt_open_memstream(buf
, &size
);
245 if (out_fp
== NULL
) {
246 BT_COMP_LOGE("Cannot open memory stream: %s.",
256 tret
= decode_packet(fp
, out_fp
, byte_order
, is_uuid_set
,
257 uuid
, log_level
, self_comp
);
259 BT_COMP_LOGE("Cannot decode packet: index=%zu",
267 /* Make sure the whole string ends with a null character */
268 tret
= fputc('\0', out_fp
);
271 "Cannot append '\\0' to the decoded metadata buffer.");
275 /* Close stream, which also flushes the buffer */
276 ret
= bt_close_memstream(buf
, &size
, out_fp
);
278 * See fclose(3). Further access to out_fp after both success
279 * and error, even through another bt_close_memstream(), results
280 * in undefined behavior. Nullify out_fp to ensure we don't
281 * fclose it twice on error.
285 BT_COMP_LOGE_ERRNO("Cannot close memory stream", ".");
295 if (bt_close_memstream(buf
, &size
, out_fp
)) {
296 BT_COMP_LOGE_ERRNO("Cannot close memory stream", ".");