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
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 {
let (input, uuid) = parser::uuid(&buf).unwrap();
if uuid != Uuid::from_u128(0x699db17b_8efb_34ff_b1d8_da6f60c15dd1) {
return Err(ErrorKind::NotTeehistorian(Some(uuid)).into());
}
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,
})
}
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();
let buf = unsafe { std::slice::from_raw_parts(buf.as_ptr(), buf.len()) };
match cstring(buf) {
Ok((new_input, header)) => {
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 {
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;
if self.num_consume != 0 {
self.buf.consume(self.num_consume);
self.num_consume = 0;
}
loop {
let buf = self.buf.get_buf();
let buf = unsafe { std::slice::from_raw_parts(buf.as_ptr(), buf.len()) };
match chunks::chunk(buf) {
Ok((new_input, chunk)) => {
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 {
return Err(crate::error::Error::Eof);
}
},
}
}
}
}