Skip to content

Commit

Permalink
[WIP] First part of the CRC, currently not working
Browse files Browse the repository at this point in the history
  • Loading branch information
S0S4 committed Sep 18, 2024
1 parent 44478e4 commit 8ec0e49
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
Binary file added client/client
Binary file not shown.
3 changes: 1 addition & 2 deletions client/include/crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdint.h>
#include <stdlib.h>

#include "fraction.h"
extern const uint32_t crc32_tab[];
/*
* A function that calculates the CRC-32 based on the table above is
Expand All @@ -12,5 +12,4 @@ extern const uint32_t crc32_tab[];
* in sys/libkern.h, where it can be inlined.
*/
uint32_t crc32(const void *buf, size_t size);

#endif // CRC32_H
3 changes: 2 additions & 1 deletion client/include/fraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ typedef struct {
} fraction_t;

int download_fraction(int sfd, char *url, fraction_t *fraction);
int fraction_parse(char *data, size_t size, fraction_t *fraction);
int fraction_parse(char *data, size_t size, fraction_t *fraction);
int check_magic(uint32_t data);
void print_fraction(fraction_t fraction);
void fraction_free(fraction_t *fraction);
int compare_fractions(const void* a, const void* b);
void check_fractions(fraction_t *fraction, size_t size);
#endif
2 changes: 2 additions & 0 deletions client/src/crc32.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../include/crc32.h"
#include "../include/fraction.h"

const uint32_t crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
Expand Down Expand Up @@ -54,3 +55,4 @@ uint32_t crc32(const void *buf, size_t size) {
crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
return crc ^ ~0U;
}

45 changes: 44 additions & 1 deletion client/src/fraction.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../include/fraction.h"
#include <stdint.h>

#include "../include/crc32.h"

// Change the return type to int to indicate success or failure
int download_fraction(int sfd, char *url, fraction_t *fraction) {
Expand Down Expand Up @@ -109,9 +109,52 @@ void print_fraction(fraction_t fraction) {
printf("\n\n");
}

uint32_t calc_crc(fraction_t *frac){

uint8_t buffer[sizeof(frac->magic) + sizeof(frac->index) + sizeof(frac->iv) + strlen(frac->data)];
size_t offset = 0;

memcpy(buffer + offset, &frac->magic, sizeof(frac->magic));
offset += sizeof(frac->magic);

memcpy(buffer + offset, &frac->index, sizeof(frac->index));
offset += sizeof(frac->index);

memcpy(buffer + offset, frac->iv, sizeof(frac->iv));
offset += sizeof(frac->iv);

memcpy(buffer + offset, frac->data, strlen(frac->data));
offset += strlen(frac->data);

uint32_t calculated_crc = crc32(buffer, offset);

if (calculated_crc == frac->crc) {
printf("Checksum correcto\n");
} else {
printf("Checksum incorrecto\n");
printf("Checksum generado: %08X\n", calculated_crc);
printf("Checksum que deberia ser: %08X\n", frac->crc);
}

return calculated_crc == frac->crc;
}

void check_fractions(fraction_t *fraction, size_t size){

for(size_t i = 0; i < size; i++){
if(calc_crc(&fraction[i])){
puts("checksum is correct");
} else{
puts("Checksum incorrect");
}
}
}

void fraction_free(fraction_t *fraction) {
free(fraction->data);
fraction->magic = 0;
fraction->index = 0;
fraction->crc = 0;
}


9 changes: 8 additions & 1 deletion client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(void) {
struct addrinfo hints, *ainfo;
int sfd; // socket file descriptor
http_res_t http_fraction_res, http_post_res;

size_t size = 0;
/* Setup socket and initiate connection with the server */
setup_hints(&hints);

Expand Down Expand Up @@ -76,6 +76,7 @@ int main(void) {
qsort(fractions, lines_read, sizeof(fraction_t), compare_fractions);
for (int i = 0; i < lines_read; i++) {
print_fraction(fractions[i]);
size++;
}

/* Notify the server that we successfully downloaded the fractions */
Expand All @@ -88,6 +89,9 @@ int main(void) {
goto cleanup_socket;
}

check_fractions(fractions,size);


/* Cleanup */
http_free(&http_fraction_res);
http_free(&http_post_res);
Expand All @@ -103,6 +107,9 @@ int main(void) {
close(sfd);
return EXIT_SUCCESS;




cleanup_socket:
close(sfd);
return EXIT_FAILURE;
Expand Down

0 comments on commit 8ec0e49

Please sign in to comment.