zvariant/ser.rs
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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
use serde::Serialize;
use std::io::{Seek, Write};
#[cfg(unix)]
use std::os::fd::OwnedFd;
#[cfg(feature = "gvariant")]
use crate::gvariant::Serializer as GVSerializer;
use crate::{
container_depths::ContainerDepths,
dbus::Serializer as DBusSerializer,
serialized::{Context, Data, Format, Size, Written},
signature_parser::SignatureParser,
utils::*,
Basic, DynamicType, Error, Result, Signature, WriteBytes,
};
struct NullWriteSeek;
impl Write for NullWriteSeek {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl Seek for NullWriteSeek {
fn seek(&mut self, _pos: std::io::SeekFrom) -> std::io::Result<u64> {
Ok(u64::MAX) // should never read the return value!
}
}
/// Calculate the serialized size of `T`.
///
/// # Examples
///
/// ```
/// use zvariant::{serialized::Context, serialized_size, LE};
///
/// let ctxt = Context::new_dbus(LE, 0);
/// let len = serialized_size(ctxt, "hello world").unwrap();
/// assert_eq!(*len, 16);
///
/// let len = serialized_size(ctxt, &("hello world!", 42_u64)).unwrap();
/// assert_eq!(*len, 32);
/// ```
pub fn serialized_size<T>(ctxt: Context, value: &T) -> Result<Size>
where
T: ?Sized + Serialize + DynamicType,
{
let mut null = NullWriteSeek;
let signature = value.dynamic_signature();
#[cfg(unix)]
let mut fds = FdList::Number(0);
let len = match ctxt.format() {
Format::DBus => {
let mut ser = DBusSerializer::<NullWriteSeek>::new(
signature,
&mut null,
#[cfg(unix)]
&mut fds,
ctxt,
)?;
value.serialize(&mut ser)?;
ser.0.bytes_written
}
#[cfg(feature = "gvariant")]
Format::GVariant => {
let mut ser = GVSerializer::<NullWriteSeek>::new(
signature,
&mut null,
#[cfg(unix)]
&mut fds,
ctxt,
)?;
value.serialize(&mut ser)?;
ser.0.bytes_written
}
};
let size = Size::new(len, ctxt);
#[cfg(unix)]
let size = match fds {
FdList::Number(n) => size.set_num_fds(n),
FdList::Fds(_) => unreachable!("`Fds::Fds` is not possible here"),
};
Ok(size)
}
/// Serialize `T` to the given `writer`.
///
/// # Examples
///
/// ```
/// use zvariant::{serialized::{Context, Data}, to_writer, LE};
///
/// let ctxt = Context::new_dbus(LE, 0);
/// let mut cursor = std::io::Cursor::new(vec![]);
/// // SAFETY: No FDs are being serialized here so its completely safe.
/// unsafe { to_writer(&mut cursor, ctxt, &42u32) }.unwrap();
/// let encoded = Data::new(cursor.get_ref(), ctxt);
/// let value: u32 = encoded.deserialize().unwrap().0;
/// assert_eq!(value, 42);
/// ```
///
/// # Safety
///
/// On Unix systems, the returned [`Written`] instance can contain file descriptors and therefore
/// the caller is responsible for not dropping the returned [`Written`] instance before the
/// `writer`. Otherwise, the file descriptors in the `Written` instance will be closed while
/// serialized data will still refer to them. Hence why this function is marked unsafe.
///
/// On non-Unix systems, the returned [`Written`] instance will not contain any file descriptors and
/// hence is safe to drop.
///
/// [`to_writer_fds`]: fn.to_writer_fds.html
pub unsafe fn to_writer<W, T>(writer: &mut W, ctxt: Context, value: &T) -> Result<Written>
where
W: Write + Seek,
T: ?Sized + Serialize + DynamicType,
{
let signature = value.dynamic_signature();
to_writer_for_signature(writer, ctxt, &signature, value)
}
/// Serialize `T` as a byte vector.
///
/// See [`Data::deserialize`] documentation for an example of how to use this function.
pub fn to_bytes<T>(ctxt: Context, value: &T) -> Result<Data<'static, 'static>>
where
T: ?Sized + Serialize + DynamicType,
{
to_bytes_for_signature(ctxt, value.dynamic_signature(), value)
}
/// Serialize `T` that has the given signature, to the given `writer`.
///
/// Use this function instead of [`to_writer`] if the value being serialized does not implement
/// [`DynamicType`].
///
/// # Safety
///
/// On Unix systems, the returned [`Written`] instance can contain file descriptors and therefore
/// the caller is responsible for not dropping the returned [`Written`] instance before the
/// `writer`. Otherwise, the file descriptors in the `Written` instance will be closed while
/// serialized data will still refer to them. Hence why this function is marked unsafe.
///
/// On non-Unix systems, the returned [`Written`] instance will not contain any file descriptors and
/// hence is safe to drop.
///
/// [`to_writer`]: fn.to_writer.html
pub unsafe fn to_writer_for_signature<'s, W, S, T>(
writer: &mut W,
ctxt: Context,
signature: S,
value: &T,
) -> Result<Written>
where
W: Write + Seek,
S: TryInto<Signature<'s>>,
S::Error: Into<Error>,
T: ?Sized + Serialize,
{
#[cfg(unix)]
let mut fds = FdList::Fds(vec![]);
let len = match ctxt.format() {
Format::DBus => {
let mut ser = DBusSerializer::<W>::new(
signature,
writer,
#[cfg(unix)]
&mut fds,
ctxt,
)?;
value.serialize(&mut ser)?;
ser.0.bytes_written
}
#[cfg(feature = "gvariant")]
Format::GVariant => {
let mut ser = GVSerializer::<W>::new(
signature,
writer,
#[cfg(unix)]
&mut fds,
ctxt,
)?;
value.serialize(&mut ser)?;
ser.0.bytes_written
}
};
let written = Written::new(len, ctxt);
#[cfg(unix)]
let written = match fds {
FdList::Fds(fds) => written.set_fds(fds),
FdList::Number(_) => unreachable!("`Fds::Number` is not possible here"),
};
Ok(written)
}
/// Serialize `T` that has the given signature, to a new byte vector.
///
/// Use this function instead of [`to_bytes`] if the value being serialized does not implement
/// [`DynamicType`]. See [`from_slice_for_signature`] documentation for an example of how to use
/// this function.
///
/// [`to_bytes`]: fn.to_bytes.html
/// [`from_slice_for_signature`]: fn.from_slice_for_signature.html#examples
pub fn to_bytes_for_signature<'s, S, T>(
ctxt: Context,
signature: S,
value: &T,
) -> Result<Data<'static, 'static>>
where
S: TryInto<Signature<'s>>,
S::Error: Into<Error>,
T: ?Sized + Serialize,
{
let mut cursor = std::io::Cursor::new(vec![]);
// SAFETY: We put the bytes and FDs in the `Data` to ensure that the data and FDs are only
// dropped together.
let ret = unsafe { to_writer_for_signature(&mut cursor, ctxt, signature, value) }?;
#[cfg(unix)]
let encoded = Data::new_fds(cursor.into_inner(), ctxt, ret.into_fds());
#[cfg(not(unix))]
let encoded = {
let _ = ret;
Data::new(cursor.into_inner(), ctxt)
};
Ok(encoded)
}
/// Context for all our serializers and provides shared functionality.
pub(crate) struct SerializerCommon<'ser, 'sig, W> {
pub(crate) ctxt: Context,
pub(crate) writer: &'ser mut W,
pub(crate) bytes_written: usize,
#[cfg(unix)]
pub(crate) fds: &'ser mut FdList,
pub(crate) sig_parser: SignatureParser<'sig>,
pub(crate) value_sign: Option<Signature<'static>>,
pub(crate) container_depths: ContainerDepths,
}
#[cfg(unix)]
pub(crate) enum FdList {
Fds(Vec<OwnedFd>),
Number(u32),
}
impl<'ser, 'sig, W> SerializerCommon<'ser, 'sig, W>
where
W: Write + Seek,
{
#[cfg(unix)]
pub(crate) fn add_fd(&mut self, fd: std::os::fd::RawFd) -> Result<u32> {
use std::os::fd::{AsRawFd, BorrowedFd};
match self.fds {
FdList::Fds(fds) => {
if let Some(idx) = fds.iter().position(|x| x.as_raw_fd() == fd) {
return Ok(idx as u32);
}
let idx = fds.len();
// Cloning implies dup and is unfortunate but we need to return owned fds
// and dup is not expensive (at least on Linux).
let fd = unsafe { BorrowedFd::borrow_raw(fd) }.try_clone_to_owned()?;
fds.push(fd);
Ok(idx as u32)
}
FdList::Number(n) => {
let idx = *n;
*n += 1;
Ok(idx)
}
}
}
pub(crate) fn add_padding(&mut self, alignment: usize) -> Result<usize> {
let padding = padding_for_n_bytes(self.abs_pos(), alignment);
if padding > 0 {
let byte = [0_u8; 1];
for _ in 0..padding {
self.write_all(&byte)
.map_err(|e| Error::InputOutput(e.into()))?;
}
}
Ok(padding)
}
pub(crate) fn prep_serialize_basic<T>(&mut self) -> Result<()>
where
T: Basic,
{
self.sig_parser.skip_char()?;
self.add_padding(T::alignment(self.ctxt.format()))?;
Ok(())
}
/// This starts the enum serialization.
///
/// It's up to the caller to do the rest: serialize the variant payload and skip the `).
pub(crate) fn prep_serialize_enum_variant(&mut self, variant_index: u32) -> Result<()> {
// Encode enum variants as a struct with first field as variant index
let signature = self.sig_parser.next_signature()?;
if self.sig_parser.next_char()? != STRUCT_SIG_START_CHAR {
return Err(Error::SignatureMismatch(
signature.to_owned(),
format!("expected `{STRUCT_SIG_START_CHAR}`"),
));
}
let alignment = alignment_for_signature(&signature, self.ctxt.format())?;
self.add_padding(alignment)?;
// Now serialize the veriant index.
self.write_u32(self.ctxt.endian(), variant_index)
.map_err(|e| Error::InputOutput(e.into()))?;
// Skip the `(`, `u`.
self.sig_parser.skip_chars(2)?;
Ok(())
}
fn abs_pos(&self) -> usize {
self.ctxt.position() + self.bytes_written
}
}
impl<'ser, 'sig, W> Write for SerializerCommon<'ser, 'sig, W>
where
W: Write + Seek,
{
/// Write `buf` and increment internal bytes written counter.
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.writer.write(buf).map(|n| {
self.bytes_written += n;
n
})
}
fn flush(&mut self) -> std::io::Result<()> {
self.writer.flush()
}
}