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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use crate::wire::marshal::MarshalContext;
mod base;
mod container;
pub use base::*;
pub use container::*;
pub trait Marshal: Signature {
fn marshal(&self, ctx: &mut MarshalContext) -> Result<(), crate::Error>;
fn marshal_as_variant(&self, ctx: &mut MarshalContext) -> Result<(), crate::Error> {
let mut sig = SignatureBuffer::new();
Self::sig_str(&mut sig);
if sig.len() > 255 {
let sig_err = crate::signature::Error::SignatureTooLong;
return Err(sig_err.into());
}
debug_assert!(crate::params::validation::validate_signature(&sig).is_ok());
crate::wire::util::write_signature(&sig, ctx.buf);
self.marshal(ctx)
}
}
#[derive(Debug)]
pub struct SignatureBuffer(Cow<'static, str>);
impl SignatureBuffer {
#[inline]
pub fn new() -> Self {
Self(Cow::Borrowed(""))
}
#[inline]
pub fn push_static(&mut self, sig: &'static str) {
match &mut self.0 {
Cow::Borrowed("") => self.0 = Cow::Borrowed(sig),
Cow::Owned(s) if s.capacity() == 0 => self.0 = Cow::Borrowed(sig),
cow => cow.to_mut().push_str(sig),
}
}
#[inline]
pub fn push_str(&mut self, sig: &str) {
self.0.to_mut().push_str(sig);
}
#[inline]
pub fn to_string_mut(&mut self) -> &mut String {
self.0.to_mut()
}
#[inline]
pub fn clear(&mut self) {
match &mut self.0 {
Cow::Borrowed(_) => *self = Self::new(),
Cow::Owned(s) => s.clear(),
}
}
#[inline]
pub fn from_string(sig: String) -> Self {
Self(Cow::Owned(sig))
}
#[inline]
pub fn as_str(&self) -> &str {
&self.0
}
pub fn truncate(&mut self, new_len: usize) -> Result<(), crate::params::validation::Error> {
if new_len > 0 {
crate::params::validate_signature(&self.0.as_ref()[0..new_len])?;
}
self.0.to_mut().truncate(new_len);
Ok(())
}
}
impl std::ops::Deref for SignatureBuffer {
type Target = str;
#[inline]
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for SignatureBuffer {
#[inline]
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Default for SignatureBuffer {
#[inline]
fn default() -> Self {
Self::new()
}
}
use std::borrow::Cow;
pub trait Signature {
fn signature() -> crate::signature::Type;
fn alignment() -> usize;
#[inline]
unsafe fn valid_slice(_bo: crate::ByteOrder) -> bool {
false
}
fn sig_str(s_buf: &mut SignatureBuffer) {
let s_buf = s_buf.to_string_mut();
let typ = Self::signature();
typ.to_str(s_buf);
}
fn has_sig(sig: &str) -> bool {
let mut s_buf = SignatureBuffer::new();
Self::sig_str(&mut s_buf);
sig == s_buf.as_str()
}
}
impl<S: Signature> Signature for &S {
fn signature() -> crate::signature::Type {
S::signature()
}
fn alignment() -> usize {
S::alignment()
}
fn has_sig(sig: &str) -> bool {
S::has_sig(sig)
}
}
impl<P: Marshal> Marshal for &P {
fn marshal(&self, ctx: &mut MarshalContext) -> Result<(), crate::Error> {
(*self).marshal(ctx)
}
}
#[cfg(test)]
mod test {
use crate::wire::marshal::MarshalContext;
use crate::wire::ObjectPath;
use crate::wire::SignatureWrapper;
#[test]
fn test_trait_signature_creation() {
let mut msg = crate::message_builder::MarshalledMessage::new();
let body = &mut msg.body;
body.push_param("a").unwrap();
body.push_param(ObjectPath::new("/a/b").unwrap()).unwrap();
body.push_param(SignatureWrapper::new("(a{su})").unwrap())
.unwrap();
let fd = crate::wire::UnixFd::new(nix::unistd::dup(1).unwrap());
body.push_param(&fd).unwrap();
body.push_param(true).unwrap();
body.push_param(0u8).unwrap();
body.push_param(0u16).unwrap();
body.push_param(0u32).unwrap();
body.push_param(0u64).unwrap();
body.push_param(0i16).unwrap();
body.push_param(0i32).unwrap();
body.push_param(0i64).unwrap();
body.push_param(&[0u8][..]).unwrap();
let map: std::collections::HashMap<String, (u64, u32, u16, u8)> =
std::collections::HashMap::new();
body.push_param(&map).unwrap();
assert_eq!("soghbyqutnixaya{s(tuqy)}", msg.get_sig());
}
#[test]
fn test_empty_array_padding() {
use crate::wire::marshal::container::marshal_container_param;
let mut msg = crate::message_builder::MarshalledMessage::new();
let body = &mut msg.body;
let empty = vec![0u64; 0];
body.push_param(&empty[..]).unwrap();
let empty = crate::params::Container::make_array_with_sig(
crate::signature::Type::Base(crate::signature::Base::Uint64),
empty.into_iter(),
)
.unwrap();
let mut fds = Vec::new();
let mut buf = Vec::new();
let mut ctx = MarshalContext {
fds: &mut fds,
buf: &mut buf,
byteorder: crate::ByteOrder::LittleEndian,
};
marshal_container_param(&empty, &mut ctx).unwrap();
assert_eq!(msg.get_buf(), &[0, 0, 0, 0, 0, 0, 0, 0]);
assert_eq!(buf.as_slice(), &[0, 0, 0, 0, 0, 0, 0, 0]);
}
#[test]
fn test_variant_marshalling() {
let mut msg = crate::message_builder::MarshalledMessage::new();
let body = &mut msg.body;
let original = (100u64, "ABCD", true);
body.push_variant(original).unwrap();
assert_eq!(
msg.get_buf(),
&[
5, b'(', b't', b's', b'b', b')', b'\0', 0, 100, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0,
b'A', b'B', b'C', b'D', b'\0', 0, 0, 0, 1, 0, 0, 0
]
)
}
}