1
1
#include "../include/fraction.h"
2
- #include <stdint.h>
2
+ #include "../include/crc32.h"
3
3
4
-
5
- // Change the return type to int to indicate success or failure
6
4
int download_fraction (int sfd , char * url , fraction_t * fraction ) {
7
5
char * path = NULL ;
8
6
http_res_t res ;
9
- fraction_t downloaded_fraction = {}; // Initialize to zero in case of failure
7
+ fraction_t downloaded_fraction = {};
10
8
11
9
// Parse the URL to get the path
12
10
path = get_path_from_url (url );
13
11
if (!path ) {
14
12
fprintf (stderr , "Invalid URL: %s\n" , url );
15
- return 1 ; // Return failure
13
+ return 1 ;
16
14
}
17
15
18
16
// Perform the HTTP GET request
19
17
if (http_get (sfd , path , & res ) != HTTP_SUCCESS ) {
20
18
fprintf (stderr , "Failed to download: %s\n" , url );
21
- return 1 ; // Return failure
19
+ return 1 ;
22
20
}
23
21
24
22
// Parse the downloaded data into a fraction
25
23
if (fraction_parse (res .data , res .size , & downloaded_fraction ) != 0 ) {
26
- http_free (& res ); // Free HTTP response
27
- return 1 ; // Return failure
24
+ http_free (& res );
25
+ return 1 ;
28
26
}
29
27
30
- // If the user provided a fraction pointer, copy the result
31
- if (fraction ) {
32
- * fraction = downloaded_fraction ;
33
- }
28
+ * fraction = downloaded_fraction ;
29
+
34
30
35
31
// Cleanup
36
32
http_free (& res );
37
33
38
- return 0 ; // Return success
34
+ return 0 ;
39
35
}
40
36
41
37
int fraction_parse (char * data , size_t size , fraction_t * fraction ) {
38
+ const size_t UINT32_SIZE = sizeof (uint32_t );
42
39
const size_t IV_SIZE = 16 ; // 16 bytes for the IV
43
- const size_t MAGIC_SIZE = sizeof ( uint32_t ) ;
44
- const size_t INDEX_SIZE = sizeof ( uint32_t ) ;
45
- const size_t CRC_SIZE = sizeof ( uint32_t ) ;
40
+ const size_t MAGIC_SIZE = UINT32_SIZE ;
41
+ const size_t INDEX_SIZE = UINT32_SIZE ;
42
+ const size_t CRC_SIZE = UINT32_SIZE ;
46
43
const size_t HEADER_SIZE = MAGIC_SIZE + INDEX_SIZE + IV_SIZE + CRC_SIZE ;
44
+ size_t data_size ;
47
45
48
46
// Ensure the data size is sufficient
49
47
if (size < HEADER_SIZE ) {
50
- return 1 ; // Failure: data size is too small
51
- }
48
+ fprintf (stderr , "Insufficient size: %lu\n" , size );
49
+ return 1 ;
50
+
51
+ }
52
52
53
53
// Extract fields from data buffer with endianess handling
54
54
uint32_t magic , index , crc ;
@@ -57,33 +57,27 @@ int fraction_parse(char *data, size_t size, fraction_t *fraction) {
57
57
memcpy (fraction -> iv , data + MAGIC_SIZE + INDEX_SIZE , IV_SIZE );
58
58
memcpy (& crc , data + MAGIC_SIZE + INDEX_SIZE + IV_SIZE , CRC_SIZE );
59
59
60
- // Convert from little-endian to host byte order if needed (for
61
- // little-endian systems, this is usually not required)
62
- magic = __bswap_32 (magic );
63
- index = __bswap_32 (index );
64
- crc = __bswap_32 (crc );
65
-
66
- // Set the extracted values in the fraction structure
67
- fraction -> magic = magic ;
68
- fraction -> index = index ;
69
- fraction -> crc = crc ;
70
-
71
60
// Check the magic number
72
- if (!check_magic (fraction -> magic )) {
73
- return 1 ; // Failure: magic number does not match
61
+ if (!check_magic (magic )) {
62
+ fprintf (stderr , "Wrong magic number: %02x\n" , magic );
63
+ return 1 ;
74
64
}
75
65
76
66
// Allocate memory for fraction data
77
- size_t data_size = size - HEADER_SIZE ;
67
+ data_size = size - HEADER_SIZE ;
78
68
fraction -> data = malloc (data_size );
79
69
if (!fraction -> data ) {
80
- return 1 ; // Failure: memory allocation error
70
+ fprintf (stderr , "Failed to allocate data for fraction\n" );
71
+ return 1 ;
81
72
}
82
-
83
- // Copy the remaining data
73
+ // Set the extracted values in the fraction structure
74
+ fraction -> magic = magic ;
75
+ fraction -> index = index ;
76
+ fraction -> crc = crc ;
77
+ fraction -> data_size = data_size ;
84
78
memcpy (fraction -> data , data + HEADER_SIZE , data_size );
85
79
86
- return 0 ; // Success
80
+ return 0 ;
87
81
}
88
82
89
83
int check_magic (uint32_t magic ) {
@@ -101,12 +95,52 @@ int compare_fractions(const void *a, const void *b) {
101
95
void print_fraction (fraction_t fraction ) {
102
96
printf ("Magic: 0x%08x\n" , fraction .magic );
103
97
printf ("Index: %u\n" , fraction .index );
104
- printf ("CRC: 0x%08x\n" , fraction .crc );
105
98
printf ("IV: " );
106
99
for (size_t i = 0 ; i < sizeof (fraction .iv ); i ++ ) {
107
100
printf ("%02x " , (unsigned char )fraction .iv [i ]);
108
101
}
109
- printf ("\n\n" );
102
+ printf ("\n" );
103
+ printf ("CRC-32: 0x%08x\n" , fraction .crc );
104
+ printf ("Data size: %lu\n\n" , fraction .data_size );
105
+ }
106
+
107
+ int calc_crc (fraction_t * frac ){
108
+ uint8_t buffer [sizeof (frac -> magic ) + sizeof (frac -> index ) + sizeof (frac -> iv ) + frac -> data_size ];
109
+ size_t offset = 0 ;
110
+
111
+ memcpy (buffer + offset , & frac -> magic , sizeof (frac -> magic ));
112
+ offset += sizeof (frac -> magic );
113
+
114
+ memcpy (buffer + offset , & frac -> index , sizeof (frac -> index ));
115
+ offset += sizeof (frac -> index );
116
+
117
+ memcpy (buffer + offset , frac -> iv , sizeof (frac -> iv ));
118
+ offset += sizeof (frac -> iv );
119
+
120
+ memcpy (buffer + offset , frac -> data , frac -> data_size );
121
+ offset += frac -> data_size ;
122
+
123
+ uint32_t calculated_crc = crc32 (buffer , offset );
124
+
125
+ if (calculated_crc != frac -> crc ) {
126
+ printf ("Checksum incorrect\n" );
127
+ printf ("Checksum generated: %08X\n" , calculated_crc );
128
+ printf ("Checksum from fraction: %08X\n\n" , frac -> crc );
129
+ }
130
+
131
+ return calculated_crc == frac -> crc ;
132
+ }
133
+
134
+ int check_fractions (fraction_t * fraction , size_t size ){
135
+ int res = 0 ;
136
+ for (size_t i = 0 ; i < size ; i ++ ){
137
+ if (!calc_crc (& fraction [i ])) {
138
+ fprintf (stderr , "Failed to validate integrity of fraction:\n" );
139
+ print_fraction (fraction [i ]);
140
+ res += 1 ;
141
+ }
142
+ }
143
+ return res ;
110
144
}
111
145
112
146
void fraction_free (fraction_t * fraction ) {
@@ -115,3 +149,5 @@ void fraction_free(fraction_t *fraction) {
115
149
fraction -> index = 0 ;
116
150
fraction -> crc = 0 ;
117
151
}
152
+
153
+
0 commit comments