commit 6da10c71dd4ae8e4a884c76ac844d5b4318d2cf0
parent 77cbcb59b3af47e96d2f87814df777ed616f13d7
Author: nolash <dev@holbrook.no>
Date: Tue, 14 Sep 2021 12:13:31 +0200
Add filehasher source files and test
Diffstat:
4 files changed, 81 insertions(+), 1 deletion(-)
diff --git a/Makefile.dev b/Makefile.dev
@@ -17,9 +17,11 @@ build_keccak: prep
build: prep build_keccak
$(CC) -c -o build/bmt.o src/bmt.c $(CFLAGS) -lkeccak-tiny
$(CC) -c -o build/endian.o $(CFLAGS) src/endian.c
+ $(CC) -c -o build/file.o $(CFLAGS) src/file.c
build_check: build
- $(CC) -I./src -o build/test/check_bmt build/bmt.o build/endian.o test/check_bmt.c $(CFLAGS) -lcheck -lkeccak-tiny
+ $(CC) -I./src -o build/test/check_bmt build/bmt.o build/endian.o build/file.o test/check_bmt.c $(CFLAGS) -lcheck -lkeccak-tiny
+ $(CC) -I./src -o build/test/check_file build/bmt.o build/endian.o build/file.o test/check_file.c $(CFLAGS) -lcheck -lkeccak-tiny
#$(CC) -I./src -o test/check_bmt build/bmt.o build/endian.o test/check_bmt.c build/libkeccak-tiny-small.a $(CFLAGS) -lcheck
build_lib: build
@@ -30,6 +32,7 @@ build_lib: build
check: build build_check
LD_LIBRARY_PATH=./build/ CK_FORK=no build/test/check_bmt
+ LD_LIBRARY_PATH=./build/ CK_FORK=no build/test/check_file
test: check
diff --git a/src/file.c b/src/file.c
@@ -0,0 +1,13 @@
+#include <stddef.h>
+
+#include "file.h"
+
+int filehash_init(filehash_t *filehash_context) {
+ int i;
+
+ for (i = 0; i < SWARM_LEVELS; i++) {
+ filehash_context->ptr[i] = filehash_context->buf;
+ }
+
+ return 0;
+}
diff --git a/src/file.h b/src/file.h
@@ -0,0 +1,18 @@
+#ifndef _LIBSWARM_FILE
+#define _LIBSWARM_FILE
+
+#include "bmt.h"
+
+#define SWARM_BATCH_SIZE (int)(BLOCK_SIZE / _WORD_SIZE)
+#define SWARM_LEVELS 9
+
+typedef struct filehash {
+ char buf[SWARM_LEVELS][BLOCK_SIZE];
+ char *ptr[SWARM_LEVELS];
+} filehash_t;
+
+int filehash_init(filehash_t *filehash_context);
+int filehash_write(filehash_t *filehash_context, char *data, size_t data_length);
+int filehash_sum(filehash_t *filehash_content);
+
+#endif // _LIBSWARM_FILE
diff --git a/test/check_file.c b/test/check_file.c
@@ -0,0 +1,46 @@
+#include <check.h>
+#include <stdlib.h>
+
+#include "file.h"
+
+
+START_TEST(check_file_init) {
+ ck_assert_int_eq(SWARM_BATCH_SIZE, 128);
+
+ filehash_t fh;
+ int r;
+
+ r = filehash_init(&fh);
+
+ ck_assert_int_eq(r, 0);
+ ck_assert_ptr_eq(fh.ptr[SWARM_LEVELS-1], fh.buf);
+}
+END_TEST
+
+Suite * common_suite(void) {
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("file");
+ tc = tcase_create("core");
+ tcase_add_test(tc, check_file_init);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
+
+int main(void) {
+ int n_fail;
+
+ Suite *s;
+ SRunner *sr;
+
+ s = common_suite();
+ sr = srunner_create(s);
+
+ srunner_run_all(sr, CK_VERBOSE);
+ n_fail = srunner_ntests_failed(sr);
+ srunner_free(sr);
+
+ return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}