ostrya-gvariant 0.3.0

Byte-exact GVariant codec for the ostree on-disk format
Documentation
use crate::{Error, Result};

/// The maximum container nesting depth in a type signature.
///
/// The value parser and the serializer use the same depth limit, so this
/// parser accepts the type of each value that one of them accepts.
const MAX_TYPE_DEPTH: usize = crate::de::MAX_VALUE_DEPTH;

/// A GVariant type.
///
/// The ostree on-disk format uses booleans, bytes, `u32`, `u64`, strings,
/// variants, arrays, tuples, and dict entries. The other GVariant types are
/// the signed and the 16-bit integers, the handle, the double, the object
/// path, the signature, and the maybe.
///
/// The other types get into a repository through the `ostree commit
/// --add-metadata` command. That command accepts a value of any type in the
/// GVariant text form, which [`from_text`] reads.
///
/// [`from_text`]: crate::from_text
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
    /// `b`
    Bool,
    /// `y`
    Byte,
    /// `n`
    I16,
    /// `q`
    U16,
    /// `i`
    I32,
    /// `u`
    U32,
    /// `x`
    I64,
    /// `t`
    U64,
    /// `h`
    Handle,
    /// `d`
    Double,
    /// `s`
    Str,
    /// `o`
    ObjectPath,
    /// `g`
    Signature,
    /// `v`
    Variant,
    /// `m<T>`
    Maybe(Box<Type>),
    /// `a<T>`
    Array(Box<Type>),
    /// `(<T>...)`
    Tuple(Vec<Type>),
    /// `{<K><V>}`
    ///
    /// The key `K` must be a [basic type](Type::is_basic).
    DictEntry(Box<Type>, Box<Type>),
}

impl Type {
    /// Parses a complete type signature.
    ///
    /// The string must hold exactly one complete type. For example,
    /// `(a{sv}aya(say)sstayay)` is the signature of an ostree commit.
    ///
    /// # Errors
    ///
    /// The function returns [`Error::InvalidTypeString`] if the parse fails.
    /// Its `offset` is the byte offset of the failure. Its `reason` is one of
    /// these:
    ///
    /// - `unexpected end of signature` if the string ends before a complete
    ///   type, for example `""`, `"a"`, or `"(s"`.
    /// - `unsupported type character` if a character is outside the GVariant
    ///   type alphabet.
    /// - `trailing characters after a complete type` if characters follow
    ///   the first complete type, for example `"ss"`.
    /// - `dict-entry key must be a basic type` if the key of a dict entry is
    ///   not a [basic type](Type::is_basic), for example `"{vs}"`.
    /// - `expected '}' after the dict-entry value` if a dict entry holds more
    ///   than two types or has no `}`.
    /// - `nesting exceeds the supported depth` if the signature nests more
    ///   than 128 levels of containers.
    ///
    /// # Depth limit
    ///
    /// A type can nest at most 128 levels of containers. Each `m`, `a`, `(`,
    /// and `{` adds one level to the types inside it. The signature of 128
    /// `a` characters and one `y` parses. With 129 `a` characters, the parse
    /// fails with the reason `nesting exceeds the supported depth`.
    ///
    /// [`from_bytes`] and [`to_bytes`] apply the same limit of 128 levels to a
    /// value.
    ///
    /// [`from_bytes`]: crate::from_bytes
    /// [`to_bytes`]: crate::to_bytes
    pub fn parse(signature: &str) -> Result<Type> {
        let sig = signature.as_bytes();
        let mut pos = 0;
        let err = |offset, reason| Error::InvalidTypeString {
            signature: signature.to_owned(),
            offset,
            reason,
        };
        let ty = parse_one(sig, &mut pos, 0).map_err(|(offset, reason)| err(offset, reason))?;
        if pos != sig.len() {
            return Err(err(pos, "trailing characters after a complete type"));
        }
        Ok(ty)
    }

    /// Returns the signature string of this type.
    pub fn signature(&self) -> String {
        let mut out = String::new();
        self.write_signature(&mut out);
        out
    }

    fn write_signature(&self, out: &mut String) {
        match self {
            Type::Bool => out.push('b'),
            Type::Byte => out.push('y'),
            Type::I16 => out.push('n'),
            Type::U16 => out.push('q'),
            Type::I32 => out.push('i'),
            Type::U32 => out.push('u'),
            Type::I64 => out.push('x'),
            Type::U64 => out.push('t'),
            Type::Handle => out.push('h'),
            Type::Double => out.push('d'),
            Type::Str => out.push('s'),
            Type::ObjectPath => out.push('o'),
            Type::Signature => out.push('g'),
            Type::Variant => out.push('v'),
            Type::Maybe(elem) => {
                out.push('m');
                elem.write_signature(out);
            }
            Type::Array(elem) => {
                out.push('a');
                elem.write_signature(out);
            }
            Type::Tuple(members) => {
                out.push('(');
                for m in members {
                    m.write_signature(out);
                }
                out.push(')');
            }
            Type::DictEntry(key, value) => {
                out.push('{');
                key.write_signature(out);
                value.write_signature(out);
                out.push('}');
            }
        }
    }

    /// Returns the alignment requirement of the serialized form, in bytes.
    pub fn alignment(&self) -> usize {
        match self {
            Type::Bool | Type::Byte | Type::Str | Type::ObjectPath | Type::Signature => 1,
            Type::I16 | Type::U16 => 2,
            Type::I32 | Type::U32 | Type::Handle => 4,
            Type::I64 | Type::U64 | Type::Double | Type::Variant => 8,
            Type::Maybe(elem) | Type::Array(elem) => elem.alignment(),
            Type::Tuple(members) => members.iter().map(Type::alignment).max().unwrap_or(1),
            Type::DictEntry(key, value) => key.alignment().max(value.alignment()),
        }
    }

    /// Returns the serialized size, or `None` if the type has a variable size.
    ///
    /// The size of a tuple or a dict entry includes the padding at its end to
    /// its alignment, so `(ty)` has the size 16. The empty tuple `()` has the
    /// fixed size 1.
    pub fn fixed_size(&self) -> Option<usize> {
        match self {
            Type::Bool | Type::Byte => Some(1),
            Type::I16 | Type::U16 => Some(2),
            Type::I32 | Type::U32 | Type::Handle => Some(4),
            Type::I64 | Type::U64 | Type::Double => Some(8),
            Type::Str
            | Type::ObjectPath
            | Type::Signature
            | Type::Variant
            | Type::Maybe(_)
            | Type::Array(_) => None,
            Type::Tuple(members) => fixed_size_of(members.iter(), self.alignment()),
            Type::DictEntry(key, value) => {
                fixed_size_of([&**key, &**value].into_iter(), self.alignment())
            }
        }
    }

    /// Returns `true` if this type is a scalar or a string.
    ///
    /// These types are the basic types. The key of a dict entry must be a
    /// basic type.
    pub fn is_basic(&self) -> bool {
        matches!(
            self,
            Type::Bool
                | Type::Byte
                | Type::I16
                | Type::U16
                | Type::I32
                | Type::U32
                | Type::I64
                | Type::U64
                | Type::Handle
                | Type::Double
                | Type::Str
                | Type::ObjectPath
                | Type::Signature
        )
    }
}

/// Returns the combined fixed size of the members of a structure.
///
/// If a member has a variable size, this function returns `None`. The empty
/// structure has the fixed size 1.
fn fixed_size_of<'a>(members: impl Iterator<Item = &'a Type>, alignment: usize) -> Option<usize> {
    let mut size = 0;
    let mut any = false;
    for m in members {
        any = true;
        size = align_up(size, m.alignment()) + m.fixed_size()?;
    }
    if !any {
        return Some(1);
    }
    Some(align_up(size, alignment))
}

pub(crate) const fn align_up(n: usize, alignment: usize) -> usize {
    (n + alignment - 1) & !(alignment - 1)
}

type SigError = (usize, &'static str);

fn parse_one(sig: &[u8], pos: &mut usize, depth: usize) -> std::result::Result<Type, SigError> {
    if depth > MAX_TYPE_DEPTH {
        return Err((*pos, "nesting exceeds the supported depth"));
    }
    let Some(&c) = sig.get(*pos) else {
        return Err((*pos, "unexpected end of signature"));
    };
    *pos += 1;
    match c {
        b'b' => Ok(Type::Bool),
        b'y' => Ok(Type::Byte),
        b'n' => Ok(Type::I16),
        b'q' => Ok(Type::U16),
        b'i' => Ok(Type::I32),
        b'u' => Ok(Type::U32),
        b'x' => Ok(Type::I64),
        b't' => Ok(Type::U64),
        b'h' => Ok(Type::Handle),
        b'd' => Ok(Type::Double),
        b's' => Ok(Type::Str),
        b'o' => Ok(Type::ObjectPath),
        b'g' => Ok(Type::Signature),
        b'v' => Ok(Type::Variant),
        b'm' => Ok(Type::Maybe(Box::new(parse_one(sig, pos, depth + 1)?))),
        b'a' => Ok(Type::Array(Box::new(parse_one(sig, pos, depth + 1)?))),
        b'(' => {
            let mut members = Vec::new();
            while sig.get(*pos) != Some(&b')') {
                members.push(parse_one(sig, pos, depth + 1)?);
            }
            *pos += 1;
            Ok(Type::Tuple(members))
        }
        b'{' => {
            let key_offset = *pos;
            let key = parse_one(sig, pos, depth + 1)?;
            if !key.is_basic() {
                return Err((key_offset, "dict-entry key must be a basic type"));
            }
            let value = parse_one(sig, pos, depth + 1)?;
            if sig.get(*pos) != Some(&b'}') {
                return Err((*pos, "expected '}' after the dict-entry value"));
            }
            *pos += 1;
            Ok(Type::DictEntry(Box::new(key), Box::new(value)))
        }
        _ => Err((*pos - 1, "unsupported type character")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The type strings that the ostree on-disk format uses.
    const OSTREE_SIGNATURES: &[&str] = &[
        "(a{sv}aya(say)sstayay)",                                 // commit
        "(a(say)a(sayay))",                                       // dirtree
        "(uuua(ayay))",                                           // dirmeta, user.ostreemeta
        "(uuuusa(ayay))",                                         // uncompressed file header
        "(tuuuusa(ayay))",                                        // archive file header
        "a{sv}",                                                  // commitmeta, summary.sig
        "(a(s(taya{sv}))a{sv})",                                  // summary
        "a{sa(s(taya{sv}))}",                                     // collection map
        "(a(uuu)aa(ayay)ayay)",                                   // delta part payload
        "(uayttay)",                                              // delta meta entry
        "(yaytt)",                                                // delta fallback
        "(a{sv}tayay(a{sv}aya(say)sstayay)aya(uayttay)a(yaytt))", // delta superblock
        "(taya{sv})",                                             // signed delta
        "(yay)",                                                  // delta part framing
        "(su)",                                                   // object name
        "ay",
        "aay",
        "as",
    ];

    /// Type strings that use the type characters outside the on-disk format.
    ///
    /// These types get into a repository through the `ostree commit
    /// --add-metadata` command.
    const METADATA_SIGNATURES: &[&str] = &[
        "n", "q", "i", "x", "h", "d", "o", "g", "ms", "mi", "ami", "a{sd}", "(sid)", "mmy",
    ];

    #[test]
    fn parses_and_round_trips_every_ostree_signature() {
        for sig in OSTREE_SIGNATURES.iter().chain(METADATA_SIGNATURES) {
            let ty = Type::parse(sig).unwrap();
            assert_eq!(ty.signature(), *sig);
        }
    }

    #[test]
    fn alignment_and_fixed_size() {
        let cases: &[(&str, usize, Option<usize>)] = &[
            ("y", 1, Some(1)),
            ("b", 1, Some(1)),
            ("u", 4, Some(4)),
            ("t", 8, Some(8)),
            ("s", 1, None),
            ("v", 8, None),
            ("ay", 1, None),
            ("a(uuu)", 4, None),
            ("(uuu)", 4, Some(12)),
            ("(ty)", 8, Some(16)), // end padding to the tuple alignment
            ("(uuua(ayay))", 4, None),
            ("(tuuuusa(ayay))", 8, None),
            ("(su)", 4, None),
            ("(yaytt)", 8, None),
            ("{sv}", 8, None),
            ("()", 1, Some(1)),
            ("n", 2, Some(2)),
            ("q", 2, Some(2)),
            ("i", 4, Some(4)),
            ("h", 4, Some(4)),
            ("x", 8, Some(8)),
            ("d", 8, Some(8)),
            ("o", 1, None),
            ("g", 1, None),
            ("ms", 1, None),
            ("mi", 4, None),
            ("(id)", 8, Some(16)),
        ];
        for (sig, alignment, fixed) in cases {
            let ty = Type::parse(sig).unwrap();
            assert_eq!(ty.alignment(), *alignment, "alignment of {sig}");
            assert_eq!(ty.fixed_size(), *fixed, "fixed size of {sig}");
        }
    }

    #[test]
    fn rejects_invalid_signatures() {
        for sig in ["", "r", "*", "?", "(s", "{vs}", "{s", "{sv", "ss", "a", "m"] {
            assert!(Type::parse(sig).is_err(), "expected {sig:?} to be rejected");
        }
    }

    #[test]
    fn rejects_overdeep_nesting() {
        // The three cases are the depth limit of the value parser and the
        // serializer, the first depth past that limit, and the depth after that.
        let sig = format!("{}y", "a".repeat(MAX_TYPE_DEPTH));
        assert!(Type::parse(&sig).is_ok());
        let sig = format!("{}y", "a".repeat(MAX_TYPE_DEPTH + 1));
        assert!(Type::parse(&sig).is_err());
        let sig = format!("{}y", "a".repeat(MAX_TYPE_DEPTH + 2));
        assert!(Type::parse(&sig).is_err());
    }
}