diff --git a/Makefile b/Makefile index 022ac42..f2313c2 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ PREFIX ?= /usr/local includedir = $(DESTDIR)$(PREFIX)/include libdir = $(DESTDIR)$(PREFIX)/lib +hdrs += include/bytes.h hdrs += include/haggis.h srcs += src/bytes.c @@ -47,6 +48,8 @@ CFLAGS += -Wall -Werror CFLAGS += -Iinclude CFLAGS += -fPIC +LIBS += -lmd + all: libhaggis.a libhaggis.so %.o: %.c @@ -56,14 +59,14 @@ libhaggis.a: $(objs) $(AR) rcs $@ $? libhaggis.so: $(objs) - $(CC) -shared -o $@ $? + $(CC) -shared -o $@ $? $(LIBS) install: libhaggis.a libhaggis.so haggis.h [ -d $(includedir) ] || install -d $(includedir) [ -d $(libdir) ] || install -d $(libdir) install -m755 libhaggis.so $(libdir)/ install -m644 libhaggis.a $(libdir)/ - install -m644 $(hdrs) $(includedir)/ + install -m644 include/haggis.h $(includedir)/ clean: rm -rf *.a *.so src/*.o diff --git a/include/bytes.h b/include/bytes.h new file mode 100644 index 0000000..0e58ee8 --- /dev/null +++ b/include/bytes.h @@ -0,0 +1,48 @@ +/* _,.---._ .-._ .--.-. ,--.--------. + * _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\ + * /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./ + * |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \ + * |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \ + * |==|,| | -|==| , '=' |==| - _ | |==|- | + * |==| '=' /\==\ - ,_ /|==| /\ , | |==|, | + * |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/ + * `-.`.____.' `--`--'' `--`./ `--` `--`--` + * _ __ ,---. .-._ .=-.-. _,.----. + * .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \ + * /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-' + * |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | . + * |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \ + * |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , | + * |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. / + * /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-' + * `--`---' `--` `--`./ `--``--`-` + * + * @(#)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. +*/ + +#ifndef BYTES_H +#define BYTES_H + +#include +#include + +#include "haggis.h" + +int load_u16(FILE *stream, union u16 num); +int store_u16(FILE *stream, union u16 num); +int load_u32(FILE *stream, union u32 num); +int store_u32(FILE *stream, union u32 num); +int load_u64(FILE *stream, union u64 num); +int store_u64(FILE *stream, union u64 num); + +#endif diff --git a/include/haggis.h b/include/haggis.h index 9cad369..e131f3e 100644 --- a/include/haggis.h +++ b/include/haggis.h @@ -53,13 +53,6 @@ union u64 { u8 bytes[8]; }; -int load_u16(FILE *stream, union u16 num); -int store_u16(FILE *stream, union u16 num); -int load_u32(FILE *stream, union u32 num); -int store_u32(FILE *stream, union u32 num); -int load_u64(FILE *stream, union u64 num); -int store_u64(FILE *stream, union u64 num); - struct haggis_device { union u32 major; union u32 minor; diff --git a/src/bytes.c b/src/bytes.c index e7b4caa..f1ad7c5 100644 --- a/src/bytes.c +++ b/src/bytes.c @@ -34,6 +34,7 @@ #include #include +#include "bytes.h" #include "haggis.h" #if __BYTE_ORDER__ == __LITTLE_ENDIAN diff --git a/src/haggis.c b/src/haggis.c index 9d07cee..87985a6 100644 --- a/src/haggis.c +++ b/src/haggis.c @@ -30,10 +30,14 @@ * other than his own. */ +#include +#include +#include #include #include #include +#include "bytes.h" #include "haggis.h" static unsigned char header[7] = {0x89, 'h', 'a', 'g', 'g', 'i', 's'}; @@ -129,8 +133,50 @@ int haggis_load_cksum(FILE *stream, struct haggis_checksum *cksum) { return 0; } +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); + MD5Final(digest, &ctx); + if (memcmp(file->cksum->sum->md5, digest, sizeof(digest))) + return 2; + return 0; +} + +int validate_sha1(struct haggis_file *file) { + SHA1_CTX ctx; + u8 digest[SHA1_DIGEST_LENGTH]; + SHA1Init(&ctx); + 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; +} + +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); + SHA256Final(digest, &ctx); + if (memcmp(file->cksum->sum->sha256, digest, sizeof(digest))) + return 2; + return 0; +} + int haggis_validate_cksum(struct haggis_file *file) { - // todo + switch (file->cksum->tag) { + case md5: + return validate_md5(file); + case sha1: + return validate_sha1(file); + case sha256: + return validate_sha256(file); + case skip: + return 0; + } return 0; }