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
use crate::Expr;
use crate::Key;

/// Dead simple, quick and dirty binary data parser combinator
pub enum Spec {
    Int {
        little_endian: bool,
        signed: bool,
        nbytes: usize,

        /// Expected value for the parse to succeed
        /// (e.g. for magic)
        expected: Option<i64>,
    },
    Array {
        size: Expr,
        member: Box<Spec>,
    },
    Enum(Vec<(Key, Spec)>),
    Struct(Vec<Spec>),
    Scope {
        args: Vec<(Key, Expr)>,
        body: Box<Spec>,
    },
    Store(Key, Box<Spec>),
}

impl Spec {
    pub fn args() -> SpecArgs {
        SpecArgs { args: vec![] }
    }
    pub fn pairs() -> SpecPairs {
        SpecPairs { pairs: vec![] }
    }
    pub fn le_magic_u64(value: u64) -> Spec {
        Self::magic_u64(true, value)
    }
    pub fn be_magic_u64(value: u64) -> Spec {
        Self::magic_u64(false, value)
    }
    pub fn magic_u64(little_endian: bool, value: u64) -> Spec {
        Self::magic(little_endian, 8, value as i64)
    }
    pub fn magic(little_endian: bool, nbytes: usize, value: i64) -> Spec {
        Spec::Int {
            little_endian,
            signed: true,
            nbytes,
            expected: Some(value),
        }
    }
    pub fn le_magic(nbytes: usize, value: i64) -> Spec {
        Self::magic(true, nbytes, value)
    }
    pub fn be_magic(nbytes: usize, value: i64) -> Spec {
        Self::magic(false, nbytes, value)
    }
    pub fn int(little_endian: bool, signed: bool, nbytes: usize) -> Spec {
        Spec::Int {
            little_endian,
            signed,
            nbytes,
            expected: None,
        }
    }
    pub fn le_int(signed: bool, nbytes: usize) -> Spec {
        Self::int(true, signed, nbytes)
    }
    pub fn be_int(signed: bool, nbytes: usize) -> Spec {
        Self::int(true, signed, nbytes)
    }
    pub fn le_sint(nbytes: usize) -> Spec {
        Self::le_int(true, nbytes)
    }
    pub fn le_uint(nbytes: usize) -> Spec {
        Self::le_int(false, nbytes)
    }
    pub fn be_sint(nbytes: usize) -> Spec {
        Self::be_int(true, nbytes)
    }
    pub fn be_uint(nbytes: usize) -> Spec {
        Self::be_int(false, nbytes)
    }
    pub fn arr<E: Into<Expr>>(p: Spec, e: E) -> Spec {
        Spec::Array {
            size: e.into(),
            member: p.into(),
        }
    }

    /// Enum -- will try each of the alternatives in order,
    /// and return the first successful variant
    /// The variant will be tagged in the resulting Data
    /// (as a Data::Enum)
    pub fn en<K: Into<Key>>(vec: Vec<(K, Spec)>) -> Spec {
        Spec::Enum(vec.into_iter().map(|(k, p)| (k.into(), p)).collect())
    }

    /// Struct -- all alternatives must succeed in order
    pub fn st(vec: Vec<Spec>) -> Spec {
        Spec::Struct(vec)
    }

    pub fn scope<K: Into<Key>, E: Into<Expr>>(args: Vec<(K, E)>, p: Spec) -> Spec {
        Spec::Scope {
            args: args
                .into_iter()
                .map(|(k, e)| (k.into(), e.into()))
                .collect(),
            body: p.into(),
        }
    }

    /// Stores the result of a parse into a key for later use
    /// (e.g. as length of an array)
    pub fn store<K: Into<Key>>(key: K, p: Spec) -> Spec {
        Spec::Store(key.into(), p.into())
    }
}

pub struct SpecArgs {
    args: Vec<(Key, Expr)>,
}

impl SpecArgs {
    pub fn arg<K: Into<Key>, E: Into<Expr>>(mut self, k: K, e: E) -> Self {
        self.args.push((k.into(), e.into()));
        self
    }
    pub fn get(self) -> Vec<(Key, Expr)> {
        self.args
    }
}

pub struct SpecPairs {
    pairs: Vec<(Key, Spec)>,
}

impl SpecPairs {
    pub fn arg<K: Into<Key>>(mut self, k: K, p: Spec) -> Self {
        self.pairs.push((k.into(), p));
        self
    }
    pub fn get(self) -> Vec<(Key, Spec)> {
        self.pairs
    }
}