Finish haggis_load_filetype function

This commit is contained in:
Nathan Fisher 2023-07-23 23:42:51 -04:00
parent a762f3555e
commit 47640e5818
2 changed files with 83 additions and 30 deletions

View file

@ -79,12 +79,12 @@ struct haggis_checksum {
struct haggis_file { struct haggis_file {
union u64 len; union u64 len;
struct haggis_checksum *cksum; struct haggis_checksum *cksum;
u8 *data[]; u8 *data;
}; };
struct haggis_filename { struct haggis_filename {
union u16 len; union u16 len;
u8 name[4096]; char *name;
}; };
enum haggis_typeflag { enum haggis_typeflag {
@ -110,12 +110,12 @@ struct haggis_filetype {
}; };
struct haggis_node { struct haggis_node {
struct haggis_filename *name;
union u32 uid; union u32 uid;
union u32 gid; union u32 gid;
union u64 mtime; union u64 mtime;
union u16 mode; union u16 mode;
struct haggis_filetype *filetype; struct haggis_filetype *filetype;
char *name[];
}; };
#endif // !HAGGIS_H #endif // !HAGGIS_H

View file

@ -30,15 +30,15 @@
* other than his own. * other than his own.
*/ */
#if defined (__FreeBSD__) || defined (__DragonFly__) #if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/types.h>
#include <sha.h> #include <sha.h>
#include <sha256.h> #include <sha256.h>
#elif defined (__NetBSD__) || defined (__OpenBSD__)
#include <sys/types.h> #include <sys/types.h>
#elif defined(__NetBSD__) || defined(__OpenBSD__)
#include <sha1.h> #include <sha1.h>
#include <sha2.h> #include <sha2.h>
#elif defined (__linux__) #include <sys/types.h>
#elif defined(__linux__)
#include <sha1.h> #include <sha1.h>
#include <sha2.h> #include <sha2.h>
#endif /* if defined (__FreeBSD__) */ #endif /* if defined (__FreeBSD__) */
@ -46,6 +46,7 @@
#include <md5.h> #include <md5.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "bytes.h" #include "bytes.h"
@ -76,10 +77,10 @@ int haggis_store_device(FILE *stream, struct haggis_device *dev) {
return 0; return 0;
} }
int haggis_load_device(FILE *stream, struct haggis_device *dev) { int haggis_load_device(FILE *stream, union haggis_ft *ft) {
if (fread(dev->major.bytes, 1, 4, stream) != 4) if (fread(ft->dev->major.bytes, 1, 4, stream) != 4)
return 1; return 1;
if (fread(dev->minor.bytes, 1, 4, stream) != 4) if (fread(ft->dev->minor.bytes, 1, 4, stream) != 4)
return 1; return 1;
return 0; return 0;
} }
@ -148,30 +149,30 @@ int validate_md5(struct haggis_file *file) {
MD5_CTX ctx; MD5_CTX ctx;
u8 digest[MD5_DIGEST_LENGTH]; u8 digest[MD5_DIGEST_LENGTH];
MD5Init(&ctx); MD5Init(&ctx);
MD5Update(&ctx, *file->data, (size_t)file->len.val); MD5Update(&ctx, file->data, (size_t)file->len.val);
MD5Final(digest, &ctx); MD5Final(digest, &ctx);
if (memcmp(file->cksum->sum->md5, digest, sizeof(digest))) if (memcmp(file->cksum->sum->md5, digest, sizeof(digest)))
return 2; return 2;
return 0; return 0;
} }
#if defined (__FreeBSD__) || defined (__DragonFly__) #if defined(__FreeBSD__) || defined(__DragonFly__)
int validate_sha1(struct haggis_file *file) { int validate_sha1(struct haggis_file *file) {
SHA1_CTX ctx; SHA1_CTX ctx;
u8 digest[20]; u8 digest[20];
SHA1_Init(&ctx); 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); SHA1_Final(digest, &ctx);
if (memcmp(file->cksum->sum->sha1, digest, sizeof(digest))) if (memcmp(file->cksum->sum->sha1, digest, sizeof(digest)))
return 2; return 2;
return 0; return 0;
} }
#elif defined (__linux__) || defined (__NetBSD__) || defined (__OpenBSD__) #elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__)
int validate_sha1(struct haggis_file *file) { int validate_sha1(struct haggis_file *file) {
SHA1_CTX ctx; SHA1_CTX ctx;
u8 digest[20]; u8 digest[20];
SHA1Init(&ctx); SHA1Init(&ctx);
SHA1Update(&ctx, *file->data, (size_t)file->len.val); SHA1Update(&ctx, file->data, (size_t)file->len.val);
SHA1Final(digest, &ctx); SHA1Final(digest, &ctx);
if (memcmp(file->cksum->sum->sha1, digest, sizeof(digest))) if (memcmp(file->cksum->sum->sha1, digest, sizeof(digest)))
return 2; return 2;
@ -179,23 +180,23 @@ int validate_sha1(struct haggis_file *file) {
} }
#endif /* if defined (__FreeBSD__) */ #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) { int validate_sha256(struct haggis_file *file) {
SHA256_CTX ctx; SHA256_CTX ctx;
u8 digest[SHA256_DIGEST_LENGTH]; u8 digest[SHA256_DIGEST_LENGTH];
SHA256_Init(&ctx); 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); SHA256_Final(digest, &ctx);
if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest))) if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest)))
return 2; return 2;
return 0; return 0;
} }
#elif defined (__linux__) || defined (__OpenBSD__) #elif defined(__linux__) || defined(__OpenBSD__)
int validate_sha256(struct haggis_file *file) { int validate_sha256(struct haggis_file *file) {
SHA2_CTX ctx; SHA2_CTX ctx;
u8 digest[SHA256_DIGEST_LENGTH]; u8 digest[SHA256_DIGEST_LENGTH];
SHA256Init(&ctx); SHA256Init(&ctx);
SHA256Update(&ctx, *file->data, (size_t)file->len.val); SHA256Update(&ctx, file->data, (size_t)file->len.val);
SHA256Final(digest, &ctx); SHA256Final(digest, &ctx);
if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest))) if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest)))
return 2; return 2;
@ -206,11 +207,11 @@ int validate_sha256(struct haggis_file *file) {
int haggis_validate_cksum(struct haggis_file *file) { int haggis_validate_cksum(struct haggis_file *file) {
switch (file->cksum->tag) { switch (file->cksum->tag) {
case md5: case md5:
return validate_md5(file); return validate_md5(file);
case sha1: case sha1:
return validate_sha1(file); return validate_sha1(file);
case sha256: case sha256:
return validate_sha256(file); return validate_sha256(file);
case skip: case skip:
return 0; return 0;
} }
@ -228,15 +229,26 @@ int haggis_store_file(FILE *stream, struct haggis_file *file) {
return 0; return 0;
} }
int haggis_load_file(FILE *stream, struct haggis_file *file) { int haggis_load_file(FILE *stream, union haggis_ft *ft) {
if (load_u64(stream, file->len) != 8) union u64 len;
len.val = 0;
if (load_u64(stream, len) != 8)
return 1; 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; return 1;
int res = fread(file->data, 1, (size_t)file->len.val, stream); u8 *data = malloc((size_t)len.val);
if (res != (size_t)file->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 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) { 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; return 0;
} }
int haggis_load_filetype(FILE *stream, struct haggis_filetype *filetype) { int haggis_load_link(FILE *stream, union haggis_ft *ft) {
// todo 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; return 0;
} }