summaryrefslogtreecommitdiff
path: root/src/demuxers/ebml.c
blob: b6f0658fcc9023ae8811041663a49f2d6179da5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * Copyright (C) 2000-2003 the xine project
 *
 * This file is part of xine, a free video player.
 *
 * xine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * xine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 * EBML parser
 * a lot of ideas from the gstreamer parser
 *
 * $Id: ebml.c,v 1.1 2004/01/05 00:40:54 tmattern Exp $
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#define LOG_MODULE "ebml"
#define LOG_VERBOSE
/*
#define LOG
*/
#include "xine_internal.h"
#include "xineutils.h"
#include "bswap.h"

#include "ebml.h"


ebml_parser_t *new_ebml_parser (xine_t *xine, input_plugin_t *input) {
  ebml_parser_t *ebml;
  
  ebml = malloc(sizeof(ebml_parser_t));
  ebml->xine                 = xine;
  ebml->input                = input;

  ebml->version              = 0;
  ebml->read_version         = 0;
  ebml->max_id_len           = 0;
  ebml->max_size_len         = 0;
  ebml->doctype              = NULL;
  ebml->doctype_version      = 0;
  ebml->doctype_read_version = 0;
  
  ebml->level                = 0;
  
  return ebml;
}


void dispose_ebml_parser(ebml_parser_t *ebml) {
  free(ebml);  
}


uint32_t ebml_get_next_level(ebml_parser_t *ebml, ebml_elem_t *elem) {
  ebml_elem_t *parent_elem;

  if (ebml->level > 0) {
    parent_elem = &ebml->elem_stack[ebml->level - 1];
    while ((elem->start + elem->len) >= (parent_elem->start + parent_elem->len)) {
      lprintf("parent: %lld, %lld; elem: %lld, %lld\n",
              parent_elem->start, parent_elem->len, elem->start, elem->len);
      ebml->level--;
      if (ebml->level == 0) break;
      parent_elem = &ebml->elem_stack[ebml->level - 1];
    }
  }
  lprintf("id: 0x%x, len: %lld, next_level: %d\n", elem->id, elem->len, ebml->level);
  return ebml->level;
}


static int ebml_read_elem_id(ebml_parser_t *ebml, uint32_t *id) {
  uint8_t   data[4];
  uint32_t  mask = 0x80;
  uint32_t  value;
  int       size = 1;
  int       i;

  if (ebml->input->read(ebml->input, data, 1) != 1) {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: read error\n");
    return 0;
  }
  value = data[0];

  /* compute the size of the ID (1-4 bytes)*/
  while (size <= 4 && !(value & mask)) {
    size++;
    mask >>= 1;
  }
  if (size > 4) {
    off_t pos = ebml->input->get_current_pos(ebml->input);
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: invalid EBML ID size (0x%x) at position %llu\n", data[0], pos);
    return 0;
  }

  /* read the rest of the id */
  if (ebml->input->read(ebml->input, data + 1, size - 1) != (size - 1)) {
    off_t pos = ebml->input->get_current_pos(ebml->input);
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: read error at position %llu\n", pos);
    return 0;
  }
  for(i = 1; i < size; i++) {
    value = (value << 8) | data[i];
  }
  *id = value;

  return 1;
}


static int ebml_read_elem_len(ebml_parser_t *ebml, uint64_t *len) {
  uint8_t data[8];
  uint32_t mask = 0x80;
  int size = 1;
  int ff_bytes;
  uint64_t value;
  int i;
                                                                                
  if (ebml->input->read(ebml->input, data, 1) != 1) {
    off_t pos = ebml->input->get_current_pos(ebml->input);
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: read error at position %llu\n", pos);
    return 0;
  }
  value = data[0];

  /* compute the size of the "data len" (1-8 bytes) */
  while (size <= 8 && !(value & mask)) {
    size++;
    mask >>= 1;
  }
  if (size > 8) {
    off_t pos = ebml->input->get_current_pos(ebml->input);
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: Invalid EBML length size (0x%x) at position %llu\n", data[0], pos);
    return 0;
  }

  /* remove size bits */
  value &= mask - 1;

  /* check if the first byte is full */
  if (value == (mask - 1))
    ff_bytes = 1;
  else
    ff_bytes = 0;
  
  /* read the rest of the len */
  if (ebml->input->read(ebml->input, data + 1, size - 1) != (size - 1)) {
    off_t pos = ebml->input->get_current_pos(ebml->input);
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: read error at position %llu\n", pos);
    return 0;
  }
  for (i = 1; i < size; i++) {
    if (data[i] == 0xff)
      ff_bytes++;
    value = (value << 8) | data[i];
  }

  if (ff_bytes == size)
    *len = -1;
  else
    *len = value;

  return 1;
}


static int ebml_read_elem_data(ebml_parser_t *ebml, int8_t *buf, int64_t len) {

  if (ebml->input->read(ebml->input, buf, len) != len) {
    off_t pos = ebml->input->get_current_pos(ebml->input);
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: read error at position %llu\n", pos);
    return 0;
  }

  return 1;
}


int ebml_skip(ebml_parser_t *ebml, ebml_elem_t *elem) {
  if (ebml->input->seek(ebml->input, elem->len, SEEK_CUR) < 0) {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: seek error\n");
    return 0;
  }

  return 1;
}


int ebml_read_elem_head(ebml_parser_t *ebml, ebml_elem_t *elem) {

  if (!ebml_read_elem_id(ebml, &elem->id))
    return 0;

  if (!ebml_read_elem_len(ebml, &elem->len))
    return 0;

  elem->start = ebml->input->get_current_pos(ebml->input);
  return 1;
}


int ebml_read_uint(ebml_parser_t *ebml, ebml_elem_t *elem, uint64_t *num) {
  uint8_t  data[8];
  uint64_t size = elem->len;

  if ((elem->len < 1) || (elem->len > 8)) {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: Invalid integer element size %lld\n", size);
    return 0;
  }
  
  if (!ebml_read_elem_data (ebml, data, size))
    return 0;

  *num = 0;
  while (size > 0) {
    *num = (*num << 8) | data[elem->len - size];
    size--;
  }

  return 1;
}

int ebml_read_sint (ebml_parser_t *ebml, ebml_elem_t  *elem, int64_t *num) {
  uint8_t  data[8];
  uint64_t size = elem->len;

  if ((elem->len < 1) || (elem->len > 8)) {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: Invalid integer element size %lld\n", size);
    return 0;
  }
  
  if (!ebml_read_elem_data(ebml, data, size))
    return 0;

  /* propagate negative bit */
  if (data[0] & 80)
    *num = -1;
  else
    *num = 0;
  
  while (size > 0) {
    *num = (*num << 8) | data[elem->len - size];
    size--;
  }

  return 1;
}


int ebml_read_float (ebml_parser_t *ebml, ebml_elem_t *elem, double *num) {
  uint8_t  data[10];
  uint64_t size = elem->len;

  if ((size != 4) && (size != 8) && (size != 10)) {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: Invalid float element size %lld\n", size);
    return 0;
  }

  if (!ebml_read_elem_data(ebml, data, size))
    return 0;

  if (size == 10) {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: FIXME! 10-byte floats unimplemented\n");
    return 0;
  }

  if (size == 4) {
    float f;

    *((uint32_t *) &f) = BE_32(data);
    *num = f;
  } else {
    double d;

    *((uint64_t *) &d) = BE_64(data);
    *num = d;
  }
  return 1;
}

int ebml_read_ascii(ebml_parser_t *ebml, ebml_elem_t *elem, char *str) {
  uint64_t size = elem->len;

  if (!ebml_read_elem_data(ebml, str, size))
    return 0;
  
  return 1;
}

int ebml_read_utf8 (ebml_parser_t *ebml, ebml_elem_t *elem, char *str) {
  return ebml_read_ascii (ebml, elem, str);
}

int ebml_read_date (ebml_parser_t *ebml, ebml_elem_t *elem, int64_t *date) {
  return ebml_read_sint (ebml, elem, date);
}

int ebml_read_master (ebml_parser_t *ebml, ebml_elem_t *elem) {
  ebml_elem_t *top_elem;

  if (ebml->level >= 0) {
    top_elem = &ebml->elem_stack[ebml->level];
    top_elem->start = elem->start;
    top_elem->len = elem->len;
    top_elem->id = elem->id;
  
    ebml->level++;
    lprintf("id: 0x%x, len: %lld, level: %d\n", elem->id, elem->len, ebml->level);
    if (ebml->level >= EBML_STACK_SIZE) {
      xprintf(ebml->xine, XINE_VERBOSITY_LOG,
              "ebml: max level exceeded\n");
      return 0;
    }
    return 1;
  } else {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: invalid current level\n");
    return 0;
  }
}

int ebml_read_binary(ebml_parser_t *ebml, ebml_elem_t *elem, uint8_t *binary) {
  if (!ebml_read_elem_data(ebml, binary, elem->len))
    return 0;

  return 1;
}

int ebml_check_header(ebml_parser_t *ebml) {
  uint32_t next_level;
  ebml_elem_t master;

  if (!ebml_read_elem_head(ebml, &master)) {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: invalid master element\n");
    return 0;
  }
  
  if (master.id != EBML_ID_EBML) {
    xprintf(ebml->xine, XINE_VERBOSITY_LOG,
            "ebml: invalid master element\n");
    return 0;
  }
  
  if (!ebml_read_master (ebml, &master))
    return 0;

  next_level = 1;
  while (next_level == 1) {
    ebml_elem_t elem;
    
    if (!ebml_read_elem_head(ebml, &elem))
      return 0;

    switch (elem.id) {
      case EBML_ID_EBMLVERSION: {
        uint64_t num;

        if (!ebml_read_uint (ebml, &elem, &num))
          return 0;
        lprintf("ebml_version: %lld\n", num);
        ebml->version = num;
        break;
      }
      
      case EBML_ID_EBMLREADVERSION: {
        uint64_t num;

        if (!ebml_read_uint (ebml, &elem, &num))
          return 0;
        lprintf("ebml_read_version: %lld\n", num);
        if (num != EBML_VERSION)
          return 0;
        ebml->read_version = num;
        break;
      }

      case EBML_ID_EBMLMAXIDLENGTH: {
        uint64_t num;

        if (!ebml_read_uint (ebml, &elem, &num))
          return 0;
        lprintf("ebml_max_id_length: %lld\n", num);
        ebml->max_id_len = num;
        break;
      }

      case EBML_ID_EBMLMAXSIZELENGTH: {
        uint64_t num;

        if (!ebml_read_uint (ebml, &elem, &num))
          return 0;
        lprintf("ebml_max_size_length: %lld\n", num);
        ebml->max_size_len = num;
        break;
      }

      case EBML_ID_DOCTYPE: {
        char *text = malloc(elem.len + 1);

        text[elem.len] = '\0';
        if (!ebml_read_ascii (ebml, &elem, text))
          return 0;

        lprintf("doctype: %s\n", text);
        if (ebml->doctype)
          free (ebml->doctype);
        ebml->doctype = text;
        break;
      }

      case EBML_ID_DOCTYPEVERSION: {
        uint64_t num;

        if (!ebml_read_uint (ebml, &elem, &num))
          return 0;
        lprintf("doctype_version: %lld\n", num);
        ebml->doctype_version = num;
        break;
      }

      case EBML_ID_DOCTYPEREADVERSION: {
        uint64_t num;

        if (!ebml_read_uint (ebml, &elem, &num))
          return 0;
        lprintf("doctype_read_version: %lld\n", num);
        ebml->doctype_read_version = num;
        break;
      }

      default:
        xprintf(ebml->xine, XINE_VERBOSITY_LOG,
                "ebml: Unknown data type 0x%x in EBML header (ignored)\n", elem.id);
    }
    next_level = ebml_get_next_level(ebml, &elem);
  }

  return 1;
}