diff --git a/cce.c b/cce.c index 27d2047e6e70afeebb7e314903d35bb6665e9a72..737687b70a8bc3eb41f8f700ae05cff902a93bc9 100644 --- a/cce.c +++ b/cce.c @@ -4,46 +4,48 @@ II - Derrière certaines matrices génératrices... */ // Q3 -char* encode (char *msg) +char *encode(char *msg) { int i, j = 0; int lenmsg = strlen(msg); - char* encoded = malloc(sizeof(char) * ((lenmsg * 2) + 1)); + char *encoded = malloc(sizeof(char) * ((lenmsg * 2) + 1)); - for (i = 0; i < lenmsg; i+=4, j+=8) { + for (i = 0; i < lenmsg; i += 4, j += 8) + { encoded[j] = msg[i]; - encoded[j+1] = msg[i+1]; - encoded[j+2] = msg[i+2]; - encoded[j+3] = msg[i+3]; + encoded[j + 1] = msg[i + 1]; + encoded[j + 2] = msg[i + 2]; + encoded[j + 3] = msg[i + 3]; - encoded[j+4] = '0' + ((msg[i] - '0') ^ (msg[i+1] - '0')); - encoded[j+5] = '0' + ((msg[i+1] - '0') ^ (msg[i+2] - '0')); - encoded[j+6] = '0' + ((msg[i] - '0') ^ (msg[i+2] - '0') ^ (msg[i+3] - '0')); - encoded[j+7] = '0' + ((msg[i] - '0') ^ (msg[i+3] - '0')); + encoded[j + 4] = '0' + ((msg[i] - '0') ^ (msg[i + 1] - '0')); + encoded[j + 5] = '0' + ((msg[i + 1] - '0') ^ (msg[i + 2] - '0')); + encoded[j + 6] = '0' + ((msg[i] - '0') ^ (msg[i + 2] - '0') ^ (msg[i + 3] - '0')); + encoded[j + 7] = '0' + ((msg[i] - '0') ^ (msg[i + 3] - '0')); } encoded[j] = '\0'; return encoded; } -char* decode (char* codeword) +char *decode(char *codeword) { int i, j = 0; int lenmsg = strlen(codeword); - - char* decoded = malloc(sizeof(char) * ((lenmsg / 2) + 1)); - for (i = 0; i < lenmsg; i+=8, j+=4) { - decoded[j] = '0' + ((codeword[i] - '0') ^ (codeword[i+1] - '0') ^ (codeword[i+4] - '0')); - decoded[j+1] = '0' + ((codeword[i+1] - '0') ^ (codeword[i+2] - '0') ^ (codeword[i+5] - '0')); - decoded[j+2] = '0' + ((codeword[i] - '0') ^ (codeword[i+2] - '0') ^ (codeword[i+3] - '0') ^ (codeword[i+6] - '0')); - decoded[j+3] = '0' + ((codeword[i] - '0') ^ (codeword[i+3] - '0') ^ (codeword[i+7] - '0')); + + char *decoded = malloc(sizeof(char) * ((lenmsg / 2) + 1)); + for (i = 0; i < lenmsg; i += 8, j += 4) + { + decoded[j] = '0' + ((codeword[i] - '0') ^ (codeword[i + 1] - '0') ^ (codeword[i + 4] - '0')); + decoded[j + 1] = '0' + ((codeword[i + 1] - '0') ^ (codeword[i + 2] - '0') ^ (codeword[i + 5] - '0')); + decoded[j + 2] = '0' + ((codeword[i] - '0') ^ (codeword[i + 2] - '0') ^ (codeword[i + 3] - '0') ^ (codeword[i + 6] - '0')); + decoded[j + 3] = '0' + ((codeword[i] - '0') ^ (codeword[i + 3] - '0') ^ (codeword[i + 7] - '0')); } decoded[j] = '\0'; return decoded; } // Q4 -int hamming_distance (char* codeword1, char* codeword2) +int hamming_distance(char *codeword1, char *codeword2) { int distance; // TODO : La distance est le nombre de bits différents entre deux codewords @@ -54,9 +56,9 @@ int hamming_distance (char* codeword1, char* codeword2) IV - Restaurez la gloire matricielle de P */ // Q1 -char* polynomial_encode (char* msg) +char *polynomial_encode(char *msg) { - char* encoded; + char *encoded; // TODO : Rajouter des 0 à la fin du msg pour qu'il soit de longueur 8 // TODO : Convertir le polynôme générateur en suite de bits (x^4 + x + 1 <=> 10011) // TODO : msg mod polynome générateur (10011) @@ -64,9 +66,9 @@ char* polynomial_encode (char* msg) return encoded; } -char* polynomial_decode (char* codeword) +char *polynomial_decode(char *codeword) { - char* decoded; + char *decoded; // TODO : Diviser le message en chunks de 8 bits // TODO : codeword mod polynome générateur (10011) // TODO : Refaire cette opération pour chaque chunk et concaténer le tout @@ -74,20 +76,19 @@ char* polynomial_decode (char* codeword) } // Q2 -char* set_error (char* codeword, int i) +char *set_error(char *codeword, int i) { - char* false_codeword; + char *false_codeword; // TODO : Inverser le bit à la position i du codeword return false_codeword; } // Q5 -char* error_correction (char* codeword) +char *error_correction(char *codeword) { - char* decoded; + char *decoded; // TODO : Décoder le codeword // TODO : Si le syndrome est différent de 0, repérer la postion de l'erreur en fonction du syndrome // TODO : Inverser le bit à la position de l'erreur return decoded; } - diff --git a/main.c b/main.c index 8cec8a3dc63afed49d41fe3e99d807306f492ead..5c5b52ef6dba7a643c7c482ac7b01f76b0ce161b 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ int main(void) printf("-- Q3 --\n\n"); printf("Message : %s\nMessage encodé : %s\n\n", "1001", encode("11100111")); - printf("Message encodé (sans erreur) : %s\nSyndrome : %s\n\n", "10101101", decode("10101101")); // Le syndrome doit être 0000 + printf("Message encodé (sans erreur) : %s\nSyndrome : %s\n\n", "10101101", decode("10101101")); // Le syndrome doit être 0000 printf("Message encodé (avec une erreur) : %s\nSyndrome : %s\n\n", "10100101", decode("10100101")); // Le syndrome doit être 1000 printf("-- Q4 --\n\n");