diff --git a/cpp/example/Makefile b/cpp/example/Makefile index fb38278..2dd32ed 100644 --- a/cpp/example/Makefile +++ b/cpp/example/Makefile @@ -1,4 +1,4 @@ .PHONY: all all: - g++ --std=c++20 test.c -I../ -lnegentropy -lcrypto -L../ + g++ --std=c++20 test.c -I../ -lnegentropy -lcrypto -L../ -L/opt/homebrew/lib/ -Wc++11-narrowing diff --git a/cpp/example/test.c b/cpp/example/test.c index ae877ec..516b23d 100644 --- a/cpp/example/test.c +++ b/cpp/example/test.c @@ -1,22 +1,79 @@ #include #include - +#include +#include +#include +#include +#include +#include #include "../negentropy_wrapper.h" + int main(){ - void* st = storage_new("",""); - if(st == NULL){ + void* st1 = storage_new("",""); + if(st1 == NULL){ perror("failed to create storage"); } - void* ngn_inst = negentropy_new(st, 153600); - if(ngn_inst == NULL){ + void* ngn_inst1 = negentropy_new(st1, 153600); + if(ngn_inst1 == NULL){ perror("failed to create negentropy instance"); } - const char* output = negentropy_initiate(ngn_inst); - if(ngn_inst == NULL){ + void* st2 = storage_new("",""); + if(st2 == NULL){ + perror("failed to create storage"); + } + void* ngn_inst2 = negentropy_new(st2, 153600); + if(ngn_inst2 == NULL){ + perror("failed to create negentropy instance"); + } + + unsigned char m1[] = {0x6a, 0xdf, 0xaa, 0xe0, 0x31, 0xeb, 0x61, 0xa8, \ + 0x3c, 0xff, 0x9c, 0xfd, 0xd2, 0xae, 0xf6, 0xed, \ + 0x63, 0xda, 0xcf, 0xaa, 0x96, 0xd0, 0x51, 0x26, \ + 0x7e, 0xf1, 0x0c, 0x8b, 0x61, 0xae, 0x35, 0xe9};//"61dfaae031eb61a83cff9cfdd2aef6ed63dacfaa96d051267ef10c8b61ae35e9"; + buffer b1 ; + b1.len = 32; + b1.data = m1; + +/* char m2[] = "28798d295c30c7e6d9a4a96cdda7e020f7aa7168cce063302ed19b856332959e"; + buffer b2 ; + b2.len = 32; + b2.data = m2; */ + + bool ret = storage_insert(st1,time(NULL),&b1); + if (ret){ + printf("inserted hash successfully in st1\n"); + } +/* + ret = storage_insert(st2,time(NULL),&b2); + if (ret){ + printf("inserted hash %s successfully in st2\n", m2); + } + + ret = storage_insert(st2,time(NULL),&b1); + if (ret){ + printf("inserted hash %s successfully in st2\n", m1); + } + */ + +/* std::string out; + negentropy_initiate(ngn_inst1, &out); + if(out.size() == 0){ perror("failed to initiate negentropy instance"); } - printf("initiated negentropy successfully with output %s \n", output); + printf("initiated negentropy successfully with output of len %zu \n", out.size()); + buffer b3 ; + b3.len = out.size(); + b3.data = (unsigned char*)malloc(b3.len); + memcpy(b3.data, out.c_str(),out.size()); + + const char* req2 = reconcile(ngn_inst2, &b3); */ + + //free(b3.data); + //b3.len = len; + //b3.data = (char*)malloc(len); + + //reconcile_with_ids(ngn_inst1, &b3, ) } \ No newline at end of file diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index 9dc9277..04eebc9 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -42,7 +42,7 @@ void* negentropy_new(void* storage, uint64_t frameSizeLimit){ return ne; } -const char* negentropy_initiate(void* negentropy){ +size_t negentropy_initiate(void* negentropy, buffer* out){ Negentropy* ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); @@ -52,9 +52,10 @@ const char* negentropy_initiate(void* negentropy){ std::cout << "output of initiate is, len:" << output->size() << std::hex << *output << std::endl; } catch(negentropy::err e){ //TODO:Find a way to return this error - return NULL; + return 0; } - return output->c_str(); + memcpy( out->data, output->c_str() ,output->size()); + return output->size(); } void negentropy_setinitiator(void* negentropy){ @@ -65,38 +66,52 @@ void negentropy_setinitiator(void* negentropy){ } +void printHexString(std::string_view toPrint){ + for (size_t i = 0; i < toPrint.size(); ++i) { + printf("%0hhx", toPrint[i]); + } + printf("\n"); +} bool storage_insert(void* storage, uint64_t createdAt, buffer* id){ negentropy::storage::BTreeMem* lmdbStorage; lmdbStorage = reinterpret_cast(storage); - std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:" << std::hex << id->data << " length is:"<< id->len << std::endl; + std::string_view data(reinterpret_cast< char const* >(id->data), id->len); + + std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:"; + printHexString(data); + //TODO: Error handling. Is it required? //How does out of memory get handled? - return lmdbStorage->insert(createdAt, std::string_view(id->data, id->len)); + return lmdbStorage->insert(createdAt, data); } bool storage_erase(void* storage, uint64_t createdAt, buffer* id){ negentropy::storage::BTreeMem* lmdbStorage; lmdbStorage = reinterpret_cast(storage); + std::string_view data(reinterpret_cast< char const* >(id->data), id->len); + + std::cout << "erasing entry from storage, createdAt:" << createdAt << ",id:"; + printHexString(data); //TODO: Error handling - return lmdbStorage->erase(createdAt, std::string_view(id->data, id->len)); + return lmdbStorage->erase(createdAt, data); } -const char* reconcile(void* negentropy, buffer* query){ +size_t reconcile(void* negentropy, buffer* query, buffer* output){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); - - std::string* output = new std::string(); + std::string* out = new std::string(); try { - *output = ngn_inst->reconcile(std::string_view(query->data, query->len)); + *out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len)); } catch(negentropy::err e){ //TODO:Find a way to return this error - return NULL; + return 0; } - return output->c_str(); + memcpy( output->data, out->c_str() ,out->size()); + return out->size(); } char *convert(const std::string & s) @@ -116,7 +131,7 @@ const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[ std::vector needIds; try { - *output = ngn_inst->reconcile(std::string_view(query->data, query->len), haveIds, needIds); + *output = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len), haveIds, needIds); *have_ids_len = haveIds.size(); *need_ids_len = needIds.size(); diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index 4210816..8974e1c 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -10,7 +10,7 @@ typedef struct _buffer_{ uint64_t len ; - char* data; + unsigned char* data; }buffer; //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. @@ -20,7 +20,7 @@ EXTERNC void* storage_new(const char* db_path, const char* name); EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit); -EXTERNC const char* negentropy_initiate(void* negentropy); +EXTERNC size_t negentropy_initiate(void* negentropy, buffer* output); EXTERNC void negentropy_setinitiator(void* negentropy); @@ -28,7 +28,7 @@ EXTERNC bool storage_insert(void* storage, uint64_t createdAt, buffer* id); EXTERNC bool storage_erase(void* storage, uint64_t createdAt, buffer* id); -EXTERNC const char* reconcile(void* negentropy, buffer* query); +EXTERNC size_t reconcile(void* negentropy, buffer* query,buffer* output); EXTERNC const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[], uint64_t *have_ids_len, char* need_ids[], uint64_t *need_ids_len); diff --git a/cpp/vendor/lmdbxx b/cpp/vendor/lmdbxx index d649a58..af64901 160000 --- a/cpp/vendor/lmdbxx +++ b/cpp/vendor/lmdbxx @@ -1 +1 @@ -Subproject commit d649a581d3cebfe7d8bd4d345bc2c1c4c2cc59a2 +Subproject commit af649014456719b16100c7da31d46378c91b0e7b