diff --git a/src/haggis.c b/src/haggis.c index 4a4fddf..11f9877 100644 --- a/src/haggis.c +++ b/src/haggis.c @@ -86,7 +86,8 @@ int haggis_load_device(FILE *stream, union haggis_ft *ft) { } int haggis_store_cksum(FILE *stream, struct haggis_checksum *cksum) { - char flag; + u8 flag; + switch (cksum->tag) { case md5: flag = 0; @@ -119,7 +120,8 @@ int haggis_store_cksum(FILE *stream, struct haggis_checksum *cksum) { } int haggis_load_cksum(FILE *stream, struct haggis_checksum *cksum) { - char flag; + u8 flag; + if (fread(&flag, 1, 1, stream) != 1) return 1; switch (flag) { @@ -147,7 +149,8 @@ int haggis_load_cksum(FILE *stream, struct haggis_checksum *cksum) { int validate_md5(struct haggis_file *file) { MD5_CTX ctx; - u8 digest[MD5_DIGEST_LENGTH]; + u8 digest[16]; + MD5Init(&ctx); MD5Update(&ctx, file->data, (size_t)file->len.val); MD5Final(digest, &ctx); @@ -160,6 +163,7 @@ int validate_md5(struct haggis_file *file) { int validate_sha1(struct haggis_file *file) { SHA1_CTX ctx; u8 digest[20]; + SHA1_Init(&ctx); SHA1_Update(&ctx, file->data, (size_t)file->len.val); SHA1_Final(digest, &ctx); @@ -171,6 +175,7 @@ int validate_sha1(struct haggis_file *file) { int validate_sha1(struct haggis_file *file) { SHA1_CTX ctx; u8 digest[20]; + SHA1Init(&ctx); SHA1Update(&ctx, file->data, (size_t)file->len.val); SHA1Final(digest, &ctx); @@ -183,7 +188,8 @@ int validate_sha1(struct haggis_file *file) { #if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__) int validate_sha256(struct haggis_file *file) { SHA256_CTX ctx; - u8 digest[SHA256_DIGEST_LENGTH]; + u8 digest[32]; + SHA256_Init(&ctx); SHA256_Update(&ctx, file->data, (size_t)file->len.val); SHA256_Final(digest, &ctx); @@ -194,7 +200,8 @@ int validate_sha256(struct haggis_file *file) { #elif defined(__linux__) || defined(__OpenBSD__) int validate_sha256(struct haggis_file *file) { SHA2_CTX ctx; - u8 digest[SHA256_DIGEST_LENGTH]; + u8 digest[32]; + SHA256Init(&ctx); SHA256Update(&ctx, file->data, (size_t)file->len.val); SHA256Final(digest, &ctx); @@ -232,6 +239,7 @@ int haggis_store_file(FILE *stream, struct haggis_file *file) { int haggis_load_file(FILE *stream, union haggis_ft *ft) { union u64 len; len.val = 0; + if (load_u64(stream, len) != 8) return 1; ft->file->len = len; @@ -315,17 +323,20 @@ int haggis_store_filetype(FILE *stream, struct haggis_filetype *filetype) { return 0; } -int haggis_load_link(FILE *stream, union haggis_ft *ft) { +int haggis_load_filename(FILE *stream, struct haggis_filename *n) { union u16 len; + char *name; + len.val = 0; if (fread(len.bytes, 1, 2, stream) != 2) return 2; - ft->target->len = len; - u8 *target = malloc((size_t)len.val); - if (fread(target, 1, (size_t)len.val, stream) != (size_t)len.val) { - free(target); + n->len = len; + name = malloc((size_t)len.val); + if (fread(name, 1, (size_t)len.val, stream) != (size_t)len.val) { + free(name); return 2; } + n->name = name; return 0; } @@ -339,18 +350,20 @@ int haggis_load_filetype( file->tag = 0; return haggis_load_file(stream, file->f_type); case hardlink: + return haggis_load_filename(stream, file->f_type->target); file->tag = 1; case softlink: + return haggis_load_filename(stream, file->f_type->target); file->tag = 2; case directory: file->tag = 3; break; case character: file->tag = 4; - return haggis_load_device(stream, file->f_type); + return haggis_load_device(stream, file->f_type); case block: file->tag = 5; - return haggis_load_device(stream, file->f_type); + return haggis_load_device(stream, file->f_type); case fifo: file->tag = 6; break; @@ -367,6 +380,25 @@ int haggis_store_node(FILE *stream, struct haggis_node *node) { } int haggis_load_node(FILE *stream, struct haggis_node *node) { - // todo + int res; + union u16 mode; + enum haggis_typeflag flag; + + res = haggis_load_filename(stream, node->name); + if (res) + return res; + res = load_u32(stream, node->uid); + if (res != 4) + return 2; + res = load_u32(stream, node->gid); + if (res != 4) + return 2; + res = load_u64(stream, node->mtime); + if (res != 8) + return 2; + res = load_u16(stream, mode); + if (res != 2) + return 2; + //todo return 0; }