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
//! teehistorian parser for DDNet
//!
//! This crate provides a library for safe parsing of [teehistorian](https://ddnet.tw/docs/libtw2/teehistorian)
//! files from the game [DDNet](https://ddnet.tw). Goals of this library are:
//!
//! - performance
//!   - the header is retrievable without loading the whole file
//!   - the teehistorian file doesn't have to fit into memory
//! - provide a C-API to eventually integrate it into [DDNet](https://github.com/ddnet/ddnet) for a
//!   teehistorian replayer
//!
//! In the very center of this library is the [Th](Th) struct to get the stream of [Chunks](Chunk) of
//! an teehistorian file
//!
//! # Examples
//!
//! With the teehistorian file loaded directly into memory
//!
//! ```rust
//! use teehistorian::{Chunk, Th};
//! let input = b"\x69\x9d\xb1\x7b\x8e\xfb\x34\xff\xb1\xd8\xda\x6f\x60\xc1\x5d\xd1\
//!            {\"version\":\"2\"}\x00\
//!            \x40";
//! let mut th = Th::parse(&input[..]).unwrap();
//! assert_eq!(th.header().unwrap(), br#"{"version":"2"}"#);
//! assert_eq!(th.next_chunk().unwrap(), Chunk::Eos);
//! assert!(th.next_chunk().unwrap_err().is_eof());
//! ```
//!
//! When operating with files:
//!
//! ```rust
//! use teehistorian::{Chunk, ThBufReader, Th};
//! use std::fs::File;
//!
//! let f = File::open("tests/minimal.teehistorian").unwrap();
//! let mut th = Th::parse(ThBufReader::new(f)).unwrap();
//! assert_eq!(th.header().unwrap(), br#"{"version":"2"}"#);
//! assert_eq!(th.next_chunk().unwrap(), Chunk::Eos);
//! assert!(th.next_chunk().unwrap_err().is_eof());
//! ```
//!

mod parser;
pub mod chunks;
mod error;

pub use error::Error;
pub use chunks::Chunk;
pub use bufread::{ThBufRead, ThBufReader};

#[cfg(cargo_c)]
mod capi;
mod bufread;

use uuid::Uuid;
use error::ErrorKind;
use crate::parser::cstring;

pub struct Th<T: ThBufRead> {
    buf: T,
    parsed_header: bool,
    parsed_chunk: bool,
    num_consume: usize,
}

impl<T: ThBufRead> Th<T> {
    pub fn parse(inp: T) -> Result<Self, crate::error::Error> {
        let mut inp = inp;
        loop {
            if inp.fill_buf()? == 0 && inp.get_buf().len() == 0 {
                return Err(crate::error::ErrorKind::NotTeehistorian(None).into());
            }
            let buf = inp.get_buf();
            if buf.len() >= 16 {
                // can unwrap since already checked, that 16 bytes are in the buffer to parse the uuid
                let (input, uuid) = parser::uuid(&buf).unwrap();
                if uuid != Uuid::from_u128(0x699db17b_8efb_34ff_b1d8_da6f60c15dd1) {
                    return Err(ErrorKind::NotTeehistorian(Some(uuid)).into());
                }
                // safe since computing difference to subslice
                let num_consumed = unsafe { input.as_ptr().offset_from(buf.as_ptr()) };
                inp.consume(num_consumed as usize);
                break;
            }
        }
        Ok(Th {
            buf: inp,
            parsed_header: false,
            parsed_chunk: false,
            num_consume: 0,
        })
    }

    /// Tries to parse the teehistorian header returns a byte slice to a json object excluding without
    /// the null terminator present in the teehistorian format
    ///
    /// # Panic
    ///
    /// `header()` must only be called before the first chunk is requested with [next_chunk()](Th.next_chunk)
    /// Otherwise the functions panics.
    pub fn header(&mut self) -> Result<&[u8], crate::error::Error> {
        if self.parsed_chunk {
            panic!("Teehistorian library misuse. Accessing the header is only allowed before accessing the first chunk");
        }
        self.parsed_header = true;
        loop {
            let buf = self.buf.get_buf();
            // FIXME: when not necessary from non-lexical lifetimes
            let buf = unsafe { std::slice::from_raw_parts(buf.as_ptr(), buf.len()) };
            match cstring(buf) {
                Ok((new_input, header)) => {
                    // safe since computing difference to subslice
                    self.num_consume = unsafe { new_input.as_ptr().offset_from(buf.as_ptr()) } as usize;
                    return Ok(header);
                }
                Err(nom::Err::Error(err)) | Err(nom::Err::Failure(err)) => {
                    self.num_consume = 0;
                    return Err(err.into());
                },
                Err(nom::Err::Incomplete(_)) => {
                    if self.buf.fill_buf()? == 0 {
                        // End of file
                        return Err(crate::error::Error::Eof);
                    }
                },
            }
        }
    }

    pub fn next_chunk(&mut self) -> Result<chunks::Chunk, crate::error::Error> {
        if !self.parsed_header {
            self.header()?;
        }
        self.parsed_chunk = true;
        // consume data from last chunk, since it isn't referenced anymore
        if self.num_consume != 0 {
            self.buf.consume(self.num_consume);
            self.num_consume = 0;
        }

        loop {
            let buf = self.buf.get_buf();
            // FIXME: when not necessary from non-lexical lifetimes
            let buf = unsafe { std::slice::from_raw_parts(buf.as_ptr(), buf.len()) };
            match chunks::chunk(buf) {
                Ok((new_input, chunk)) => {
                    // safe since computing difference to subslice
                    self.num_consume = unsafe { new_input.as_ptr().offset_from(buf.as_ptr()) } as usize;
                    return Ok(chunk);
                }
                Err(nom::Err::Error(err)) | Err(nom::Err::Failure(err)) => {
                    self.num_consume = 0;
                    return Err(err.into());
                },
                Err(nom::Err::Incomplete(_)) => {
                    if self.buf.fill_buf()? == 0 {
                        // End of file
                        return Err(crate::error::Error::Eof);
                    }
                },
            }
        }
    }
}