1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::fmt;
use std::ops::Deref;
use nom::*;
use core::parsers::*;
use core::types::*;
use tree_reader::branch::TBranch;
use tree_reader::branch::tbranch_hdr;
use tree_reader::leafs::TLeaf;
use tree_reader::leafs::tleaf;
struct Pointer(pub Vec<u8>);
impl Deref for Pointer {
type Target = Vec<u8>;
fn deref(&self) -> &Vec<u8> {
&self.0
}
}
impl fmt::Debug for Pointer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Buffer of {} bytes ", self.len())
}
}
#[derive(Debug)]
pub struct Tree {
ver: u16,
tnamed: TNamed,
fentries: i64,
ftotbytes: i64,
fzipbytes: i64,
fsavedbytes: i64,
fflushedbytes: i64,
fweight: f64,
ftimerinterval: i32,
fscanfield: i32,
fupdate: i32,
fmaxentries: i64,
fmaxentryloop: i64,
festimate: i64,
pub(crate) fbranches: Vec<TBranch>,
pub(crate) fleaves: Vec<TLeaf>,
faliases: Option<Vec<u8>>,
findexvalues: Vec<f64>,
findex: Vec<i32>,
ftreeindex: Option<Pointer>,
ffriends: Option<Pointer>,
fuserinfo: Option<Pointer>,
fbranchref: Option<Pointer>,
}
impl<'s> Tree {
pub(crate) fn branches(&self) -> Vec<(&TBranch)> {
self.fbranches.iter()
.flat_map(|b| vec![b].into_iter().chain(b.branches().into_iter()))
.collect()
}
pub fn branch_names_and_types(&self) -> Vec<(String, Vec<String>)> {
self.fbranches.iter()
.flat_map(|b| vec![b].into_iter().chain(b.branches().into_iter()))
.map(|b| (b.name(), b.element_types()))
.collect()
}
}
#[allow(unused_variables)]
pub fn ttree<'s>(input: &'s[u8], context: &Context) -> IResult<&'s[u8], Tree> {
let _curried_raw = |i| raw(i, context);
let none_or_u8_buf = |i: &'s [u8]| switch!(i, peek!(be_u32),
0 => map!(call!(be_u32), | _ | None) |
_ => map!(
map!(call!(_curried_raw), |r| r.obj.to_vec()),
Some));
let grab_checked_byte_count = |i| length_data!(i, checked_byte_count);
let wrapped_tobjarray = |i: &'s[u8]| length_value!(i, checked_byte_count, apply!(tobjarray, context));
do_parse!(input,
ver: be_u16 >>
tnamed: length_value!(checked_byte_count, tnamed) >>
_tattline: grab_checked_byte_count >>
_tattfill: grab_checked_byte_count >>
_tattmarker: grab_checked_byte_count >>
fentries: be_i64 >>
ftotbytes: be_i64 >>
fzipbytes: be_i64 >>
fsavedbytes: be_i64 >>
fflushedbytes: be_i64 >>
fweight: be_f64 >>
ftimerinterval: be_i32 >>
fscanfield: be_i32 >>
fupdate: be_i32 >>
_fdefaultentryoffsetlen: cond!(ver >= 18, be_i32) >>
fnclusterrange: cond!(ver >= 19, be_i32) >>
fmaxentries: be_i64 >>
fmaxentryloop: be_i64 >>
_fmaxvirtualsize: be_i64 >>
_fautosave: be_i64 >>
_fautoflush: be_i64 >>
festimate: be_i64 >>
_fclusterrangeend: cond!(fnclusterrange.is_some(),
preceded!(be_u8,
count!(be_i64, fnclusterrange.unwrap() as usize))) >>
_fclustersize: cond!(fnclusterrange.is_some(),
preceded!(be_u8,
count!(be_i64, fnclusterrange.unwrap() as usize))) >>
fbranches: wrapped_tobjarray >>
fleaves: wrapped_tobjarray >>
faliases: none_or_u8_buf >>
findexvalues: tarrayd >>
findex: tarrayi >>
ftreeindex: none_or_u8_buf >>
ffriends: none_or_u8_buf >>
fuserinfo: none_or_u8_buf >>
fbranchref: none_or_u8_buf >>
({
let fbranches = fbranches.iter()
.map(|s| tbranch_hdr(s, context).unwrap().1)
.collect();
let fleaves = fleaves.into_iter()
.map(|raw| tleaf(raw.obj, context, &raw.classinfo).unwrap().1)
.collect();
let ftreeindex = ftreeindex.map(Pointer);
let ffriends = ffriends.map(Pointer);
let fuserinfo = fuserinfo.map(Pointer);
let fbranchref = fbranchref.map(Pointer);
Tree {ver,
tnamed,
fentries,
ftotbytes,
fzipbytes,
fsavedbytes,
fflushedbytes,
fweight,
ftimerinterval,
fscanfield,
fupdate,
fmaxentries,
fmaxentryloop,
festimate,
fbranches,
fleaves,
faliases,
findexvalues,
findex,
ftreeindex,
ffriends,
fuserinfo,
fbranchref } }))
}