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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * MIT License
 *
 * Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

use core::mem;
use core::str;

use crate::encoding::variable_byte_integer::VariableByteIntegerDecoder;
use crate::utils::types::{BinaryData, BufferError, EncodedString, StringPair};

/// Buff reader is reading corresponding types from buffer (Byte array) and stores current position
/// (later as cursor)
pub struct BuffReader<'a> {
    buffer: &'a [u8],
    pub position: usize,
    len: usize,
}

impl<'a> BuffReader<'a> {
    pub fn increment_position(&mut self, increment: usize) {
        self.position += increment;
    }

    pub fn new(buffer: &'a [u8], buff_len: usize) -> Self {
        Self {
            buffer,
            position: 0,
            len: buff_len,
        }
    }

    /// Variable byte integer can be 1-4 Bytes long. Buffer reader takes all 4 Bytes at first and
    /// than check what is true length of varbyteint and increment cursor by that
    pub fn read_variable_byte_int(&mut self) -> Result<u32, BufferError> {
        let mut variable_byte_integer: [u8; 4] = [0; 4];
        let mut len: usize = 1;

        // Everytime checking first bit of Byte which determines whenever there is continuous Byte
        let mut x = 0;
        loop {
            if x >= 4 {
                break;
            }
            if self.position + x >= self.len {
                return Err(BufferError::InsufficientBufferSize);
            }
            if self.buffer[self.position + x] & 0x80 != 0 {
                variable_byte_integer[x] = self.buffer[self.position + x];
                len += 1
            } else {
                variable_byte_integer[x] = self.buffer[self.position + x];
                x += 1;
                if x != 4 {
                    loop {
                        variable_byte_integer[x] = 0;
                        x += 1;
                        if x == 4 {
                            break;
                        }
                    }
                    break;
                }
            }
            x += 1;
        }
        self.increment_position(len);
        VariableByteIntegerDecoder::decode(variable_byte_integer)
    }

    /// Reading u32 from buffer as `Big endian`
    pub fn read_u32(&mut self) -> Result<u32, BufferError> {
        if self.position + 4 > self.len {
            return Err(BufferError::InsufficientBufferSize);
        }
        let (int_bytes, _rest) = self.buffer[self.position..].split_at(mem::size_of::<u32>());
        let ret: u32 = u32::from_be_bytes(int_bytes.try_into().unwrap());
        self.increment_position(4);
        Ok(ret)
    }

    /// Reading u16 from buffer as `Big endinan`
    pub fn read_u16(&mut self) -> Result<u16, BufferError> {
        if self.position + 2 > self.len {
            return Err(BufferError::InsufficientBufferSize);
        }
        let (int_bytes, _rest) = self.buffer[self.position..].split_at(mem::size_of::<u16>());
        let ret: u16 = u16::from_be_bytes(int_bytes.try_into().unwrap());
        self.increment_position(2);
        Ok(ret)
    }

    /// Reading one byte from buffer as `Big endian`
    pub fn read_u8(&mut self) -> Result<u8, BufferError> {
        if self.position >= self.len {
            return Err(BufferError::InsufficientBufferSize);
        }
        let ret: u8 = self.buffer[self.position];
        self.increment_position(1);
        Ok(ret)
    }

    /// Reading UTF-8 encoded string from buffer
    pub fn read_string(&mut self) -> Result<EncodedString<'a>, BufferError> {
        let len = self.read_u16()? as usize;

        if self.position + len > self.len {
            return Err(BufferError::InsufficientBufferSize);
        }

        let res_str = str::from_utf8(&(self.buffer[self.position..(self.position + len)]));
        if res_str.is_err() {
            error!("Could not parse utf-8 string");
            return Err(BufferError::Utf8Error);
        }
        self.increment_position(len);
        Ok(EncodedString {
            string: res_str.unwrap(),
            len: len as u16,
        })
    }

    /// Read Binary data from buffer
    pub fn read_binary(&mut self) -> Result<BinaryData<'a>, BufferError> {
        let len = self.read_u16()?;

        if self.position + len as usize > self.len {
            return Err(BufferError::InsufficientBufferSize);
        }

        let res_bin = &(self.buffer[self.position..(self.position + len as usize)]);
        Ok(BinaryData { bin: res_bin, len })
    }

    /// Read string pair from buffer
    pub fn read_string_pair(&mut self) -> Result<StringPair<'a>, BufferError> {
        let name = self.read_string()?;
        let value = self.read_string()?;
        Ok(StringPair { name, value })
    }

    /// Read payload message from buffer
    pub fn read_message(&mut self, total_len: usize) -> &'a [u8] {
        if total_len > self.len {
            return &self.buffer[self.position..self.len];
        }
        &self.buffer[self.position..total_len]
    }

    /// Peeking (without incremental internal pointer) one byte from buffer as `Big endian`
    pub fn peek_u8(&self) -> Result<u8, BufferError> {
        if self.position >= self.len {
            return Err(BufferError::InsufficientBufferSize);
        }
        Ok(self.buffer[self.position])
    }
}