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
use amplify::Wrapper;
use core::any::Any;
use core::borrow::Borrow;
use std::collections::BTreeMap;
use std::io;
use std::sync::Arc;
use super::{BigSize, Error, EvenOdd, Unmarshall, UnmarshallFn};
use crate::lightning_encoding::{self, LightningDecode};
pub type Map = BTreeMap<Type, RawRecord>;
#[derive(
Wrapper,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Default,
Debug,
Display,
From,
)]
#[display(inner)]
#[wrapper(LowerHex, UpperHex, Octal, FromStr)]
pub struct Type(u64);
#[derive(
Wrapper, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug, From,
)]
pub struct RawRecord(Vec<u8>);
impl EvenOdd for Type {}
#[derive(Debug, Display, Default)]
#[display(Debug)]
pub struct Stream(BTreeMap<Type, Arc<dyn Any>>);
impl Stream {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn get<T: Any>(&self, type_id: &Type) -> Option<&T> {
self.0.get(type_id).and_then(|v| v.downcast_ref::<T>())
}
#[inline]
pub fn insert<T: Any>(&mut self, type_id: Type, value: T) -> bool {
self.0.insert(type_id, Arc::new(value)).is_none()
}
#[inline]
pub fn contains_key(&self, type_id: &Type) -> bool {
self.0.contains_key(type_id)
}
}
pub struct Unmarshaller {
known_types: BTreeMap<Type, UnmarshallFn<Error>>,
raw_parser: UnmarshallFn<Error>,
}
impl Unmarshall for Unmarshaller {
type Data = Stream;
type Error = Error;
fn unmarshall(
&self,
data: &dyn Borrow<[u8]>,
) -> Result<Stream, Self::Error> {
let mut reader = io::Cursor::new(data.borrow());
let mut tlv = Stream::new();
let mut prev_type_id = Type(0);
loop {
match BigSize::lightning_decode(&mut reader)
.map(|big_size| Type(big_size.into_inner()))
{
Err(lightning_encoding::Error::BigSizeEof) => break Ok(tlv),
Err(err) => break Err(err.into()),
Ok(type_id) if type_id > prev_type_id => {
break Err(Error::TlvStreamWrongOrder)
}
Ok(type_id) if tlv.contains_key(&type_id) => {
break Err(Error::TlvStreamDuplicateItem)
}
Ok(type_id) => {
let rec = if let Some(parser) =
self.known_types.get(&type_id)
{
parser(&mut reader)?
}
else if type_id.is_even() {
break Err(Error::TlvRecordEvenType);
}
else {
(self.raw_parser)(&mut reader)?
};
tlv.insert(type_id, rec);
prev_type_id = type_id;
}
}
}
}
}
impl Unmarshaller {
pub fn new() -> Self {
Self {
known_types: BTreeMap::new(),
raw_parser: Unmarshaller::raw_parser,
}
}
fn raw_parser(
mut reader: &mut dyn io::Read,
) -> Result<Arc<dyn Any>, Error> {
let len = BigSize::lightning_decode(&mut reader)?.into_inner() as usize;
if len > crate::LNP_MSG_MAX_LEN {
Err(Error::TlvRecordInvalidLen)?;
}
let mut buf = vec![0u8; len];
reader
.read_exact(&mut buf[..])
.map_err(|_| Error::TlvRecordInvalidLen)?;
let rec = RawRecord(buf.to_vec());
Ok(Arc::new(rec))
}
}