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
/*
 * Copyright 2021 Fluence Labs Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use super::LiResult;
use crate::read_array_ty;
use crate::IValue;

use it_memory_traits::{SequentialMemoryView, SequentialReader};

pub struct MemoryReader<MV> {
    pub(self) view: MV,
}

impl<MV: for<'a> SequentialMemoryView<'a>> MemoryReader<MV> {
    pub fn new(view: MV) -> Self {
        Self { view }
    }

    /// Returns reader that allows read sequentially. It's important that memory limit is checked
    /// only inside this function. All others functions of the returned reader don't have any
    /// checks assuming that reader is well-formed.
    pub fn sequential_reader(
        &self,
        offset: usize,
        size: usize,
    ) -> LiResult<<MV as SequentialMemoryView<'_>>::SR> {
        let seq_reader = self.view.sequential_reader(offset, size)?;
        Ok(seq_reader)
    }

    pub fn read_raw_u8_array(&self, offset: usize, elements_count: usize) -> LiResult<Vec<u8>> {
        let reader = self.sequential_reader(offset, elements_count)?;
        let mut result = Vec::with_capacity(elements_count);

        for _ in 0..elements_count {
            let value = reader.read_u8();
            result.push(value);
        }

        Ok(result)
    }

    pub fn read_bool_array(&self, offset: usize, elements_count: usize) -> LiResult<Vec<IValue>> {
        let reader = self.sequential_reader(offset, elements_count)?;
        let mut result = Vec::with_capacity(elements_count);

        for _ in 0..elements_count {
            let value = reader.read_u8();
            result.push(IValue::Boolean(value != 0));
        }

        Ok(result)
    }

    read_array_ty!(read_u8_array, u8, U8);
    read_array_ty!(read_s8_array, i8, S8);
    read_array_ty!(read_u16_array, u16, U16);
    read_array_ty!(read_s16_array, i16, S16);
    read_array_ty!(read_u32_array, u32, U32);
    read_array_ty!(read_s32_array, i32, S32);
    read_array_ty!(read_i32_array, i32, I32);
    read_array_ty!(read_f32_array, f32, F32);
    read_array_ty!(read_u64_array, u64, U64);
    read_array_ty!(read_s64_array, i64, S64);
    read_array_ty!(read_i64_array, i64, I64);
    read_array_ty!(read_f64_array, f64, F64);
}