2121 * You should have received a copy of the GNU Affero General Public License
2222 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2323 */
24+ #include <assert.h>
2425#include <stdint.h>
2526#include <stdio.h>
2627#include <stdlib.h>
@@ -38,28 +39,49 @@ extern "C"{
3839const size_t SXBP_FILE_HEADER_SIZE = 37 ;
3940const size_t SXBP_LINE_T_PACK_SIZE = 4 ;
4041
41- // loads a 64-bit unsigned integer from buffer starting at given index
42+ /*
43+ * loads a 64-bit unsigned integer from buffer starting at given index
44+ *
45+ * Asserts:
46+ * - That buffer->bytes is not NULL
47+ */
4248static uint64_t load_uint64_t (sxbp_buffer_t * buffer , size_t start_index ) {
49+ // preconditional assertions
50+ assert (buffer -> bytes != NULL );
4351 uint64_t value = 0 ;
4452 for (size_t i = 0 ; i < 8 ; i ++ ) {
4553 value |= (buffer -> bytes [start_index + i ]) << (8 * (7 - i ));
4654 }
4755 return value ;
4856}
4957
50- // loads a 32-bit unsigned integer from buffer starting at given index
58+ /*
59+ * loads a 32-bit unsigned integer from buffer starting at given index
60+ *
61+ * Asserts:
62+ * - That buffer->bytes is not NULL
63+ */
5164static uint32_t load_uint32_t (sxbp_buffer_t * buffer , size_t start_index ) {
65+ // preconditional assertions
66+ assert (buffer -> bytes != NULL );
5267 uint32_t value = 0 ;
5368 for (size_t i = 0 ; i < 4 ; i ++ ) {
5469 value |= (buffer -> bytes [start_index + i ]) << (8 * (3 - i ));
5570 }
5671 return value ;
5772}
5873
59- // dumps a 64-bit unsigned integer of value to buffer at given index
74+ /*
75+ * dumps a 64-bit unsigned integer of value to buffer at given index
76+ *
77+ * Asserts:
78+ * - That buffer->bytes is not NULL
79+ */
6080static void dump_uint64_t (
6181 uint64_t value , sxbp_buffer_t * buffer , size_t start_index
6282) {
83+ // preconditional assertions
84+ assert (buffer -> bytes != NULL );
6385 for (uint8_t i = 0 ; i < 8 ; i ++ ) {
6486 uint8_t shift = (8 * (7 - i ));
6587 buffer -> bytes [start_index + i ] = (uint8_t )(
@@ -68,10 +90,17 @@ static void dump_uint64_t(
6890 }
6991}
7092
71- // dumps a 32-bit unsigned integer of value to buffer at given index
93+ /*
94+ * dumps a 32-bit unsigned integer of value to buffer at given index
95+ *
96+ * Asserts:
97+ * - That buffer->bytes is not NULL
98+ */
7299static void dump_uint32_t (
73100 uint32_t value , sxbp_buffer_t * buffer , size_t start_index
74101) {
102+ // preconditional assertions
103+ assert (buffer -> bytes != NULL );
75104 for (uint8_t i = 0 ; i < 4 ; i ++ ) {
76105 uint8_t shift = (8 * (3 - i ));
77106 buffer -> bytes [start_index + i ] = (uint8_t )(
@@ -86,10 +115,17 @@ static void dump_uint32_t(
86115 * returns a serialise_result_t struct, which will contain information about
87116 * whether the operation was successful or not and information about what went
88117 * wrong if it was not successful
118+ *
119+ * Asserts:
120+ * - That buffer.bytes is not NULL
121+ * - That spiral->lines is NULL
89122 */
90123sxbp_serialise_result_t sxbp_load_spiral (
91124 sxbp_buffer_t buffer , sxbp_spiral_t * spiral
92125) {
126+ // preconditional assertions
127+ assert (buffer .bytes != NULL );
128+ assert (spiral -> lines == NULL );
93129 sxbp_serialise_result_t result ; // build struct for returning success / failure
94130 // first, if header is too small for header + 1 line, then return early
95131 if (buffer .size < SXBP_FILE_HEADER_SIZE + SXBP_LINE_T_PACK_SIZE ) {
@@ -177,10 +213,17 @@ sxbp_serialise_result_t sxbp_load_spiral(
177213 * returns a serialise_result_t struct, which will contain information about
178214 * whether the operation was successful or not and information about what went
179215 * wrong if it was not successful
216+ *
217+ * Asserts:
218+ * - That spiral.lines is not NULL
219+ * - That buffer->bytes is NULL
180220 */
181221sxbp_serialise_result_t sxbp_dump_spiral (
182222 sxbp_spiral_t spiral , sxbp_buffer_t * buffer
183223) {
224+ // preconditional assertions
225+ assert (buffer -> bytes == NULL );
226+ assert (spiral .lines != NULL );
184227 sxbp_serialise_result_t result ; // build struct for returning success / failure
185228 // populate buffer struct, base size on header + spiral size
186229 buffer -> size = (SXBP_FILE_HEADER_SIZE + (SXBP_LINE_T_PACK_SIZE * spiral .size ));
0 commit comments