libswarm-ng

C implementation of BMT hasher, Swarmhash and Single Owner Chunk for swarm
git clone git://git.defalsify.org/libswarm-ng.git
Log | Files | Refs | Submodules | README

commit 650c5a87edf4582a78f2fc9046d0ee5d8db516e9
parent e7510f57c0988c016e8e891076c41e6f7e0a6a1c
Author: nolash <dev@holbrook.no>
Date:   Fri, 17 Sep 2021 09:02:39 +0200

Revert to stack buffer for bmt

Diffstat:
Msrc/bmt.c | 3++-
Msrc/bmt.h | 7+------
Msrc/swarmfile.c | 17+++++++++--------
Msrc/swarmfile.h | 2+-
4 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/src/bmt.c b/src/bmt.c @@ -45,7 +45,8 @@ static int bmt_rollup(bmt_t *bctx) { } void bmt_init(bmt_t *bctx, const char *input, const size_t input_length, const bmt_spansize_t data_length) { - bctx->buf = malloc(SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE); + //TODO: python fails on malloc, wasm needs malloc + //bctx->buf = malloc(sizeof(char) * SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE); bctx->w_ptr = bctx->buf + SWARM_DATA_LENGTH_TYPESIZE; bctx->r_ptr = bctx->w_ptr; bctx->target = bctx->w_ptr + SWARM_BLOCK_SIZE; diff --git a/src/bmt.h b/src/bmt.h @@ -3,13 +3,8 @@ #include "swarm.h" -#define _SWARM_WORD_SIZE SWARM_WORD_SIZE -#define _SWARM_DATA_LENGTH_TYPESIZE SWARM_DATA_LENGTH_TYPESIZE - - typedef struct bmt { - //char buf[_SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE]; - char *buf; + char buf[SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE]; char *w_ptr; char *r_ptr; char *target; diff --git a/src/swarmfile.c b/src/swarmfile.c @@ -1,6 +1,7 @@ #include <stddef.h> #include <string.h> +#include "def.h" #include "swarmfile.h" static inline void filehash_callback(filehash_t *fctx, const char *hash, const char *data, const size_t data_length) { @@ -38,7 +39,7 @@ static bmt_spansize_t filehash_finalize_level(filehash_t *fctx, int level) { } l = fctx->ptr[level] - fctx->ptr[next_level]; - blocks = (l / _SWARM_WORD_SIZE); + blocks = (l / SWARM_WORD_SIZE); remainder = fctx->length % fctx->spans[level]; @@ -58,8 +59,8 @@ static bmt_spansize_t filehash_finalize_level(filehash_t *fctx, int level) { return -1; } filehash_callback(fctx, bctx->buf, fctx->ptr[next_level], l); - memcpy(fctx->ptr[next_level], bctx->buf, _SWARM_WORD_SIZE); - fctx->ptr[next_level] += _SWARM_WORD_SIZE; + memcpy(fctx->ptr[next_level], bctx->buf, SWARM_WORD_SIZE); + fctx->ptr[next_level] += SWARM_WORD_SIZE; } return filehash_finalize_level(fctx, next_level); } @@ -78,9 +79,9 @@ void filehash_init_callback(filehash_t *fctx, void (*callback)(const char*, cons for (i = 0; i < SWARM_LEVELS; i++) { fctx->spans[i] = l; - l *= _SWARM_BATCH_SIZE; + l *= SWARM_BATCH_SIZE; } - fctx->target = fctx->buf + _SWARM_WORD_SIZE; + fctx->target = fctx->buf + SWARM_WORD_SIZE; fctx->callback = callback; fctx->callback_static = callback_static; @@ -98,8 +99,8 @@ static int filehash_write_hash(filehash_t *fctx, int level, const char *data) { int r; fctx->writes[level] += 1; - memcpy(fctx->ptr[level], data, _SWARM_WORD_SIZE); - if (fctx->writes[level] % _SWARM_BATCH_SIZE == 0) { + memcpy(fctx->ptr[level], data, SWARM_WORD_SIZE); + if (fctx->writes[level] % SWARM_BATCH_SIZE == 0) { bctx = &fctx->bmt_context; next_level = level + 1; bmt_init(bctx, fctx->ptr[next_level], SWARM_BLOCK_SIZE, (long long)fctx->spans[next_level]); @@ -114,7 +115,7 @@ static int filehash_write_hash(filehash_t *fctx, int level, const char *data) { } fctx->ptr[level] = fctx->ptr[next_level]; } else { - fctx->ptr[level] += _SWARM_WORD_SIZE; + fctx->ptr[level] += SWARM_WORD_SIZE; } return 0; } diff --git a/src/swarmfile.h b/src/swarmfile.h @@ -3,7 +3,7 @@ #include "bmt.h" -#define _SWARM_BATCH_SIZE (int)(SWARM_BLOCK_SIZE / _SWARM_WORD_SIZE) +#define SWARM_BATCH_SIZE (int)(SWARM_BLOCK_SIZE / SWARM_WORD_SIZE) #define SWARM_LEVELS 9 typedef struct filehash {