More work on Cid

This commit is contained in:
John Jones 2016-11-14 18:02:18 -05:00
parent 02c5552cd6
commit 77e2cc279f
5 changed files with 48 additions and 23 deletions

2
.gitignore vendored
View file

@ -8,4 +8,4 @@
.cproject .cproject
.project .project
.settings/language.settings.xml .settings/language.settings.xml
test/test_ipfs

View file

@ -1,5 +1,5 @@
CC = gcc 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 ifdef DEBUG
CFLAGS += -g3 CFLAGS += -g3

View file

@ -11,6 +11,7 @@
#include "libp2p/crypto/encoding/base58.h" #include "libp2p/crypto/encoding/base58.h"
#include "ipfs/multibase/multibase.h" #include "ipfs/multibase/multibase.h"
#include "mh/multihash.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->hash_length = mh_multihash_length(incoming, incoming_size);
cid->codec = CID_PROTOBUF; cid->codec = CID_PROTOBUF;
cid->version = 0; 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); mh_multihash_digest(incoming, incoming_size, &cid->hash, &cid->hash_length);
return 1; return 1;
} }
/* // This is not a multihash. Perhaps it is using varints. Try to peel the information out of the bytes.
*TODO: Implement this
// This is not a multihash. Try to peel the information out of the bytes
// first the version // first the version
int pos = 0, retVal = 0; int pos = 0, retVal = 0;
int num_bytes = 0; size_t num_bytes = 0;
retVal = varint_to_int(&incoming[pos], &cid->version, &num_bytes); num_bytes = uvarint_decode32(&incoming[pos], incoming_size - pos, &cid->version);
if (retVal == 0) if (num_bytes < 0 || cid->version > 1 || cid->version < 0)
return 0; return 0;
pos = num_bytes; pos = num_bytes;
// now the codec // now the codec
retVal = varint_to_int(&incoming[pos], &cid->codec, &num_bytes); uint32_t codec = 0;
if (retVal == 0) num_bytes = uvarint_decode32(&incoming[pos], incoming_size - pos, &codec);
if (num_bytes < 0)
return 0; return 0;
cid->codec = codec;
pos += num_bytes; pos += num_bytes;
// now what is left // now what is left
cid->hash_length = incoming_size - pos; cid->hash_length = incoming_size - pos;
// TODO: allocate memory cid->hash = &incoming[pos];
memcpy(cid->hash, &incoming[pos], cid->hash_length);
*/ return 1;
return 0;
} }

View file

@ -1,6 +1,6 @@
CC = gcc CC = gcc
CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -g3 CFLAGS = -O0 -I../include -I../../c-libp2p/include -I../../c-multihash/include -I../../c-multiaddr/ -g3
LFLAGS = -L../../c-libp2p -L../../c-multihash -lp2p -lm -lmultihash 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 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 \ 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 \ ../commands/command.o ../commands/cli/parse.o ../core/builder.o ../repo/fsrepo/fs_repo.o \

View file

@ -70,10 +70,39 @@ int test_cid_cast_multihash() {
} }
int test_cid_cast_non_multihash() { int test_cid_cast_non_multihash() {
// this should turn a multibase encoded string into a cid struct // first, build a hash
// first, build a multibase encoded string 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 // now call cast
struct Cid cid;
int retVal = cid_cast(array, array_size, &cid);
if (retVal == 0)
return 0;
// check results // 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;
} }