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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/// Parsers of the ROOT core types. Note that objects in ROOT files
/// are often, but not always, preceeded by their size. The parsers in
/// this module do therefore not included this leading size
/// information. Usually, the user will want to do that with something
/// along the lines of `length_value!(checked_byte_count, tobject)`
/// themself.

// Much of this is based on infromation from Much of this is derived
// from streamerinfo.txt which is included in the official ROOT source
// code for (old) layout

use std::str;
use std::io::Read;

use failure::Error;
use nom::{self, be_i32, be_u16, be_u32, be_u8, be_f64, rest};
use flate2::bufread::ZlibDecoder;
use xz2::read::XzDecoder;

use core::*;

fn is_byte_count(v: u32) -> bool {
    Flags::from_bits_truncate(u64::from(v))
        .intersects(Flags::BYTE_COUNT_MASK)
}

/// Check that the given byte count is not zero after applying bit mask
/// 
named!(
    #[doc="Return the size in bytes of the following object in the
    input. The count is the remainder of this object minus the size
    of the count."],
    pub checked_byte_count<&[u8], u64>,
    verify!(
        map!(verify!(be_u32, is_byte_count),
             |v| u64::from(v) & !Flags::BYTE_COUNT_MASK.bits()),
        |v| v != 0)
);


/// Read ROOT's version of short and long strings (preceeded by u8). Does not read null terminated!
#[allow(unused_variables)]
pub fn string(input: &[u8]) -> nom::IResult<&[u8], String>{
    do_parse!(input,
              len: switch!(be_u8,
                           255 => call!(be_u32) |
                           a => value!(u32::from(a))) >>
              s: map!(
                  map_res!(take!(len), |s| str::from_utf8(s)),
                  |s| s.to_string()) >>
              (s)
    )
}

named!(
    #[doc="Parser for the most basic of ROOT types"],
    pub tobject<&[u8], TObject>,
    do_parse!(ver: be_u16 >> // version_consume_extra_virtual >>
              id: be_u32 >>
              bits: map!(be_u32, |v| {
                  // TObjects read from disc must have the ON_HEAP flag
                  TObjectFlags::from_bits_truncate(v| TObjectFlags::IS_ON_HEAP.bits())}
              ) >>
              _ref: cond!(bits.intersects(TObjectFlags::IS_REFERENCED), be_u16) >>
              ({TObject {
                  ver, id, bits
              }})
    )
);

/// Parse a `TList`
pub fn tlist<'s, 'c>(input: &'s[u8], context: &'c Context) -> nom::IResult<&'s[u8], TList<'c>>
    where 's: 'c
{
    let wrapped_raw = |i| raw(i, context);
    do_parse!(input,
              ver: be_u16 >>
              tobj: tobject >>
              name: string >>
              len: be_i32 >>
              objs: count!(
                  do_parse!(obj: length_value!(checked_byte_count, wrapped_raw) >>
                            // It seems like the TList can have gaps
                            // between the elements. The size of the
                            // gap is specified with a be_u8 following
                            // the previous element.
                            _gap: opt!(complete!(length_data!(be_u8))) >>
                            (obj)),
                  len as usize) >>
              _rest: rest >>
              ({
                  TList{
                      ver: ver,
                      tobj: tobj,
                      name: name,
                      len: len as usize,
                      objs: objs
                  }}))
}

named!(
    #[doc="Parser for `TNamed` objects"],
    pub tnamed<&[u8], TNamed>,
    do_parse!(_ver: be_u16 >>
              _tobj: tobject >>
              name: string >>
              title: string >>
              ({TNamed{name, title}})
    )
);

/// Parse a `TObjArray`
pub fn tobjarray<'s, 'c>(input: &'s[u8], context: &'c Context) -> nom::IResult<&'s[u8], Vec<Raw<'c>>>
    where 's: 'c
{
    let wrapped_raw = |i| raw(i, context);
    do_parse!(input,
              _ver: be_u16 >>
              _tobj: tobject >>
              _name: c_string >>
              _size: be_i32 >>
              _low: be_i32 >>
              objs: count!(wrapped_raw, _size as usize) >>
              (objs)
    )
}

/// Parse a `TObjArray` which does not have references pointing outside of the input buffer
pub fn tobjarray_no_context(input: &[u8]) -> nom::IResult<&[u8], Vec<(ClassInfo, Vec<u8>)>>
{
    do_parse!(input,
              _ver: be_u16 >>
              _tobj: tobject >>
              _name: c_string >>
              _size: be_i32 >>
              _low: be_i32 >>
              objs: map!(count!(raw_no_context, _size as usize),
                         |v| v.into_iter().map(|(ci, s)| (ci, s.to_vec())).collect()) >>
              (objs)
    )
}

named!(
    #[doc="Parser for `TObjString`"],
    pub tobjstring<&[u8], String>,
    do_parse!(_ver: be_u16 >>
              _tobj: tobject >>
              name: string >>
              _eof: eof!() >>
              ({name})
    )
);

named!(
    #[doc="Parse a so-called `TArrayI`. Note that ROOT's `TArray`s are actually not fixed size..."],
    pub tarrayi<&[u8], Vec<i32>>,
    length_count!(be_i32, be_i32)
);
named!(
    #[doc="Parse a so-called `TArrayI`. Note that ROOT's `TArray`s are actually not fixed size..."],
    pub tarrayd<&[u8], Vec<f64>>,
    length_count!(be_i32, be_f64)
);


fn decode_reader(bytes: &[u8], magic: &str) -> Result<Vec<u8>, Error> {
    let mut ret = vec![];
    match magic {
        "ZL" => {
            let mut decoder = ZlibDecoder::new(&bytes[..]);
            decoder.read_to_end(&mut ret)?
        },
        "XZ" => {
            let mut decoder = XzDecoder::new(&bytes[..]);
            decoder.read_to_end(&mut ret)?
        },
        m => Err(format_err!("Unsupported compression format `{}`", m))?,
    };
    Ok(ret)
}

named!(
    #[doc="Decompress the given buffer. Figures out the compression algorithm from the preceeding \"magic\" bytes"],
    pub decompress<&[u8], Vec<u8>>,
    do_parse!(magic: take_str!(2) >>
              _header: take!(7) >>
              comp_buf: call!(nom::rest) >>
              ret: expr_res!(decode_reader(comp_buf, magic)) >> 
              (ret)
    )
);

named!(
    #[doc="Parse a null terminated string"],
    pub c_string<&[u8], String>,
    map!(
        map_res!(
            take_until_and_consume!(b"\x00".as_ref()),
            |s| str::from_utf8(s)),
        |s| s.to_string())
);

/// Figure out the class we are looking at. The data might not be
/// saved locally but rather in a reference to some other place in the
/// buffer.This is modeled after ROOT's `TBufferFile::ReadObjectAny` and
/// `TBufferFile::ReadClass`
#[allow(unused_variables)]
pub fn classinfo(input: &[u8]) -> nom::IResult<&[u8], ClassInfo>
{
    do_parse!(input,
              bcnt: be_u32 >>
              tag: switch!(
                  value!(!is_byte_count(bcnt) || bcnt as u64 == Flags::NEW_CLASSTAG.bits()),
                  true => value!(bcnt) |
                  false => call!(be_u32)) >>
              cl: switch!(
                  value!(tag as u32), // If new class, this should be like TClass::Load
                  // 0xFFFFFFFF is new class tag
                  0xFFFF_FFFF => map!(c_string, ClassInfo::New) |
                  // Is this an existing class or is it another tag (pointer elswhere)
                  tag => switch!(
                      value!(Flags::from_bits_truncate(u64::from(tag)).contains(Flags::CLASS_MASK)),
                      false => value!(ClassInfo::References(u64::from(tag))) |
                      true => value!(ClassInfo::Exists(u64::from(tag) & !Flags::CLASS_MASK.bits()))
                  )) >>
              (cl)
    )
}

/// Figure out the class we are looking at. This parser immediately
/// resolves possible references returning the name of the object in
/// this buffer and the associated data. This function needs a
/// `Context`, though, which may not be avialable. If so, have a look
/// at the `classinfo` parser.
#[allow(unused_variables)]
pub fn class_name_and_buffer<'s, 'c>(input: &'s[u8], context: &'c Context)
                      -> nom::IResult<&'s[u8], (String, &'c[u8])>
    where 's: 'c
{
    let get_name_elsewhere = |tag| {
        let abs_offset = tag & !Flags::CLASS_MASK.bits();
        let s = &context.s[((abs_offset - context.offset) as usize)..];
        let (_, (name, _)) = class_name_and_buffer(s, context).unwrap();
        name
    };
    let get_name_and_buf_elsewhere = |tag| {
        let abs_offset = tag;
        // Sometimes, the reference points to `0`; so we return an empty slice
        if abs_offset == 0 {
            return ("".to_string(), &context.s[..0]);
        }
        let s = &context.s[((abs_offset - context.offset) as usize)..];
        let (_, (name, buf)) = class_name_and_buffer(s, context).unwrap();
        (name, buf)
    };
    do_parse!(input,
              ci: switch!(classinfo,
                          ClassInfo::New(s) => tuple!(value!(s), length_value!(checked_byte_count, call!(nom::rest))) |
                          ClassInfo::Exists(tag) => tuple!(value!(get_name_elsewhere(tag)),
                                                             length_value!(checked_byte_count, call!(nom::rest))) |
                          ClassInfo::References(tag) => value!(get_name_and_buf_elsewhere(tag))) >>
              (ci)
    )
}

/// Parse a `Raw` chunk from the given input buffer. This is usefull when one does not know the exact type at the time of parsing
pub fn raw<'s, 'c>(input: &'s[u8], context: &'c Context) -> nom::IResult<&'s[u8], Raw<'c>>
    where 's: 'c
{
    do_parse!(input,
              string_and_obj: apply!(class_name_and_buffer, context) >>
              // obj: length_value!(checked_byte_count, call!(nom::rest)) >>
              ({let (classinfo, obj) = string_and_obj;
                Raw{classinfo, obj}})
    )
}

/// Same as `raw` but doesn't require a `Context` as input. Panics if
/// a `Context` is required to parse the underlying buffer (i.e., the
/// given buffer contains a reference to some other part of the file.
pub fn raw_no_context(input: &[u8]) -> nom::IResult<&[u8], (ClassInfo, &[u8])>
{
    use self::ClassInfo::*;
    let first = do_parse!(input,
              ci: classinfo >>
              rest: call!(nom::rest) >> 
              (ci, rest)
    );
    if first.is_done() {
        let (_, (ci, rest)) = first.unwrap();
        let obj = match ci {
            References(0) => value!(rest, &input[..0]),
            New(_) | Exists(_) => length_value!(rest, checked_byte_count, call!(nom::rest)),
            // If its a reference to any other thing but 0 it needs a context
            _ => panic!("Object needs context!"),
        };
        obj.map(|o| (ci, o))
    } else {
        first
    }
}

#[cfg(test)]
mod classinfo_test {
    use super::classinfo;

    /// There is an issue where the following is parsed differently on
    /// nightly ( rustc 1.25.0-nightly (79a521bb9 2018-01-15)), than
    /// on stable, if verbose-errors are enabled for nom in the
    /// cargo.toml
    #[test]
    fn classinfo_not_complete_read() {
        let i = vec![128, 0, 0, 150, 64, 0, 1, 92, 0, 3, 0, 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 64, 0, 0, 103, 128, 0, 0, 193, 64, 0, 0, 95, 0, 3, 64, 0, 0, 85, 0, 4, 64, 0, 0, 38, 0, 1, 0, 1, 0, 0, 0, 0, 3, 0, 0, 0, 7, 84, 79, 98, 106, 101, 99, 116, 17, 66, 97, 115, 105, 99, 32, 82, 79, 79, 84, 32, 111, 98, 106, 101, 99, 116, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 27, 192, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 66, 65, 83, 69, 0, 0, 0, 1, 64, 0, 0, 116, 255, 255, 255, 255, 84, 83, 116, 114, 101, 97, 109, 101, 114, 83, 116, 114, 105, 110, 103, 0, 64, 0, 0, 92, 0, 2, 64, 0, 0, 86, 0, 4, 64, 0, 0, 36, 0, 1, 0, 1, 0, 0, 0, 0, 3, 0, 0, 0, 5, 102, 78, 97, 109, 101, 17, 111, 98, 106, 101, 99, 116, 32, 105, 100, 101, 110, 116, 105, 102, 105, 101, 114, 0, 0, 0, 65, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 84, 83, 116, 114, 105, 110, 103, 64, 0, 0, 96, 128, 0, 18, 227, 64, 0, 0, 88, 0, 2, 64, 0, 0, 82, 0, 4, 64, 0, 0, 32, 0, 1, 0, 1, 0, 0, 0, 0, 3, 0, 0, 0, 6, 102, 84, 105, 116, 108, 101, 12, 111, 98, 106, 101, 99, 116, 32, 116, 105, 116, 108, 101, 0, 0, 0, 65, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 84, 83, 116, 114, 105, 110, 103];
        let i = i.as_slice();
        let (i, _ci) = classinfo(i).unwrap();
        // The error manifests in the entire input being (wrongly) consumed, instead of having some left overs
        assert_eq!(i.len(), 352);
    }
}