diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2019-06-23 21:36:16 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2019-06-23 21:36:16 -0700 |
commit | fc0b68f105a191c7cd4dbb85251ada870119d470 (patch) | |
tree | 614de7d54761c4e3c12b386cd2b0f9bfbea342c3 /chksum.c | |
parent | 5fc1472e9ce7ebf3d3d25898f0e6288ffe87d08e (diff) | |
download | txr-fc0b68f105a191c7cd4dbb85251ada870119d470.tar.gz txr-fc0b68f105a191c7cd4dbb85251ada870119d470.tar.bz2 txr-fc0b68f105a191c7cd4dbb85251ada870119d470.zip |
* Makefile (OBJS): New objects chksum.o and chksums/sha256.o.
* chksum.c, chksum.h, chksums/sha256.c, chksums/sha256.h: New files.
* lib.c (init): Call chksum_init.
* txr.1: Documented.
* LICENSE: Add SHA-256 copyright notice.
Diffstat (limited to 'chksum.c')
-rw-r--r-- | chksum.c | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/chksum.c b/chksum.c new file mode 100644 index 00000000..78af6b8d --- /dev/null +++ b/chksum.c @@ -0,0 +1,142 @@ +/* Copyright 2019 + * Kaz Kylheku <kaz@kylheku.com> + * Vancouver, Canada + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stddef.h> +#include <wchar.h> +#include <limits.h> +#include <float.h> +#include <string.h> +#include <stdlib.h> +#include <stdarg.h> +#include <signal.h> +#include <stdio.h> +#include "config.h" +#include "lib.h" +#include "gc.h" +#include "itypes.h" +#include "signal.h" +#include "unwind.h" +#include "eval.h" +#include "stream.h" +#include "arith.h" +#include "utf8.h" +#include "buf.h" +#include "chksums/sha256.h" +#include "chksum.h" + +val sha256_stream(val stream, val nbytes) +{ + SHA256_t s256; + unsigned char *hash = chk_malloc(SHA256_DIGEST_LENGTH); + val bfsz = num_fast(BUFSIZ); + val buf = make_buf(bfsz, nil, nil); + SHA256_init(&s256); + + if (null_or_missing_p(nbytes)) { + for (;;) { + val read = fill_buf(buf, zero, stream); + cnum rd = c_num(read); + + if (!rd) + break; + + SHA256_update(&s256, buf->b.data, rd); + } + } else { + while (ge(nbytes, bfsz)) { + val read = fill_buf(buf, zero, stream); + cnum rd = c_num(read); + + if (zerop(read)) + break; + + SHA256_update(&s256, buf->b.data, rd); + nbytes = minus(nbytes, read); + } + + buf_set_length(buf, nbytes, nil); + + { + val read = fill_buf(buf, zero, stream); + cnum rd = c_num(read); + if (rd) + SHA256_update(&s256, buf->b.data, rd); + } + } + + SHA256_final(&s256, hash); + return make_borrowed_buf(num_fast(SHA256_DIGEST_LENGTH), hash); +} + +static val sha256_buf(val buf, val self) +{ + SHA256_t s256; + unsigned char *hash = chk_malloc(SHA256_DIGEST_LENGTH); + SHA256_init(&s256); + ucnum len = c_unum(buf->b.len); + mem_t *data = buf->b.data; + const size_t szmax = convert(size_t, -1) / 4 + 1; + + while (len >= szmax) { + SHA256_update(&s256, data, szmax); + data += szmax; + len -= szmax; + } + + if (len > 0) + SHA256_update(&s256, data, len); + + SHA256_final(&s256, hash); + return make_borrowed_buf(num_fast(SHA256_DIGEST_LENGTH), hash); +} + +static val sha256_str(val str, val self) +{ + val s = make_byte_input_stream(str); + return sha256_stream(s, nil); +} + +val sha256(val obj) +{ + val self = lit("sha256"); + + switch (type(obj)) { + case STR: + case LSTR: + case LIT: + return sha256_str(obj, self); + case BUF: + return sha256_buf(obj, self); + default: + uw_throwf(error_s, lit("~a: cannot hash ~s, only buffer and strings"), + self, obj, nao); + } +} + +void chksum_init(void) +{ + reg_fun(intern(lit("sha256-stream"), user_package), func_n2o(sha256_stream, 1)); + reg_fun(intern(lit("sha256"), user_package), func_n1(sha256)); +} |