Move bit storing function declarations into private "bytes.h" header;

Link to libmd for cryptographic hash functions and implement comparing
checksums when loading a file from an archive;
This commit is contained in:
Nathan Fisher 2023-07-22 19:01:49 -04:00
parent b5f571be5d
commit 632f93e084
5 changed files with 101 additions and 10 deletions

View file

@ -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

48
include/bytes.h Normal file
View file

@ -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 <stdint.h>
#include <stdio.h>
#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

View file

@ -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;

View file

@ -34,6 +34,7 @@
#include <stdio.h>
#include <stdint.h>
#include "bytes.h"
#include "haggis.h"
#if __BYTE_ORDER__ == __LITTLE_ENDIAN

View file

@ -30,10 +30,14 @@
* other than his own.
*/
#include <md5.h>
#include <sha1.h>
#include <sha2.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#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;
}