2024-04-14 19:13:15 +03:00
|
|
|
|
use std::collections::HashMap;
|
2024-08-09 23:52:53 +03:00
|
|
|
|
use std::sync::LazyLock;
|
2024-02-06 22:35:52 +03:00
|
|
|
|
|
2024-04-19 00:30:51 +03:00
|
|
|
|
use crate::{
|
|
|
|
|
controller::view_utils::{hamming_code_input_utils::*, input_utils::*},
|
2024-08-09 23:52:53 +03:00
|
|
|
|
model::{models::*, Result},
|
2024-04-19 00:30:51 +03:00
|
|
|
|
};
|
2024-03-10 11:35:02 +03:00
|
|
|
|
|
2024-08-09 23:36:27 +03:00
|
|
|
|
static SYNDROMES: LazyLock<HashMap<usize, (bool, bool, bool)>> = LazyLock::new(|| {
|
|
|
|
|
HashMap::from([
|
|
|
|
|
(1, (false, true, true)),
|
|
|
|
|
(2, (false, false, true)),
|
|
|
|
|
(3, (true, false, true)),
|
|
|
|
|
(4, (false, true, false)),
|
|
|
|
|
(5, (true, true, false)),
|
|
|
|
|
(6, (true, false, false)),
|
|
|
|
|
(7, (false, false, false)),
|
|
|
|
|
])
|
|
|
|
|
});
|
2024-03-10 11:35:02 +03:00
|
|
|
|
|
2024-08-10 00:10:43 +03:00
|
|
|
|
pub fn hamming(raw_input: String, mode: HammingMode) -> Result<String> {
|
2024-04-14 19:13:15 +03:00
|
|
|
|
let length_of_code: usize = mode.clone() as usize;
|
2024-03-10 11:35:02 +03:00
|
|
|
|
|
2024-04-14 19:13:15 +03:00
|
|
|
|
let prepared_input: String = processing_input(&raw_input);
|
2024-03-10 11:35:02 +03:00
|
|
|
|
|
2024-08-10 00:10:43 +03:00
|
|
|
|
let (first_condition, second_condition): (bool, bool) =
|
2024-04-19 00:30:51 +03:00
|
|
|
|
check_correct_binary_code(&raw_input, &prepared_input, length_of_code);
|
2024-03-10 11:35:02 +03:00
|
|
|
|
|
2024-08-10 00:10:43 +03:00
|
|
|
|
if raw_input.is_empty() {
|
|
|
|
|
Err("Введите код.".into())
|
|
|
|
|
} else if !first_condition || !second_condition {
|
|
|
|
|
Err("Проверьте корректность ввода.".into())
|
2024-03-10 11:54:32 +03:00
|
|
|
|
} else {
|
|
|
|
|
let prepared_data: Vec<u8> = from_string_to_vec_bits(prepared_input);
|
2024-03-10 11:35:02 +03:00
|
|
|
|
|
2024-03-10 11:54:32 +03:00
|
|
|
|
match mode {
|
2024-08-10 00:10:43 +03:00
|
|
|
|
HammingMode::Encrypt => Ok(hamming_encrypt_data(&prepared_data, length_of_code)),
|
|
|
|
|
HammingMode::Decrypt => Ok(hamming_decrypt_data(&prepared_data, length_of_code)),
|
2024-03-10 11:35:02 +03:00
|
|
|
|
}
|
2024-03-10 11:54:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-06 22:35:52 +03:00
|
|
|
|
|
2024-04-14 19:13:15 +03:00
|
|
|
|
pub fn hamming_encrypt_data(data: &Vec<u8>, result_string: &mut String, length_of_code: usize) {
|
|
|
|
|
let mut i: usize = length_of_code;
|
2024-02-06 22:35:52 +03:00
|
|
|
|
|
2024-04-14 19:13:15 +03:00
|
|
|
|
while i <= data.len() {
|
2024-04-19 00:30:51 +03:00
|
|
|
|
let data_bits: &[u8] = &data[i - length_of_code..i];
|
2024-03-10 11:54:32 +03:00
|
|
|
|
let (check_bit_1, check_bit_2, check_bit_3) = (
|
|
|
|
|
data_bits[0] ^ data_bits[1] ^ data_bits[3],
|
|
|
|
|
data_bits[0] ^ data_bits[2] ^ data_bits[3],
|
2024-04-14 19:13:15 +03:00
|
|
|
|
data_bits[1] ^ data_bits[2] ^ data_bits[3],
|
2024-02-06 22:35:52 +03:00
|
|
|
|
);
|
2024-04-14 19:13:15 +03:00
|
|
|
|
result_string.push_str(&*format!(
|
|
|
|
|
"{check_bit_1}{}{check_bit_2}{}{check_bit_3}{}{} ",
|
|
|
|
|
data_bits[0], data_bits[1], data_bits[2], data_bits[3]
|
|
|
|
|
));
|
2024-03-10 11:54:32 +03:00
|
|
|
|
i += length_of_code;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 19:13:15 +03:00
|
|
|
|
pub fn hamming_decrypt_data(data: &Vec<u8>, result_string: &mut String, length_of_code: usize) {
|
|
|
|
|
let mut general_length: usize = length_of_code;
|
2024-03-10 11:54:32 +03:00
|
|
|
|
|
|
|
|
|
|
2024-04-14 19:13:15 +03:00
|
|
|
|
let mut errors: String = String::new();
|
2024-03-10 11:54:32 +03:00
|
|
|
|
|
2024-04-14 19:13:15 +03:00
|
|
|
|
while general_length <= data.len() {
|
|
|
|
|
let data_bits = &data[general_length - length_of_code..general_length];
|
2024-03-10 11:54:32 +03:00
|
|
|
|
|
2024-04-14 19:13:15 +03:00
|
|
|
|
let checked_bits: (bool, bool, bool) = (
|
|
|
|
|
(data_bits[1] ^ data_bits[3] ^ data_bits[6]) == data_bits[0],
|
|
|
|
|
(data_bits[1] ^ data_bits[5] ^ data_bits[6]) == data_bits[2],
|
|
|
|
|
(data_bits[3] ^ data_bits[5] ^ data_bits[6]) == data_bits[4],
|
|
|
|
|
);
|
2024-02-06 22:35:52 +03:00
|
|
|
|
|
2024-04-16 20:45:31 +03:00
|
|
|
|
if let (true, true, true) = checked_bits {
|
|
|
|
|
general_length += length_of_code;
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
2024-04-19 00:30:51 +03:00
|
|
|
|
let error_position: &usize = syndromes
|
2024-04-16 20:45:31 +03:00
|
|
|
|
.iter()
|
|
|
|
|
.find(move |&(&_error_position, &error)| error == checked_bits)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.0;
|
|
|
|
|
|
2024-07-29 00:19:15 +03:00
|
|
|
|
let uncorrect_code: String = from_vec_bits_to_string(data_bits);
|
2024-04-19 00:30:51 +03:00
|
|
|
|
let correct_code: String = from_vec_bits_to_string(
|
|
|
|
|
data_bits
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.map(|(index, &bit)| {
|
|
|
|
|
if index == error_position - 1 {
|
|
|
|
|
(bit == 0u8).into()
|
|
|
|
|
} else {
|
|
|
|
|
bit
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<u8>>()
|
|
|
|
|
.as_slice(),
|
|
|
|
|
);
|
2024-04-16 20:45:31 +03:00
|
|
|
|
|
|
|
|
|
let error = format!(
|
2024-04-19 00:30:51 +03:00
|
|
|
|
"Ошибка в коде {} [{uncorrect_code}], позиция ошибки {}, корректный код: [{correct_code}]; \n",
|
2024-04-16 20:45:31 +03:00
|
|
|
|
general_length / 7,
|
2024-04-19 00:30:51 +03:00
|
|
|
|
error_position
|
2024-04-16 20:45:31 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
errors.push_str(error.as_str());
|
|
|
|
|
|
|
|
|
|
general_length += length_of_code;
|
2024-02-06 22:35:52 +03:00
|
|
|
|
}
|
2024-03-10 11:54:32 +03:00
|
|
|
|
}
|
2024-02-06 22:35:52 +03:00
|
|
|
|
|
2024-07-21 23:02:16 +03:00
|
|
|
|
if !data.is_empty() {
|
|
|
|
|
if errors.is_empty() {
|
2024-04-16 20:45:31 +03:00
|
|
|
|
result_string.push_str("Все коды корректны.");
|
|
|
|
|
} else {
|
|
|
|
|
result_string.push_str(errors.as_str());
|
|
|
|
|
}
|
2024-03-10 11:54:32 +03:00
|
|
|
|
} else {
|
2024-04-16 20:45:31 +03:00
|
|
|
|
result_string.push_str("Введите код для проверки.")
|
2024-02-06 22:35:52 +03:00
|
|
|
|
}
|
2024-04-14 19:13:15 +03:00
|
|
|
|
}
|