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 40812cd3dc16e7821149e6369c7d9ef4f5c34aac
parent 1eeaa706bf18000e3a75aaa4c28eab46691c6cdd
Author: nolash <dev@holbrook.no>
Date:   Fri,  1 Oct 2021 15:06:42 +0200

Include span in chunk callback

Diffstat:
Msrc/bmt.c | 2+-
Msrc/swarmfile.c | 19++++++++++++++-----
2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/bmt.c b/src/bmt.c @@ -57,7 +57,7 @@ void bmt_init(bmt_t *bctx, const unsigned char *input, const size_t input_length bctx->target = bctx->w_ptr + SWARM_BLOCK_SIZE; memset(bctx->buf, 0, SWARM_DATA_LENGTH_TYPESIZE + SWARM_BLOCK_SIZE); - memcpy((char*)bctx->buf, &data_length, sizeof(bmt_spansize_t)); + memcpy((unsigned char*)bctx->buf, &data_length, sizeof(bmt_spansize_t)); to_endian(CONVERT_LITTLEENDIAN, SWARM_DATA_LENGTH_TYPESIZE, (unsigned char*)bctx->buf); memcpy(bctx->w_ptr, input, input_length); diff --git a/src/swarmfile.c b/src/swarmfile.c @@ -2,11 +2,20 @@ #include <string.h> #include "def.h" +#include "endian.h" #include "swarmfile.h" -static inline void filehash_callback(filehash_t *fctx, const unsigned char *hash, const unsigned char *data, const size_t data_length) { +static inline void filehash_callback(filehash_t *fctx, const unsigned char *hash, const unsigned char *data, const size_t data_length, const bmt_spansize_t span_size) { if (fctx->callback != NULL) { - (*fctx->callback)(hash, data, data_length, fctx->callback_static); + if (span_size < 0) { + (*fctx->callback)(hash, data, data_length + SWARM_DATA_LENGTH_TYPESIZE, fctx->callback_static); + } else { + unsigned char out_data[SWARM_BLOCK_SIZE + SWARM_DATA_LENGTH_TYPESIZE]; + memcpy(out_data, &span_size, SWARM_DATA_LENGTH_TYPESIZE); + to_endian(CONVERT_LITTLEENDIAN, SWARM_DATA_LENGTH_TYPESIZE, (unsigned char*)out_data); + memcpy(out_data + SWARM_DATA_LENGTH_TYPESIZE, data, data_length); + (*fctx->callback)(hash, out_data, data_length + SWARM_DATA_LENGTH_TYPESIZE, fctx->callback_static); + } } } @@ -58,7 +67,7 @@ static bmt_spansize_t filehash_finalize_level(filehash_t *fctx, int level) { if (r != 0) { return -1; } - filehash_callback(fctx, bctx->buf, fctx->ptr[next_level], l); + filehash_callback(fctx, bctx->buf, fctx->ptr[next_level], l, (bmt_spansize_t)(blocks_span_length)); memcpy(fctx->ptr[next_level], bctx->buf, SWARM_WORD_SIZE); fctx->ptr[next_level] += SWARM_WORD_SIZE; } @@ -108,7 +117,7 @@ static int filehash_write_hash(filehash_t *fctx, int level, const unsigned char if (r != 0) { return -1; } - filehash_callback(fctx, bctx->buf, fctx->ptr[next_level], SWARM_BLOCK_SIZE); + filehash_callback(fctx, bctx->buf, fctx->ptr[next_level], SWARM_BLOCK_SIZE, (bmt_spansize_t)fctx->spans[next_level]); r = filehash_write_hash(fctx, level + 1, bctx->buf); if (r != 0) { return -1; @@ -131,7 +140,7 @@ int filehash_write(filehash_t *fctx, const unsigned char *data, const size_t dat if (r != 0) { return -1; } - filehash_callback(fctx, bctx->buf, data, data_length); + filehash_callback(fctx, bctx->buf, data, data_length, (bmt_spansize_t)data_length); r = filehash_write_hash(fctx, 0, bctx->buf); if (r != 0) { return -1;