From 77e2cc279fbe27310398f0dcfd52c407cea39359 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 14 Nov 2016 18:02:18 -0500 Subject: [PATCH] More work on Cid --- .gitignore | 2 +- cid/Makefile | 2 +- cid/cid.c | 28 ++++++++++++---------------- test/Makefile | 4 ++-- test/cid/test_cid.h | 35 ++++++++++++++++++++++++++++++++--- 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 91e7bad..acb7d7b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ .cproject .project .settings/language.settings.xml - +test/test_ipfs diff --git a/cid/Makefile b/cid/Makefile index ac42db6..f7e0269 100644 --- a/cid/Makefile +++ b/cid/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include +CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/include ifdef DEBUG CFLAGS += -g3 diff --git a/cid/cid.c b/cid/cid.c index 640f274..4d9b395 100644 --- a/cid/cid.c +++ b/cid/cid.c @@ -11,6 +11,7 @@ #include "libp2p/crypto/encoding/base58.h" #include "ipfs/multibase/multibase.h" #include "mh/multihash.h" +#include "multiaddr/varint.h" /** @@ -104,34 +105,29 @@ int cid_cast(unsigned char* incoming, size_t incoming_size, struct Cid* cid) { cid->hash_length = mh_multihash_length(incoming, incoming_size); cid->codec = CID_PROTOBUF; cid->version = 0; - // allocate memory for hash - cid->hash = malloc(cid->hash_length); - if (cid->hash == NULL) - return 0; mh_multihash_digest(incoming, incoming_size, &cid->hash, &cid->hash_length); return 1; } - /* - *TODO: Implement this - // This is not a multihash. Try to peel the information out of the bytes + // This is not a multihash. Perhaps it is using varints. Try to peel the information out of the bytes. // first the version int pos = 0, retVal = 0; - int num_bytes = 0; - retVal = varint_to_int(&incoming[pos], &cid->version, &num_bytes); - if (retVal == 0) + size_t num_bytes = 0; + num_bytes = uvarint_decode32(&incoming[pos], incoming_size - pos, &cid->version); + if (num_bytes < 0 || cid->version > 1 || cid->version < 0) return 0; pos = num_bytes; // now the codec - retVal = varint_to_int(&incoming[pos], &cid->codec, &num_bytes); - if (retVal == 0) + uint32_t codec = 0; + num_bytes = uvarint_decode32(&incoming[pos], incoming_size - pos, &codec); + if (num_bytes < 0) return 0; + cid->codec = codec; pos += num_bytes; // now what is left cid->hash_length = incoming_size - pos; - // TODO: allocate memory - memcpy(cid->hash, &incoming[pos], cid->hash_length); - */ - return 0; + cid->hash = &incoming[pos]; + + return 1; } diff --git a/test/Makefile b/test/Makefile index 5f7663a..6c4f6a0 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ CC = gcc -CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -g3 -LFLAGS = -L../../c-libp2p -L../../c-multihash -lp2p -lm -lmultihash +CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/ -g3 +LFLAGS = -L../../c-libp2p -L../../c-multihash -L../../c-multiaddr -lp2p -lm -lmultihash -lmultiaddr DEPS = cmd/ipfs/test_init.h repo/test_repo_bootstrap_peers.h repo/test_repo_config.h repo/test_repo_identity.h cid/test_cid.h OBJS = testit.o ../cmd/ipfs/init.o ../commands/argument.o ../commands/command_option.o \ ../commands/command.o ../commands/cli/parse.o ../core/builder.o ../repo/fsrepo/fs_repo.o \ diff --git a/test/cid/test_cid.h b/test/cid/test_cid.h index 9cc1341..28507c1 100644 --- a/test/cid/test_cid.h +++ b/test/cid/test_cid.h @@ -70,10 +70,39 @@ int test_cid_cast_multihash() { } int test_cid_cast_non_multihash() { - // this should turn a multibase encoded string into a cid struct - // first, build a multibase encoded string + // first, build a hash + char* string_to_hash = "Hello, World!"; + unsigned char hashed[32]; + memset(hashed, 0, 32); + // hash the string + libp2p_crypto_hashing_sha256(string_to_hash, strlen(string_to_hash), hashed); + + // now make it a hash with a version and codec embedded in varints before the hash + size_t array_size = 34; // 32 for the hash, 2 for the 2 varints + unsigned char array[array_size]; + memset(array, 0, array_size); + // first the version + array[0] = 0; + // then the codec + array[1] = CID_PROTOBUF; + // then the hash + memcpy(&array[2], hashed, 32); + // now call cast + struct Cid cid; + int retVal = cid_cast(array, array_size, &cid); + if (retVal == 0) + return 0; // check results - return 0; + if (cid.version != 0) + return 0; + if (cid.hash_length != 32) + return 0; + if (cid.codec != CID_PROTOBUF) + return 0; + if (strncmp(hashed, cid.hash, 32) != 0) + return 0; + + return 1; }