55
55
//!
56
56
//! Predefined tags with an implicit length:
57
57
//!
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.
62
62
//! Any of `U*` tags can be used to encode primitive unsigned integer types,
63
63
//! as long as it is no greater than the actual size.
64
64
//! For example, `u8` can only be represented via the `U8` tag.
65
65
//!
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.
70
70
//! Similar to `U*` tags. Always uses two's complement encoding.
71
71
//!
72
72
//! - `Bool` (`08`): 1-byte boolean value, `00` for false and `01` for true.
73
73
//!
74
74
//! - `Char` (`09`): 4-byte big endian Unicode scalar value.
75
75
//! Surrogate pairs or out-of-bound values are invalid.
76
76
//!
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
80
78
//! IEEE 754 binary32 floating-point format.
79
+ //! - `F64` (`0b`): 8-byte big endian unsigned integer representing
80
+ //! IEEE 754 binary64 floating-point format.
81
81
//!
82
82
//! - `Sub8` (`0c`): 1-byte unsigned integer for supplementary information.
83
83
//! - `Sub32` (`0d`): 4-byte unsigned integer for supplementary information.
87
87
//!
88
88
//! Predefined tags with an explicit length:
89
89
//!
90
- //! - `Str` (`0e `): A UTF-8-encoded string.
90
+ //! - `Str` (`10 `): A UTF-8-encoded string.
91
91
//!
92
- //! - `Enum` (`0f `): An enum.
92
+ //! - `Enum` (`11 `): An enum.
93
93
//! The first subdocument should be `Sub*` tags with a variant ID.
94
94
//! Subsequent subdocuments, if any, encode variant arguments.
95
95
//!
96
- //! - `Vec` (`10 `): A vector (sequence).
97
- //! - `VecElt` (`11 `): A vector element.
96
+ //! - `Vec` (`12 `): A vector (sequence).
97
+ //! - `VecElt` (`13 `): A vector element.
98
98
//! The first subdocument should be `Sub*` tags with the number of elements.
99
99
//! Subsequent subdocuments should be `VecElt` tag per each element.
100
100
//!
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.
104
104
//! The first subdocument should be `Sub*` tags with the number of entries.
105
105
//! Subsequent subdocuments should be an alternating sequence of
106
106
//! `MapKey` and `MapVal` tags per each entry.
107
107
//!
108
- //! - `Opaque` (`15 `): An opaque, custom-format tag.
108
+ //! - `Opaque` (`17 `): An opaque, custom-format tag.
109
109
//! Used to wrap ordinary custom tags or data in the auto-serialized context.
110
110
//! Rustc typically uses this to encode type informations.
111
111
//!
@@ -183,40 +183,41 @@ pub enum EbmlEncoderTag {
183
183
// tags 00..1f are reserved for auto-serialization.
184
184
// first NUM_IMPLICIT_TAGS tags are implicitly sized and lengths are not encoded.
185
185
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
194
194
EsBool = 0x08 , // + 1 byte
195
195
EsChar = 0x09 , // + 4 bytes
196
- EsF64 = 0x0a , // + 8 bytes
197
- EsF32 = 0x0b , // + 4 bytes
196
+ EsF32 = 0x0a , // + 4 bytes
197
+ EsF64 = 0x0b , // + 8 bytes
198
198
EsSub8 = 0x0c , // + 1 byte
199
199
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 ,
209
210
}
210
211
211
212
const NUM_TAGS : uint = 0x1000 ;
212
213
const NUM_IMPLICIT_TAGS : uint = 0x0e ;
213
214
214
215
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*
217
218
1 , // EsBool
218
219
4 , // EsChar
219
- 8 , 4 , // EsF*
220
+ 4 , 8 , // EsF*
220
221
1 , 4 , // EsSub*
221
222
] ;
222
223
@@ -554,7 +555,10 @@ pub mod reader {
554
555
Ok ( r)
555
556
}
556
557
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.
558
562
fn _next_int ( & mut self ,
559
563
first_tag : EbmlEncoderTag ,
560
564
last_tag : EbmlEncoderTag ) -> DecodeResult < u64 > {
@@ -566,7 +570,7 @@ pub mod reader {
566
570
let TaggedDoc { tag : r_tag, doc : r_doc } =
567
571
try!( doc_at ( self . parent . data , self . pos ) ) ;
568
572
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 {
570
574
0 => doc_as_u8 ( r_doc) as u64 ,
571
575
1 => doc_as_u16 ( r_doc) as u64 ,
572
576
2 => doc_as_u32 ( r_doc) as u64 ,
@@ -608,25 +612,25 @@ pub mod reader {
608
612
type Error = Error ;
609
613
fn read_nil ( & mut self ) -> DecodeResult < ( ) > { Ok ( ( ) ) }
610
614
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 ) }
614
618
fn read_u8 ( & mut self ) -> DecodeResult < u8 > { Ok ( doc_as_u8 ( try!( self . next_doc ( EsU8 ) ) ) ) }
615
619
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 ) ) ;
617
621
if v > ( :: std:: usize:: MAX as u64 ) {
618
622
Err ( IntTooBig ( v as uint ) )
619
623
} else {
620
624
Ok ( v as uint )
621
625
}
622
626
}
623
627
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 ) }
627
631
fn read_i8 ( & mut self ) -> DecodeResult < i8 > { Ok ( doc_as_u8 ( try!( self . next_doc ( EsI8 ) ) ) as i8 ) }
628
632
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 ;
630
634
if v > ( isize:: MAX as i64 ) || v < ( isize:: MIN as i64 ) {
631
635
debug ! ( "FIXME \\ #6122: Removing this makes this function miscompile" ) ;
632
636
Err ( IntTooBig ( v as uint ) )
0 commit comments