Skip to content

Commit 8ffdc21

Browse files
committed
---
yaml --- r: 193327 b: refs/heads/beta c: c9840b6 h: refs/heads/master i: 193325: ec84f0d 193323: 3ad3e26 193319: 4fda6b2 193311: 42d3ccb v: v3
1 parent 900c90f commit 8ffdc21

File tree

2 files changed

+122
-53
lines changed

2 files changed

+122
-53
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ refs/heads/automation-fail: 1bf06495443584539b958873e04cc2f864ab10e4
3131
refs/heads/issue-18208-method-dispatch-3-quick-reject: 2009f85b9f99dedcec4404418eda9ddba90258a2
3232
refs/heads/batch: b7fd822592a4fb577552d93010c4a4e14f314346
3333
refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
34-
refs/heads/beta: 38a965a747cb5998cce85369aa30a53062dcf363
34+
refs/heads/beta: c9840b644c7e69551c6f9d737125375e4aec602d
3535
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928
3636
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
3737
refs/heads/tmp: de8a23bbc3a7b9cbd7574b5b91a34af59bf030e6

branches/beta/src/librbml/lib.rs

Lines changed: 121 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -80,38 +80,56 @@ pub struct TaggedDoc<'a> {
8080

8181
#[derive(Copy, Debug)]
8282
pub enum EbmlEncoderTag {
83-
EsUint, // 0
84-
EsU64, // 1
85-
EsU32, // 2
86-
EsU16, // 3
87-
EsU8, // 4
88-
EsInt, // 5
89-
EsI64, // 6
90-
EsI32, // 7
91-
EsI16, // 8
92-
EsI8, // 9
93-
EsBool, // 10
94-
EsChar, // 11
95-
EsStr, // 12
96-
EsF64, // 13
97-
EsF32, // 14
98-
EsFloat, // 15
99-
EsEnum, // 16
100-
EsEnumVid, // 17
101-
EsEnumBody, // 18
102-
EsVec, // 19
103-
EsVecLen, // 20
104-
EsVecElt, // 21
105-
EsMap, // 22
106-
EsMapLen, // 23
107-
EsMapKey, // 24
108-
EsMapVal, // 25
109-
110-
EsOpaque,
111-
112-
EsLabel, // Used only when debugging
83+
// tags 00..1f are reserved for auto-serialization.
84+
// first NUM_IMPLICIT_TAGS tags are implicitly sized and lengths are not encoded.
85+
86+
EsUint = 0x00, // + 8 bytes
87+
EsU64 = 0x01, // + 8 bytes
88+
EsU32 = 0x02, // + 4 bytes
89+
EsU16 = 0x03, // + 2 bytes
90+
EsU8 = 0x04, // + 1 byte
91+
EsInt = 0x05, // + 8 bytes
92+
EsI64 = 0x06, // + 8 bytes
93+
EsI32 = 0x07, // + 4 bytes
94+
EsI16 = 0x08, // + 2 bytes
95+
EsI8 = 0x09, // + 1 byte
96+
EsBool = 0x0a, // + 1 byte
97+
EsChar = 0x0b, // + 4 bytes
98+
EsF64 = 0x0c, // + 8 bytes
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+
EsEnumBody = 0x13,
107+
EsVec = 0x14,
108+
EsVecElt = 0x15,
109+
EsMap = 0x16,
110+
EsMapKey = 0x17,
111+
EsMapVal = 0x18,
112+
113+
EsOpaque = 0x19,
114+
115+
// Used only when debugging
116+
EsLabel = 0x1a,
113117
}
114118

119+
const NUM_TAGS: uint = 0x1000;
120+
const NUM_IMPLICIT_TAGS: uint = 0x11;
121+
122+
static TAG_IMPLICIT_LEN: [i8; NUM_IMPLICIT_TAGS] = [
123+
8, 8, 4, 2, 1, // EsU*
124+
8, 8, 4, 2, 1, // ESI*
125+
1, // EsBool
126+
4, // EsChar
127+
8, 4, // EsF*
128+
4, // EsEnumVid
129+
4, // EsVecLen
130+
4, // EsMapLen
131+
];
132+
115133
#[derive(Debug)]
116134
pub enum Error {
117135
IntTooBig(uint),
@@ -143,7 +161,7 @@ pub mod reader {
143161
EsMapLen, EsMapKey, EsEnumVid, EsU64, EsU32, EsU16, EsU8, EsInt, EsI64,
144162
EsI32, EsI16, EsI8, EsBool, EsF64, EsF32, EsChar, EsStr, EsMapVal,
145163
EsEnumBody, EsUint, EsOpaque, EsLabel, EbmlEncoderTag, Doc, TaggedDoc,
146-
Error, IntTooBig, InvalidTag, Expected };
164+
Error, IntTooBig, InvalidTag, Expected, NUM_IMPLICIT_TAGS, TAG_IMPLICIT_LEN };
147165

148166
pub type DecodeResult<T> = Result<T, Error>;
149167
// rbml reading
@@ -250,9 +268,17 @@ pub mod reader {
250268
}
251269
}
252270

271+
pub fn tag_len_at(data: &[u8], tag: Res) -> DecodeResult<Res> {
272+
if tag.val < NUM_IMPLICIT_TAGS && TAG_IMPLICIT_LEN[tag.val] >= 0 {
273+
Ok(Res { val: TAG_IMPLICIT_LEN[tag.val] as uint, next: tag.next })
274+
} else {
275+
vuint_at(data, tag.next)
276+
}
277+
}
278+
253279
pub fn doc_at<'a>(data: &'a [u8], start: uint) -> DecodeResult<TaggedDoc<'a>> {
254280
let elt_tag = try!(tag_at(data, start));
255-
let elt_size = try!(vuint_at(data, elt_tag.next));
281+
let elt_size = try!(tag_len_at(data, elt_tag));
256282
let end = elt_size.next + elt_size.val;
257283
Ok(TaggedDoc {
258284
tag: elt_tag.val,
@@ -264,7 +290,7 @@ pub mod reader {
264290
let mut pos = d.start;
265291
while pos < d.end {
266292
let elt_tag = try_or!(tag_at(d.data, pos), None);
267-
let elt_size = try_or!(vuint_at(d.data, elt_tag.next), None);
293+
let elt_size = try_or!(tag_len_at(d.data, elt_tag), None);
268294
pos = elt_size.next + elt_size.val;
269295
if elt_tag.val == tg {
270296
return Some(Doc { data: d.data, start: elt_size.next,
@@ -290,7 +316,7 @@ pub mod reader {
290316
let mut pos = d.start;
291317
while pos < d.end {
292318
let elt_tag = try_or!(tag_at(d.data, pos), false);
293-
let elt_size = try_or!(vuint_at(d.data, elt_tag.next), false);
319+
let elt_size = try_or!(tag_len_at(d.data, elt_tag), false);
294320
pos = elt_size.next + elt_size.val;
295321
let doc = Doc { data: d.data, start: elt_size.next, end: pos };
296322
if !it(elt_tag.val, doc) {
@@ -306,7 +332,7 @@ pub mod reader {
306332
let mut pos = d.start;
307333
while pos < d.end {
308334
let elt_tag = try_or!(tag_at(d.data, pos), false);
309-
let elt_size = try_or!(vuint_at(d.data, elt_tag.next), false);
335+
let elt_size = try_or!(tag_len_at(d.data, elt_tag), false);
310336
pos = elt_size.next + elt_size.val;
311337
if elt_tag.val == tg {
312338
let doc = Doc { data: d.data, start: elt_size.next,
@@ -718,7 +744,7 @@ pub mod writer {
718744
use super::{ EsVec, EsMap, EsEnum, EsVecLen, EsVecElt, EsMapLen, EsMapKey,
719745
EsEnumVid, EsU64, EsU32, EsU16, EsU8, EsInt, EsI64, EsI32, EsI16, EsI8,
720746
EsBool, EsF64, EsF32, EsChar, EsStr, EsMapVal, EsEnumBody, EsUint,
721-
EsOpaque, EsLabel, EbmlEncoderTag };
747+
EsOpaque, EsLabel, EbmlEncoderTag, NUM_IMPLICIT_TAGS, NUM_TAGS };
722748

723749
use serialize;
724750

@@ -734,7 +760,7 @@ pub mod writer {
734760
fn write_tag<W: Writer>(w: &mut W, n: uint) -> EncodeResult {
735761
if n < 0xf0 {
736762
w.write_all(&[n as u8])
737-
} else if 0x100 <= n && n < 0x1000 {
763+
} else if 0x100 <= n && n < NUM_TAGS {
738764
w.write_all(&[0xf0 | (n >> 8) as u8, n as u8])
739765
} else {
740766
Err(old_io::IoError {
@@ -791,6 +817,7 @@ pub mod writer {
791817

792818
pub fn start_tag(&mut self, tag_id: uint) -> EncodeResult {
793819
debug!("Start tag {:?}", tag_id);
820+
assert!(tag_id >= NUM_IMPLICIT_TAGS);
794821

795822
// Write the enum ID:
796823
try!(write_tag(self.writer, tag_id));
@@ -822,6 +849,7 @@ pub mod writer {
822849
}
823850

824851
pub fn wr_tagged_bytes(&mut self, tag_id: uint, b: &[u8]) -> EncodeResult {
852+
assert!(tag_id >= NUM_IMPLICIT_TAGS);
825853
try!(write_tag(self.writer, tag_id));
826854
try!(write_vuint(self.writer, b.len()));
827855
self.writer.write_all(b)
@@ -866,6 +894,47 @@ pub mod writer {
866894
self.wr_tagged_bytes(tag_id, v.as_bytes())
867895
}
868896

897+
// for auto-serialization
898+
fn wr_tagged_raw_bytes(&mut self, tag_id: uint, b: &[u8]) -> EncodeResult {
899+
try!(write_tag(self.writer, tag_id));
900+
self.writer.write_all(b)
901+
}
902+
903+
fn wr_tagged_raw_u64(&mut self, tag_id: uint, v: u64) -> EncodeResult {
904+
let bytes: [u8; 8] = unsafe { mem::transmute(v.to_be()) };
905+
self.wr_tagged_raw_bytes(tag_id, &bytes)
906+
}
907+
908+
fn wr_tagged_raw_u32(&mut self, tag_id: uint, v: u32) -> EncodeResult{
909+
let bytes: [u8; 4] = unsafe { mem::transmute(v.to_be()) };
910+
self.wr_tagged_raw_bytes(tag_id, &bytes)
911+
}
912+
913+
fn wr_tagged_raw_u16(&mut self, tag_id: uint, v: u16) -> EncodeResult {
914+
let bytes: [u8; 2] = unsafe { mem::transmute(v.to_be()) };
915+
self.wr_tagged_raw_bytes(tag_id, &bytes)
916+
}
917+
918+
fn wr_tagged_raw_u8(&mut self, tag_id: uint, v: u8) -> EncodeResult {
919+
self.wr_tagged_raw_bytes(tag_id, &[v])
920+
}
921+
922+
fn wr_tagged_raw_i64(&mut self, tag_id: uint, v: i64) -> EncodeResult {
923+
self.wr_tagged_raw_u64(tag_id, v as u64)
924+
}
925+
926+
fn wr_tagged_raw_i32(&mut self, tag_id: uint, v: i32) -> EncodeResult {
927+
self.wr_tagged_raw_u32(tag_id, v as u32)
928+
}
929+
930+
fn wr_tagged_raw_i16(&mut self, tag_id: uint, v: i16) -> EncodeResult {
931+
self.wr_tagged_raw_u16(tag_id, v as u16)
932+
}
933+
934+
fn wr_tagged_raw_i8(&mut self, tag_id: uint, v: i8) -> EncodeResult {
935+
self.wr_tagged_raw_bytes(tag_id, &[v as u8])
936+
}
937+
869938
pub fn wr_bytes(&mut self, b: &[u8]) -> EncodeResult {
870939
debug!("Write {:?} bytes", b.len());
871940
self.writer.write_all(b)
@@ -891,7 +960,7 @@ pub mod writer {
891960
// used internally to emit things like the vector length and so on
892961
fn _emit_tagged_uint(&mut self, t: EbmlEncoderTag, v: uint) -> EncodeResult {
893962
assert!(v <= 0xFFFF_FFFF);
894-
self.wr_tagged_u32(t as uint, v as u32)
963+
self.wr_tagged_raw_u32(t as uint, v as u32)
895964
}
896965

897966
fn _emit_label(&mut self, label: &str) -> EncodeResult {
@@ -922,51 +991,51 @@ pub mod writer {
922991
}
923992

924993
fn emit_uint(&mut self, v: uint) -> EncodeResult {
925-
self.wr_tagged_u64(EsUint as uint, v as u64)
994+
self.wr_tagged_raw_u64(EsUint as uint, v as u64)
926995
}
927996
fn emit_u64(&mut self, v: u64) -> EncodeResult {
928-
self.wr_tagged_u64(EsU64 as uint, v)
997+
self.wr_tagged_raw_u64(EsU64 as uint, v)
929998
}
930999
fn emit_u32(&mut self, v: u32) -> EncodeResult {
931-
self.wr_tagged_u32(EsU32 as uint, v)
1000+
self.wr_tagged_raw_u32(EsU32 as uint, v)
9321001
}
9331002
fn emit_u16(&mut self, v: u16) -> EncodeResult {
934-
self.wr_tagged_u16(EsU16 as uint, v)
1003+
self.wr_tagged_raw_u16(EsU16 as uint, v)
9351004
}
9361005
fn emit_u8(&mut self, v: u8) -> EncodeResult {
937-
self.wr_tagged_u8(EsU8 as uint, v)
1006+
self.wr_tagged_raw_u8(EsU8 as uint, v)
9381007
}
9391008

9401009
fn emit_int(&mut self, v: int) -> EncodeResult {
941-
self.wr_tagged_i64(EsInt as uint, v as i64)
1010+
self.wr_tagged_raw_i64(EsInt as uint, v as i64)
9421011
}
9431012
fn emit_i64(&mut self, v: i64) -> EncodeResult {
944-
self.wr_tagged_i64(EsI64 as uint, v)
1013+
self.wr_tagged_raw_i64(EsI64 as uint, v)
9451014
}
9461015
fn emit_i32(&mut self, v: i32) -> EncodeResult {
947-
self.wr_tagged_i32(EsI32 as uint, v)
1016+
self.wr_tagged_raw_i32(EsI32 as uint, v)
9481017
}
9491018
fn emit_i16(&mut self, v: i16) -> EncodeResult {
950-
self.wr_tagged_i16(EsI16 as uint, v)
1019+
self.wr_tagged_raw_i16(EsI16 as uint, v)
9511020
}
9521021
fn emit_i8(&mut self, v: i8) -> EncodeResult {
953-
self.wr_tagged_i8(EsI8 as uint, v)
1022+
self.wr_tagged_raw_i8(EsI8 as uint, v)
9541023
}
9551024

9561025
fn emit_bool(&mut self, v: bool) -> EncodeResult {
957-
self.wr_tagged_u8(EsBool as uint, v as u8)
1026+
self.wr_tagged_raw_u8(EsBool as uint, v as u8)
9581027
}
9591028

9601029
fn emit_f64(&mut self, v: f64) -> EncodeResult {
9611030
let bits = unsafe { mem::transmute(v) };
962-
self.wr_tagged_u64(EsF64 as uint, bits)
1031+
self.wr_tagged_raw_u64(EsF64 as uint, bits)
9631032
}
9641033
fn emit_f32(&mut self, v: f32) -> EncodeResult {
9651034
let bits = unsafe { mem::transmute(v) };
966-
self.wr_tagged_u32(EsF32 as uint, bits)
1035+
self.wr_tagged_raw_u32(EsF32 as uint, bits)
9671036
}
9681037
fn emit_char(&mut self, v: char) -> EncodeResult {
969-
self.wr_tagged_u32(EsChar as uint, v as u32)
1038+
self.wr_tagged_raw_u32(EsChar as uint, v as u32)
9701039
}
9711040

9721041
fn emit_str(&mut self, v: &str) -> EncodeResult {

0 commit comments

Comments
 (0)