diff --git a/APKBUILD b/APKBUILD new file mode 100644 index 0000000..d0c5e21 --- /dev/null +++ b/APKBUILD @@ -0,0 +1,29 @@ +# Maintainer: Imbus +pkgname=libhash +pkgver=0.1.2 +pkgrel=0 +pkgdesc="Various hash functions in C" +url="https://git.silversoft.se/Imbus/libhash" +arch="all" +license="MIT" +makedepends="gcc make" +checkdepends="" +subpackages="$pkgname-static $pkgname-dev" +source="$pkgname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz" +builddir="$srcdir/$pkgname" + +build() { + make +} + +check() { + make test +} + +package() { + PREFIX="$pkgdir/usr" make install +} + +sha512sums=" +2e7d02e96f26569a2ae2e29d8644fa93e08229fbb288a67418c986d928cfa3d93e16793ca20cc9a39cbc74e5842395a37923f843fba2215350db350a307ff080 libhash-0.1.2.tar.gz +" diff --git a/Makefile b/Makefile index 9e1504d..ca364c9 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ -CC = gcc -CFLAGS = -Wall -fPIC -Ihash -O2 -AR = ar -ARFLAGS = rcs +CC ?= gcc +CFLAGS ?= -Wall -Wextra -fPIC -Ihash -O2 +AR ?= ar +ARFLAGS ?= rcs SRCS = djb2.c OBJS = $(SRCS:.c=.o) @@ -9,7 +9,16 @@ OBJS = $(SRCS:.c=.o) STATIC_LIB = libhash.a SHARED_LIB = libhash.so -all: $(STATIC_LIB) $(SHARED_LIB) +PREFIX ?= /usr/local +INCLUDEDIR = $(PREFIX)/include +LIBDIR = $(PREFIX)/lib + +TEST_SRC = test.c +TEST_BIN = test.elf + +all: $(STATIC_LIB) $(SHARED_LIB) $(TEST_BIN) +test: $(TEST_BIN) + ./$< %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ @@ -20,7 +29,23 @@ $(STATIC_LIB): $(OBJS) $(SHARED_LIB): $(OBJS) $(CC) -shared -o $@ $^ -clean: - rm -f $(OBJS) $(STATIC_LIB) $(SHARED_LIB) +$(TEST_BIN): $(TEST_SRC) $(STATIC_LIB) + $(CC) -Wall -Ihash -o $@ $(TEST_SRC) $(STATIC_LIB) -.PHONY: all clean +install: $(STATIC_LIB) $(SHARED_LIB) + install -d $(DESTDIR)$(INCLUDEDIR)/hash + install -m 644 hash/*.h $(DESTDIR)$(INCLUDEDIR)/hash/ + install -d $(DESTDIR)$(LIBDIR) + install -m 644 $(STATIC_LIB) $(DESTDIR)$(LIBDIR)/ + install -m 755 $(SHARED_LIB) $(DESTDIR)$(LIBDIR)/ + @echo "Installed libraries to $(DESTDIR)$(LIBDIR) and headers to $(DESTDIR)$(INCLUDEDIR)/hash" + +uninstall: + rm -f $(DESTDIR)$(LIBDIR)/$(STATIC_LIB) $(DESTDIR)$(LIBDIR)/$(SHARED_LIB) + rm -rf $(DESTDIR)$(INCLUDEDIR)/hash + @echo "Uninstalled libraries from $(DESTDIR)$(LIBDIR) and headers from $(DESTDIR)$(INCLUDEDIR)/hash" + +clean: + rm -f $(OBJS) $(STATIC_LIB) $(SHARED_LIB) $(TEST_BIN) + +.PHONY: all clean install uninstall test diff --git a/test.c b/test.c new file mode 100644 index 0000000..bf69dbc --- /dev/null +++ b/test.c @@ -0,0 +1,31 @@ +#include "hash/crc32.h" +#include "hash/djb2.h" +#include +#include +#include + +int main(void) { + assert(crc32("123456789", 9) == 0xCBF43926); + assert(crc32("", 0) == 0x00000000); + assert(crc32("a", 1) == 0xE8B7BE43); + assert(crc32("A", 1) == 0xD3D99E8B); + assert(crc32("hello", 5) == 0x3610A686); + assert(crc32("The quick brown fox jumps over the lazy dog", 43) == 0x414FA339); + assert(crc32("The quick brown fox jumps over the lazy dog.", 44) == 0x519025E9); + + /* Keep in mind the update api requires initialization and finalization */ + uint32_t crc = crc32_init(); + crc = crc32_update_raw(crc, "a", 1); + crc = crc32_finalize(crc); + + assert(crc == crc32("a", 1)); + assert(crc == 0xE8B7BE43); + + assert(djb2("Hello") == 0x000000310D4F2079); + assert(djb2("a") == 0x000000000002B606); + assert(djb2("A") == 0x000000000002B5E6); + assert(djb2("123456789") == 0x0377821035CDBB82); + + printf("All good!\n"); + exit(EXIT_SUCCESS); +}