Напишите функции encode и decode кодирующие и декодирующие данные таким образом, чтобы случайный один (или ноль) битфлип не мог помешать их восстановить
void encode(const void *data, void *encoded, size_t n) кодирует n байт по указателю data в указатель encoded
void decode(const void *encoded, void *data, size_t n) декодирует n байт из указателя encoded в указатель data (n - размер изначальных данных до кодирования)
Гарантируется что массив data содержит ровно n байт
Гарантируется что массив encoded содержит ровно 2n байт
Рекомендуется использовать Код Хэмминга
В задаче присутствуют тесты на производительность. Ваш код должен достаточно быстро работать как на коротких так и на длинных входных данных.
В данной задаче можно получить частичные баллы
Submit a solution
Language: gcc - GNU C 11.3.0
Assistant
Certainly! Below is a C implementation of the encode and decode functions using the Hamming (12,8) code. This implementation maps each byte of input data to two bytes of encoded data, allowing for single-bit error correction.
Explanation
Hamming (12,8) Code:
Data Bits: 8 bits from the original byte.
Parity Bits: 4 bits added to enable single-bit error correction.
Total: 12 bits per byte, which are stored in 2 bytes (16 bits) with 4 unused bits.
Here's how you can use the encode and decode functions:
c
#include <stdio.h>
#include <string.h>
// Prototype declarations
void encode(const void *data, void *encoded, size_t n);
void decode(const void *encoded, void *data, size_t n);
int main() {
// Original data
uint8_t original_data[] = "Hello, World!";
size_t n = sizeof(original_data) - 1; // Exclude null terminator
// Encoded data (2n bytes)
uint8_t encoded_data[2 * n];
memset(encoded_data, 0, sizeof(encoded_data));
// Decode buffer
uint8_t decoded_data[n + 1];
memset(decoded_data, 0, sizeof(decoded_data));
// Encode
encode(original_data, encoded_data, n);
// Introduce a single-bit error for testing
// For example, flip the first bit of the first encoded byte
encoded_data[0] ^= 0x01;
// Decode
decode(encoded_data, decoded_data, n);
decoded_data[n] = '\0'; // Null-terminate the string
// Output the results
printf("Original Data: %s\n", original_data);
printf("Decoded Data : %s\n", decoded_data);
return 0;
}
Explanation of the Usage Example
Original Data: The string "Hello, World!" is used as the input data.
Encoding: The encode function transforms the original data into encoded data with redundancy.
Error Introduction: A single-bit error is introduced by flipping the first bit of the first encoded byte.
Decoding: The decode function corrects the single-bit error and recovers the original data.
Output: Both the original and decoded data are printed to verify correctness.
Notes
Performance Optimization: This implementation processes data byte-by-byte, which should be efficient for most applications. However, further optimizations can be made using bitwise operations and lookup tables if needed.
Error Handling: The current implementation assumes that there is at most one bit error in the entire encoded data. If more errors exist, the behavior is undefined.
This implementation meets the requirements by ensuring that any single-bit error in the encoded data can be corrected during decoding, thus preserving the integrity of the original data.