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
use crate::{DecodeError, DecodeResult, Decoder, Value};
use std::ops::Deref;

impl<'a, T> Decoder<'a, T>
where
    T: Deref<Target = [u8]>,
{
    /// Decode from a byte array at a specific offset to a `Value::Variant`.
    pub fn variant(&mut self, is_le: bool) -> DecodeResult<Value> {
        let signature = self.sig()?;
        let v = self.value(is_le, &signature)?;
        Ok(Value::Variant(v))
    }

    /// Decode from a byte array at a specific offset to a `Value::Array`.
    pub fn array(
        &mut self,
        is_le: bool,
        mut array_recursion: u8,
        struct_recursion: u8,
        signature: &str,
    ) -> DecodeResult<Value> {
        array_recursion += 1;

        let array_size = self.u_32(is_le)?;

        if 67108864 < array_size {
            return Err(DecodeError::ArrayTooBig);
        }

        match signature.get(0..1) {
            Some(s) => match s {
                "v" | "y" | "g" => {}
                "n" | "q" => self.algin(2)?,
                "b" | "i" | "u" | "a" | "s" | "o" => self.algin(4)?,
                "x" | "t" | "(" | "{" => self.algin(8)?,
                _ => return Err(DecodeError::Signature),
            },
            None => return Err(DecodeError::Signature),
        };

        let mut r = Vec::new();

        let end = self.offset + array_size as usize;
        while self.offset < end {
            let mut v = self.v(is_le, array_recursion, struct_recursion, signature)?;
            if v.len() == 1 {
                r.push(v.pop().unwrap());
            } else {
                return Err(DecodeError::ArrayVecLen);
            }
        }

        if self.offset == end {
            Ok(Value::Array(r, String::from(signature)))
        } else {
            Err(DecodeError::ArrayLen)
        }
    }

    /// Decode from a byte array at a specific offset to a `Value::Struct`.
    pub fn decode_struct(
        &mut self,
        is_le: bool,
        array_recursion: u8,
        mut struct_recursion: u8,
        signature: &str,
    ) -> DecodeResult<Value> {
        struct_recursion += 1;
        let v = self.v(is_le, array_recursion, struct_recursion, signature)?;
        Ok(Value::Struct(v))
    }

    /// Decode from a byte array at a specific offset to a `Value::DictEntry`.
    pub fn dict_entry(
        &mut self,
        is_le: bool,
        array_recursion: u8,
        struct_recursion: u8,
        signature_key: &str,
        signature_value: &str,
    ) -> DecodeResult<Value> {
        let mut v = self.v(is_le, array_recursion, struct_recursion, signature_key)?;
        let k = if v.len() == 1 {
            v.pop().unwrap()
        } else {
            return Err(DecodeError::ArrayVecLen);
        };

        let mut v = self.v(is_le, array_recursion, struct_recursion, signature_value)?;
        let v = if v.len() == 1 {
            v.pop().unwrap()
        } else {
            return Err(DecodeError::ArrayVecLen);
        };

        Ok(Value::DictEntry(Box::new((k, v))))
    }
}