seahag/src/haggis.c

607 lines
17 KiB
C
Raw Normal View History

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.
*/
#include <limits.h> // PATH_MAX
#include <stdint.h> // uint<x>_t
2023-08-07 18:55:47 -04:00
2023-07-23 23:42:51 -04:00
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sha.h>
#include <sha256.h>
#include <sys/types.h>
2023-07-23 23:42:51 -04:00
#elif defined(__NetBSD__) || defined(__OpenBSD__)
#include <sha1.h>
#include <sha2.h>
2023-07-23 23:42:51 -04:00
#include <sys/types.h>
#elif defined(__linux__)
#include <sha1.h>
#include <sha2.h>
#include <sys/sysmacros.h> // major, minor, dev_t
#endif /* if defined (__FreeBSD__) */
#include <md5.h>
#include <stdio.h> // fopen, fread, fwrite, FILE
#include <stdlib.h> // free, malloc
#include <string.h> // memcpy, strlen
#include <unistd.h> // readlink
#include <sys/stat.h> // stat
2023-07-19 22:38:59 -04:00
#include "bytes.h"
2023-07-19 22:38:59 -04:00
#include "haggis.h"
2023-08-07 18:55:47 -04:00
#include "linklist.h"
2023-07-19 22:38:59 -04:00
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;
}
void haggis_device_init(dev_t rdev, haggis_device *dev) {
2023-08-07 18:55:47 -04:00
dev->major.val = (uint32_t)major(rdev);
dev->minor.val = (uint32_t)minor(rdev);
}
int haggis_store_device(FILE *stream, haggis_device *dev) {
if (fwrite(dev->major.bytes, 1, 4, stream) != 4)
return 1;
if (fwrite(dev->minor.bytes, 1, 4, stream) != 4)
return 1;
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_load_device(FILE *stream, haggis_device *dev) {
if (fread(dev->major.bytes, 1, 4, stream) != 4)
return 1;
if (fread(dev->minor.bytes, 1, 4, stream) != 4)
return 1;
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_store_cksum(FILE *stream, haggis_checksum *cksum) {
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)
2023-07-20 01:06:38 -04:00
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)
2023-07-20 01:06:38 -04:00
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)
2023-07-20 01:06:38 -04:00
return 1;
break;
case skip:
flag = 3;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
break;
}
return 0;
}
int haggis_load_cksum(FILE *stream, haggis_checksum *cksum) {
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)
2023-07-20 01:06:38 -04:00
return 1;
break;
case sha1:
cksum->tag = 1;
if (fread(&cksum->sum.sha1, 1, 20, stream) != 20)
2023-07-20 01:06:38 -04:00
return 1;
break;
case sha256:
cksum->tag = 2;
if (fread(&cksum->sum.sha256, 1, 32, stream) != 32)
2023-07-20 01:06:38 -04:00
return 1;
break;
case skip:
cksum->tag = 3;
break;
}
return 0;
}
int validate_md5(haggis_file *file) {
MD5_CTX ctx;
u8 digest[16];
MD5Init(&ctx);
2023-07-23 23:42:51 -04:00
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;
}
2023-07-23 23:42:51 -04:00
#if defined(__FreeBSD__) || defined(__DragonFly__)
int validate_sha1(haggis_file *file) {
SHA1_CTX ctx;
u8 digest[20];
SHA1_Init(&ctx);
2023-07-23 23:42:51 -04:00
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;
}
2023-07-23 23:42:51 -04:00
#elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__)
int validate_sha1(haggis_file *file) {
SHA1_CTX ctx;
u8 digest[20];
SHA1Init(&ctx);
2023-07-23 23:42:51 -04:00
SHA1Update(&ctx, file->data, (size_t)file->len.val);
SHA1Final(digest, &ctx);
if (memcmp(file->cksum.sum.sha1, digest, sizeof(digest)))
return 2;
return 0;
}
#endif /* if defined (__FreeBSD__) */
2023-07-23 23:42:51 -04:00
#if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__)
int validate_sha256(haggis_file *file) {
SHA256_CTX ctx;
u8 digest[32];
SHA256_Init(&ctx);
2023-07-23 23:42:51 -04:00
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;
}
2023-07-23 23:42:51 -04:00
#elif defined(__linux__) || defined(__OpenBSD__)
int validate_sha256(haggis_file *file) {
SHA2_CTX ctx;
u8 digest[32];
SHA256Init(&ctx);
2023-07-23 23:42:51 -04:00
SHA256Update(&ctx, file->data, (size_t)file->len.val);
SHA256Final(digest, &ctx);
if (memcmp(file->cksum.sum.sha256, digest, sizeof(digest)))
return 2;
return 0;
}
#endif /* if defined (__FreeBSD__) */
int haggis_validate_cksum(haggis_file *file) {
switch (file->cksum.tag) {
case md5:
2023-07-23 23:42:51 -04:00
return validate_md5(file);
case sha1:
2023-07-23 23:42:51 -04:00
return validate_sha1(file);
case sha256:
2023-07-23 23:42:51 -04:00
return validate_sha256(file);
case skip:
return 0;
}
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_file_init(char *path, haggis_file *hf) {
2023-08-07 18:55:47 -04:00
FILE *f;
long len;
f = fopen(path, "r");
if (f == NULL) return 2;
2023-08-07 18:55:47 -04:00
if (fseek(f, 0, SEEK_END) == -1) {
fclose(f);
return 2;
2023-08-07 18:55:47 -04:00
}
len = ftell(f);
if (len == -1) {
fclose(f);
return 2;
2023-08-07 18:55:47 -04:00
}
hf->len.val = (uint64_t)len;
rewind(f);
hf->data = malloc((size_t)len);
if (fread(hf->data, 1, (size_t)len, f) != (size_t)len) {
free(hf->data);
fclose(f);
return 1;
2023-08-07 18:55:47 -04:00
}
fclose(f);
return 0;
2023-08-07 18:55:47 -04:00
}
void haggis_file_deinit(haggis_file *f) {
if (f->data != NULL) free(f->data);
}
int haggis_store_file(FILE *stream, haggis_file *file) {
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;
}
int haggis_load_file(FILE *stream, haggis_file *f) {
if (load_u64(stream, f->len) != 8)
return 1;
if (haggis_load_cksum(stream, &f->cksum) != 0)
return 1;
f->data = malloc((size_t)f->len.val);
if (f->data == NULL)
2023-07-25 19:50:46 -04:00
return -1;
int res = fread(f->data, 1, (size_t)f->len.val, stream);
if (res != (size_t)f->len.val) {
free(f->data);
return 1;
2023-07-23 23:42:51 -04:00
}
if (haggis_validate_cksum(f)) {
free(f->data);
2023-07-23 23:42:51 -04:00
return 1;
}
return 0;
}
void haggis_filename_init(char *target, haggis_filename *fname) {
2023-08-07 18:55:47 -04:00
size_t len;
len = strlen(target) - 1;
fname->len.val = (uint16_t)len;
fname->name = target;
}
void haggis_filename_deinit(haggis_filename *fname) {
if (fname->name != NULL) free(fname->name);
2023-08-07 18:55:47 -04:00
}
int haggis_load_filename(FILE *stream, haggis_filename *n) {
u16 len;
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;
n->len = len;
name = malloc((size_t)len.val);
2023-07-29 13:51:37 -04:00
if (name == NULL) return -1;
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;
}
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;
}
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.file);
2023-07-23 23:42:51 -04:00
case hardlink:
return haggis_load_filename(stream, &file->f_type.target);
2023-07-23 23:42:51 -04:00
file->tag = 1;
case softlink:
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;
break;
2023-07-23 23:42:51 -04:00
case character:
file->tag = 4;
return haggis_load_device(stream, &file->f_type.dev);
2023-07-23 23:42:51 -04:00
case block:
file->tag = 5;
return haggis_load_device(stream, &file->f_type.dev);
2023-07-23 23:42:51 -04:00
case fifo:
file->tag = 6;
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;
}
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;
}
haggis_typeflag haggis_filetype_from_mode(u16 mode) {
u8 mask = 07 << 5;
int filetype = (int)((mode.bytes[0] & mask) >> 5);
return filetype;
}
u16 haggis_derive_mode(u16 raw, haggis_filetype *ft) {
2023-07-29 13:51:37 -04:00
u16 mode;
mode.val = ((uint16_t)ft->tag << 5) & raw.val;
2023-07-29 13:51:37 -04:00
return mode;
}
2023-08-07 18:55:47 -04:00
void haggis_node_deinit(haggis_node *node) {
if (node == NULL) return;
if (node->name.name != NULL) haggis_filename_deinit(&node->name);
switch (node->filetype.tag) {
2023-08-07 18:55:47 -04:00
case normal:
if (node->filetype.f_type.file.data != NULL) {
free(node->filetype.f_type.file.data);
2023-08-07 18:55:47 -04:00
}
break;
case hardlink:
case softlink:
if (node->filetype.f_type.target.name != NULL) {
haggis_filename_deinit(&node->filetype.f_type.target);
2023-08-07 18:55:47 -04:00
}
break;
case character:
case block:
case directory:
case fifo:
case eof:
break;
};
free(node);
}
haggis_node* haggis_create_node(char *file, haggis_hardlink_list *list) {
struct stat *st = NULL;
u16 mode;
2023-08-07 18:55:47 -04:00
char *target;
char pathbuf[PATH_MAX];
int res;
2023-08-07 18:55:47 -04:00
haggis_node *node;
2023-08-07 18:55:47 -04:00
node = malloc(sizeof(haggis_node));
if (node == NULL) return NULL;
if (stat(file, st) != 0) {
free(node);
return NULL;
}
if (S_ISBLK(st->st_mode)) {
node->filetype.tag = block;
} else if (S_ISCHR(st->st_mode)) {
node->filetype.tag = character;
} else if (S_ISDIR(st->st_mode)) {
node->filetype.tag = directory;
} else if (S_ISFIFO(st->st_mode)) {
node->filetype.tag = fifo;
} else if (S_ISLNK(st->st_mode)) {
node->filetype.tag = softlink;
} else if (S_ISREG(st->st_mode)) {
node->filetype.tag = normal;
} else {
free(node);
return NULL;
}
node->uid.val = (uint32_t)st->st_uid;
node->gid.val = (uint32_t)st->st_gid;
node->mtime.val = (uint64_t)st->st_mtim.tv_sec;
mode.val = (uint16_t)(st->st_mode & 07777);
node->mode = mode;
switch (node->filetype.tag) {
case normal:
if (st->st_nlink > 1) {
target = haggis_linklist_get_or_put(list, st->st_ino, file);
if (target != NULL) {
node->filetype.tag = hardlink;
haggis_filename_init(target, &node->filetype.f_type.target);
return node;
2023-08-07 18:55:47 -04:00
}
}
res = haggis_file_init(file, &node->filetype.f_type.file);
if (res != 0) {
haggis_node_deinit(node);
return NULL;
}
2023-08-07 18:55:47 -04:00
break;
case block:
if (st->st_nlink > 1) {
target = haggis_linklist_get_or_put(list, st->st_ino, file);
if (target != NULL) {
node->filetype.tag = hardlink;
haggis_filename_init(target, &node->filetype.f_type.target);
return node;
2023-08-07 18:55:47 -04:00
}
}
haggis_device_init(st->st_rdev, &node->filetype.f_type.dev);
2023-08-07 18:55:47 -04:00
break;
case character:
if (st->st_nlink > 1) {
target = haggis_linklist_get_or_put(list, st->st_ino, file);
if (target != NULL) {
node->filetype.tag = hardlink;
haggis_filename_init(target, &node->filetype.f_type.target);
return node;
2023-08-07 18:55:47 -04:00
}
}
haggis_device_init(st->st_rdev, &node->filetype.f_type.dev);
2023-08-07 18:55:47 -04:00
break;
case fifo:
if (st->st_nlink > 1) {
target = haggis_linklist_get_or_put(list, st->st_ino, file);
if (target != NULL) {
node->filetype.tag = hardlink;
haggis_filename_init(target, &node->filetype.f_type.target);
return node;
2023-08-07 18:55:47 -04:00
}
}
return node;
case directory:
case hardlink:
case eof:
return node;
case softlink:
node->filetype.tag = softlink;
2023-08-07 18:55:47 -04:00
ssize_t res = readlink(file, pathbuf, PATH_MAX);
if (res == -1) {
haggis_node_deinit(node);
return NULL;
}
target = malloc(res + 1);
2023-08-07 18:55:47 -04:00
memcpy(target, pathbuf, (unsigned long)res);
haggis_filename_init(target, &node->filetype.f_type.target);
return node;
}
return node;
}
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
int haggis_load_node(FILE *stream, haggis_node *node) {
int res;
u16 mode;
haggis_typeflag tag;
mode.val = 0;
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;
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;
}
int haggis_store_node(FILE *stream, haggis_node *node) {
u16 mode;
if (haggis_store_filename(stream, &node->name) != (size_t)(node->name.len.val) + 2)
2023-07-29 13:51:37 -04:00
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;
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
}