From 3d73bab4fe014ea4c1348b3893961abbeadf7d16 Mon Sep 17 00:00:00 2001
From: Valentin Decat <valentin.decat@student.uclouvain.be>
Date: Wed, 27 Apr 2022 09:00:40 +0000
Subject: [PATCH] Update main.c

---
 main.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 73 insertions(+), 2 deletions(-)

diff --git a/main.c b/main.c
index db0c30e..f30a8ee 100644
--- a/main.c
+++ b/main.c
@@ -9,9 +9,11 @@
 #include <inttypes.h>
 #include <stdbool.h>
 #include "headers/tinymt32.h"
+//includes ajoutés
+#include <math.h>
 
 typedef struct
-{ // Comment
+{
     DIR *input_dir;
     char input_dir_path[PATH_MAX];
     FILE *output_stream;
@@ -138,8 +140,77 @@ int main(int argc, char *argv[])
             fprintf(stderr, "Successfully opened the file %s\n", full_path);
         }
 
+        //---------------------------------------------------------------------------------------------------------
         // TODO: parse the input binary file, decode the encoded message with RLC and write the output in the output stream following the statement
         
+        // BEAUCOUP de choses à corriger !
+
+
+        message_t* mymessage = ???;
+        get_file_infos(binary_data[:24], message); //erreur sur le [:24] fread(24)
+        if (verbose){
+            printf("Seed : %i , block_size : %i , word_size : %i, redundancy : %i", seed, block_size, word_size, redundancy)
+        }
+        binary_data = binary_data[24:]; //erreur sur le [:24] et type de binary_data
+        // Génère les coefficients
+        coeffs = gen_coeffs(seed, nss, nrs); //que vaut nrs et nss?
+        if (verbose){
+            print_coeffs();
+            printf("%i", binary_data);
+        }
+        int step = word_size * (block_size + redundancy);  // Taille de bloc en bytes 
+        size_t length = strlen(binary_data);
+        // Ecrit la taille du titre et du contenu du message
+        // Ecrit le nom du fichier dans l'output
+        if (output_fd == sys.stdout || output_fd == sys.stderr){ //sys.stdout et sys.stderr?
+            printf("%i ", strlen(filename));
+            printf("%i ", message_size);
+            printf("%s ", filename); //on fait quoi de file=output_fd?
+        }
+        else{
+            output_fd.write(len(filename).to_bytes(4, "big"))   //Pas fait
+            output_fd.write(message_size.to_bytes(8, "big"))    //Pas fait
+            output_fd.write(filename.encode("ASCII"))           //Pas fait
+        }
+
+        // Contient le nombre de blocs complets (sans le dernier bloc s'il n'est pas complet)
+        uint8_t nb_blocks = ceil(length / step); //ceil calcule la division et return le nombre supérieur si le resultat n'est pas entier.
+        uint8_t contains_uncomplete_block = 0;
+        if (message_size != nb_blocks * block_size * word_size){  // Dernier bloc non complet (i.e., moins de block_size symboles)
+            nb_blocks -= 1
+            contains_uncomplete_block = 1;
+        }
+        int readed = 0  // Nombre de bytes lus jusqu'à présent
+        uint8_t** current_block; //Il faut intialiser current_block
+        uint8_t** response; //Il faut intialiser response
+        for (int i = 0; i < nb_blocks; i++){
+            current_block = make_block(binary_data[i * step:(i + 1) * step], block_size); //changer [i * step:(i + 1) * step]
+            response = process_block(current_block, block_size);
+            if (verbose){
+                printf("%i "current_block); //on peut pas print uint8_t **
+                printf("%c "block_to_string(response, block_size));
+            }
+
+            write_block(output_fd, response, block_size, word_size); //output_fd ??
+            readed += step;
+        }
+        int readed_symbols = block_size * word_size * nb_blocks;
+        int nb_remaining_symbols = ((int) strlen(binary_data[readed:]) / word_size) - redundancy; //[readed:]
+        if (contains_uncomplete_block){
+            uint8_t** last_block = make_block(binary_data[readed:], nb_remaining_symbols); //[readed:]
+            uint8_t** decoded = process_block(last_block, nb_remaining_symbols);
+            // Le dernier symbole peut ne pas etre complet et contenir du padding
+            // => on utilise la taille totale du fichier pour retirer le padding
+            int padding = readed_symbols + nb_remaining_symbols * word_size - message_size;
+            int true_length_last_symbol = word_size - padding;
+            if (verbose){
+                printf("%i", last_block); //on peut pas print uint8_t **
+            }
+            write_last_block(output_fd, decoded, nb_remaining_symbols, word_size, true_length_last_symbol); //Pas fait
+        }
+        
+        //-----------------------------------------------------------------------------------------------------------
+        
         // You may modify or delete the following lines. This is just an example of how to use tinymt32
         uint32_t seed = 42; // Replace with the seed from the instance file!
         
@@ -187,4 +258,4 @@ file_read_error:
         fclose(args.output_stream);
     }
     exit(EXIT_FAILURE);
-}
\ No newline at end of file
+}
-- 
GitLab