Line | Count | Source |
1 | | /* |
2 | | * native ebml reader for the Matroska demuxer |
3 | | * new parser copyright (c) 2010 Uoti Urpala |
4 | | * copyright (c) 2004 Aurelien Jacobs <aurel@gnuage.org> |
5 | | * based on the one written by Ronald Bultje for gstreamer |
6 | | * |
7 | | * This file is part of mpv. |
8 | | * |
9 | | * mpv is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public |
11 | | * License as published by the Free Software Foundation; either |
12 | | * version 2.1 of the License, or (at your option) any later version. |
13 | | * |
14 | | * mpv is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public |
20 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | #include <stdlib.h> |
24 | | #include <stdbool.h> |
25 | | #include <inttypes.h> |
26 | | #include <stddef.h> |
27 | | #include <assert.h> |
28 | | |
29 | | #include <libavutil/intfloat.h> |
30 | | #include <libavutil/common.h> |
31 | | #include "mpv_talloc.h" |
32 | | #include "ebml.h" |
33 | | #include "stream/stream.h" |
34 | | #include "common/common.h" |
35 | | #include "common/msg.h" |
36 | | |
37 | | // Whether the id is a known Matroska level 1 element (allowed as element on |
38 | | // global file level, after the level 0 MATROSKA_ID_SEGMENT). |
39 | | // This (intentionally) doesn't include "global" elements. |
40 | | bool ebml_is_mkv_level1_id(uint32_t id) |
41 | 0 | { |
42 | 0 | switch (id) { |
43 | 0 | case MATROSKA_ID_SEEKHEAD: |
44 | 0 | case MATROSKA_ID_INFO: |
45 | 0 | case MATROSKA_ID_CLUSTER: |
46 | 0 | case MATROSKA_ID_TRACKS: |
47 | 0 | case MATROSKA_ID_CUES: |
48 | 0 | case MATROSKA_ID_ATTACHMENTS: |
49 | 0 | case MATROSKA_ID_CHAPTERS: |
50 | 0 | case MATROSKA_ID_TAGS: |
51 | 0 | return true; |
52 | 0 | default: |
53 | 0 | return false; |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | | /* |
58 | | * Read: the element content data ID. |
59 | | * Return: the ID. |
60 | | */ |
61 | | uint32_t ebml_read_id(stream_t *s) |
62 | 0 | { |
63 | 0 | int i, len_mask = 0x80; |
64 | 0 | uint32_t id; |
65 | |
|
66 | 0 | for (i = 0, id = stream_read_char(s); i < 4 && !(id & len_mask); i++) |
67 | 0 | len_mask >>= 1; |
68 | 0 | if (i >= 4) |
69 | 0 | return EBML_ID_INVALID; |
70 | 0 | while (i--) |
71 | 0 | id = (id << 8) | stream_read_char(s); |
72 | 0 | return id; |
73 | 0 | } |
74 | | |
75 | | /* |
76 | | * Read: element content length. |
77 | | */ |
78 | | uint64_t ebml_read_length(stream_t *s) |
79 | 0 | { |
80 | 0 | int byte = stream_read_char(s); |
81 | 0 | if (byte == STREAM_EOF || byte < 1) |
82 | 0 | return EBML_UINT_INVALID; |
83 | | |
84 | 0 | uint8_t leading_zeros = 7 - mp_log2((uint8_t)byte); |
85 | 0 | uint64_t len = (uint8_t)byte & ((1 << (8 - leading_zeros - 1)) - 1); |
86 | 0 | for (uint8_t i = 0; i < leading_zeros; ++i) { |
87 | 0 | byte = stream_read_char(s); |
88 | 0 | if (byte == STREAM_EOF) |
89 | 0 | return EBML_UINT_INVALID; |
90 | 0 | len = (len << 8) | (uint8_t)byte; |
91 | 0 | } |
92 | | |
93 | 0 | return len; |
94 | 0 | } |
95 | | |
96 | | /* |
97 | | * Read a variable length signed int. |
98 | | */ |
99 | | int64_t ebml_read_signed_length(stream_t *s) |
100 | 0 | { |
101 | 0 | uint64_t unum; |
102 | 0 | int l; |
103 | | |
104 | | /* read as unsigned number first */ |
105 | 0 | uint64_t offset = stream_tell(s); |
106 | 0 | unum = ebml_read_length(s); |
107 | 0 | if (unum == EBML_UINT_INVALID) |
108 | 0 | return EBML_INT_INVALID; |
109 | 0 | l = stream_tell(s) - offset; |
110 | |
|
111 | 0 | return unum - ((1LL << ((7 * l) - 1)) - 1); |
112 | 0 | } |
113 | | |
114 | | /* |
115 | | * Read the next element as an unsigned int. |
116 | | */ |
117 | | bool ebml_read_uint(stream_t *s, uint64_t *out) |
118 | 0 | { |
119 | 0 | uint64_t len, value = 0; |
120 | |
|
121 | 0 | len = ebml_read_length(s); |
122 | 0 | if (len == EBML_UINT_INVALID || len > 8) |
123 | 0 | return false; |
124 | | |
125 | 0 | while (len--) |
126 | 0 | value = (value << 8) | stream_read_char(s); |
127 | |
|
128 | 0 | *out = value; |
129 | 0 | return true; |
130 | 0 | } |
131 | | |
132 | | /* |
133 | | * Read the next element as a signed int. |
134 | | */ |
135 | | bool ebml_read_int(stream_t *s, int64_t *out) |
136 | 0 | { |
137 | 0 | uint64_t value = 0; |
138 | 0 | uint64_t len; |
139 | 0 | int l; |
140 | |
|
141 | 0 | len = ebml_read_length(s); |
142 | 0 | if (len == EBML_UINT_INVALID || len > 8) |
143 | 0 | return false; |
144 | 0 | if (!len) { |
145 | 0 | *out = 0; |
146 | 0 | return true; |
147 | 0 | } |
148 | | |
149 | 0 | len--; |
150 | 0 | l = stream_read_char(s); |
151 | 0 | if (l & 0x80) |
152 | 0 | value = -1; |
153 | 0 | value = (value << 8) | l; |
154 | 0 | while (len--) |
155 | 0 | value = (value << 8) | stream_read_char(s); |
156 | |
|
157 | 0 | *out = (int64_t)value; // assume complement of 2 |
158 | 0 | return true; |
159 | 0 | } |
160 | | |
161 | | /* |
162 | | * Skip the current element. |
163 | | * end: the end of the parent element or -1 (for robust error handling) |
164 | | */ |
165 | | int ebml_read_skip(struct mp_log *log, int64_t end, stream_t *s) |
166 | 0 | { |
167 | 0 | uint64_t len; |
168 | |
|
169 | 0 | int64_t pos = stream_tell(s); |
170 | |
|
171 | 0 | len = ebml_read_length(s); |
172 | 0 | if (len == EBML_UINT_INVALID) |
173 | 0 | goto invalid; |
174 | | |
175 | 0 | int64_t pos2 = stream_tell(s); |
176 | 0 | if (len >= INT64_MAX - pos2 || (end > 0 && pos2 + len > end)) |
177 | 0 | goto invalid; |
178 | | |
179 | 0 | if (!stream_seek_skip(s, pos2 + len)) |
180 | 0 | goto invalid; |
181 | | |
182 | 0 | return 0; |
183 | | |
184 | 0 | invalid: |
185 | 0 | mp_err(log, "Invalid EBML length at position %"PRId64"\n", pos); |
186 | 0 | stream_seek_skip(s, pos); |
187 | 0 | return 1; |
188 | 0 | } |
189 | | |
190 | | /* |
191 | | * Skip to (probable) next cluster (MATROSKA_ID_CLUSTER) element start position. |
192 | | */ |
193 | | int ebml_resync_cluster(struct mp_log *log, stream_t *s) |
194 | 0 | { |
195 | 0 | int64_t pos = stream_tell(s); |
196 | 0 | uint32_t last_4_bytes = 0; |
197 | 0 | stream_read_peek(s, &(char){0}, 1); |
198 | 0 | if (!s->eof) { |
199 | 0 | mp_err(log, "Corrupt file detected. " |
200 | 0 | "Trying to resync starting from position %"PRId64"...\n", pos); |
201 | 0 | } |
202 | 0 | while (!s->eof) { |
203 | | // Assumes MATROSKA_ID_CLUSTER is 4 bytes, with no 0 bytes. |
204 | 0 | if (last_4_bytes == MATROSKA_ID_CLUSTER) { |
205 | 0 | mp_err(log, "Cluster found at %"PRId64".\n", pos - 4); |
206 | 0 | stream_seek(s, pos - 4); |
207 | 0 | return 0; |
208 | 0 | } |
209 | 0 | last_4_bytes = (last_4_bytes << 8) | stream_read_char(s); |
210 | 0 | pos++; |
211 | 0 | } |
212 | 0 | return -1; |
213 | 0 | } |
214 | | |
215 | | |
216 | | |
217 | | static_assert(sizeof(((struct ebml_elem_desc *)0)->defval) == sizeof(((struct ebml_elem_desc *)0)->defval.u), |
218 | | "defval.u must cover the entire union for {0} initialization"); |
219 | | |
220 | | #define EVALARGS(F, ...) F(__VA_ARGS__) |
221 | | #define E(str, N, type, length, context, ...) const struct ebml_elem_desc ebml_ ## N ## _desc = { str, type, length, .context_default = context, .defval = __VA_ARGS__ }; |
222 | | #define E_SN(str, count, N) const struct ebml_elem_desc ebml_ ## N ## _desc = { str, EBML_TYPE_SUBELEMENTS, sizeof(struct ebml_ ## N), count, (const struct ebml_field_desc[]){ |
223 | | #define E_S(str, count) EVALARGS(E_SN, str, count, N) |
224 | | #define FN(id, name, multiple, N) { id, multiple, offsetof(struct ebml_ ## N, name), offsetof(struct ebml_ ## N, n_ ## name), &ebml_##name##_desc}, |
225 | | #define F(id, name, multiple) EVALARGS(FN, id, name, multiple, N) |
226 | | #include "ebml_defs.inc" |
227 | | #undef EVALARGS |
228 | | #undef SN |
229 | | #undef S |
230 | | #undef FN |
231 | | #undef F |
232 | | |
233 | | // Used to read/write pointers to different struct types |
234 | | struct generic; |
235 | | #define generic_struct struct generic |
236 | | |
237 | | static uint32_t ebml_parse_id(uint8_t *data, size_t data_len, int *length) |
238 | 0 | { |
239 | 0 | *length = -1; |
240 | 0 | uint8_t *end = data + data_len; |
241 | 0 | if (data == end) |
242 | 0 | return EBML_ID_INVALID; |
243 | 0 | int len = 1; |
244 | 0 | uint32_t id = *data++; |
245 | 0 | for (int len_mask = 0x80; !(id & len_mask); len_mask >>= 1) { |
246 | 0 | len++; |
247 | 0 | if (len > 4) |
248 | 0 | return EBML_ID_INVALID; |
249 | 0 | } |
250 | 0 | *length = len; |
251 | 0 | while (--len && data < end) |
252 | 0 | id = (id << 8) | *data++; |
253 | 0 | return id; |
254 | 0 | } |
255 | | |
256 | | static uint64_t ebml_parse_length(uint8_t *data, size_t data_len, int *length) |
257 | 0 | { |
258 | 0 | *length = -1; |
259 | 0 | uint8_t *end = data + data_len; |
260 | 0 | if (data == end) |
261 | 0 | return -1; |
262 | 0 | uint64_t r = *data++; |
263 | 0 | int len = 1; |
264 | 0 | int len_mask; |
265 | 0 | for (len_mask = 0x80; !(r & len_mask); len_mask >>= 1) { |
266 | 0 | len++; |
267 | 0 | if (len > 8) |
268 | 0 | return -1; |
269 | 0 | } |
270 | 0 | r &= len_mask - 1; |
271 | |
|
272 | 0 | int num_allones = 0; |
273 | 0 | if (r == len_mask - 1) |
274 | 0 | num_allones++; |
275 | 0 | for (int i = 1; i < len; i++) { |
276 | 0 | if (data == end) |
277 | 0 | return -1; |
278 | 0 | if (*data == 255) |
279 | 0 | num_allones++; |
280 | 0 | r = (r << 8) | *data++; |
281 | 0 | } |
282 | | // According to Matroska specs this means "unknown length" |
283 | | // Could be supported if there are any actual files using it |
284 | 0 | if (num_allones == len) |
285 | 0 | return -1; |
286 | 0 | *length = len; |
287 | 0 | return r; |
288 | 0 | } |
289 | | |
290 | | static uint64_t ebml_parse_uint(uint8_t *data, int length) |
291 | 0 | { |
292 | 0 | mp_assert(length >= 0 && length <= 8); |
293 | 0 | uint64_t r = 0; |
294 | 0 | while (length--) |
295 | 0 | r = (r << 8) + *data++; |
296 | 0 | return r; |
297 | 0 | } |
298 | | |
299 | | static int64_t ebml_parse_sint(uint8_t *data, int length) |
300 | 0 | { |
301 | 0 | mp_assert(length >= 0 && length <= 8); |
302 | 0 | if (!length) |
303 | 0 | return 0; |
304 | 0 | uint64_t r = 0; |
305 | 0 | if (*data & 0x80) |
306 | 0 | r = -1; |
307 | 0 | while (length--) |
308 | 0 | r = (r << 8) | *data++; |
309 | 0 | return (int64_t)r; // assume complement of 2 |
310 | 0 | } |
311 | | |
312 | | static double ebml_parse_float(uint8_t *data, int length) |
313 | 0 | { |
314 | 0 | mp_assert(length == 0 || length == 4 || length == 8); |
315 | 0 | uint64_t i = ebml_parse_uint(data, length); |
316 | 0 | if (length == 4) |
317 | 0 | return av_int2float(i); |
318 | 0 | else |
319 | 0 | return av_int2double(i); |
320 | 0 | } |
321 | | |
322 | | |
323 | | // target must be initialized to zero |
324 | | static void ebml_parse_element(struct ebml_parse_ctx *ctx, void *target, |
325 | | uint8_t *data, int size, |
326 | | const struct ebml_elem_desc *type, int level) |
327 | 0 | { |
328 | 0 | mp_assert(type->type == EBML_TYPE_SUBELEMENTS); |
329 | 0 | mp_assert(level < 8); |
330 | 0 | MP_TRACE(ctx, "%.*sParsing element %s\n", level, " ", type->name); |
331 | |
|
332 | 0 | char *s = target; |
333 | 0 | uint8_t *end = data + size; |
334 | 0 | uint8_t *p = data; |
335 | 0 | int num_elems[MAX_EBML_SUBELEMENTS] = {0}; |
336 | 0 | while (p < end) { |
337 | 0 | uint8_t *startp = p; |
338 | 0 | int len; |
339 | 0 | uint32_t id = ebml_parse_id(p, end - p, &len); |
340 | 0 | if (len > end - p) |
341 | 0 | goto past_end_error; |
342 | 0 | if (len < 0) { |
343 | 0 | MP_ERR(ctx, "Error parsing subelement id\n"); |
344 | 0 | goto other_error; |
345 | 0 | } |
346 | 0 | p += len; |
347 | 0 | uint64_t length = ebml_parse_length(p, end - p, &len); |
348 | 0 | if (len > end - p) |
349 | 0 | goto past_end_error; |
350 | 0 | if (len < 0) { |
351 | 0 | MP_ERR(ctx, "Error parsing subelement length\n"); |
352 | 0 | goto other_error; |
353 | 0 | } |
354 | 0 | p += len; |
355 | |
|
356 | 0 | int field_idx = -1; |
357 | 0 | for (int i = 0; i < type->field_count; i++) |
358 | 0 | if (type->fields[i].id == id) { |
359 | 0 | field_idx = i; |
360 | 0 | num_elems[i]++; |
361 | 0 | if (num_elems[i] >= 0x70000000) { |
362 | 0 | MP_ERR(ctx, "Too many EBML subelements.\n"); |
363 | 0 | goto other_error; |
364 | 0 | } |
365 | 0 | break; |
366 | 0 | } |
367 | | |
368 | 0 | if (length > end - p) { |
369 | 0 | if (field_idx >= 0 && type->fields[field_idx].desc->type |
370 | 0 | != EBML_TYPE_SUBELEMENTS) { |
371 | 0 | MP_ERR(ctx, "Subelement content goes " |
372 | 0 | "past end of containing element\n"); |
373 | 0 | goto other_error; |
374 | 0 | } |
375 | | // Try to parse what is possible from inside this partial element |
376 | 0 | ctx->has_errors = true; |
377 | 0 | length = end - p; |
378 | 0 | } |
379 | 0 | p += length; |
380 | |
|
381 | 0 | continue; |
382 | | |
383 | 0 | past_end_error: |
384 | 0 | MP_ERR(ctx, "Subelement headers go past end of containing element\n"); |
385 | 0 | other_error: |
386 | 0 | ctx->has_errors = true; |
387 | 0 | end = startp; |
388 | 0 | break; |
389 | 0 | } |
390 | | |
391 | 0 | for (int i = 0; i < type->field_count; i++) { |
392 | 0 | if (num_elems[i] && type->fields[i].multiple) { |
393 | 0 | char *ptr = s + type->fields[i].offset; |
394 | 0 | switch (type->fields[i].desc->type) { |
395 | 0 | case EBML_TYPE_SUBELEMENTS: { |
396 | 0 | size_t max = 1000000000 / type->fields[i].desc->size; |
397 | 0 | if (num_elems[i] > max) { |
398 | 0 | MP_ERR(ctx, "Too many subelements.\n"); |
399 | 0 | num_elems[i] = max; |
400 | 0 | } |
401 | 0 | int sz = num_elems[i] * type->fields[i].desc->size; |
402 | 0 | *(generic_struct **) ptr = talloc_zero_size(ctx->talloc_ctx, sz); |
403 | 0 | break; |
404 | 0 | } |
405 | 0 | case EBML_TYPE_UINT: |
406 | 0 | *(uint64_t **) ptr = talloc_zero_array(ctx->talloc_ctx, |
407 | 0 | uint64_t, num_elems[i]); |
408 | 0 | break; |
409 | 0 | case EBML_TYPE_SINT: |
410 | 0 | *(int64_t **) ptr = talloc_zero_array(ctx->talloc_ctx, |
411 | 0 | int64_t, num_elems[i]); |
412 | 0 | break; |
413 | 0 | case EBML_TYPE_FLOAT: |
414 | 0 | *(double **) ptr = talloc_zero_array(ctx->talloc_ctx, |
415 | 0 | double, num_elems[i]); |
416 | 0 | break; |
417 | 0 | case EBML_TYPE_STR: |
418 | 0 | *(char ***) ptr = talloc_zero_array(ctx->talloc_ctx, |
419 | 0 | char *, num_elems[i]); |
420 | 0 | break; |
421 | 0 | case EBML_TYPE_BINARY: |
422 | 0 | *(struct bstr **) ptr = talloc_zero_array(ctx->talloc_ctx, |
423 | 0 | struct bstr, |
424 | 0 | num_elems[i]); |
425 | 0 | break; |
426 | 0 | case EBML_TYPE_EBML_ID: |
427 | 0 | *(int32_t **) ptr = talloc_zero_array(ctx->talloc_ctx, |
428 | 0 | uint32_t, num_elems[i]); |
429 | 0 | break; |
430 | 0 | default: |
431 | 0 | MP_ASSERT_UNREACHABLE(); |
432 | 0 | } |
433 | 0 | } |
434 | 0 | } |
435 | | |
436 | 0 | while (data < end) { |
437 | 0 | int len; |
438 | 0 | uint32_t id = ebml_parse_id(data, end - data, &len); |
439 | 0 | if (len < 0 || len > end - data) { |
440 | 0 | MP_ERR(ctx, "Error parsing subelement\n"); |
441 | 0 | break; |
442 | 0 | } |
443 | 0 | data += len; |
444 | 0 | uint64_t length = ebml_parse_length(data, end - data, &len); |
445 | 0 | if (len < 0 || len > end - data) { |
446 | 0 | MP_ERR(ctx, "Error parsing subelement length\n"); |
447 | 0 | break; |
448 | 0 | } |
449 | 0 | data += len; |
450 | 0 | if (length > end - data) { |
451 | | // Try to parse what is possible from inside this partial element |
452 | 0 | length = end - data; |
453 | 0 | MP_ERR(ctx, "Next subelement content goes " |
454 | 0 | "past end of containing element, will be truncated\n"); |
455 | 0 | } |
456 | 0 | int field_idx = -1; |
457 | 0 | for (int i = 0; i < type->field_count; i++) |
458 | 0 | if (type->fields[i].id == id) { |
459 | 0 | field_idx = i; |
460 | 0 | break; |
461 | 0 | } |
462 | 0 | if (field_idx < 0) { |
463 | 0 | if (id == 0xec) { |
464 | 0 | MP_TRACE(ctx, "%.*sIgnoring Void element " |
465 | 0 | "size: %"PRIu64"\n", level+1, " ", length); |
466 | 0 | } else if (id == 0xbf) { |
467 | 0 | MP_TRACE(ctx, "%.*sIgnoring CRC-32 " |
468 | 0 | "element size: %"PRIu64"\n", level+1, " ", |
469 | 0 | length); |
470 | 0 | } else { |
471 | 0 | MP_DBG(ctx, "Ignoring unrecognized " |
472 | 0 | "subelement. ID: %x size: %"PRIu64"\n", id, length); |
473 | 0 | } |
474 | 0 | data += length; |
475 | 0 | continue; |
476 | 0 | } |
477 | 0 | const struct ebml_field_desc *fd = &type->fields[field_idx]; |
478 | 0 | const struct ebml_elem_desc *ed = fd->desc; |
479 | 0 | bool multiple = fd->multiple; |
480 | 0 | int *countptr = (int *) (s + fd->count_offset); |
481 | 0 | if (*countptr >= num_elems[field_idx]) { |
482 | | // Shouldn't happen on any sane file without bugs |
483 | 0 | MP_ERR(ctx, "Too many subelements.\n"); |
484 | 0 | ctx->has_errors = true; |
485 | 0 | data += length; |
486 | 0 | continue; |
487 | 0 | } |
488 | 0 | if (*countptr > 0 && !multiple) { |
489 | 0 | MP_WARN(ctx, "Another subelement of type " |
490 | 0 | "%x %s (size: %"PRIu64"). Only one allowed. Ignoring.\n", |
491 | 0 | id, ed->name, length); |
492 | 0 | ctx->has_errors = true; |
493 | 0 | data += length; |
494 | 0 | continue; |
495 | 0 | } |
496 | 0 | MP_TRACE(ctx, "%.*sParsing %x %s size: %"PRIu64 |
497 | 0 | " value: ", level+1, " ", id, ed->name, length); |
498 | |
|
499 | 0 | char *fieldptr = s + fd->offset; |
500 | 0 | switch (ed->type) { |
501 | 0 | case EBML_TYPE_SUBELEMENTS: |
502 | 0 | MP_TRACE(ctx, "subelements\n"); |
503 | 0 | char *subelptr; |
504 | 0 | if (multiple) { |
505 | 0 | char *array_start = (char *) *(generic_struct **) fieldptr; |
506 | 0 | subelptr = array_start + *countptr * ed->size; |
507 | 0 | } else |
508 | 0 | subelptr = fieldptr; |
509 | 0 | ebml_parse_element(ctx, subelptr, data, length, ed, level + 1); |
510 | 0 | break; |
511 | | |
512 | 0 | case EBML_TYPE_UINT:; |
513 | 0 | uint64_t *uintptr; |
514 | 0 | #define GETPTR(subelptr, fieldtype) \ |
515 | 0 | if (multiple) \ |
516 | 0 | subelptr = *(fieldtype **) fieldptr + *countptr; \ |
517 | 0 | else \ |
518 | 0 | subelptr = (fieldtype *) fieldptr |
519 | 0 | GETPTR(uintptr, uint64_t); |
520 | 0 | if (length > 8) { |
521 | 0 | MP_ERR(ctx, "uint invalid length %"PRIu64"\n", length); |
522 | 0 | goto error; |
523 | 0 | } |
524 | 0 | if (!length && ed->context_default) |
525 | 0 | goto skip; |
526 | 0 | *uintptr = length ? ebml_parse_uint(data, length) : ed->defval.u; |
527 | 0 | MP_TRACE(ctx, "uint %"PRIu64"\n", *uintptr); |
528 | 0 | break; |
529 | | |
530 | 0 | case EBML_TYPE_SINT:; |
531 | 0 | int64_t *sintptr; |
532 | 0 | GETPTR(sintptr, int64_t); |
533 | 0 | if (length > 8) { |
534 | 0 | MP_ERR(ctx, "sint invalid length %"PRIu64"\n", length); |
535 | 0 | goto error; |
536 | 0 | } |
537 | 0 | if (!length && ed->context_default) |
538 | 0 | goto skip; |
539 | 0 | *sintptr = length ? ebml_parse_sint(data, length) : ed->defval.i; |
540 | 0 | MP_TRACE(ctx, "sint %"PRId64"\n", *sintptr); |
541 | 0 | break; |
542 | | |
543 | 0 | case EBML_TYPE_FLOAT:; |
544 | 0 | double *floatptr; |
545 | 0 | GETPTR(floatptr, double); |
546 | 0 | if (length != 0 && length != 4 && length != 8) { |
547 | 0 | MP_ERR(ctx, "float invalid length %"PRIu64"\n", length); |
548 | 0 | goto error; |
549 | 0 | } |
550 | 0 | if (!length && ed->context_default) |
551 | 0 | goto skip; |
552 | 0 | *floatptr = length ? ebml_parse_float(data, length) : ed->defval.f; |
553 | 0 | MP_DBG(ctx, "float %f\n", *floatptr); |
554 | 0 | break; |
555 | | |
556 | 0 | case EBML_TYPE_STR: |
557 | 0 | if (length > 1024 * 1024) { |
558 | 0 | MP_ERR(ctx, "Not reading overly long string element.\n"); |
559 | 0 | goto error; |
560 | 0 | } |
561 | 0 | char **strptr; |
562 | 0 | GETPTR(strptr, char *); |
563 | 0 | if (!length && ed->context_default) |
564 | 0 | goto skip; |
565 | 0 | if (!length && ed->defval.s) { |
566 | 0 | *strptr = talloc_strdup(ctx->talloc_ctx, ed->defval.s); |
567 | 0 | } else { |
568 | 0 | *strptr = talloc_strndup(ctx->talloc_ctx, data, length); |
569 | 0 | } |
570 | 0 | MP_TRACE(ctx, "string \"%s\"\n", *strptr); |
571 | 0 | break; |
572 | | |
573 | 0 | case EBML_TYPE_BINARY:; |
574 | 0 | if (length > 0x80000000) { |
575 | 0 | MP_ERR(ctx, "Not reading overly long EBML element.\n"); |
576 | 0 | goto error; |
577 | 0 | } |
578 | 0 | if (ed->size && length != ed->size) { |
579 | 0 | MP_WARN(ctx, "%s invalid length %"PRIu64", ignoring\n", |
580 | 0 | ed->name, length); |
581 | 0 | goto error; |
582 | 0 | } |
583 | 0 | struct bstr *binptr; |
584 | 0 | GETPTR(binptr, struct bstr); |
585 | 0 | binptr->start = data; |
586 | 0 | binptr->len = length; |
587 | 0 | MP_TRACE(ctx, "binary %zd bytes\n", binptr->len); |
588 | 0 | break; |
589 | | |
590 | 0 | case EBML_TYPE_EBML_ID:; |
591 | 0 | uint32_t *idptr; |
592 | 0 | GETPTR(idptr, uint32_t); |
593 | 0 | *idptr = ebml_parse_id(data, end - data, &len); |
594 | 0 | if (len != length) { |
595 | 0 | MP_ERR(ctx, "ebml_id broken value\n"); |
596 | 0 | goto error; |
597 | 0 | } |
598 | 0 | MP_TRACE(ctx, "ebml_id %x\n", (unsigned)*idptr); |
599 | 0 | break; |
600 | 0 | default: |
601 | 0 | MP_ASSERT_UNREACHABLE(); |
602 | 0 | } |
603 | 0 | *countptr += 1; |
604 | 0 | error: |
605 | 0 | skip: |
606 | 0 | data += length; |
607 | 0 | } |
608 | 0 | } |
609 | | |
610 | | // target must be initialized to zero |
611 | | int ebml_read_element(struct stream *s, struct ebml_parse_ctx *ctx, |
612 | | void *target, const struct ebml_elem_desc *desc) |
613 | 0 | { |
614 | 0 | ctx->has_errors = false; |
615 | 0 | int msglevel = ctx->no_error_messages ? MSGL_DEBUG : MSGL_WARN; |
616 | 0 | uint64_t length = ebml_read_length(s); |
617 | 0 | if (s->eof) { |
618 | 0 | MP_MSG(ctx, msglevel, "Unexpected end of file " |
619 | 0 | "- partial or corrupt file?\n"); |
620 | 0 | return -1; |
621 | 0 | } |
622 | 0 | if (length == EBML_UINT_INVALID) { |
623 | 0 | MP_MSG(ctx, msglevel, "EBML element with unknown length - unsupported\n"); |
624 | 0 | return -1; |
625 | 0 | } |
626 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
627 | | if (length > (512 << 20)) { |
628 | | #else |
629 | 0 | if (length > (64 << 20)) { |
630 | 0 | #endif |
631 | 0 | MP_MSG(ctx, msglevel, "Element too big (%" PRIu64 " MiB) - skipping\n", length >> 20); |
632 | 0 | return -1; |
633 | 0 | } |
634 | 0 | ctx->talloc_ctx = talloc_size(NULL, length); |
635 | 0 | int read_len = stream_read(s, ctx->talloc_ctx, length); |
636 | 0 | if (read_len < length) |
637 | 0 | MP_MSG(ctx, msglevel, "Unexpected end of file - partial or corrupt file?\n"); |
638 | 0 | ebml_parse_element(ctx, target, ctx->talloc_ctx, read_len, desc, 0); |
639 | 0 | if (ctx->has_errors) |
640 | 0 | MP_MSG(ctx, msglevel, "Error parsing element %s\n", desc->name); |
641 | 0 | return 0; |
642 | 0 | } |