diff --git a/include/bytes.h b/include/bytes.h index 31911e8..320b3a6 100644 --- a/include/bytes.h +++ b/include/bytes.h @@ -37,8 +37,8 @@ #include "haggis.h" -int load_u16(FILE *stream, u16 num); -int store_u16(FILE *stream, u16 num); +int load_u16(FILE *stream, u16 *num); +int store_u16(FILE *stream, u16 *num); int load_u32(FILE *stream, u32 num); int store_u32(FILE *stream, u32 num); int load_u64(FILE *stream, u64 num); diff --git a/src/bytes.c b/src/bytes.c index 6c118ee..b465ddc 100644 --- a/src/bytes.c +++ b/src/bytes.c @@ -38,12 +38,12 @@ #include "haggis.h" #if __BYTE_ORDER__ == __LITTLE_ENDIAN -int load_u16(FILE *stream, u16 num) { - return fread(num.bytes, 1, 2, stream); +int load_u16(FILE *stream, u16 *num) { + return fread(&num->bytes, 1, 2, stream); } -int store_u16(FILE *stream, u16 num) { - return fwrite(num.bytes, 1, 2, stream); +int store_u16(FILE *stream, u16 *num) { + return fwrite(&num->bytes, 1, 2, stream); } int load_u32(FILE *stream, u32 num) { diff --git a/src/haggis.c b/src/haggis.c index f504135..99a3b09 100644 --- a/src/haggis.c +++ b/src/haggis.c @@ -351,6 +351,8 @@ int haggis_load_file(FILE *stream, haggis_file *f) { return -1; int res = fread(f->data, 1, (size_t)f->len.val, stream); if (res != (size_t)f->len.val) { + fprintf(stderr, "Error reading file:\n length: %i\n expected: %lu\n", + res, f->len.val); free(f->data); return 1; } @@ -643,7 +645,7 @@ int haggis_load_node(FILE *stream, haggis_node *node) { res = load_u64(stream, node->mtime); if (res != 8) return 2; - res = load_u16(stream, mode); + res = load_u16(stream, &mode); if (res != 2) return 2; tag = haggis_filetype_from_mode(mode); @@ -664,6 +666,6 @@ int haggis_store_node(FILE *stream, haggis_node *node) { if (store_u32(stream, node->gid) != 4) return 2; if (store_u64(stream, node->mtime) != 8) return 2; mode = haggis_derive_mode(node->mode, &node->filetype); - if (store_u16(stream, mode) != 2) return 2; + if (store_u16(stream, &mode) != 2) return 2; return haggis_store_filetype(stream, &node->filetype); } diff --git a/test/Makefile b/test/Makefile index 58a4f98..d90d555 100644 --- a/test/Makefile +++ b/test/Makefile @@ -34,6 +34,8 @@ CFLAGS += -I../include LIBS += ../libhaggis.a LIBS += -lmd +tests += store_u16 +tests += load_u16 tests += store_header tests += check_header tests += store_device @@ -46,6 +48,9 @@ tests += store_sha256 tests += load_sha256 tests += init_file_md5 tests += init_file_sha1 +tests += init_file_sha256 +tests += store_file_md5 +#tests += load_file_md5 total != echo $(tests) | wc -w | awk '{ print $$1 }' .PHONY: test diff --git a/test/init_file_sha1.c b/test/init_file_sha1.c index fca9667..c6dfae7 100644 --- a/test/init_file_sha1.c +++ b/test/init_file_sha1.c @@ -6,7 +6,8 @@ int main() { haggis_file hf; char *f = "check_header.c"; - const u8 buf[20] = { 175, 45, 7, 226, 140, 184, 187, 69, 59, 208, 246, 108, 187, 249, 63, 111, 56, 65, 1, 58 }; + const u8 buf[20] = { 175, 45, 7, 226, 140, 184, 187, 69, 59, 208, 246, 108, + 187, 249, 63, 111, 56, 65, 1, 58 }; assert(haggis_file_init(f, &hf, sha1) == 0); assert(hf.cksum.tag == sha1); diff --git a/test/init_file_sha256.c b/test/init_file_sha256.c new file mode 100644 index 0000000..485826d --- /dev/null +++ b/test/init_file_sha256.c @@ -0,0 +1,17 @@ +#include "haggis_private.h" +#include +#include +#include + +int main() { + haggis_file hf; + char *f = "check_header.c"; + const u8 buf[32] = { 254, 29, 31, 135, 226, 153, 9, 124, 189, 232, 10, 158, + 250, 203, 77, 108, 230, 7, 159, 124, 6, 237, 169, 239, 70, 218, 173, 188, + 153, 219, 121, 82 }; + + assert(haggis_file_init(f, &hf, sha256) == 0); + assert(hf.cksum.tag == sha256); + assert(memcmp(buf, hf.cksum.sum.sha256, 32) == 0); +} + diff --git a/test/load_file_md5 b/test/load_file_md5 new file mode 100755 index 0000000..627f1ba Binary files /dev/null and b/test/load_file_md5 differ diff --git a/test/load_file_md5.c b/test/load_file_md5.c new file mode 100644 index 0000000..66ea029 --- /dev/null +++ b/test/load_file_md5.c @@ -0,0 +1,16 @@ +#include "haggis_private.h" +#include +#include +#include + +int main() { + haggis_file hf; + char *f = "output/store_file_md5"; + FILE *fd; + int res; + + fd = fopen(f, "r"); + res = haggis_load_file(fd, &hf); + fclose(fd); + return 1; +} diff --git a/test/load_u16.c b/test/load_u16.c new file mode 100644 index 0000000..2c4060b --- /dev/null +++ b/test/load_u16.c @@ -0,0 +1,16 @@ +#include "bytes.h" +#include "haggis_private.h" +#include +#include + +int main() { + u16 n; + FILE *fd; + + n.val = 0; + fd = fopen("output/u16", "r"); + assert(load_u16(fd, &n) == 2); + assert(n.val == 300); + fclose(fd); +} + diff --git a/test/store_file_md5.c b/test/store_file_md5.c new file mode 100644 index 0000000..4fa1efd --- /dev/null +++ b/test/store_file_md5.c @@ -0,0 +1,17 @@ +#include "haggis_private.h" +#include +#include +#include + +int main() { + haggis_file hf; + char *f = "load_device.c"; + FILE *fd; + + assert(haggis_file_init(f, &hf, md5) == 0); + fd = fopen("output/store_file_md5", "w"); + assert(haggis_store_file(fd, &hf) == 0); + fclose(fd); + return 0; +} + diff --git a/test/store_u16.c b/test/store_u16.c new file mode 100644 index 0000000..d2cfe15 --- /dev/null +++ b/test/store_u16.c @@ -0,0 +1,15 @@ +#include "bytes.h" +#include "haggis_private.h" +#include +#include + +int main() { + u16 n; + FILE *fd; + + n.val = 300; + fd = fopen("output/u16", "w"); + assert(store_u16(fd, &n) == 2); + fflush(fd); + fclose(fd); +}