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

chunk.c (749B)


      1 #include <stddef.h>
      2 #include "string.h"
      3 
      4 #include "chunk.h"
      5 #ifdef LIBSWARM_MALLOC
      6 #include "bmt_malloc.h"
      7 #else
      8 #include "bmt.h"
      9 #endif
     10 
     11 unsigned char* chunk_serialize(const swarm_chunk_t *chunk, unsigned char *z, size_t *sz) {
     12 	int crsr;
     13 
     14 	crsr = 0;
     15 
     16 	memcpy(z, chunk->span, SWARM_DATA_LENGTH_TYPESIZE);
     17 	crsr += SWARM_DATA_LENGTH_TYPESIZE;
     18 
     19 	memcpy(z + crsr, chunk->payload, chunk->payload_sz);
     20 	*sz = crsr + chunk->payload_sz;
     21 
     22 	return z;
     23 }
     24 
     25 int chunk_verify(const swarm_chunk_t *chunk) {
     26 	int r;
     27 	bmt_t bctx;
     28 
     29 	bmt_init(&bctx, chunk->payload, chunk->payload_sz, (bmt_spansize_t)*chunk->span);
     30 	r = bmt_sum(&bctx);
     31 	if (r != 0) {
     32 		return 1;
     33 	}	
     34 	
     35 	r = memcmp(chunk->hash, bctx.buf, SWARM_WORD_SIZE);
     36 	if (r != 0) {
     37 		return 1;
     38 	}
     39 
     40 	return 0;
     41 }