2011-06-15 13:19:50 -05:00
|
|
|
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
// Simple Extensible Binary Markup Language (ebml) reader and writer on a
|
2011-03-22 15:59:17 -05:00
|
|
|
// cursor model. See the specification here:
|
|
|
|
// http://www.matroska.org/technical/specs/rfc/index.html
|
2011-12-13 18:25:51 -06:00
|
|
|
import core::option;
|
2011-09-12 18:13:28 -05:00
|
|
|
import option::{some, none};
|
2011-03-22 15:59:17 -05:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
export doc;
|
|
|
|
|
|
|
|
export new_doc;
|
|
|
|
export doc_at;
|
|
|
|
export maybe_get_doc;
|
|
|
|
export get_doc;
|
|
|
|
export docs;
|
|
|
|
export tagged_docs;
|
|
|
|
export doc_data;
|
|
|
|
export doc_as_str;
|
|
|
|
export doc_as_u8;
|
|
|
|
export doc_as_u16;
|
|
|
|
export doc_as_u32;
|
|
|
|
export doc_as_u64;
|
|
|
|
export doc_as_i8;
|
|
|
|
export doc_as_i16;
|
|
|
|
export doc_as_i32;
|
|
|
|
export doc_as_i64;
|
|
|
|
export writer;
|
|
|
|
export mk_writer;
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type ebml_tag = {id: uint, size: uint};
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type ebml_state = {ebml_tag: ebml_tag, tag_pos: uint, data_pos: uint};
|
2011-03-22 15:59:17 -05:00
|
|
|
|
|
|
|
// TODO: When we have module renaming, make "reader" and "writer" separate
|
|
|
|
// modules within this file.
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
// ebml reading
|
2011-08-04 19:24:54 -05:00
|
|
|
type doc = {data: @[u8], start: uint, end: uint};
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-02-10 18:38:33 -06:00
|
|
|
type tagged_doc = {tag: uint, doc: doc};
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn vuint_at(data: [u8], start: uint) -> {val: uint, next: uint} {
|
2011-08-19 17:16:48 -05:00
|
|
|
let a = data[start];
|
2012-02-09 04:50:54 -06:00
|
|
|
if a & 0x80u8 != 0u8 {
|
2012-02-14 17:21:53 -06:00
|
|
|
ret {val: (a & 0x7fu8) as uint, next: start + 1u};
|
2012-02-09 04:50:54 -06:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
if a & 0x40u8 != 0u8 {
|
2012-02-14 17:21:53 -06:00
|
|
|
ret {val: ((a & 0x3fu8) as uint) << 8u |
|
|
|
|
(data[start + 1u] as uint),
|
2011-07-27 07:19:39 -05:00
|
|
|
next: start + 2u};
|
2011-08-19 17:16:48 -05:00
|
|
|
} else if a & 0x20u8 != 0u8 {
|
2012-02-14 17:21:53 -06:00
|
|
|
ret {val: ((a & 0x1fu8) as uint) << 16u |
|
|
|
|
(data[start + 1u] as uint) << 8u |
|
|
|
|
(data[start + 2u] as uint),
|
2011-07-27 07:19:39 -05:00
|
|
|
next: start + 3u};
|
2011-08-19 17:16:48 -05:00
|
|
|
} else if a & 0x10u8 != 0u8 {
|
2012-02-14 17:21:53 -06:00
|
|
|
ret {val: ((a & 0x0fu8) as uint) << 24u |
|
|
|
|
(data[start + 1u] as uint) << 16u |
|
|
|
|
(data[start + 2u] as uint) << 8u |
|
|
|
|
(data[start + 3u] as uint),
|
2011-07-27 07:19:39 -05:00
|
|
|
next: start + 4u};
|
2011-12-22 16:42:52 -06:00
|
|
|
} else { #error("vint too big"); fail; }
|
2011-03-22 15:59:17 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn new_doc(data: @[u8]) -> doc {
|
2011-08-12 12:56:57 -05:00
|
|
|
ret {data: data, start: 0u, end: vec::len::<u8>(*data)};
|
2011-04-06 19:53:25 -05:00
|
|
|
}
|
2011-03-22 15:59:17 -05:00
|
|
|
|
2012-02-10 18:38:33 -06:00
|
|
|
fn doc_at(data: @[u8], start: uint) -> tagged_doc {
|
2012-02-12 21:02:08 -06:00
|
|
|
let elt_tag = vuint_at(*data, start);
|
|
|
|
let elt_size = vuint_at(*data, elt_tag.next);
|
2011-07-27 07:19:39 -05:00
|
|
|
let end = elt_size.next + elt_size.val;
|
2012-02-10 18:38:33 -06:00
|
|
|
ret {tag: elt_tag.val,
|
|
|
|
doc: {data: data, start: elt_size.next, end: end}};
|
2011-03-22 15:59:17 -05:00
|
|
|
}
|
|
|
|
|
2012-01-31 19:05:20 -06:00
|
|
|
fn maybe_get_doc(d: doc, tg: uint) -> option<doc> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pos = d.start;
|
|
|
|
while pos < d.end {
|
2012-02-12 21:02:08 -06:00
|
|
|
let elt_tag = vuint_at(*d.data, pos);
|
|
|
|
let elt_size = vuint_at(*d.data, elt_tag.next);
|
2011-07-26 07:06:02 -05:00
|
|
|
pos = elt_size.next + elt_size.val;
|
2011-07-27 07:19:39 -05:00
|
|
|
if elt_tag.val == tg {
|
2011-08-12 12:56:57 -05:00
|
|
|
ret some::<doc>({data: d.data, start: elt_size.next, end: pos});
|
2011-03-25 19:53:46 -05:00
|
|
|
}
|
2011-03-22 15:59:17 -05:00
|
|
|
}
|
2011-08-12 12:56:57 -05:00
|
|
|
ret none::<doc>;
|
2011-03-22 15:59:17 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn get_doc(d: doc, tg: uint) -> doc {
|
|
|
|
alt maybe_get_doc(d, tg) {
|
|
|
|
some(d) { ret d; }
|
2012-01-19 00:37:22 -06:00
|
|
|
none {
|
2012-02-14 17:21:53 -06:00
|
|
|
#error("failed to find block with tag %u", tg);
|
2011-07-27 07:19:39 -05:00
|
|
|
fail;
|
|
|
|
}
|
2011-04-06 19:53:25 -05:00
|
|
|
}
|
2011-03-22 15:59:17 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 16:59:00 -06:00
|
|
|
fn docs(d: doc, it: fn(uint, doc)) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pos = d.start;
|
|
|
|
while pos < d.end {
|
2012-02-12 21:02:08 -06:00
|
|
|
let elt_tag = vuint_at(*d.data, pos);
|
|
|
|
let elt_size = vuint_at(*d.data, elt_tag.next);
|
2011-07-26 07:06:02 -05:00
|
|
|
pos = elt_size.next + elt_size.val;
|
2011-10-21 05:31:48 -05:00
|
|
|
it(elt_tag.val, {data: d.data, start: elt_size.next, end: pos});
|
2011-03-31 16:37:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-23 16:59:00 -06:00
|
|
|
fn tagged_docs(d: doc, tg: uint, it: fn(doc)) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pos = d.start;
|
|
|
|
while pos < d.end {
|
2012-02-12 21:02:08 -06:00
|
|
|
let elt_tag = vuint_at(*d.data, pos);
|
|
|
|
let elt_size = vuint_at(*d.data, elt_tag.next);
|
2011-07-26 07:06:02 -05:00
|
|
|
pos = elt_size.next + elt_size.val;
|
2011-07-27 07:19:39 -05:00
|
|
|
if elt_tag.val == tg {
|
2011-10-21 05:31:48 -05:00
|
|
|
it({data: d.data, start: elt_size.next, end: pos});
|
2011-04-06 19:53:25 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-31 16:37:09 -05:00
|
|
|
}
|
|
|
|
|
2011-08-12 12:56:57 -05:00
|
|
|
fn doc_data(d: doc) -> [u8] { ret vec::slice::<u8>(*d.data, d.start, d.end); }
|
2011-03-22 15:59:17 -05:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn doc_as_str(d: doc) -> str { ret str::from_bytes(doc_data(d)); }
|
2011-03-31 16:37:09 -05:00
|
|
|
|
2012-02-12 21:02:08 -06:00
|
|
|
fn doc_as_u8(d: doc) -> u8 {
|
|
|
|
assert d.end == d.start + 1u;
|
|
|
|
ret (*d.data)[d.start];
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn doc_as_u16(d: doc) -> u16 {
|
|
|
|
assert d.end == d.start + 2u;
|
|
|
|
ret io::u64_from_be_bytes(*d.data, d.start, 2u) as u16;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn doc_as_u32(d: doc) -> u32 {
|
|
|
|
assert d.end == d.start + 4u;
|
|
|
|
ret io::u64_from_be_bytes(*d.data, d.start, 4u) as u32;
|
2011-03-22 15:59:17 -05:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn doc_as_u64(d: doc) -> u64 {
|
|
|
|
assert d.end == d.start + 8u;
|
|
|
|
ret io::u64_from_be_bytes(*d.data, d.start, 8u);
|
2012-02-12 21:02:08 -06:00
|
|
|
}
|
2011-03-22 15:59:17 -05:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn doc_as_i8(d: doc) -> i8 { doc_as_u8(d) as i8 }
|
|
|
|
fn doc_as_i16(d: doc) -> i16 { doc_as_u16(d) as i16 }
|
|
|
|
fn doc_as_i32(d: doc) -> i32 { doc_as_u32(d) as i32 }
|
|
|
|
fn doc_as_i64(d: doc) -> i64 { doc_as_u64(d) as i64 }
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
// ebml writing
|
2012-01-11 08:15:54 -06:00
|
|
|
type writer = {writer: io::writer, mutable size_positions: [uint]};
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn write_sized_vuint(w: io::writer, n: uint, size: uint) {
|
2011-08-04 19:24:54 -05:00
|
|
|
let buf: [u8];
|
2011-07-27 07:19:39 -05:00
|
|
|
alt size {
|
2011-08-19 17:16:48 -05:00
|
|
|
1u { buf = [0x80u8 | (n as u8)]; }
|
2012-02-14 17:21:53 -06:00
|
|
|
2u { buf = [0x40u8 | ((n >> 8_u) as u8), n as u8]; }
|
2011-07-27 07:19:39 -05:00
|
|
|
3u {
|
2012-02-14 17:21:53 -06:00
|
|
|
buf = [0x20u8 | ((n >> 16_u) as u8), (n >> 8_u) as u8,
|
2012-02-10 18:38:33 -06:00
|
|
|
n as u8];
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
4u {
|
2012-02-14 17:21:53 -06:00
|
|
|
buf = [0x10u8 | ((n >> 24_u) as u8), (n >> 16_u) as u8,
|
|
|
|
(n >> 8_u) as u8, n as u8];
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-02-14 17:21:53 -06:00
|
|
|
_ { fail #fmt("vint to write too big: %?", n); }
|
2011-03-22 19:28:35 -05:00
|
|
|
}
|
|
|
|
w.write(buf);
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn write_vuint(w: io::writer, n: uint) {
|
|
|
|
if n < 0x7f_u { write_sized_vuint(w, n, 1u); ret; }
|
|
|
|
if n < 0x4000_u { write_sized_vuint(w, n, 2u); ret; }
|
|
|
|
if n < 0x200000_u { write_sized_vuint(w, n, 3u); ret; }
|
|
|
|
if n < 0x10000000_u { write_sized_vuint(w, n, 4u); ret; }
|
|
|
|
fail #fmt("vint to write too big: %?", n);
|
2011-03-22 19:28:35 -05:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn mk_writer(w: io::writer) -> writer {
|
2011-08-19 17:16:48 -05:00
|
|
|
let size_positions: [uint] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {writer: w, mutable size_positions: size_positions};
|
2011-03-22 19:28:35 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
// TODO: Provide a function to write the standard ebml header.
|
2012-02-14 17:21:53 -06:00
|
|
|
impl writer for writer {
|
|
|
|
fn start_tag(tag_id: uint) {
|
|
|
|
#debug["Start tag %u", tag_id];
|
2012-02-12 21:02:08 -06:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
// Write the enum ID:
|
|
|
|
write_vuint(self.writer, tag_id);
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
// Write a placeholder four-byte size.
|
|
|
|
self.size_positions += [self.writer.tell()];
|
|
|
|
let zeroes: [u8] = [0u8, 0u8, 0u8, 0u8];
|
|
|
|
self.writer.write(zeroes);
|
|
|
|
}
|
2011-03-22 19:28:35 -05:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn end_tag() {
|
|
|
|
let last_size_pos = vec::pop::<uint>(self.size_positions);
|
|
|
|
let cur_pos = self.writer.tell();
|
|
|
|
self.writer.seek(last_size_pos as int, io::seek_set);
|
|
|
|
let size = (cur_pos - last_size_pos - 4u);
|
|
|
|
write_sized_vuint(self.writer, size, 4u);
|
|
|
|
self.writer.seek(cur_pos as int, io::seek_set);
|
2012-02-12 21:02:08 -06:00
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
#debug["End tag (size = %u)", size];
|
|
|
|
}
|
2012-02-06 09:13:14 -06:00
|
|
|
|
|
|
|
fn wr_tag(tag_id: uint, blk: fn()) {
|
2012-02-14 17:21:53 -06:00
|
|
|
self.start_tag(tag_id);
|
2012-02-06 09:13:14 -06:00
|
|
|
blk();
|
2012-02-14 17:21:53 -06:00
|
|
|
self.end_tag();
|
2012-02-06 09:13:14 -06:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn wr_tagged_bytes(tag_id: uint, b: [u8]) {
|
|
|
|
write_vuint(self.writer, tag_id);
|
|
|
|
write_vuint(self.writer, vec::len(b));
|
|
|
|
self.writer.write(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_tagged_u64(tag_id: uint, v: u64) {
|
|
|
|
self.wr_tagged_bytes(tag_id, io::u64_to_be_bytes(v, 8u));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_tagged_u32(tag_id: uint, v: u32) {
|
|
|
|
self.wr_tagged_bytes(tag_id, io::u64_to_be_bytes(v as u64, 4u));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_tagged_u16(tag_id: uint, v: u16) {
|
|
|
|
self.wr_tagged_bytes(tag_id, io::u64_to_be_bytes(v as u64, 2u));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_tagged_u8(tag_id: uint, v: u8) {
|
|
|
|
self.wr_tagged_bytes(tag_id, [v]);
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn wr_tagged_i64(tag_id: uint, v: i64) {
|
|
|
|
self.wr_tagged_bytes(tag_id, io::u64_to_be_bytes(v as u64, 8u));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_tagged_i32(tag_id: uint, v: i32) {
|
|
|
|
self.wr_tagged_bytes(tag_id, io::u64_to_be_bytes(v as u64, 4u));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_tagged_i16(tag_id: uint, v: i16) {
|
|
|
|
self.wr_tagged_bytes(tag_id, io::u64_to_be_bytes(v as u64, 2u));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_tagged_i8(tag_id: uint, v: i8) {
|
|
|
|
self.wr_tagged_bytes(tag_id, [v as u8]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_tagged_str(tag_id: uint, v: str) {
|
|
|
|
// Lame: can't use str::as_bytes() here because the resulting
|
|
|
|
// vector is NULL-terminated. Annoyingly, the underlying
|
|
|
|
// writer interface doesn't permit us to write a slice of a
|
|
|
|
// vector. We need first-class slices, I think.
|
|
|
|
|
|
|
|
// str::as_bytes(v) {|b| self.wr_tagged_bytes(tag_id, b); }
|
|
|
|
self.wr_tagged_bytes(tag_id, str::bytes(v));
|
2012-02-06 09:13:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_bytes(b: [u8]) {
|
2012-02-12 21:02:08 -06:00
|
|
|
#debug["Write %u bytes", vec::len(b)];
|
2012-02-06 09:13:14 -06:00
|
|
|
self.writer.write(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wr_str(s: str) {
|
2012-02-12 21:02:08 -06:00
|
|
|
#debug["Write str: %?", s];
|
|
|
|
self.writer.write(str::bytes(s));
|
2012-02-06 09:13:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 19:28:35 -05:00
|
|
|
// TODO: optionally perform "relaxations" on end_tag to more efficiently
|
|
|
|
// encode sizes; this is a fixed point iteration
|