@@ -11,10 +11,12 @@ extern "C"{
1111#endif
1212
1313const version_t VERSION = {
14- .major = 0 , .minor = 5 , .patch = 8 ,
14+ .major = 0 , .minor = 5 , .patch = 9 ,
1515};
1616
17+ // constants related to how spiral data is packed in files - measured in bytes
1718const size_t FILE_HEADER_SIZE = 25 ;
19+ const size_t LINE_T_PACK_SIZE = 4 ;
1820
1921// vector direction constants
2022const vector_t VECTOR_DIRECTIONS [4 ] = {
@@ -287,7 +289,7 @@ load_spiral(buffer_t buffer) {
287289 spiral_size |= (buffer .bytes [16 + i ]) << (8 * (7 - i ));
288290 }
289291 // Check that the file data section is large enough for the spiral size
290- if ((buffer .size - FILE_HEADER_SIZE ) != (sizeof ( line_t ) * spiral_size )) {
292+ if ((buffer .size - FILE_HEADER_SIZE ) != (LINE_T_PACK_SIZE * spiral_size )) {
291293 // this check failed, so return it as it is
292294 return output ;
293295 }
@@ -299,18 +301,18 @@ load_spiral(buffer_t buffer) {
299301 for (size_t i = 0 ; i < spiral_size ; i ++ ) {
300302 // direction is stored in 2 most significant bits of each 32-bit sequence
301303 output .lines [i ].direction = (
302- buffer .bytes [FILE_HEADER_SIZE + (i * sizeof ( line_t ) )] >> 6
304+ buffer .bytes [FILE_HEADER_SIZE + (i * LINE_T_PACK_SIZE )] >> 6
303305 );
304306 // length is stored as 30 least significant bits, so we have to unpack it
305307 // handle first byte on it's own as we only need least 6 bits of it
306308 // bit mask and shift 3 bytes to left
307309 output .lines [i ].length = (
308- buffer .bytes [FILE_HEADER_SIZE + (i * sizeof ( line_t ) )] & 0b00111111
310+ buffer .bytes [FILE_HEADER_SIZE + (i * LINE_T_PACK_SIZE )] & 0b00111111
309311 ) << 24 ;
310312 // handle remaining 3 bytes in loop
311313 for (uint8_t j = 0 ; j < 3 ; j ++ ) {
312314 output .lines [i ].length |= (
313- buffer .bytes [FILE_HEADER_SIZE + (i * sizeof ( line_t ) ) + 1 + j ]
315+ buffer .bytes [FILE_HEADER_SIZE + (i * LINE_T_PACK_SIZE ) + 1 + j ]
314316 ) << (8 * (2 - j ));
315317 }
316318 }
@@ -325,7 +327,7 @@ buffer_t
325327dump_spiral (spiral_t spiral ) {
326328 // build output buffer struct, base size on header + spiral size
327329 buffer_t output = {
328- .size = (FILE_HEADER_SIZE + (sizeof ( line_t ) * spiral .size )),
330+ .size = (FILE_HEADER_SIZE + (LINE_T_PACK_SIZE * spiral .size )),
329331 };
330332 // allocate memory
331333 output .bytes = calloc (1 , output .size );
@@ -348,16 +350,16 @@ dump_spiral(spiral_t spiral) {
348350 // serialise each line in the spiral to 4 bytes, handle first byte first
349351 // map direction to 2 most significant bits
350352 output .bytes [
351- FILE_HEADER_SIZE + (i * sizeof ( line_t ) )
353+ FILE_HEADER_SIZE + (i * LINE_T_PACK_SIZE )
352354 ] = (spiral .lines [i ].direction << 6 );
353355 // handle first 6 bits of the length
354356 output .bytes [
355- FILE_HEADER_SIZE + (i * sizeof ( line_t ) )
357+ FILE_HEADER_SIZE + (i * LINE_T_PACK_SIZE )
356358 ] |= (spiral .lines [i ].length >> 24 );
357359 // handle remaining 3 bytes in a loop
358360 for (uint8_t j = 0 ; j < 3 ; j ++ ) {
359361 output .bytes [
360- FILE_HEADER_SIZE + (i * sizeof ( line_t ) ) + 1 + j
362+ FILE_HEADER_SIZE + (i * LINE_T_PACK_SIZE ) + 1 + j
361363 ] = (uint8_t )(spiral .lines [i ].length >> (8 * (2 - j )));
362364 }
363365 }
0 commit comments