2 * native ebml reader for the Matroska demuxer
3 * Written by Aurelien Jacobs <aurel@gnuage.org>
4 * Based on the one written by Ronald Bultje for gstreamer
18 #define SIZE_MAX ((size_t)-1)
22 * Read: the element content data ID.
26 ebml_read_id (stream_t
*s
, int *length
)
28 int i
, len_mask
= 0x80;
31 for (i
=0, id
=stream_read_char (s
); i
<4 && !(id
& len_mask
); i
++)
34 return EBML_ID_INVALID
;
38 id
= (id
<< 8) | stream_read_char (s
);
43 * Read a variable length unsigned int.
46 ebml_read_vlen_uint (uint8_t *buffer
, int *length
)
48 int i
, j
, num_ffs
= 0, len_mask
= 0x80;
51 for (i
=0, num
=*buffer
++; i
<8 && !(num
& len_mask
); i
++)
54 return EBML_UINT_INVALID
;
58 if ((int)(num
&= (len_mask
- 1)) == len_mask
- 1)
62 num
= (num
<< 8) | *buffer
++;
63 if ((num
& 0xFF) == 0xFF)
67 return EBML_UINT_INVALID
;
72 * Read a variable length signed int.
75 ebml_read_vlen_int (uint8_t *buffer
, int *length
)
80 /* read as unsigned number first */
81 unum
= ebml_read_vlen_uint (buffer
, &l
);
82 if (unum
== EBML_UINT_INVALID
)
83 return EBML_INT_INVALID
;
87 return unum
- ((1 << ((7 * l
) - 1)) - 1);
91 * Read: element content length.
94 ebml_read_length (stream_t
*s
, int *length
)
96 int i
, j
, num_ffs
= 0, len_mask
= 0x80;
99 for (i
=0, len
=stream_read_char (s
); i
<8 && !(len
& len_mask
); i
++)
102 return EBML_UINT_INVALID
;
106 if ((int)(len
&= (len_mask
- 1)) == len_mask
- 1)
110 len
= (len
<< 8) | stream_read_char (s
);
111 if ((len
& 0xFF) == 0xFF)
115 return EBML_UINT_INVALID
;
120 * Read the next element as an unsigned int.
123 ebml_read_uint (stream_t
*s
, uint64_t *length
)
125 uint64_t len
, value
= 0;
128 len
= ebml_read_length (s
, &l
);
129 if (len
== EBML_UINT_INVALID
|| len
< 1 || len
> 8)
130 return EBML_UINT_INVALID
;
135 value
= (value
<< 8) | stream_read_char (s
);
141 * Read the next element as a signed int.
144 ebml_read_int (stream_t
*s
, uint64_t *length
)
150 len
= ebml_read_length (s
, &l
);
151 if (len
== EBML_UINT_INVALID
|| len
< 1 || len
> 8)
152 return EBML_INT_INVALID
;
157 l
= stream_read_char (s
);
160 value
= (value
<< 8) | l
;
162 value
= (value
<< 8) | stream_read_char (s
);
168 * Read the next element as a float.
171 ebml_read_float (stream_t
*s
, uint64_t *length
)
177 len
= ebml_read_length (s
, &l
);
182 union {uint32_t i
; float f
;} u
;
183 u
.i
= stream_read_dword (s
);
190 union {uint64_t i
; double d
;} u
;
191 u
.i
= stream_read_qword (s
);
198 union {uint8_t data
[10]; long double ld
;} u
;
199 if (stream_read (s
, u
.data
, 10) != 10)
200 return EBML_FLOAT_INVALID
;
201 value
= be2me_ldbl(u
.ld
);
206 return EBML_FLOAT_INVALID
;
216 * Read the next element as an ASCII string.
219 ebml_read_ascii (stream_t
*s
, uint64_t *length
)
225 len
= ebml_read_length (s
, &l
);
226 if (len
== EBML_UINT_INVALID
)
228 if (len
> SIZE_MAX
- 1)
233 str
= (char *) malloc (len
+1);
234 if (stream_read(s
, str
, len
) != (int) len
)
245 * Read the next element as a UTF-8 string.
248 ebml_read_utf8 (stream_t
*s
, uint64_t *length
)
250 return ebml_read_ascii (s
, length
);
254 * Skip the next element.
257 ebml_read_skip (stream_t
*s
, uint64_t *length
)
262 len
= ebml_read_length (s
, &l
);
263 if (len
== EBML_UINT_INVALID
)
274 * Read the next element, but only the header. The contents
275 * are supposed to be sub-elements which can be read separately.
278 ebml_read_master (stream_t
*s
, uint64_t *length
)
283 id
= ebml_read_id (s
, NULL
);
284 if (id
== EBML_ID_INVALID
)
287 len
= ebml_read_length (s
, NULL
);
288 if (len
== EBML_UINT_INVALID
)
289 return EBML_ID_INVALID
;
298 * Read an EBML header.
301 ebml_read_header (stream_t
*s
, int *version
)
303 uint64_t length
, l
, num
;
307 if (ebml_read_master (s
, &length
) != EBML_ID_HEADER
)
315 id
= ebml_read_id (s
, NULL
);
316 if (id
== EBML_ID_INVALID
)
322 /* is our read version uptodate? */
323 case EBML_ID_EBMLREADVERSION
:
324 num
= ebml_read_uint (s
, &l
);
325 if (num
!= EBML_VERSION
)
329 /* we only handle 8 byte lengths at max */
330 case EBML_ID_EBMLMAXSIZELENGTH
:
331 num
= ebml_read_uint (s
, &l
);
332 if (num
!= sizeof (uint64_t))
336 /* we handle 4 byte IDs at max */
337 case EBML_ID_EBMLMAXIDLENGTH
:
338 num
= ebml_read_uint (s
, &l
);
339 if (num
!= sizeof (uint32_t))
343 case EBML_ID_DOCTYPE
:
344 str
= ebml_read_ascii (s
, &l
);
349 case EBML_ID_DOCTYPEREADVERSION
:
350 num
= ebml_read_uint (s
, &l
);
351 if (num
== EBML_UINT_INVALID
)
357 /* we ignore these two, they don't tell us anything we care about */
359 case EBML_ID_EBMLVERSION
:
360 case EBML_ID_DOCTYPEVERSION
:
362 if (ebml_read_skip (s
, &l
))