diff --git a/include/haggis.h b/include/haggis.h index e131f3e..87cf72b 100644 --- a/include/haggis.h +++ b/include/haggis.h @@ -79,12 +79,12 @@ struct haggis_checksum { struct haggis_file { union u64 len; struct haggis_checksum *cksum; - u8 *data[]; + u8 *data; }; struct haggis_filename { union u16 len; - u8 name[4096]; + char *name; }; enum haggis_typeflag { @@ -110,12 +110,12 @@ struct haggis_filetype { }; struct haggis_node { + struct haggis_filename *name; union u32 uid; union u32 gid; union u64 mtime; union u16 mode; struct haggis_filetype *filetype; - char *name[]; }; #endif // !HAGGIS_H diff --git a/src/haggis.c b/src/haggis.c index 3ca20a0..4a4fddf 100644 --- a/src/haggis.c +++ b/src/haggis.c @@ -30,15 +30,15 @@ * other than his own. */ -#if defined (__FreeBSD__) || defined (__DragonFly__) -#include +#if defined(__FreeBSD__) || defined(__DragonFly__) #include #include -#elif defined (__NetBSD__) || defined (__OpenBSD__) #include +#elif defined(__NetBSD__) || defined(__OpenBSD__) #include #include -#elif defined (__linux__) +#include +#elif defined(__linux__) #include #include #endif /* if defined (__FreeBSD__) */ @@ -46,6 +46,7 @@ #include #include #include +#include #include #include "bytes.h" @@ -76,10 +77,10 @@ int haggis_store_device(FILE *stream, struct haggis_device *dev) { return 0; } -int haggis_load_device(FILE *stream, struct haggis_device *dev) { - if (fread(dev->major.bytes, 1, 4, stream) != 4) +int haggis_load_device(FILE *stream, union haggis_ft *ft) { + if (fread(ft->dev->major.bytes, 1, 4, stream) != 4) return 1; - if (fread(dev->minor.bytes, 1, 4, stream) != 4) + if (fread(ft->dev->minor.bytes, 1, 4, stream) != 4) return 1; return 0; } @@ -148,30 +149,30 @@ int validate_md5(struct haggis_file *file) { MD5_CTX ctx; u8 digest[MD5_DIGEST_LENGTH]; MD5Init(&ctx); - MD5Update(&ctx, *file->data, (size_t)file->len.val); + MD5Update(&ctx, file->data, (size_t)file->len.val); MD5Final(digest, &ctx); if (memcmp(file->cksum->sum->md5, digest, sizeof(digest))) return 2; return 0; } -#if defined (__FreeBSD__) || defined (__DragonFly__) +#if defined(__FreeBSD__) || defined(__DragonFly__) 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_Update(&ctx, file->data, (size_t)file->len.val); SHA1_Final(digest, &ctx); if (memcmp(file->cksum->sum->sha1, digest, sizeof(digest))) return 2; return 0; } -#elif defined (__linux__) || defined (__NetBSD__) || defined (__OpenBSD__) +#elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) int validate_sha1(struct haggis_file *file) { SHA1_CTX ctx; u8 digest[20]; SHA1Init(&ctx); - SHA1Update(&ctx, *file->data, (size_t)file->len.val); + SHA1Update(&ctx, file->data, (size_t)file->len.val); SHA1Final(digest, &ctx); if (memcmp(file->cksum->sum->sha1, digest, sizeof(digest))) return 2; @@ -179,23 +180,23 @@ int validate_sha1(struct haggis_file *file) { } #endif /* if defined (__FreeBSD__) */ -#if defined (__FreeBSD__) || defined (__DragonFly) || defined (__NetBSD__) +#if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__) int validate_sha256(struct haggis_file *file) { SHA256_CTX ctx; u8 digest[SHA256_DIGEST_LENGTH]; SHA256_Init(&ctx); - SHA256_Update(&ctx, *file->data, (size_t)file->len.val); + SHA256_Update(&ctx, file->data, (size_t)file->len.val); SHA256_Final(digest, &ctx); if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest))) return 2; return 0; } -#elif defined (__linux__) || defined (__OpenBSD__) +#elif defined(__linux__) || defined(__OpenBSD__) int validate_sha256(struct haggis_file *file) { SHA2_CTX ctx; u8 digest[SHA256_DIGEST_LENGTH]; SHA256Init(&ctx); - SHA256Update(&ctx, *file->data, (size_t)file->len.val); + SHA256Update(&ctx, file->data, (size_t)file->len.val); SHA256Final(digest, &ctx); if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest))) return 2; @@ -206,11 +207,11 @@ int validate_sha256(struct haggis_file *file) { int haggis_validate_cksum(struct haggis_file *file) { switch (file->cksum->tag) { case md5: - return validate_md5(file); + return validate_md5(file); case sha1: - return validate_sha1(file); + return validate_sha1(file); case sha256: - return validate_sha256(file); + return validate_sha256(file); case skip: return 0; } @@ -228,15 +229,26 @@ int haggis_store_file(FILE *stream, struct haggis_file *file) { return 0; } -int haggis_load_file(FILE *stream, struct haggis_file *file) { - if (load_u64(stream, file->len) != 8) +int haggis_load_file(FILE *stream, union haggis_ft *ft) { + union u64 len; + len.val = 0; + if (load_u64(stream, len) != 8) return 1; - if (haggis_load_cksum(stream, file->cksum) != 0) + ft->file->len = len; + if (haggis_load_cksum(stream, ft->file->cksum) != 0) return 1; - int res = fread(file->data, 1, (size_t)file->len.val, stream); - if (res != (size_t)file->len.val) + u8 *data = malloc((size_t)len.val); + int res = fread(data, 1, (size_t)ft->file->len.val, stream); + if (res != (size_t)ft->file->len.val) { + free(ft); return 1; - return haggis_validate_cksum(file); + } + ft->file->data = data; + if (haggis_validate_cksum(ft->file)) { + free(ft); + return 1; + } + return 0; } int haggis_store_filetype(FILE *stream, struct haggis_filetype *filetype) { @@ -303,8 +315,49 @@ int haggis_store_filetype(FILE *stream, struct haggis_filetype *filetype) { return 0; } -int haggis_load_filetype(FILE *stream, struct haggis_filetype *filetype) { - // todo +int haggis_load_link(FILE *stream, union haggis_ft *ft) { + union u16 len; + 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); + return 2; + } + return 0; +} + +int haggis_load_filetype( + FILE *stream, + enum haggis_typeflag tag, + struct haggis_filetype *file +) { + switch (tag) { + case normal: + file->tag = 0; + return haggis_load_file(stream, file->f_type); + case hardlink: + file->tag = 1; + case softlink: + file->tag = 2; + case directory: + file->tag = 3; + break; + case character: + file->tag = 4; + return haggis_load_device(stream, file->f_type); + case block: + file->tag = 5; + return haggis_load_device(stream, file->f_type); + case fifo: + file->tag = 6; + break; + case eof: + file->tag = 7; + break; + } return 0; }