2023-07-20 01:06:38 -04:00
|
|
|
/* _,.---._ .-._ .--.-. ,--.--------.
|
2023-07-19 22:38:59 -04:00
|
|
|
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
2023-07-20 01:06:38 -04:00
|
|
|
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
2023-07-19 22:38:59 -04:00
|
|
|
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
|
|
|
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
2023-07-20 01:06:38 -04:00
|
|
|
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
|
|
|
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
|
|
|
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
|
|
|
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
|
|
|
* _ __ ,---. .-._ .=-.-. _,.----.
|
2023-07-19 22:38:59 -04:00
|
|
|
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
2023-07-20 01:06:38 -04:00
|
|
|
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
|
|
|
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
2023-07-19 22:38:59 -04:00
|
|
|
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
2023-07-20 01:06:38 -04:00
|
|
|
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
|
|
|
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
|
|
|
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
2023-07-19 22:38:59 -04:00
|
|
|
* `--`---' `--` `--`./ `--``--`-`
|
|
|
|
*
|
|
|
|
* @(#)Copyright (c) 2023, Nathan D. Fisher.
|
|
|
|
*
|
|
|
|
* This is free software. It comes with NO WARRANTY.
|
|
|
|
* Permission to use, modify and distribute this source code
|
|
|
|
* is granted subject to the following conditions.
|
|
|
|
* 1/ that the above copyright notice and this notice
|
|
|
|
* are preserved in all copies and that due credit be given
|
|
|
|
* to the author.
|
|
|
|
* 2/ that any changes to this code are clearly commented
|
|
|
|
* as such so that the author does not get blamed for bugs
|
|
|
|
* other than his own.
|
|
|
|
*/
|
|
|
|
|
2023-07-24 18:56:04 -04:00
|
|
|
#include <stdint.h>
|
2023-07-23 23:42:51 -04:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2023-07-22 23:34:32 -04:00
|
|
|
#include <sha.h>
|
|
|
|
#include <sha256.h>
|
|
|
|
#include <sys/types.h>
|
2023-07-23 23:42:51 -04:00
|
|
|
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
2023-07-22 23:34:32 -04:00
|
|
|
#include <sha1.h>
|
|
|
|
#include <sha2.h>
|
2023-07-23 23:42:51 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#elif defined(__linux__)
|
2023-07-22 19:01:49 -04:00
|
|
|
#include <sha1.h>
|
|
|
|
#include <sha2.h>
|
2023-07-22 23:34:32 -04:00
|
|
|
#endif /* if defined (__FreeBSD__) */
|
|
|
|
|
|
|
|
#include <md5.h>
|
2023-07-22 11:37:11 -04:00
|
|
|
#include <stddef.h>
|
2023-07-19 22:38:59 -04:00
|
|
|
#include <stdio.h>
|
2023-07-23 23:42:51 -04:00
|
|
|
#include <stdlib.h>
|
2023-07-20 01:06:38 -04:00
|
|
|
#include <string.h>
|
2023-07-19 22:38:59 -04:00
|
|
|
|
2023-07-22 19:01:49 -04:00
|
|
|
#include "bytes.h"
|
2023-07-19 22:38:59 -04:00
|
|
|
#include "haggis.h"
|
|
|
|
|
2023-07-20 01:06:38 -04:00
|
|
|
static unsigned char header[7] = {0x89, 'h', 'a', 'g', 'g', 'i', 's'};
|
|
|
|
|
|
|
|
int haggis_store_header(FILE *stream) {
|
|
|
|
if (fwrite(header, 1, 7, stream) < 7)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int haggis_check_header(FILE *stream) {
|
|
|
|
unsigned char *buf[7];
|
|
|
|
if (fread(buf, 1, 7, stream) < 7)
|
|
|
|
return 1;
|
|
|
|
if (memcmp(buf, header, 7))
|
|
|
|
return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_store_device(FILE *stream, haggis_device *dev) {
|
2023-07-22 11:37:11 -04:00
|
|
|
if (fwrite(dev->major.bytes, 1, 4, stream) != 4)
|
2023-07-21 09:07:24 -04:00
|
|
|
return 1;
|
2023-07-22 11:37:11 -04:00
|
|
|
if (fwrite(dev->minor.bytes, 1, 4, stream) != 4)
|
2023-07-21 09:07:24 -04:00
|
|
|
return 1;
|
2023-07-20 01:06:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_load_device(FILE *stream, haggis_ft *ft) {
|
2023-07-23 23:42:51 -04:00
|
|
|
if (fread(ft->dev->major.bytes, 1, 4, stream) != 4)
|
2023-07-21 09:07:24 -04:00
|
|
|
return 1;
|
2023-07-23 23:42:51 -04:00
|
|
|
if (fread(ft->dev->minor.bytes, 1, 4, stream) != 4)
|
2023-07-21 09:07:24 -04:00
|
|
|
return 1;
|
2023-07-20 01:06:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_store_cksum(FILE *stream, haggis_checksum *cksum) {
|
2023-07-24 11:04:55 -04:00
|
|
|
u8 flag;
|
|
|
|
|
2023-07-20 01:06:38 -04:00
|
|
|
switch (cksum->tag) {
|
|
|
|
case md5:
|
|
|
|
flag = 0;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
if (fwrite(cksum->sum->md5, 1, 16, stream) != 16)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case sha1:
|
|
|
|
flag = 1;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
if (fwrite(cksum->sum->sha1, 1, 20, stream) != 20)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case sha256:
|
|
|
|
flag = 2;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
if (fwrite(cksum->sum->sha256, 1, 32, stream) != 32)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case skip:
|
|
|
|
flag = 3;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_load_cksum(FILE *stream, haggis_checksum *cksum) {
|
2023-07-24 11:04:55 -04:00
|
|
|
u8 flag;
|
|
|
|
|
2023-07-20 01:06:38 -04:00
|
|
|
if (fread(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
switch (flag) {
|
|
|
|
case md5:
|
|
|
|
cksum->tag = 0;
|
|
|
|
if (fread(&cksum->sum->md5, 1, 16, stream) != 16)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case sha1:
|
|
|
|
cksum->tag = 1;
|
|
|
|
if (fread(&cksum->sum->sha1, 1, 20, stream) != 20)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case sha256:
|
|
|
|
cksum->tag = 2;
|
|
|
|
if (fread(&cksum->sum->sha256, 1, 32, stream) != 32)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case skip:
|
|
|
|
cksum->tag = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int validate_md5(haggis_file *file) {
|
2023-07-22 19:01:49 -04:00
|
|
|
MD5_CTX ctx;
|
2023-07-24 11:04:55 -04:00
|
|
|
u8 digest[16];
|
|
|
|
|
2023-07-22 19:01:49 -04:00
|
|
|
MD5Init(&ctx);
|
2023-07-23 23:42:51 -04:00
|
|
|
MD5Update(&ctx, file->data, (size_t)file->len.val);
|
2023-07-22 19:01:49 -04:00
|
|
|
MD5Final(digest, &ctx);
|
|
|
|
if (memcmp(file->cksum->sum->md5, digest, sizeof(digest)))
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-23 23:42:51 -04:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2023-07-28 18:39:30 -04:00
|
|
|
int validate_sha1(haggis_file *file) {
|
2023-07-22 19:01:49 -04:00
|
|
|
SHA1_CTX ctx;
|
2023-07-22 23:34:32 -04:00
|
|
|
u8 digest[20];
|
2023-07-24 11:04:55 -04:00
|
|
|
|
2023-07-22 23:34:32 -04:00
|
|
|
SHA1_Init(&ctx);
|
2023-07-23 23:42:51 -04:00
|
|
|
SHA1_Update(&ctx, file->data, (size_t)file->len.val);
|
2023-07-22 23:34:32 -04:00
|
|
|
SHA1_Final(digest, &ctx);
|
|
|
|
if (memcmp(file->cksum->sum->sha1, digest, sizeof(digest)))
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-23 23:42:51 -04:00
|
|
|
#elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
2023-07-28 18:39:30 -04:00
|
|
|
int validate_sha1(haggis_file *file) {
|
2023-07-22 23:34:32 -04:00
|
|
|
SHA1_CTX ctx;
|
|
|
|
u8 digest[20];
|
2023-07-24 11:04:55 -04:00
|
|
|
|
2023-07-22 19:01:49 -04:00
|
|
|
SHA1Init(&ctx);
|
2023-07-23 23:42:51 -04:00
|
|
|
SHA1Update(&ctx, file->data, (size_t)file->len.val);
|
2023-07-22 19:01:49 -04:00
|
|
|
SHA1Final(digest, &ctx);
|
|
|
|
if (memcmp(file->cksum->sum->sha1, digest, sizeof(digest)))
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-22 23:34:32 -04:00
|
|
|
#endif /* if defined (__FreeBSD__) */
|
2023-07-22 19:01:49 -04:00
|
|
|
|
2023-07-23 23:42:51 -04:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__)
|
2023-07-28 18:39:30 -04:00
|
|
|
int validate_sha256(haggis_file *file) {
|
2023-07-22 23:34:32 -04:00
|
|
|
SHA256_CTX ctx;
|
2023-07-24 11:04:55 -04:00
|
|
|
u8 digest[32];
|
|
|
|
|
2023-07-22 23:34:32 -04:00
|
|
|
SHA256_Init(&ctx);
|
2023-07-23 23:42:51 -04:00
|
|
|
SHA256_Update(&ctx, file->data, (size_t)file->len.val);
|
2023-07-22 23:34:32 -04:00
|
|
|
SHA256_Final(digest, &ctx);
|
|
|
|
if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest)))
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-23 23:42:51 -04:00
|
|
|
#elif defined(__linux__) || defined(__OpenBSD__)
|
2023-07-28 18:39:30 -04:00
|
|
|
int validate_sha256(haggis_file *file) {
|
2023-07-22 19:01:49 -04:00
|
|
|
SHA2_CTX ctx;
|
2023-07-24 11:04:55 -04:00
|
|
|
u8 digest[32];
|
|
|
|
|
2023-07-22 19:01:49 -04:00
|
|
|
SHA256Init(&ctx);
|
2023-07-23 23:42:51 -04:00
|
|
|
SHA256Update(&ctx, file->data, (size_t)file->len.val);
|
2023-07-22 19:01:49 -04:00
|
|
|
SHA256Final(digest, &ctx);
|
|
|
|
if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest)))
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-22 23:34:32 -04:00
|
|
|
#endif /* if defined (__FreeBSD__) */
|
2023-07-22 19:01:49 -04:00
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_validate_cksum(haggis_file *file) {
|
2023-07-22 19:01:49 -04:00
|
|
|
switch (file->cksum->tag) {
|
|
|
|
case md5:
|
2023-07-23 23:42:51 -04:00
|
|
|
return validate_md5(file);
|
2023-07-22 19:01:49 -04:00
|
|
|
case sha1:
|
2023-07-23 23:42:51 -04:00
|
|
|
return validate_sha1(file);
|
2023-07-22 19:01:49 -04:00
|
|
|
case sha256:
|
2023-07-23 23:42:51 -04:00
|
|
|
return validate_sha256(file);
|
2023-07-22 19:01:49 -04:00
|
|
|
case skip:
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-20 01:06:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_store_file(FILE *stream, haggis_file *file) {
|
2023-07-22 11:37:11 -04:00
|
|
|
if (store_u64(stream, file->len) != 8)
|
|
|
|
return 1;
|
|
|
|
if (haggis_store_cksum(stream, file->cksum) != 0)
|
|
|
|
return 1;
|
|
|
|
int res = fwrite(file->data, 1, (size_t)file->len.val, stream);
|
|
|
|
if (res != (size_t)file->len.val)
|
|
|
|
return 1;
|
2023-07-20 01:06:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_load_file(FILE *stream, haggis_ft *ft) {
|
|
|
|
u64 len;
|
2023-07-23 23:42:51 -04:00
|
|
|
len.val = 0;
|
2023-07-24 11:04:55 -04:00
|
|
|
|
2023-07-23 23:42:51 -04:00
|
|
|
if (load_u64(stream, len) != 8)
|
2023-07-22 11:37:11 -04:00
|
|
|
return 1;
|
2023-07-23 23:42:51 -04:00
|
|
|
ft->file->len = len;
|
|
|
|
if (haggis_load_cksum(stream, ft->file->cksum) != 0)
|
2023-07-22 11:37:11 -04:00
|
|
|
return 1;
|
2023-07-23 23:42:51 -04:00
|
|
|
u8 *data = malloc((size_t)len.val);
|
2023-07-25 19:50:46 -04:00
|
|
|
if (data == NULL)
|
|
|
|
return -1;
|
2023-07-23 23:42:51 -04:00
|
|
|
int res = fread(data, 1, (size_t)ft->file->len.val, stream);
|
|
|
|
if (res != (size_t)ft->file->len.val) {
|
|
|
|
free(ft);
|
2023-07-22 11:37:11 -04:00
|
|
|
return 1;
|
2023-07-23 23:42:51 -04:00
|
|
|
}
|
|
|
|
ft->file->data = data;
|
|
|
|
if (haggis_validate_cksum(ft->file)) {
|
|
|
|
free(ft);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2023-07-22 11:37:11 -04:00
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_load_filename(FILE *stream, haggis_filename *n) {
|
|
|
|
u16 len;
|
2023-07-24 11:04:55 -04:00
|
|
|
char *name;
|
|
|
|
|
2023-07-23 23:42:51 -04:00
|
|
|
len.val = 0;
|
2023-07-29 13:51:37 -04:00
|
|
|
if (fread(len.bytes, 1, 2, stream) != 2) return 2;
|
2023-07-24 11:04:55 -04:00
|
|
|
n->len = len;
|
|
|
|
name = malloc((size_t)len.val);
|
2023-07-29 13:51:37 -04:00
|
|
|
if (name == NULL) return -1;
|
2023-07-24 11:04:55 -04:00
|
|
|
if (fread(name, 1, (size_t)len.val, stream) != (size_t)len.val) {
|
|
|
|
free(name);
|
2023-07-23 23:42:51 -04:00
|
|
|
return 2;
|
|
|
|
}
|
2023-07-24 11:04:55 -04:00
|
|
|
n->name = name;
|
2023-07-23 23:42:51 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-29 13:51:37 -04:00
|
|
|
int haggis_store_filename(FILE *stream, haggis_filename *n) {
|
|
|
|
if (fwrite(n->len.bytes, 1, 2, stream) != 2) return 2;
|
|
|
|
if (fwrite(n->name, 1, (size_t)n->len.val, stream) != (size_t)n->len.val)
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_load_filetype(FILE *stream, haggis_typeflag tag, haggis_filetype *file) {
|
2023-07-23 23:42:51 -04:00
|
|
|
switch (tag) {
|
|
|
|
case normal:
|
|
|
|
file->tag = 0;
|
|
|
|
return haggis_load_file(stream, file->f_type);
|
|
|
|
case hardlink:
|
2023-07-24 11:04:55 -04:00
|
|
|
return haggis_load_filename(stream, file->f_type->target);
|
2023-07-23 23:42:51 -04:00
|
|
|
file->tag = 1;
|
|
|
|
case softlink:
|
2023-07-24 11:04:55 -04:00
|
|
|
return haggis_load_filename(stream, file->f_type->target);
|
2023-07-23 23:42:51 -04:00
|
|
|
file->tag = 2;
|
|
|
|
case directory:
|
|
|
|
file->tag = 3;
|
2023-07-24 18:56:04 -04:00
|
|
|
break;
|
2023-07-23 23:42:51 -04:00
|
|
|
case character:
|
|
|
|
file->tag = 4;
|
2023-07-24 11:04:55 -04:00
|
|
|
return haggis_load_device(stream, file->f_type);
|
2023-07-23 23:42:51 -04:00
|
|
|
case block:
|
|
|
|
file->tag = 5;
|
2023-07-24 11:04:55 -04:00
|
|
|
return haggis_load_device(stream, file->f_type);
|
2023-07-23 23:42:51 -04:00
|
|
|
case fifo:
|
|
|
|
file->tag = 6;
|
2023-07-24 18:56:04 -04:00
|
|
|
break;
|
2023-07-23 23:42:51 -04:00
|
|
|
case eof:
|
|
|
|
file->tag = 7;
|
|
|
|
break;
|
|
|
|
}
|
2023-07-20 01:06:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-29 18:39:07 -04:00
|
|
|
int haggis_store_filetype(FILE *stream, haggis_filetype *filetype) {
|
|
|
|
u8 flag;
|
|
|
|
|
|
|
|
switch (filetype->tag) {
|
|
|
|
case normal:
|
|
|
|
flag = 0;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
if (haggis_store_file(stream, filetype->f_type->file) != 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case hardlink:
|
|
|
|
flag = 1;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
return haggis_store_filename(stream, filetype->f_type->target);
|
|
|
|
case softlink:
|
|
|
|
flag = 2;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
return haggis_store_filename(stream, filetype->f_type->target);
|
|
|
|
case directory:
|
|
|
|
flag = 3;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case character:
|
|
|
|
flag = 4;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
if (haggis_store_device(stream, filetype->f_type->dev) != 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case block:
|
|
|
|
flag = 5;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
if (haggis_store_device(stream, filetype->f_type->dev) != 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case fifo:
|
|
|
|
flag = 6;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case eof:
|
|
|
|
flag = 7;
|
|
|
|
if (fwrite(&flag, 1, 1, stream) != 1)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
haggis_typeflag haggis_filetype_from_mode(u16 mode) {
|
2023-07-24 18:56:04 -04:00
|
|
|
u8 mask = 07 << 5;
|
|
|
|
int filetype = (int)((mode.bytes[0] & mask) >> 5);
|
|
|
|
return filetype;
|
|
|
|
}
|
|
|
|
|
2023-07-29 18:39:07 -04:00
|
|
|
u16 haggis_derive_mode(u16 raw, haggis_filetype *ft) {
|
2023-07-29 13:51:37 -04:00
|
|
|
u16 mode;
|
2023-07-29 18:39:07 -04:00
|
|
|
mode.val = ((uint16_t)ft->tag << 5) & raw.val;
|
2023-07-29 13:51:37 -04:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
haggis_node* haggis_create_node(char *file) {
|
|
|
|
haggis_node *node = malloc(sizeof(haggis_node));
|
2023-07-25 19:50:46 -04:00
|
|
|
if (node == NULL)
|
|
|
|
return NULL;
|
2023-07-24 18:56:04 -04:00
|
|
|
// todo
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_extract_node(FILE *stram, haggis_node *node) {
|
2023-07-20 01:06:38 -04:00
|
|
|
// todo
|
|
|
|
return 0;
|
2023-07-19 22:38:59 -04:00
|
|
|
}
|
2023-07-20 01:06:38 -04:00
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_load_node(FILE *stream, haggis_node *node) {
|
2023-07-24 11:04:55 -04:00
|
|
|
int res;
|
2023-07-28 18:39:30 -04:00
|
|
|
u16 mode;
|
|
|
|
haggis_typeflag tag;
|
2023-07-24 11:04:55 -04:00
|
|
|
|
2023-07-24 18:56:04 -04:00
|
|
|
mode.val = 0;
|
2023-07-24 11:04:55 -04:00
|
|
|
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;
|
2023-07-24 18:56:04 -04:00
|
|
|
tag = haggis_filetype_from_mode(mode);
|
|
|
|
node->mode.bytes[0] = mode.bytes[0] & 037;
|
|
|
|
node->mode.bytes[1] = mode.bytes[1];
|
|
|
|
res = haggis_load_filetype(stream, tag, node->filetype);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-28 18:39:30 -04:00
|
|
|
int haggis_store_node(FILE *stream, haggis_node *node) {
|
2023-07-29 18:39:07 -04:00
|
|
|
u16 mode;
|
|
|
|
|
2023-07-29 13:51:37 -04:00
|
|
|
if (haggis_store_filename(stream, node->name) != (size_t)(node->name->len.val) + 2)
|
|
|
|
return 2;
|
|
|
|
if (store_u32(stream, node->uid) != 4) return 2;
|
|
|
|
if (store_u32(stream, node->gid) != 4) return 2;
|
|
|
|
if (store_u64(stream, node->mtime) != 8) return 2;
|
2023-07-29 18:39:07 -04:00
|
|
|
mode = haggis_derive_mode(node->mode, node->filetype);
|
|
|
|
if (store_u16(stream, mode) != 2) return 2;
|
|
|
|
return haggis_store_filetype(stream, node->filetype);
|
2023-07-20 01:06:38 -04:00
|
|
|
}
|