rs-matter/matter/src/codec/base38.rs

81 lines
2.4 KiB
Rust
Raw Normal View History

2023-01-11 09:57:03 +01:00
/*
*
* Copyright (c) 2020-2022 Project CHIP Authors
*
* 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.
*/
2023-01-11 09:54:12 +01:00
//! Base38 encoding functions.
2023-01-11 09:57:03 +01:00
2023-01-10 21:01:02 +01:00
const BASE38_CHARS: &str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-.";
2023-01-11 09:54:12 +01:00
/// Encodes a byte array into a base38 string.
2023-01-12 15:42:21 +01:00
pub fn encode(bytes: &[u8], length: usize) -> String {
2023-01-10 21:01:02 +01:00
let mut offset = 0;
let mut result = String::new();
while offset < length {
let remaining = length - offset;
match remaining.cmp(&2) {
std::cmp::Ordering::Greater => {
result.push_str(&encode_base38(
((bytes[offset + 2] as u32) << 16)
| ((bytes[offset + 1] as u32) << 8)
| (bytes[offset] as u32),
5,
));
offset += 3;
}
std::cmp::Ordering::Equal => {
result.push_str(&encode_base38(
((bytes[offset + 1] as u32) << 8) | (bytes[offset] as u32),
4,
));
break;
}
std::cmp::Ordering::Less => {
result.push_str(&encode_base38(bytes[offset] as u32, 2));
break;
}
}
}
result
}
2023-01-10 21:25:05 +01:00
2023-01-11 09:54:12 +01:00
fn encode_base38(mut value: u32, char_count: u8) -> String {
let mut result = String::new();
2023-01-13 11:40:16 +01:00
let chars = BASE38_CHARS.chars();
2023-01-11 09:54:12 +01:00
for _ in 0..char_count {
2023-01-13 11:40:16 +01:00
let mut use_chars = chars.clone();
2023-01-11 09:54:12 +01:00
let remainder = value % 38;
2023-01-13 11:40:16 +01:00
result.push(use_chars.nth(remainder as usize).unwrap());
2023-01-11 09:54:12 +01:00
value = (value - remainder) / 38;
}
result
}
2023-01-10 21:25:05 +01:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_base38_encode() {
const ENCODED: &str = "-MOA57ZU02IT2L2BJ00";
const DECODED: [u8; 11] = [
0x88, 0xff, 0xa7, 0x91, 0x50, 0x40, 0x00, 0x47, 0x51, 0xdd, 0x02,
];
2023-01-12 15:42:21 +01:00
assert_eq!(encode(&DECODED, 11), ENCODED);
2023-01-10 21:25:05 +01:00
}
}