seahag/src/haggis.c

170 lines
4.8 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 <stdio.h>
2023-07-20 01:06:38 -04:00
#include <string.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;
}
int haggis_store_device(FILE *stream, struct haggis_device *dev) {
u8 major[4];
u8 minor[4];
u32_to_le_bytes(dev->major, major);
u32_to_le_bytes(dev->minor, minor);
if (fwrite(major, 1, 4, stream) != 4)
return 1;
if (fwrite(minor, 1, 4, stream) != 4)
return 1;
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_load_device(FILE *stream, struct haggis_device *dev) {
u8 major[4];
u8 minor[4];
if (fread(major, 1, 4, stream) != 4)
return 1;
if (fread(minor, 1, 4, stream) != 4)
return 1;
dev->major = le_bytes_to_u32(major);
dev->minor = le_bytes_to_u32(minor);
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_store_cksum(FILE *stream, struct haggis_checksum *cksum) {
char flag;
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;
}
int haggis_load_cksum(FILE *stream, struct haggis_checksum *cksum) {
// todo
char flag;
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;
}
int haggis_store_file(FILE *stream, struct haggis_normal *file) {
// todo
return 0;
}
int haggis_load_file(FILE *stream, struct haggis_normal *file) {
// todo
return 0;
}
int haggis_store_filetype(FILE *stream, struct haggis_filetype *filetype) {
// todo
return 0;
}
int haggis_load_filetype(FILE *stream, struct haggis_filetype *filetype) {
// todo
return 0;
}
int haggis_store_node(FILE *stream, struct haggis_node *node) {
// 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, struct haggis_node *node) {
// todo
return 0;
}