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
use crate::error::*;
use anyhow::Result;
use std::collections::HashMap;
use std::fmt;
const NAME_LEN: usize = 255;
#[derive(Default, PartialEq, Debug, Clone)]
pub struct Name {
pub data: String,
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.data)
}
}
impl Name {
pub fn new(data: &str) -> Result<Self> {
if data.len() > NAME_LEN {
Err(Error::ErrCalcLen.into())
} else {
Ok(Name {
data: data.to_owned(),
})
}
}
pub fn pack(
&self,
mut msg: Vec<u8>,
compression: &mut Option<HashMap<String, usize>>,
compression_off: usize,
) -> Result<Vec<u8>> {
let data = self.data.as_bytes();
if data.is_empty() || data[data.len() - 1] != b'.' {
return Err(Error::ErrNonCanonicalName.into());
}
if data.len() == 1 && data[0] == b'.' {
msg.push(0);
return Ok(msg);
}
let mut begin = 0;
for i in 0..data.len() {
if data[i] == b'.' {
if i - begin >= (1 << 6) {
return Err(Error::ErrSegTooLong.into());
}
if i - begin == 0 {
return Err(Error::ErrZeroSegLen.into());
}
msg.push((i - begin) as u8);
msg.extend_from_slice(&data[begin..i]);
begin = i + 1;
continue;
}
if i == 0 || data[i - 1] == b'.' {
if let Some(compression) = compression {
let key: String = self.data.chars().skip(i).collect();
if let Some(ptr) = compression.get(&key) {
msg.push(((ptr >> 8) | 0xC0) as u8);
msg.push((ptr & 0xFF) as u8);
return Ok(msg);
}
if msg.len() <= 0x3FFF {
compression.insert(key, msg.len() - compression_off);
}
}
}
}
msg.push(0);
Ok(msg)
}
pub fn unpack(&mut self, msg: &[u8], off: usize) -> Result<usize> {
self.unpack_compressed(msg, off, true )
}
pub fn unpack_compressed(
&mut self,
msg: &[u8],
off: usize,
allow_compression: bool,
) -> Result<usize> {
let mut curr_off = off;
let mut new_off = off;
let mut ptr = 0;
let mut name = String::new();
loop {
if curr_off >= msg.len() {
return Err(Error::ErrBaseLen.into());
}
let c = msg[curr_off];
curr_off += 1;
match c & 0xC0 {
0x00 => {
if c == 0x00 {
break;
}
let end_off = curr_off + c as usize;
if end_off > msg.len() {
return Err(Error::ErrCalcLen.into());
}
name.push_str(String::from_utf8(msg[curr_off..end_off].to_vec())?.as_str());
name.push('.');
curr_off = end_off;
}
0xC0 => {
if !allow_compression {
return Err(Error::ErrCompressedSrv.into());
}
if curr_off >= msg.len() {
return Err(Error::ErrInvalidPtr.into());
}
let c1 = msg[curr_off];
curr_off += 1;
if ptr == 0 {
new_off = curr_off;
}
ptr += 1;
if ptr > 10 {
return Err(Error::ErrTooManyPtr.into());
}
curr_off = ((c ^ 0xC0) as usize) << 8 | (c1 as usize);
}
_ => {
return Err(Error::ErrReserved.into());
}
}
}
if name.is_empty() {
name.push('.');
}
if name.len() > NAME_LEN {
return Err(Error::ErrCalcLen.into());
}
self.data = name;
if ptr == 0 {
new_off = curr_off;
}
Ok(new_off)
}
pub(crate) fn skip(msg: &[u8], off: usize) -> Result<usize> {
let mut new_off = off;
loop {
if new_off >= msg.len() {
return Err(Error::ErrBaseLen.into());
}
let c = msg[new_off];
new_off += 1;
match c & 0xC0 {
0x00 => {
if c == 0x00 {
break;
}
new_off += c as usize;
if new_off > msg.len() {
return Err(Error::ErrCalcLen.into());
}
}
0xC0 => {
new_off += 1;
break;
}
_ => {
return Err(Error::ErrReserved.into());
}
}
}
Ok(new_off)
}
}