rust_mqtt/encoding/variable_byte_integer.rs
1/*
2 * MIT License
3 *
4 * Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25use crate::utils::types::BufferError;
26
27/// VariableByteIntegerEncoder and VariableByteIntegerDecoder are implemented based on
28/// pseudo code which is introduced in MQTT version 5.0 OASIS standard accesible from
29/// https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901107
30
31/// Variable byte integer encoder structure is help structure which implements function used to
32/// encode integer into MQTT variable byte integer format. This format is mainly used to encode
33/// lenghts stored in a packet.
34pub struct VariableByteIntegerEncoder;
35
36/// Variable byte integers error enumeration is used by both encoder and decoder for
37/// error notification.
38
39pub type VariableByteInteger = [u8; 4];
40
41impl VariableByteIntegerEncoder {
42 /// Encode function takes as parameter integer as u32 type and encodes
43 /// this integer into maximal 4 Bytes. MSb of each Byte is controll bit.
44 /// This bit is saying if there is continuing Byte in stream or not, this way
45 /// we can effectively use 1 to 4 Bytes based in integer len.
46 pub fn encode(mut target: u32) -> Result<VariableByteInteger, BufferError> {
47 // General known informations from OASIS
48 const MAX_ENCODABLE: u32 = 268435455;
49 const MOD: u32 = 128;
50 if target > MAX_ENCODABLE {
51 error!("Maximal value of integer for encoding was exceeded");
52 return Err(BufferError::EncodingError);
53 }
54
55 let mut res: [u8; 4] = [0; 4];
56 let mut encoded_byte: u8;
57 let mut i: usize = 0;
58
59 loop {
60 encoded_byte = (target % MOD) as u8;
61 target /= 128;
62 if target > 0 {
63 encoded_byte |= 128;
64 }
65 res[i] = encoded_byte;
66 i += 1;
67 if target == 0 {
68 break;
69 }
70 }
71 Ok(res)
72 }
73
74 pub fn len(var_int: VariableByteInteger) -> usize {
75 let mut i: usize = 0;
76 loop {
77 let encoded_byte = var_int[i];
78 i += 1;
79 if (encoded_byte & 128) == 0 {
80 break;
81 }
82 }
83 i
84 }
85}
86
87/// Variable byte integer decoder structure is help structure which implements function used to
88/// decode message lenghts in MQTT packet and other parts encoded into variable byte integer.
89pub struct VariableByteIntegerDecoder;
90
91impl VariableByteIntegerDecoder {
92 /// Decode function takes as paramater encoded integer represented
93 /// as array of 4 unsigned numbers of exactly 1 Byte each -> 4 Bytes maximal
94 /// same as maximal amount of bytes for variable byte encoding in MQTT.
95 pub fn decode(encoded: VariableByteInteger) -> Result<u32, BufferError> {
96 let mut multiplier: u32 = 1;
97 let mut ret: u32 = 0;
98
99 let mut encoded_byte: u8;
100 let mut i: usize = 0;
101
102 loop {
103 encoded_byte = encoded[i];
104 i += 1;
105 ret += (encoded_byte & 127) as u32 * multiplier;
106 if multiplier > 128 * 128 * 128 {
107 return Err(BufferError::DecodingError);
108 }
109 multiplier *= 128;
110 if (encoded_byte & 128) == 0 {
111 break;
112 }
113 }
114
115 Ok(ret)
116 }
117}