diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 19 | ||||
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | main.c | 145 | ||||
-rw-r--r-- | man/murmur.1 | 58 | ||||
-rw-r--r-- | man/murmur.1.md | 45 | ||||
-rw-r--r-- | murmurhash.c | 6 | ||||
-rw-r--r-- | murmurhash.h | 2 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test.c | 2 |
10 files changed, 286 insertions, 6 deletions
@@ -21,3 +21,4 @@ *.i*86 *.x86_64 *.hex +murmur @@ -1,12 +1,29 @@ +BIN ?= murmur SRC = murmurhash.c CFLAGS += -std=c99 -Wall -I. +PREFIX ?= /usr/local +MANPREFIX ?= $(PREFIX)/share/man +MAN_FILES = $(wildcard man/*.md) + +$(BIN): + $(CC) main.c $(SRC) $(CFLAGS) -o $(BIN) clean: rm -f test + rm -f $(BIN) test: $(CC) test.c $(SRC) $(CFLAGS) -o test ./test -.PHONY: test +install: $(BIN) + install $(BIN) $(PREFIX)/bin + install man/*.1 $(MANPREFIX)/man1 + +docs: $(MAN_FILES) + +$(MAN_FILES): + curl -# -F page=@$(@) -o $(@:%.md=%) http://mantastic.herokuapp.com + +.PHONY: test $(MAN_FILES) @@ -34,6 +34,18 @@ main (void) { } ``` +A command line executable is also available: + +``` +$ echo -n kinkajou | murmur +3067714808 +``` + +``` +$ echo -n panda | murmur --seed=10 +1406483717 +``` + ## api ```c @@ -0,0 +1,145 @@ + +/** + * `main.c' - libbeaufort + * + * copyright (c) 2014 joseph werle <joseph.werle@gmail.com> + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <inttypes.h> +#include "murmurhash.h" + +static void +usage () { + fprintf(stderr, "usage: murmur [-hV] [options]\n"); +} + +static void +help () { + fprintf(stderr, "\noptions:\n"); + fprintf(stderr, "\n --seed=[seed] hash seed (optional)"); + fprintf(stderr, "\n"); +} + +static char * +read_stdin () { + size_t bsize = 1024; + size_t size = 1; + char buf[bsize]; + char *res = (char *) malloc(sizeof(char) * bsize); + char *tmp = NULL; + + // memory issue + if (NULL == res) { return NULL; } + + // cap + res[0] = '\0'; + + // read + if (NULL != fgets(buf, bsize, stdin)) { + // store + tmp = res; + // resize + size += (size_t) strlen(buf); + // realloc + res = (char *) realloc(res, size); + + // memory issues + if (NULL == res) { + free(tmp); + return NULL; + } + + // yield + strcat(res, buf); + + return res; + } + + free(res); + + return NULL; +} + +#define isopt(opt, str) (0 == strncmp(opt, str, strlen(str))) + +#define setopt(opt, key, var) { \ + char tmp = 0; \ + size_t len = strlen(key) + 1; \ + for (int i = 0; i < len; ++i) { tmp = *opt++; } \ + var = opt; \ +} + +int +main (int argc, char **argv) { + char *buf = NULL; + char *key = NULL; + char *seed = NULL; + uint32_t h = 0; + + // parse opts + { + char *opt = NULL; + char tmp = 0; + + opt = *argv++; // unused + + while ((opt = *argv++)) { + + // flags + if ('-' == *opt++) { + switch (*opt++) { + case 'h': + return usage(), help(), 0; + + case 'V': + fprintf(stderr, "%s\n", MURMURHASH_VERSION); + return 0; + + case '-': + if (isopt(opt, "seed")) { + setopt(opt, "seed", seed); + } + break; + + default: + tmp = *opt--; + // error + fprintf(stderr, "unknown option: `%s'\n", opt); + usage(); + return 1; + } + } + } + } + + if (NULL == seed) { + seed = "0"; + } + +#define hash(key) murmurhash(key, (uint32_t) strlen(key), (uint32_t) atoi(seed)); + + if (1 == isatty(0)) { return 1; } + else if (ferror(stdin)) { return 1; } + else { + buf = read_stdin(); + if (NULL == buf) { return 1; } + else if (0 == strlen(buf)) { buf = ""; } + h = hash(buf); + printf("%" PRIu32 "\n", h); + do { + key = read_stdin(); + if (NULL == key) { break; } + else if (0 == strlen(buf)) { buf = ""; } + h = hash(buf); + printf("%d" PRIu32 "\n", h); + } while (NULL != key); + } + +#undef hash + + return 0; +} diff --git a/man/murmur.1 b/man/murmur.1 new file mode 100644 index 0000000..e842e22 --- /dev/null +++ b/man/murmur.1 @@ -0,0 +1,58 @@ +.\" Generated with Ronnjs 0.3.8 +.\" http://github.com/kapouer/ronnjs/ +. +.TH "BEAUFORT" "1" "May 2014" "" "" +. +.SH "NAME" +\fBbeaufort\fR \-\- Command line Beaufort Cipher utility +. +.SH "SYNOPSIS" +\fBmurmur\fR [\-hV] [options] +. +.SH "OPTIONS" + \-V, \-\-version + output program version +. +.P + \-h, \-\-help + output help information +. +.P + \-\-seed=[seed] + hash seed (optional) +. +.SH "EXAMPLES" + \fB + $ echo \-n kinkajou | murmur + 3067714808 + \fR +. +.P + \fB + $ echo \-n panda | murmur \-\-seed=10 + 1406483717 + \fR +. +.SH "AUTHOR" +. +.IP "\(bu" 4 +Joseph Werle \fIjoseph\.werle@gmail\.com\fR +. +.IP "" 0 +. +.SH "REPORTING BUGS" +. +.IP "\(bu" 4 +\fIhttps://github\.com/jwerle/murmurhash\.c/issues\fR +. +.IP "" 0 +. +.SH "SEE ALSO" +. +.IP "\(bu" 4 +\fIhttps://github\.com/jwerle/murmurhash\.c\fR +. +.IP "" 0 +. +.SH "LICENSE" +MIT
\ No newline at end of file diff --git a/man/murmur.1.md b/man/murmur.1.md new file mode 100644 index 0000000..4a7ed7f --- /dev/null +++ b/man/murmur.1.md @@ -0,0 +1,45 @@ +beaufort(1) -- Command line Beaufort Cipher utility +================================= + +## SYNOPSIS + +`murmur` \[-hV\] \[options\] + +## OPTIONS + + -V, --version + output program version + + -h, --help + output help information + + --seed=[seed] + hash seed (optional) + +## EXAMPLES + + `` + $ echo -n kinkajou | murmur + 3067714808 + `` + + `` + $ echo -n panda | murmur --seed=10 + 1406483717 + `` + +## AUTHOR + + - Joseph Werle <joseph.werle@gmail.com> + +## REPORTING BUGS + + - <https://github.com/jwerle/murmurhash.c/issues> + +## SEE ALSO + + - <https://github.com/jwerle/murmurhash.c> + +## LICENSE + +MIT diff --git a/murmurhash.c b/murmurhash.c index 427b3d2..8224eb6 100644 --- a/murmurhash.c +++ b/murmurhash.c @@ -7,7 +7,7 @@ #include <stdlib.h> #include <stdio.h> -#include <murmurhash.h> +#include "murmurhash.h" uint32_t murmurhash (const char *key, uint32_t len, uint32_t seed) { @@ -20,8 +20,8 @@ murmurhash (const char *key, uint32_t len, uint32_t seed) { uint32_t h = 0; uint32_t k = 0; uint8_t *d = (uint8_t *) key; // 32 bit extract from `key' - const uint32_t *chunks = 0; - const uint8_t *tail = 0; // tail - last 8 bytes + const uint32_t *chunks = NULL; + const uint8_t *tail = NULL; // tail - last 8 bytes int i = 0; int l = len / 4; // chunk length diff --git a/murmurhash.h b/murmurhash.h index 1941e0f..9d7da0c 100644 --- a/murmurhash.h +++ b/murmurhash.h @@ -8,6 +8,8 @@ #ifndef MURMURHASH_H #define MURMURHASH_H 1 +#define MURMURHASH_VERSION "0.0.2" + /** * Returns a murmur hash of `key' based on `seed' * using the MurmurHash3 algorithm diff --git a/package.json b/package.json index 940797c..24f74d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "murmurhash", - "version": "0.0.1", + "version": "0.0.2", "repo": "jwerle/murmurhash.c", "description": "MurmurHash3 general hash bashed lookup function implementation", "keywords": ["murmur", "hash", "murmurhash"], @@ -10,7 +10,7 @@ #include <string.h> #include <inttypes.h> #include <assert.h> -#include <murmurhash.h> +#include "murmurhash.h" int main (void) { |