Commit | Line | Data |
---|---|---|
a248254b PP |
1 | /* |
2 | * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
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: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | */ | |
14 | ||
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" | |
3fa1b6a3 | 18 | #include "logging/comp-logging.h" |
a248254b | 19 | |
f3847c75 SM |
20 | #include "decoder-packetized-file-stream-to-buf.h" |
21 | ||
a248254b PP |
22 | #include <stdio.h> |
23 | #include <stdbool.h> | |
24 | #include <stdint.h> | |
25 | #include <stdlib.h> | |
26 | #include <inttypes.h> | |
27 | #include "common/assert.h" | |
d126826c | 28 | #include "common/uuid.h" |
a248254b PP |
29 | #include "compat/memstream.h" |
30 | #include <babeltrace2/babeltrace.h> | |
31 | #include <glib.h> | |
32 | #include <string.h> | |
33 | ||
34 | #include "ast.h" | |
35 | #include "decoder.h" | |
36 | #include "scanner.h" | |
37 | #include "logging.h" | |
38 | ||
39 | #define TSDL_MAGIC 0x75d11d57 | |
40 | ||
a248254b PP |
41 | struct ctf_metadata_decoder { |
42 | struct ctf_visitor_generate_ir *visitor; | |
d126826c | 43 | bt_uuid_t uuid; |
a248254b PP |
44 | bool is_uuid_set; |
45 | int bo; | |
46 | struct ctf_metadata_decoder_config config; | |
47 | }; | |
48 | ||
49 | struct packet_header { | |
50 | uint32_t magic; | |
d126826c | 51 | bt_uuid_t uuid; |
a248254b PP |
52 | uint32_t checksum; |
53 | uint32_t content_size; | |
54 | uint32_t packet_size; | |
55 | uint8_t compression_scheme; | |
56 | uint8_t encryption_scheme; | |
57 | uint8_t checksum_scheme; | |
58 | uint8_t major; | |
59 | uint8_t minor; | |
60 | } __attribute__((__packed__)); | |
61 | ||
62 | static | |
63 | int decode_packet(FILE *in_fp, FILE *out_fp, | |
64 | int byte_order, bool *is_uuid_set, uint8_t *uuid, | |
65 | bt_logging_level log_level, bt_self_component *self_comp) | |
66 | { | |
67 | struct packet_header header; | |
68 | size_t readlen, writelen, toread; | |
69 | uint8_t buf[512 + 1]; /* + 1 for debug-mode \0 */ | |
70 | int ret = 0; | |
71 | const long offset = ftell(in_fp); | |
72 | ||
73 | if (offset < 0) { | |
74 | BT_COMP_LOGE_ERRNO("Failed to get current metadata file position", | |
75 | "."); | |
76 | goto error; | |
77 | } | |
78 | BT_COMP_LOGD("Decoding metadata packet: offset=%ld", offset); | |
79 | readlen = fread(&header, sizeof(header), 1, in_fp); | |
80 | if (feof(in_fp) != 0) { | |
81 | BT_COMP_LOGI("Reached end of file: offset=%ld", ftell(in_fp)); | |
82 | goto end; | |
83 | } | |
84 | if (readlen < 1) { | |
85 | BT_COMP_LOGE("Cannot decode metadata packet: offset=%ld", offset); | |
86 | goto error; | |
87 | } | |
88 | ||
89 | if (byte_order != BYTE_ORDER) { | |
90 | header.magic = GUINT32_SWAP_LE_BE(header.magic); | |
91 | header.checksum = GUINT32_SWAP_LE_BE(header.checksum); | |
92 | header.content_size = GUINT32_SWAP_LE_BE(header.content_size); | |
93 | header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size); | |
94 | } | |
95 | ||
96 | if (header.compression_scheme) { | |
97 | BT_COMP_LOGE("Metadata packet compression is not supported as of this version: " | |
98 | "compression-scheme=%u, offset=%ld", | |
99 | (unsigned int) header.compression_scheme, offset); | |
100 | goto error; | |
101 | } | |
102 | ||
103 | if (header.encryption_scheme) { | |
104 | BT_COMP_LOGE("Metadata packet encryption is not supported as of this version: " | |
105 | "encryption-scheme=%u, offset=%ld", | |
106 | (unsigned int) header.encryption_scheme, offset); | |
107 | goto error; | |
108 | } | |
109 | ||
110 | if (header.checksum || header.checksum_scheme) { | |
111 | BT_COMP_LOGE("Metadata packet checksum verification is not supported as of this version: " | |
112 | "checksum-scheme=%u, checksum=%x, offset=%ld", | |
113 | (unsigned int) header.checksum_scheme, header.checksum, | |
114 | offset); | |
115 | goto error; | |
116 | } | |
117 | ||
118 | if (!ctf_metadata_decoder_is_packet_version_valid(header.major, | |
119 | header.minor)) { | |
120 | BT_COMP_LOGE("Invalid metadata packet version: " | |
121 | "version=%u.%u, offset=%ld", | |
122 | header.major, header.minor, offset); | |
123 | goto error; | |
124 | } | |
125 | ||
126 | /* Set expected trace UUID if not set; otherwise validate it */ | |
127 | if (is_uuid_set) { | |
128 | if (!*is_uuid_set) { | |
d126826c | 129 | bt_uuid_copy(uuid, header.uuid); |
a248254b PP |
130 | *is_uuid_set = true; |
131 | } else if (bt_uuid_compare(header.uuid, uuid)) { | |
132 | BT_COMP_LOGE("Metadata UUID mismatch between packets of the same stream: " | |
d126826c MJ |
133 | "packet-uuid=\"" BT_UUID_FMT "\", " |
134 | "expected-uuid=\"" BT_UUID_FMT "\", " | |
a248254b | 135 | "offset=%ld", |
d126826c MJ |
136 | BT_UUID_FMT_VALUES(header.uuid), |
137 | BT_UUID_FMT_VALUES(uuid), | |
a248254b PP |
138 | offset); |
139 | goto error; | |
140 | } | |
141 | } | |
142 | ||
143 | if ((header.content_size / CHAR_BIT) < sizeof(header)) { | |
144 | BT_COMP_LOGE("Bad metadata packet content size: content-size=%u, " | |
145 | "offset=%ld", header.content_size, offset); | |
146 | goto error; | |
147 | } | |
148 | ||
149 | toread = header.content_size / CHAR_BIT - sizeof(header); | |
150 | ||
151 | for (;;) { | |
152 | size_t loop_read; | |
153 | ||
154 | loop_read = MIN(sizeof(buf) - 1, toread); | |
155 | readlen = fread(buf, sizeof(uint8_t), loop_read, in_fp); | |
156 | if (ferror(in_fp)) { | |
157 | BT_COMP_LOGE("Cannot read metadata packet buffer: " | |
158 | "offset=%ld, read-size=%zu", | |
159 | ftell(in_fp), loop_read); | |
160 | goto error; | |
161 | } | |
162 | if (readlen > loop_read) { | |
163 | BT_COMP_LOGE("fread returned more byte than expected: " | |
164 | "read-size-asked=%zu, read-size-returned=%zu", | |
165 | loop_read, readlen); | |
166 | goto error; | |
167 | } | |
168 | ||
169 | writelen = fwrite(buf, sizeof(uint8_t), readlen, out_fp); | |
170 | if (writelen < readlen || ferror(out_fp)) { | |
171 | BT_COMP_LOGE("Cannot write decoded metadata text to buffer: " | |
172 | "read-offset=%ld, write-size=%zu", | |
173 | ftell(in_fp), readlen); | |
174 | goto error; | |
175 | } | |
176 | ||
177 | toread -= readlen; | |
178 | if (toread == 0) { | |
179 | int fseek_ret; | |
180 | ||
181 | /* Read leftover padding */ | |
182 | toread = (header.packet_size - header.content_size) / | |
183 | CHAR_BIT; | |
184 | fseek_ret = fseek(in_fp, toread, SEEK_CUR); | |
185 | if (fseek_ret < 0) { | |
186 | BT_COMP_LOGW_STR("Missing padding at the end of the metadata stream."); | |
187 | } | |
188 | break; | |
189 | } | |
190 | } | |
191 | ||
192 | goto end; | |
193 | ||
194 | error: | |
195 | ret = -1; | |
196 | ||
197 | end: | |
198 | return ret; | |
199 | } | |
200 | ||
201 | BT_HIDDEN | |
202 | int ctf_metadata_decoder_packetized_file_stream_to_buf(FILE *fp, | |
203 | char **buf, int byte_order, bool *is_uuid_set, | |
204 | uint8_t *uuid, bt_logging_level log_level, | |
205 | bt_self_component *self_comp) | |
206 | { | |
207 | FILE *out_fp; | |
208 | size_t size; | |
209 | int ret = 0; | |
210 | int tret; | |
211 | size_t packet_index = 0; | |
212 | ||
213 | out_fp = bt_open_memstream(buf, &size); | |
8e01f2d9 | 214 | if (!out_fp) { |
a248254b PP |
215 | BT_COMP_LOGE("Cannot open memory stream: %s.", |
216 | strerror(errno)); | |
217 | goto error; | |
218 | } | |
219 | ||
220 | for (;;) { | |
221 | if (feof(fp) != 0) { | |
222 | break; | |
223 | } | |
224 | ||
225 | tret = decode_packet(fp, out_fp, byte_order, is_uuid_set, | |
226 | uuid, log_level, self_comp); | |
227 | if (tret) { | |
228 | BT_COMP_LOGE("Cannot decode packet: index=%zu", | |
229 | packet_index); | |
230 | goto error; | |
231 | } | |
232 | ||
233 | packet_index++; | |
234 | } | |
235 | ||
236 | /* Make sure the whole string ends with a null character */ | |
237 | tret = fputc('\0', out_fp); | |
238 | if (tret == EOF) { | |
239 | BT_COMP_LOGE_STR( | |
240 | "Cannot append '\\0' to the decoded metadata buffer."); | |
241 | goto error; | |
242 | } | |
243 | ||
244 | /* Close stream, which also flushes the buffer */ | |
245 | ret = bt_close_memstream(buf, &size, out_fp); | |
246 | /* | |
247 | * See fclose(3). Further access to out_fp after both success | |
248 | * and error, even through another bt_close_memstream(), results | |
249 | * in undefined behavior. Nullify out_fp to ensure we don't | |
250 | * fclose it twice on error. | |
251 | */ | |
252 | out_fp = NULL; | |
253 | if (ret < 0) { | |
254 | BT_COMP_LOGE_ERRNO("Cannot close memory stream", "."); | |
255 | goto error; | |
256 | } | |
257 | ||
258 | goto end; | |
259 | ||
260 | error: | |
261 | ret = -1; | |
262 | ||
263 | if (out_fp) { | |
264 | if (bt_close_memstream(buf, &size, out_fp)) { | |
265 | BT_COMP_LOGE_ERRNO("Cannot close memory stream", "."); | |
266 | } | |
267 | } | |
268 | ||
269 | if (*buf) { | |
270 | free(*buf); | |
271 | *buf = NULL; | |
272 | } | |
273 | ||
274 | end: | |
275 | return ret; | |
276 | } |