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 846373e74b7ebda76074eaf6c037f2fb479fa1b9
parent 3a36efb2a722fda6a64a571342c527c30da36f12
Author: nolash <dev@holbrook.no>
Date:   Tue, 14 Sep 2021 19:28:04 +0200

Add missing renamed swarmfile files

Diffstat:
Asrc/swarmfile.c | 125+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/swarmfile.h | 24++++++++++++++++++++++++
2 files changed, 149 insertions(+), 0 deletions(-)

diff --git a/src/swarmfile.c b/src/swarmfile.c @@ -0,0 +1,125 @@ +#include <stddef.h> +#include <string.h> + +#include "swarmfile.h" + +void filehash_reset(filehash_t *fctx) { + int i; + for (i = 0; i < SWARM_LEVELS; i++) { + fctx->ptr[i] = fctx->buf; + fctx->writes[i] = 0; + } + fctx->length = 0; +} + +static bmt_spansize_t filehash_finalize_level(filehash_t *fctx, int level) { + bmt_t *bctx; + int l; + int r; + int next_level; + int blocks; + int blocks_span_length; + int remainder; + + bctx = &fctx->bmt_context; + + next_level = level + 1; + + if (fctx->ptr[level] == fctx->target) { + return fctx->length; + } + + l = fctx->ptr[level] - fctx->ptr[next_level]; + blocks = (l / _SWARM_WORD_SIZE); + + remainder = fctx->length % fctx->spans[level]; + + if (blocks == 1) { + fctx->ptr[next_level] = fctx->ptr[level]; + return filehash_finalize_level(fctx, next_level); + } else { + blocks_span_length = (blocks - 1) * fctx->spans[level]; + if (remainder > 0) { + blocks_span_length += remainder; + } else { + blocks_span_length += fctx->spans[level]; + } + bmt_init(bctx, fctx->ptr[next_level], l, (bmt_spansize_t)(blocks_span_length)); + r = bmt_sum(bctx); + if (r != 0) { + return -1; + } + memcpy(fctx->ptr[next_level], bctx->buf, _SWARM_WORD_SIZE); + fctx->ptr[next_level] += _SWARM_WORD_SIZE; + } + return filehash_finalize_level(fctx, next_level); +} + + +bmt_spansize_t filehash_sum(filehash_t *fctx) { + return filehash_finalize_level(fctx, 0); +} + + +void filehash_init(filehash_t *fctx) { + int i; + int l; + + l = SWARM_BLOCK_SIZE; + + for (i = 0; i < SWARM_LEVELS; i++) { + fctx->spans[i] = l; + l *= _SWARM_BATCH_SIZE; + } + fctx->target = fctx->buf + _SWARM_WORD_SIZE; + + filehash_reset(fctx); +} + +static int filehash_write_hash(filehash_t *fctx, int level, const char *data) { + bmt_t *bctx; + int next_level; + int r; + + fctx->writes[level] += 1; + 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]); + r = bmt_sum(bctx); + if (r != 0) { + return -1; + } + r = filehash_write_hash(fctx, level + 1, bctx->buf); + if (r != 0) { + return -1; + } + fctx->ptr[level] = fctx->ptr[next_level]; + } else { + fctx->ptr[level] += _SWARM_WORD_SIZE; + } + return 0; +} + +int filehash_write(filehash_t *fctx, const char *data, const size_t data_length) { + bmt_t *bctx; + int r; + + bctx = &fctx->bmt_context; + + bmt_init(bctx, data, data_length, (long long)data_length); + r = bmt_sum(bctx); + if (r != 0) { + return -1; + } + r = filehash_write_hash(fctx, 0, bctx->buf); + if (r != 0) { + return -1; + } + fctx->length += data_length; + + return data_length; +} + + diff --git a/src/swarmfile.h b/src/swarmfile.h @@ -0,0 +1,24 @@ +#ifndef _LIBSWARM_FILE +#define _LIBSWARM_FILE + +#include "bmt.h" + +#define _SWARM_BATCH_SIZE (int)(SWARM_BLOCK_SIZE / _SWARM_WORD_SIZE) +#define SWARM_LEVELS 9 + +typedef struct filehash { + char buf[SWARM_LEVELS * SWARM_BLOCK_SIZE]; + char *ptr[SWARM_LEVELS]; + char *target; + long long writes[SWARM_LEVELS]; + long long spans[SWARM_LEVELS]; + long long length; + bmt_t bmt_context; +} filehash_t; + +void filehash_reset(filehash_t *filehash_context); +void filehash_init(filehash_t *filehash_context); +int filehash_write(filehash_t *filehash_context, const char *data, const size_t data_length); +bmt_spansize_t filehash_sum(filehash_t *filehash_content); + +#endif // _LIBSWARM_FILE