array_object/pack/
unpack.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
use crate::bitfield::*;
use crate::error::ArrayObjectError;
use crate::misc::Product;
use crate::pack::unpack_float::*;
use crate::pack::unpack_integer::*;
use crate::pack::unpack_string::*;
use crate::pack::varint::*;
use crate::storage::*;

/// Restore from binary.
pub trait Unpack {
    /// Restore ArrayObject from a binary data.
    fn unpack(data: Vec<u8>) -> Result<Self, ArrayObjectError>
    where
        Self: Sized;
}

impl Unpack for ArrayObject {
    fn unpack(mut data: Vec<u8>) -> Result<Self, ArrayObjectError> {
        let (datatype, format, shape, shortdata) = read_footer(&mut data);
        match datatype & TYPE_MASK {
            SHORT_UNSIGNED_INTEGER => {
                if !data.is_empty() {
                    return Err(ArrayObjectError::UnableToDecode);
                }
                Ok(Self {
                    data: shortdata.unwrap(),
                    shape: vec![],
                    datatype: DataType::UnsignedInteger,
                })
            }
            SHORT_SIGNED_INTEGER => {
                if !data.is_empty() {
                    return Err(ArrayObjectError::UnableToDecode);
                }
                Ok(Self {
                    data: shortdata.unwrap(),
                    shape: vec![],
                    datatype: DataType::SignedInteger,
                })
            }
            UNSIGNED_INTEGER => {
                let shape = shape.unwrap();
                if format == VARIABLE_LENGTH {
                    data = from_variable_integer(data);
                } else {
                    let total_len = shape.product();
                    while (data.len() == 0 && total_len > 0)
                        || (total_len == 1 && 2usize.pow(data.len().ilog2()) != data.len())
                    {
                        data.push(0);
                    }
                }
                Ok(Self {
                    data,
                    shape,
                    datatype: DataType::UnsignedInteger,
                })
            }
            SIGNED_INTEGER => {
                let shape = shape.unwrap();
                if format == VARIABLE_LENGTH {
                    data = from_variable_integer(data);
                } else {
                    let total_len = shape.product();
                    while (data.len() == 0 && total_len > 0)
                        || (total_len == 1 && 2usize.pow(data.len().ilog2()) != data.len())
                    {
                        data.push(0);
                    }
                }
                Ok(Self {
                    data,
                    shape,
                    datatype: DataType::SignedInteger,
                })
            }
            REAL => {
                if format == VARIABLE_LENGTH {
                    data = from_variable_float(data);
                }
                Ok(Self {
                    data,
                    shape: shape.unwrap(),
                    datatype: DataType::Real,
                })
            }
            COMPLEX => {
                if format == VARIABLE_LENGTH {
                    data = from_variable_float(data);
                }
                Ok(Self {
                    data,
                    shape: shape.unwrap(),
                    datatype: DataType::Complex,
                })
            }
            STRING => {
                if format == DICTIONARY {
                    data = from_dictionary(data);
                }
                Ok(Self {
                    data,
                    shape: shape.unwrap(),
                    datatype: DataType::String,
                })
            }
            _ => {
                return Err(ArrayObjectError::UnableToDecode);
            }
        }
    }
}

fn read_footer(bytes: &mut Vec<u8>) -> (u8, u8, Option<Vec<u64>>, Option<Vec<u8>>) {
    let last = bytes.pop().unwrap();
    let ty = last & TYPE_MASK;
    let format = last & FORMAT_MASK;
    if ty == SHORT_UNSIGNED_INTEGER || ty == SHORT_SIGNED_INTEGER {
        let data = last & SHORTDATA_MASK;
        (ty, format, None, Some(vec![data]))
    } else {
        let dim = (last & DIMENSION_MASK) as usize;
        let iter = bytes.iter().rev();
        let (shape, len) = varint_decode(iter, dim);
        bytes.truncate(bytes.len() - len);
        (ty, format, Some(shape), None)
    }
}