@@ -97,32 +97,29 @@ pub enum EbmlEncoderTag {
97
97
EsChar = 0x0b , // + 4 bytes
98
98
EsF64 = 0x0c , // + 8 bytes
99
99
EsF32 = 0x0d , // + 4 bytes
100
- EsEnumVid = 0x0e , // + 4 bytes
101
- EsVecLen = 0x0f , // + 4 bytes
102
- EsMapLen = 0x10 , // + 4 bytes
103
-
104
- EsStr = 0x11 ,
105
- EsEnum = 0x12 ,
106
- EsVec = 0x13 ,
107
- EsVecElt = 0x14 ,
108
- EsMap = 0x15 ,
109
- EsMapKey = 0x16 ,
110
- EsMapVal = 0x17 ,
111
- EsOpaque = 0x18 ,
100
+ EsSub8 = 0x0e , // + 1 byte
101
+ EsSub32 = 0x0f , // + 4 bytes
102
+
103
+ EsStr = 0x10 ,
104
+ EsEnum = 0x11 , // encodes the variant id as the first EsSub*
105
+ EsVec = 0x12 , // encodes the # of elements as the first EsSub*
106
+ EsVecElt = 0x13 ,
107
+ EsMap = 0x14 , // encodes the # of pairs as the first EsSub*
108
+ EsMapKey = 0x15 ,
109
+ EsMapVal = 0x16 ,
110
+ EsOpaque = 0x17 ,
112
111
}
113
112
114
113
const NUM_TAGS : uint = 0x1000 ;
115
- const NUM_IMPLICIT_TAGS : uint = 0x11 ;
114
+ const NUM_IMPLICIT_TAGS : uint = 0x10 ;
116
115
117
116
static TAG_IMPLICIT_LEN : [ i8 ; NUM_IMPLICIT_TAGS ] = [
118
117
8 , 8 , 4 , 2 , 1 , // EsU*
119
118
8 , 8 , 4 , 2 , 1 , // ESI*
120
119
1 , // EsBool
121
120
4 , // EsChar
122
121
8 , 4 , // EsF*
123
- 4 , // EsEnumVid
124
- 4 , // EsVecLen
125
- 4 , // EsMapLen
122
+ 1 , 4 , // EsSub*
126
123
] ;
127
124
128
125
#[ derive( Debug ) ]
@@ -152,8 +149,8 @@ pub mod reader {
152
149
153
150
use serialize;
154
151
155
- use super :: { ApplicationError , EsVec , EsMap , EsEnum , EsVecLen , EsVecElt ,
156
- EsMapLen , EsMapKey , EsEnumVid , EsU64 , EsU32 , EsU16 , EsU8 , EsInt , EsI64 ,
152
+ use super :: { ApplicationError , EsVec , EsMap , EsEnum , EsSub8 , EsSub32 ,
153
+ EsVecElt , EsMapKey , EsU64 , EsU32 , EsU16 , EsU8 , EsInt , EsI64 ,
157
154
EsI32 , EsI16 , EsI8 , EsBool , EsF64 , EsF32 , EsChar , EsStr , EsMapVal ,
158
155
EsUint , EsOpaque , EbmlEncoderTag , Doc , TaggedDoc ,
159
156
Error , IntTooBig , InvalidTag , Expected , NUM_IMPLICIT_TAGS , TAG_IMPLICIT_LEN } ;
@@ -419,6 +416,37 @@ pub mod reader {
419
416
Ok ( r_doc)
420
417
}
421
418
419
+ fn next_doc2 ( & mut self ,
420
+ exp_tag1 : EbmlEncoderTag ,
421
+ exp_tag2 : EbmlEncoderTag ) -> DecodeResult < ( bool , Doc < ' doc > ) > {
422
+ assert ! ( ( exp_tag1 as uint) != ( exp_tag2 as uint) ) ;
423
+ debug ! ( ". next_doc2(exp_tag1={:?}, exp_tag2={:?})" , exp_tag1, exp_tag2) ;
424
+ if self . pos >= self . parent . end {
425
+ return Err ( Expected ( format ! ( "no more documents in \
426
+ current node!") ) ) ;
427
+ }
428
+ let TaggedDoc { tag : r_tag, doc : r_doc } =
429
+ try!( doc_at ( self . parent . data , self . pos ) ) ;
430
+ debug ! ( "self.parent={:?}-{:?} self.pos={:?} r_tag={:?} r_doc={:?}-{:?}" ,
431
+ self . parent. start,
432
+ self . parent. end,
433
+ self . pos,
434
+ r_tag,
435
+ r_doc. start,
436
+ r_doc. end) ;
437
+ if r_tag != ( exp_tag1 as uint ) && r_tag != ( exp_tag2 as uint ) {
438
+ return Err ( Expected ( format ! ( "expected EBML doc with tag {:?} or {:?} but \
439
+ found tag {:?}", exp_tag1, exp_tag2, r_tag) ) ) ;
440
+ }
441
+ if r_doc. end > self . parent . end {
442
+ return Err ( Expected ( format ! ( "invalid EBML, child extends to \
443
+ {:#x}, parent to {:#x}",
444
+ r_doc. end, self . parent. end) ) ) ;
445
+ }
446
+ self . pos = r_doc. end ;
447
+ Ok ( ( r_tag == ( exp_tag2 as uint ) , r_doc) )
448
+ }
449
+
422
450
fn push_doc < T , F > ( & mut self , exp_tag : EbmlEncoderTag , f : F ) -> DecodeResult < T > where
423
451
F : FnOnce ( & mut Decoder < ' doc > ) -> DecodeResult < T > ,
424
452
{
@@ -433,10 +461,15 @@ pub mod reader {
433
461
Ok ( r)
434
462
}
435
463
436
- fn _next_uint ( & mut self , exp_tag : EbmlEncoderTag ) -> DecodeResult < uint > {
437
- let r = doc_as_u32 ( try!( self . next_doc ( exp_tag) ) ) ;
438
- debug ! ( "_next_uint exp_tag={:?} result={:?}" , exp_tag, r) ;
439
- Ok ( r as uint )
464
+ fn _next_sub ( & mut self ) -> DecodeResult < uint > {
465
+ let ( big, doc) = try!( self . next_doc2 ( EsSub8 , EsSub32 ) ) ;
466
+ let r = if big {
467
+ doc_as_u32 ( doc) as uint
468
+ } else {
469
+ doc_as_u8 ( doc) as uint
470
+ } ;
471
+ debug ! ( "_next_sub result={:?}" , r) ;
472
+ Ok ( r)
440
473
}
441
474
442
475
pub fn read_opaque < R , F > ( & mut self , op : F ) -> DecodeResult < R > where
@@ -538,7 +571,7 @@ pub mod reader {
538
571
where F : FnMut ( & mut Decoder < ' doc > , uint ) -> DecodeResult < T > ,
539
572
{
540
573
debug ! ( "read_enum_variant()" ) ;
541
- let idx = try!( self . _next_uint ( EsEnumVid ) ) ;
574
+ let idx = try!( self . _next_sub ( ) ) ;
542
575
debug ! ( " idx={}" , idx) ;
543
576
544
577
f ( self , idx)
@@ -556,7 +589,7 @@ pub mod reader {
556
589
where F : FnMut ( & mut Decoder < ' doc > , uint ) -> DecodeResult < T > ,
557
590
{
558
591
debug ! ( "read_enum_struct_variant()" ) ;
559
- let idx = try!( self . _next_uint ( EsEnumVid ) ) ;
592
+ let idx = try!( self . _next_sub ( ) ) ;
560
593
debug ! ( " idx={}" , idx) ;
561
594
562
595
f ( self , idx)
@@ -647,7 +680,7 @@ pub mod reader {
647
680
{
648
681
debug ! ( "read_seq()" ) ;
649
682
self . push_doc ( EsVec , move |d| {
650
- let len = try!( d. _next_uint ( EsVecLen ) ) ;
683
+ let len = try!( d. _next_sub ( ) ) ;
651
684
debug ! ( " len={}" , len) ;
652
685
f ( d, len)
653
686
} )
@@ -665,7 +698,7 @@ pub mod reader {
665
698
{
666
699
debug ! ( "read_map()" ) ;
667
700
self . push_doc ( EsMap , move |d| {
668
- let len = try!( d. _next_uint ( EsMapLen ) ) ;
701
+ let len = try!( d. _next_sub ( ) ) ;
669
702
debug ! ( " len={}" , len) ;
670
703
f ( d, len)
671
704
} )
@@ -697,10 +730,10 @@ pub mod writer {
697
730
use std:: old_io:: { Writer , Seek } ;
698
731
use std:: old_io;
699
732
700
- use super :: { EsVec , EsMap , EsEnum , EsVecLen , EsVecElt , EsMapLen , EsMapKey ,
701
- EsEnumVid , EsU64 , EsU32 , EsU16 , EsU8 , EsInt , EsI64 , EsI32 , EsI16 , EsI8 ,
733
+ use super :: { EsVec , EsMap , EsEnum , EsSub8 , EsSub32 , EsVecElt , EsMapKey ,
734
+ EsU64 , EsU32 , EsU16 , EsU8 , EsInt , EsI64 , EsI32 , EsI16 , EsI8 ,
702
735
EsBool , EsF64 , EsF32 , EsChar , EsStr , EsMapVal , EsUint ,
703
- EsOpaque , EbmlEncoderTag , NUM_IMPLICIT_TAGS , NUM_TAGS } ;
736
+ EsOpaque , NUM_IMPLICIT_TAGS , NUM_TAGS } ;
704
737
705
738
use serialize;
706
739
@@ -907,9 +940,18 @@ pub mod writer {
907
940
908
941
impl < ' a , W : Writer + Seek > Encoder < ' a , W > {
909
942
// used internally to emit things like the vector length and so on
910
- fn _emit_tagged_uint ( & mut self , t : EbmlEncoderTag , v : uint ) -> EncodeResult {
911
- assert ! ( v <= 0xFFFF_FFFF ) ;
912
- self . wr_tagged_raw_u32 ( t as uint , v as u32 )
943
+ fn _emit_tagged_sub ( & mut self , v : uint ) -> EncodeResult {
944
+ if let Some ( v) = v. to_u8 ( ) {
945
+ self . wr_tagged_raw_u8 ( EsSub8 as uint , v)
946
+ } else if let Some ( v) = v. to_u32 ( ) {
947
+ self . wr_tagged_raw_u32 ( EsSub32 as uint , v)
948
+ } else {
949
+ Err ( old_io:: IoError {
950
+ kind : old_io:: OtherIoError ,
951
+ desc : "length or variant id too big" ,
952
+ detail : Some ( format ! ( "{}" , v) )
953
+ } )
954
+ }
913
955
}
914
956
915
957
pub fn emit_opaque < F > ( & mut self , f : F ) -> EncodeResult where
@@ -995,7 +1037,7 @@ pub mod writer {
995
1037
f : F ) -> EncodeResult where
996
1038
F : FnOnce ( & mut Encoder < ' a , W > ) -> EncodeResult ,
997
1039
{
998
- try!( self . _emit_tagged_uint ( EsEnumVid , v_id) ) ;
1040
+ try!( self . _emit_tagged_sub ( v_id) ) ;
999
1041
f ( self )
1000
1042
}
1001
1043
@@ -1078,7 +1120,7 @@ pub mod writer {
1078
1120
{
1079
1121
1080
1122
try!( self . start_tag ( EsVec as uint ) ) ;
1081
- try!( self . _emit_tagged_uint ( EsVecLen , len) ) ;
1123
+ try!( self . _emit_tagged_sub ( len) ) ;
1082
1124
try!( f ( self ) ) ;
1083
1125
self . end_tag ( )
1084
1126
}
@@ -1097,7 +1139,7 @@ pub mod writer {
1097
1139
{
1098
1140
1099
1141
try!( self . start_tag ( EsMap as uint ) ) ;
1100
- try!( self . _emit_tagged_uint ( EsMapLen , len) ) ;
1142
+ try!( self . _emit_tagged_sub ( len) ) ;
1101
1143
try!( f ( self ) ) ;
1102
1144
self . end_tag ( )
1103
1145
}
0 commit comments