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
use std::collections::HashMap;
use std::sync::Arc as Rc;
use ::error::{EncodeErrorKind, EncodeResult};
pub struct BinEncoder<'a> {
offset: u32,
buffer: &'a mut Vec<u8>,
name_pointers: HashMap<Vec<Rc<String>>, u16>,
mode: EncodeMode,
canonical_names: bool,
}
impl<'a> BinEncoder<'a> {
pub fn new(buf: &'a mut Vec<u8>) -> Self {
Self::with_offset(buf, 0, EncodeMode::Normal)
}
pub fn with_mode(buf: &'a mut Vec<u8>, mode: EncodeMode) -> Self {
Self::with_offset(buf, 0, mode)
}
pub fn with_offset(buf: &'a mut Vec<u8>, offset: u32, mode: EncodeMode) -> Self {
BinEncoder { offset: offset, buffer: buf, name_pointers: HashMap::new(), mode: mode, canonical_names: false }
}
pub fn as_bytes(self) -> &'a Vec<u8> {
self.buffer
}
pub fn len(&self) -> usize {
self.buffer.len()
}
pub fn offset(&self) -> u32 {
self.offset
}
pub fn mode(&self) -> EncodeMode {
self.mode
}
pub fn set_canonical_names(&mut self, canonical_names: bool) {
self.canonical_names = canonical_names;
}
pub fn is_canonical_names(&self) -> bool {
self.canonical_names
}
pub fn reserve(&mut self, extra: usize) {
self.buffer.reserve(extra);
}
pub fn emit(&mut self, b: u8) -> EncodeResult {
self.offset += 1;
self.buffer.push(b);
Ok(())
}
pub fn store_label_pointer(&mut self, labels: Vec<Rc<String>>) {
if self.offset < 0x3FFFu32 {
self.name_pointers.insert(labels, self.offset as u16);
}
}
pub fn get_label_pointer(&self, labels: &[Rc<String>]) -> Option<u16> {
self.name_pointers.get(labels).map(|i|*i)
}
pub fn emit_character_data(&mut self, char_data: &str) -> EncodeResult {
let char_bytes = char_data.as_bytes();
if char_bytes.len() > 255 { return Err(EncodeErrorKind::CharacterDataTooLong(char_bytes.len()).into()) }
self.buffer.reserve(char_bytes.len() + 1);
try!(self.emit(char_bytes.len() as u8));
for b in char_bytes {
try!(self.emit(*b));
}
Ok(())
}
pub fn emit_u16(&mut self, data: u16) -> EncodeResult {
self.buffer.reserve(2);
let b1: u8 = (data >> 8 & 0xFF) as u8;
let b2: u8 = (data & 0xFF) as u8;
try!(self.emit(b1));
try!(self.emit(b2));
Ok(())
}
pub fn emit_i32(&mut self, data: i32) -> EncodeResult {
self.buffer.reserve(4);
let b1: u8 = (data >> 24 & 0xFF) as u8;
let b2: u8 = (data >> 16 & 0xFF) as u8;
let b3: u8 = (data >> 8 & 0xFF) as u8;
let b4: u8 = (data & 0xFF) as u8;
try!(self.emit(b1));
try!(self.emit(b2));
try!(self.emit(b3));
try!(self.emit(b4));
Ok(())
}
pub fn emit_u32(&mut self, data: u32) -> EncodeResult {
self.buffer.reserve(4);
let b1: u8 = (data >> 24 & 0xFF) as u8;
let b2: u8 = (data >> 16 & 0xFF) as u8;
let b3: u8 = (data >> 8 & 0xFF) as u8;
let b4: u8 = (data & 0xFF) as u8;
try!(self.emit(b1));
try!(self.emit(b2));
try!(self.emit(b3));
try!(self.emit(b4));
Ok(())
}
pub fn emit_vec(&mut self, data: &[u8]) -> EncodeResult {
self.buffer.reserve(data.len());
for i in data {
try!(self.emit(*i));
}
Ok(())
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum EncodeMode { Signing, Normal }