This commit is contained in:
Rens Pastoor
2025-06-12 11:20:08 +02:00
parent 37013ec1fc
commit 1086760c4a
21 changed files with 444 additions and 256 deletions

View File

@@ -1,9 +1,46 @@
#include "channel.h"
// add here includes, if needed
#include <stdio.h>
#include <stdbool.h>
int channel_main(int argc, char* argv[])
{
//add your code here
bool running = true;
FILE *input_file = fopen(argv[2], "rb");
if (input_file == NULL) {
fprintf(stderr, "Error opening input file: %s\n", argv[2]);
return 1;
}
FILE *output_file = fopen(argv[3], "wb");
if (output_file == NULL) {
fprintf(stderr, "Error opening output file: %s\n", argv[3]);
fclose(input_file);
return 1;
}
while (running){
// Read data
unsigned char buffer[1024];
size_t bytes_read = fread(buffer, 1, sizeof(buffer), input_file);
if (bytes_read == 0) {
if (feof(input_file)) {
running = false; // eof reached
} else {
fprintf(stderr, "Error reading from input file\n");
fclose(input_file);
fclose(output_file);
return 1;
}
}
// Channel data
unsigned char processed_data[1024];
size_t processed_size = process_channel(buffer, bytes_read, processed_data);
// Write data
fwrite(processed_data, 1, processed_size, output_file);
}
fclose(input_file);
fclose(output_file);
return 0;
}

View File

@@ -1,10 +1,51 @@
#include "decode.h"
// add here includes, if needed
#include <stdio.h>
#include <stdbool.h>
int decode_main(int argc, char* argv[])
{
//add your code here
bool decoding = true;
FILE *input_file = fopen(argv[2], "rb");
if (input_file == NULL) {
fprintf(stderr, "Error opening input file: %s\n", argv[2]);
return 1;
}
FILE *output_file = fopen(argv[3], "wb");
if (output_file == NULL) {
fprintf(stderr, "Error opening output file: %s\n", argv[3]);
fclose(input_file);
return 1;
}
while (decoding)
{
// Read data
unsigned char data[1024];
unsigned char buffer_High[sizeof(data)/2];
unsigned char buffer_Low[sizeof(data)/2];
size_t bytes_read = fread(&data, 1, sizeof(data), input_file);
for (size_t i = 0; i < bytes_read; i++) {
buffer_High[i] = data[i + (sizeof(data)/2)];
buffer_Low[i] = data[i];
}
if (bytes_read == 0) {
if (feof(input_file)) {
decoding = false; // eof reached
} else {
fprintf(stderr, "Error reading from input file\n");
fclose(input_file);
fclose(output_file);
return 1;
}
}
// Decode data
unsigned char decoded_data[sizeof(data)];
decoded_data[sizeof(data)] = decode_combine_nibbles(*buffer_High, *buffer_Low);
// Write data
fwrite(decoded_data, 1, bytes_read, output_file);
}
return 0;
}

View File

@@ -1,10 +1,46 @@
#include "encode.h"
// add here includes, if needed
#include <stdbool.h>
#include <stdio.h>
int encode_main(int argc, char* argv[])
{
//add your code here
bool encoding = true;
FILE *input_file = fopen(argv[2], "rb");
if (input_file == NULL) {
fprintf(stderr, "Error opening input file: %s\n", argv[2]);
return 1;
}
FILE *output_file = fopen(argv[3], "wb");
if (output_file == NULL) {
fprintf(stderr, "Error opening output file: %s\n", argv[3]);
fclose(input_file);
return 1;
}
while (encoding)
{
// Read data
unsigned char buffer[512];
size_t bytes_read = fread(buffer, 1, sizeof(buffer), input_file);
if (bytes_read == 0) {
if (feof(input_file)) {
encoding = false; // eof reached
} else {
fprintf(stderr, "Error reading from input file\n");
fclose(input_file);
fclose(output_file);
return 1;
}
}
// Encode data
unsigned char encoded_data_high[sizeof(buffer)];
unsigned char encoded_data_low[sizeof(buffer)];
encode_value(buffer, encoded_data_high, encoded_data_low);
// Write data
fwrite(encoded_data_high, 1, bytes_read, output_file);
fwrite(encoded_data_low, 1, bytes_read, output_file);
}
return 0;
}

View File

@@ -23,7 +23,7 @@ void decode_byte(uint8_t in, uint8_t* nibble){
// Check parity bits
uint8_t error = 0;
if ((p0 ^ d0 ^ d1 ^ d2) != 0) error |= 0x01; // Error in p0 group
if ((p1 ^ d0 ^ d1 ^ d3) != 0) error |= 0x02; // Error in p1 group
if ((p1 ^ d0 ^ d1 ^ d3) != 0) error |= 0x02; // Error in p1 group // 0000111
if ((p2 ^ d0 ^ d2 ^ d3) != 0) error |= 0x04; // Error in p2 group
if (MSB == 0 && error != 0) {