Skip to content

Commit 2008b54

Browse files
committed
metadata: Reordered integral tags in the ascending order.
Also clarified the mysterious `_next_int` method.
1 parent ef3c7af commit 2008b54

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

src/librbml/lib.rs

+55-51
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,29 @@
5555
//!
5656
//! Predefined tags with an implicit length:
5757
//!
58-
//! - `U64` (`00`): 8-byte big endian unsigned integer.
59-
//! - `U32` (`01`): 4-byte big endian unsigned integer.
60-
//! - `U16` (`02`): 2-byte big endian unsigned integer.
61-
//! - `U8` (`03`): 1-byte unsigned integer.
58+
//! - `U8` (`00`): 1-byte unsigned integer.
59+
//! - `U16` (`01`): 2-byte big endian unsigned integer.
60+
//! - `U32` (`02`): 4-byte big endian unsigned integer.
61+
//! - `U64` (`03`): 8-byte big endian unsigned integer.
6262
//! Any of `U*` tags can be used to encode primitive unsigned integer types,
6363
//! as long as it is no greater than the actual size.
6464
//! For example, `u8` can only be represented via the `U8` tag.
6565
//!
66-
//! - `I64` (`04`): 8-byte big endian signed integer.
67-
//! - `I32` (`05`): 4-byte big endian signed integer.
68-
//! - `I16` (`06`): 2-byte big endian signed integer.
69-
//! - `I8` (`07`): 1-byte signed integer.
66+
//! - `I8` (`04`): 1-byte signed integer.
67+
//! - `I16` (`05`): 2-byte big endian signed integer.
68+
//! - `I32` (`06`): 4-byte big endian signed integer.
69+
//! - `I64` (`07`): 8-byte big endian signed integer.
7070
//! Similar to `U*` tags. Always uses two's complement encoding.
7171
//!
7272
//! - `Bool` (`08`): 1-byte boolean value, `00` for false and `01` for true.
7373
//!
7474
//! - `Char` (`09`): 4-byte big endian Unicode scalar value.
7575
//! Surrogate pairs or out-of-bound values are invalid.
7676
//!
77-
//! - `F64` (`0a`): 8-byte big endian unsigned integer representing
78-
//! IEEE 754 binary64 floating-point format.
79-
//! - `F32` (`0b`): 4-byte big endian unsigned integer representing
77+
//! - `F32` (`0a`): 4-byte big endian unsigned integer representing
8078
//! IEEE 754 binary32 floating-point format.
79+
//! - `F64` (`0b`): 8-byte big endian unsigned integer representing
80+
//! IEEE 754 binary64 floating-point format.
8181
//!
8282
//! - `Sub8` (`0c`): 1-byte unsigned integer for supplementary information.
8383
//! - `Sub32` (`0d`): 4-byte unsigned integer for supplementary information.
@@ -87,25 +87,25 @@
8787
//!
8888
//! Predefined tags with an explicit length:
8989
//!
90-
//! - `Str` (`0e`): A UTF-8-encoded string.
90+
//! - `Str` (`10`): A UTF-8-encoded string.
9191
//!
92-
//! - `Enum` (`0f`): An enum.
92+
//! - `Enum` (`11`): An enum.
9393
//! The first subdocument should be `Sub*` tags with a variant ID.
9494
//! Subsequent subdocuments, if any, encode variant arguments.
9595
//!
96-
//! - `Vec` (`10`): A vector (sequence).
97-
//! - `VecElt` (`11`): A vector element.
96+
//! - `Vec` (`12`): A vector (sequence).
97+
//! - `VecElt` (`13`): A vector element.
9898
//! The first subdocument should be `Sub*` tags with the number of elements.
9999
//! Subsequent subdocuments should be `VecElt` tag per each element.
100100
//!
101-
//! - `Map` (`12`): A map (associated array).
102-
//! - `MapKey` (`13`): A key part of the map entry.
103-
//! - `MapVal` (`14`): A value part of the map entry.
101+
//! - `Map` (`14`): A map (associated array).
102+
//! - `MapKey` (`15`): A key part of the map entry.
103+
//! - `MapVal` (`16`): A value part of the map entry.
104104
//! The first subdocument should be `Sub*` tags with the number of entries.
105105
//! Subsequent subdocuments should be an alternating sequence of
106106
//! `MapKey` and `MapVal` tags per each entry.
107107
//!
108-
//! - `Opaque` (`15`): An opaque, custom-format tag.
108+
//! - `Opaque` (`17`): An opaque, custom-format tag.
109109
//! Used to wrap ordinary custom tags or data in the auto-serialized context.
110110
//! Rustc typically uses this to encode type informations.
111111
//!
@@ -183,40 +183,41 @@ pub enum EbmlEncoderTag {
183183
// tags 00..1f are reserved for auto-serialization.
184184
// first NUM_IMPLICIT_TAGS tags are implicitly sized and lengths are not encoded.
185185

186-
EsU64 = 0x00, // + 8 bytes
187-
EsU32 = 0x01, // + 4 bytes
188-
EsU16 = 0x02, // + 2 bytes
189-
EsU8 = 0x03, // + 1 byte
190-
EsI64 = 0x04, // + 8 bytes
191-
EsI32 = 0x05, // + 4 bytes
192-
EsI16 = 0x06, // + 2 bytes
193-
EsI8 = 0x07, // + 1 byte
186+
EsU8 = 0x00, // + 1 byte
187+
EsU16 = 0x01, // + 2 bytes
188+
EsU32 = 0x02, // + 4 bytes
189+
EsU64 = 0x03, // + 8 bytes
190+
EsI8 = 0x04, // + 1 byte
191+
EsI16 = 0x05, // + 2 bytes
192+
EsI32 = 0x06, // + 4 bytes
193+
EsI64 = 0x07, // + 8 bytes
194194
EsBool = 0x08, // + 1 byte
195195
EsChar = 0x09, // + 4 bytes
196-
EsF64 = 0x0a, // + 8 bytes
197-
EsF32 = 0x0b, // + 4 bytes
196+
EsF32 = 0x0a, // + 4 bytes
197+
EsF64 = 0x0b, // + 8 bytes
198198
EsSub8 = 0x0c, // + 1 byte
199199
EsSub32 = 0x0d, // + 4 bytes
200-
201-
EsStr = 0x0e,
202-
EsEnum = 0x0f, // encodes the variant id as the first EsSub*
203-
EsVec = 0x10, // encodes the # of elements as the first EsSub*
204-
EsVecElt = 0x11,
205-
EsMap = 0x12, // encodes the # of pairs as the first EsSub*
206-
EsMapKey = 0x13,
207-
EsMapVal = 0x14,
208-
EsOpaque = 0x15,
200+
// 0x0e and 0x0f are reserved
201+
202+
EsStr = 0x10,
203+
EsEnum = 0x11, // encodes the variant id as the first EsSub*
204+
EsVec = 0x12, // encodes the # of elements as the first EsSub*
205+
EsVecElt = 0x13,
206+
EsMap = 0x14, // encodes the # of pairs as the first EsSub*
207+
EsMapKey = 0x15,
208+
EsMapVal = 0x16,
209+
EsOpaque = 0x17,
209210
}
210211

211212
const NUM_TAGS: uint = 0x1000;
212213
const NUM_IMPLICIT_TAGS: uint = 0x0e;
213214

214215
static TAG_IMPLICIT_LEN: [i8; NUM_IMPLICIT_TAGS] = [
215-
8, 4, 2, 1, // EsU*
216-
8, 4, 2, 1, // ESI*
216+
1, 2, 4, 8, // EsU*
217+
1, 2, 4, 8, // ESI*
217218
1, // EsBool
218219
4, // EsChar
219-
8, 4, // EsF*
220+
4, 8, // EsF*
220221
1, 4, // EsSub*
221222
];
222223

@@ -554,7 +555,10 @@ pub mod reader {
554555
Ok(r)
555556
}
556557

557-
// variable-length unsigned integer with different tags
558+
// variable-length unsigned integer with different tags.
559+
// `first_tag` should be a tag for u8 or i8.
560+
// `last_tag` should be the largest allowed integer tag with the matching signedness.
561+
// all tags between them should be valid, in the order of u8, u16, u32 and u64.
558562
fn _next_int(&mut self,
559563
first_tag: EbmlEncoderTag,
560564
last_tag: EbmlEncoderTag) -> DecodeResult<u64> {
@@ -566,7 +570,7 @@ pub mod reader {
566570
let TaggedDoc { tag: r_tag, doc: r_doc } =
567571
try!(doc_at(self.parent.data, self.pos));
568572
let r = if first_tag as uint <= r_tag && r_tag <= last_tag as uint {
569-
match last_tag as uint - r_tag {
573+
match r_tag - first_tag as uint {
570574
0 => doc_as_u8(r_doc) as u64,
571575
1 => doc_as_u16(r_doc) as u64,
572576
2 => doc_as_u32(r_doc) as u64,
@@ -608,25 +612,25 @@ pub mod reader {
608612
type Error = Error;
609613
fn read_nil(&mut self) -> DecodeResult<()> { Ok(()) }
610614

611-
fn read_u64(&mut self) -> DecodeResult<u64> { self._next_int(EsU64, EsU8) }
612-
fn read_u32(&mut self) -> DecodeResult<u32> { Ok(try!(self._next_int(EsU32, EsU8)) as u32) }
613-
fn read_u16(&mut self) -> DecodeResult<u16> { Ok(try!(self._next_int(EsU16, EsU8)) as u16) }
615+
fn read_u64(&mut self) -> DecodeResult<u64> { self._next_int(EsU8, EsU64) }
616+
fn read_u32(&mut self) -> DecodeResult<u32> { Ok(try!(self._next_int(EsU8, EsU32)) as u32) }
617+
fn read_u16(&mut self) -> DecodeResult<u16> { Ok(try!(self._next_int(EsU8, EsU16)) as u16) }
614618
fn read_u8(&mut self) -> DecodeResult<u8> { Ok(doc_as_u8(try!(self.next_doc(EsU8)))) }
615619
fn read_uint(&mut self) -> DecodeResult<uint> {
616-
let v = try!(self._next_int(EsU64, EsU8));
620+
let v = try!(self._next_int(EsU8, EsU64));
617621
if v > (::std::usize::MAX as u64) {
618622
Err(IntTooBig(v as uint))
619623
} else {
620624
Ok(v as uint)
621625
}
622626
}
623627

624-
fn read_i64(&mut self) -> DecodeResult<i64> { Ok(try!(self._next_int(EsI64, EsI8)) as i64) }
625-
fn read_i32(&mut self) -> DecodeResult<i32> { Ok(try!(self._next_int(EsI32, EsI8)) as i32) }
626-
fn read_i16(&mut self) -> DecodeResult<i16> { Ok(try!(self._next_int(EsI16, EsI8)) as i16) }
628+
fn read_i64(&mut self) -> DecodeResult<i64> { Ok(try!(self._next_int(EsI8, EsI64)) as i64) }
629+
fn read_i32(&mut self) -> DecodeResult<i32> { Ok(try!(self._next_int(EsI8, EsI32)) as i32) }
630+
fn read_i16(&mut self) -> DecodeResult<i16> { Ok(try!(self._next_int(EsI8, EsI16)) as i16) }
627631
fn read_i8(&mut self) -> DecodeResult<i8> { Ok(doc_as_u8(try!(self.next_doc(EsI8))) as i8) }
628632
fn read_int(&mut self) -> DecodeResult<int> {
629-
let v = try!(self._next_int(EsI64, EsI8)) as i64;
633+
let v = try!(self._next_int(EsI8, EsI64)) as i64;
630634
if v > (isize::MAX as i64) || v < (isize::MIN as i64) {
631635
debug!("FIXME \\#6122: Removing this makes this function miscompile");
632636
Err(IntTooBig(v as uint))

0 commit comments

Comments
 (0)